Summary Files Commits Branches @commit @diff @stats
path: root / src / http / callback_http.c
blob: 5b2b944cf3cae9661a5babe474385a88bebafc64 (plain) (blame)
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
#include "callback_http.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../utils/strings.h"
#include "http_headers.h"
#include "request_method.h"
#include "response_code.h"

static const char* SEPS = " \n\r";
#define return_bad_request() {                     \
		args->output = "HTTP/1.1 400 Bad Request"; \
		args->output_size = 24;                    \
		return;                                    \
	}

static void append_header(enum HttpHeader header, const char* value, const char** parts_array, size_t *ind);

void callback_http(callback_args* args) {
	if (args->extra == NULL) {
		printf("No HTTP callbacks were set!");
		return;
	}

	http_callbacks *callbacks = args->extra;

	/* Parse input request */

	client_message in_message = {
		.method  = RM_UNKNOWN,
		.url     = NULL,
		.version = NULL,
		.headers = { 0 },
	};

	/** Request method **/

	char* word = strtok(args->input, SEPS);
	if (word == NULL) return_bad_request()

	for (enum RequestMethod rm = RM_GET; rm < RM_UNKNOWN; rm++) {
		if (streq(REQUEST_METHOD_NAME[rm], word)) {
			in_message.method = rm;
			break;
		}
	}

	if (in_message.method == RM_UNKNOWN)
		return_bad_request()

	/** URL **/

	word = strtok(NULL, SEPS);
	if (word == NULL) return_bad_request()

	in_message.url = word;

	/** HTTP version **/

	word = strtok(NULL, SEPS);
	if (word == NULL || !streq("HTTP/1.1", word))
		return_bad_request()

	in_message.version = word;

	/** Headers **/

	word = strtok(NULL, ":\n\r");

	while(word != NULL) {
		enum HttpHeader he = 0;

		while (he < HE_LENGTH) {
			if (streq(HTTP_HEADER_NAME[he], word))
				break;
			++he;
		}
		word = strtok(NULL, "\n\r");

		if (word == NULL) break;

		if (he < HE_LENGTH) {
			while (*word == ' ') ++word;
			in_message.headers[he] = word;
		}

		word = strtok(NULL, ":\n\r");
	}

	/* Respond to request */

	response_message out_message = {
		.response_code = RC_500_INTERNAL_SERVER_ERRROR,

		.headers = { 0 },

		.body = NULL,
		.body_size = 0,
		.free_body = 0,
	};

	if (in_message.method < RM_UNKNOWN)
		callbacks->all_callbacks[in_message.method](&in_message, &out_message);
	else
		printf("Unsupported method!");

	const char* response[1024] = { 0 };
	size_t rind = 0;

	response[rind++] = "HTTP/1.1 ";
	response[rind++] = RESPONSE_CODE_NAME[out_message.response_code];
	response[rind++] = "\n";

	for (enum HttpHeader he = 0; he < HE_LENGTH; he++) {
		if (out_message.headers[he] != NULL)
			append_header(he, out_message.headers[he], response, &rind);
	}

	response[rind++] = "\n";

	if (out_message.body != NULL && out_message.body_size > 0)
		response[rind++] = out_message.body;

	args->output      = concat_array(response, rind, &args->output_size);
	args->free_output = 1;

	if (out_message.free_body)
		free(out_message.body);
}

static void append_header(enum HttpHeader header, const char* value, const char** parts_array, size_t *ind) {
	parts_array[(*ind)++] = HTTP_HEADER_NAME[header];
	parts_array[(*ind)++] = ": ";
	parts_array[(*ind)++] = value;
	parts_array[(*ind)++] = "\n";
}