#include #include #include #include #include #include #include "buffer.h" #include "http.h" #include "server.h" static struct reply *handle(struct request *req, void *data) { (void)data; struct reply *ret = malloc(sizeof(struct reply)); cJSON *obj = cJSON_CreateObject(); cJSON_AddStringToObject(obj, "path", req->path); cJSON_AddStringToObject(obj, "query", req->query); cJSON_AddStringToObject(obj, "method", req->method); cJSON_AddStringToObject(obj, "version", req->http_version); cJSON_AddStringToObject(obj, "hostname", http_get_header(req, "Host")); if (req->data) { cJSON_AddItemReferenceToObject(obj, "data", cJSON_Parse(req->data)); } ret->status = 200; ret->body = cJSON_Print(obj); cJSON_Delete(obj); return ret; } int main(int argc, char **argv) { (void)argc; (void)argv; struct http *http = http_init(); http_register_handler(http, "GET", "/", NULL, &handle); http_register_handler(http, "GET", "/nyaa", NULL, &handle); http_register_handler(http, "POST", "/nyaa", NULL, &handle); struct server *server = server_init(http); server_poll(server); }