summaryrefslogtreecommitdiff
path: root/src/http.c
blob: 99204ecd3ca59e62af1d05b99ed20e0b02dd49b1 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <cjson/cJSON.h>
#include <stdio.h>

#include "http.h"
#include "util.h"
#include "buffer.h"

struct http {
	struct request_handler *requests;
	size_t req_siz, n_requests;
};

struct request_handler {
	char *method;
	char *path;
	void *data;
	request_callback *callback;
};

struct header_item {
	char *key;
	char *value;
	struct header_item *next;
};

bool parse_query(char *path, char **query) {
	char *sep = strchr(path, '?');
	if (!sep) {
		*query = NULL;
		return false;
	}
	
	*sep = '\0';
	*query = strdup(sep + 1);

	return true;
}

void add_header(struct header_list *list, char *header, char *value) {
	size_t index = hash(header, MAX_HEADERS);
	struct header_item to_insert = {0};
	struct header_item *item = NULL;
	to_insert.key = strdup(header);
	to_insert.value = strdup(value);

	if (!list->items[index]) {
		list->items[index] = malloc(sizeof(struct header_item));
		*(list->items[index]) = to_insert;
	} else {
		item = list->items[index];
		while (item->next != NULL) {
			item = item->next;
		}
		item->next = malloc(sizeof(struct header_item));
		*(item->next) = to_insert;
	}
}

const char *http_get_header(struct request *req, const char *key) {
	size_t index = hash(key, MAX_HEADERS);

	struct header_item *item = req->header.items[index];

	while (item != NULL) {
		if (strcmp(item->key, key) == 0) {
			return item->value;
		}
		item = item->next;
	}

	return NULL;
}

enum parse_result {
	PARSE_OK,
	PARSE_ERR_UNSUPPORTED_HTTP_VERSION,
	PARSE_MALFORMED_INPUT
};

static enum parse_result parse_request(struct request *req, const char *request) {
	assert(req);
	assert(request);

	char *copy = strdup(request);
	char *saveptr, *line;
	char *path, *query;
	char *header;

	char *data = strstr(copy, "\r\n\r\n");
	if (data) {
		*data = '\0';
		data += 4;
	}

	line = strtok_r(copy, "\r\n", &saveptr);

	if (sscanf(line, "%ms %ms HTTP/%s", &req->method, &path, req->http_version) != 3) {
		free(copy);
		free(path);
		free(req->method);
		return PARSE_MALFORMED_INPUT;
	}

	if (parse_query(path, &query)) {
		req->path = path;
		req->query = query;
	} else {
		req->path = path;
		req->query = strdup("");
	}

	for (line = strtok_r(NULL, "\r\n", &saveptr);
			line != NULL && strlen(line) > 0;
			line = strtok_r(NULL, "\r\n", &saveptr)) {

		header = strchr(line, ':');
		if (!header)
			continue;
		*(header++) = '\0';
		while (*header == ' ')
			header++;
		add_header(&req->header, line, header);
	}

	if (data)
		req->data = strdup(data);

	return PARSE_OK;
}

struct buffer *http_build_reply(struct reply *reply) {
	assert(reply);

	struct buffer *buf = buf_new(NULL);

	buf_printf(buf,
			"HTTP/1.1 %d %s\r\n"
			"Content-Length: %ld\r\n"
			"Content-Type: %s\r\n"
			"\r\n"
			"%s",
			reply->status, "OK", strlen(reply->body), "application/json", reply->body);

	return buf;
}

struct reply *http_handle_request(struct http *http, struct buffer *req) {
	struct request data = {0};
	struct reply *ret = NULL;

	if (parse_request(&data, req->data) != PARSE_OK) {
		// todo: log
		return NULL;
	}

	for (size_t i = 0; i < http->n_requests; i++) {
		if (strcmp(data.method, http->requests[i].method) == 0 &&
			strcmp(data.path, http->requests[i].path) == 0) {
			ret = http->requests[i].callback(&data, http->requests[i].data);
		}
	}

	if (!ret) {
		ret = malloc(sizeof(struct reply));
		ret->status = 404;
		ret->body = strdup("");
	}

	return ret;
}

struct http *http_init(void) {
	struct http *http = malloc(sizeof(struct http));
	http->requests = malloc(sizeof(struct request_handler) * 5);
	http->n_requests = 0;
	http->req_siz = 5;
	return http;
}

bool http_register_handler(struct http *http, char *method, char *path,
		void *data, request_callback *callback) {
	if (http->n_requests + 1 >= http->req_siz) {
		http->req_siz += 10;
		http->requests = realloc(http->requests,
				sizeof(struct request_handler) * http->req_siz);
		memset(http->requests, 0,
				sizeof(struct request_handler) * (http->req_siz - http->n_requests));
	}
	struct request_handler *handle = &http->requests[http->n_requests++];

	handle->method = strdup(method);
	handle->path = strdup(path);
	handle->data = data;
	handle->callback = callback;

	return true;
}