| 1 | /* |
| 2 | * Copyright (c) 2014 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <kore/kore.h> |
| 18 | #include <kore/http.h> |
| 19 | |
| 20 | int page(struct http_request *); |
| 21 | |
| 22 | int |
| 23 | page(struct http_request *req) |
| 24 | { |
| 25 | u_int16_t id; |
| 26 | char *sid; |
| 27 | struct kore_buf *buf; |
| 28 | |
| 29 | /* |
| 30 | * Before we are able to obtain any parameters given to |
| 31 | * us via the query string we must tell Kore to parse and |
| 32 | * validate them. |
| 33 | * |
| 34 | * NOTE: All parameters MUST be declared in a params {} block |
| 35 | * inside the configuration for Kore! Kore will filter out |
| 36 | * any parameters not explicitly defined. |
| 37 | * |
| 38 | * See conf/parameters.conf on how that is done, this is an |
| 39 | * important step as without the params block you will never |
| 40 | * get any parameters returned from Kore. |
| 41 | */ |
| 42 | http_populate_get(req); |
| 43 | |
| 44 | /* |
| 45 | * Lets grab the "id" parameter if available. Kore can obtain |
| 46 | * parameters in different data types native to C. |
| 47 | * |
| 48 | * In this scenario, lets grab it both as an actual string and |
| 49 | * as an u_int16_t (unsigned short). |
| 50 | * |
| 51 | * When trying to obtain a parameter as something else then |
| 52 | * a string, Kore will automatically check if the value fits |
| 53 | * in said data type. |
| 54 | * |
| 55 | * For example if id is 65536 it won't fit in an u_int16_t |
| 56 | * and Kore will return an error when trying to read it as such. |
| 57 | */ |
| 58 | |
| 59 | buf = kore_buf_alloc(128); |
| 60 | |
| 61 | /* Grab it as a string, we shouldn't free the result in sid. */ |
| 62 | if (http_argument_get_string(req, "id", &sid)) |
| 63 | kore_buf_appendf(buf, "id as a string: '%s'\n", sid); |
| 64 | |
| 65 | /* Grab it as an actual u_int16_t. */ |
| 66 | if (http_argument_get_uint16(req, "id", &id)) |
| 67 | kore_buf_appendf(buf, "id as an u_int16_t: %d\n", id); |
| 68 | |
| 69 | /* Now return the result to the client with a 200 status code. */ |
| 70 | http_response(req, 200, buf->data, buf->offset); |
| 71 | kore_buf_free(buf); |
| 72 | |
| 73 | return (KORE_RESULT_OK); |
| 74 | } |