summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 8429797121bfc1275ae0b7f3bfa7a963a9204520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <cjson/cJSON.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#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);
}