|
|
|
#include "../../include/rapida.h"
|
|
|
|
#include "../../include/servers/tcp.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static void products_handler(rpd_req *req, rpd_res *res, void *userdata)
|
|
|
|
{
|
|
|
|
char *body = "<!DOCTYPE html>"
|
|
|
|
"<html>"
|
|
|
|
"<head><title>Product</title></head>"
|
|
|
|
"<body>"
|
|
|
|
"<h1>Product page</h1>"
|
|
|
|
"<pre>"
|
|
|
|
"Category: %s\n"
|
|
|
|
"Id: %s"
|
|
|
|
"</pre>"
|
|
|
|
"</body>"
|
|
|
|
"</html>";
|
|
|
|
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");
|
|
|
|
}
|