#include "../../include/rapida.h" #include "../../include/servers/tcp.h" #include #include #include static void products_handler(rpd_req *req, rpd_res *res, void *userdata) { char *body = "" "" "Product" "" "

Product page

" "
"
                 "Category: %s\n"
                 "Id: %s"
                 "
" "" ""; rpd_keyval_item *cat = rpd_keyval_find(&req->params, "category"); rpd_keyval_item *id = rpd_keyval_find(&req->params, "id"); if (!cat || !id) { res->status = rpd_res_st_internal_server_error; return; } int bufflen = strlen(body) + strlen(cat->val) + strlen(id->val); res->body = (char *) malloc(sizeof(char) * (bufflen + 1)); if (!res->body) { res->status = rpd_res_st_internal_server_error; return; } res->body[bufflen] = '\0'; sprintf(res->body, body, cat->val, id->val); res->status = rpd_res_st_ok; /* * place all values in heap, please, * because after call handler * `req` and `res` will be freed */ res->content_type = strdup("text/html"); } int main() { rpd_app app; rpd_app_create(&app); rpd_app_add_route(&app, "/products/:category/:id", &products_handler, NULL); return rpd_tcp_server_start(&app, "http://localhost:8080"); }