-
Notifications
You must be signed in to change notification settings - Fork 0
/
on_read.c
73 lines (66 loc) · 1.76 KB
/
on_read.c
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
#include "main.h"
/**
* on_read - reads the passed buffer, and parse the HTTP request
* @client: pointer to client socket
* @nread: number of bytes read
* @buf: pointer to buffer containing data to be read
* return: void
*/
void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
{
file_request_t *file_request;
http_parser *parser = malloc(sizeof(http_parser));
size_t parsed;
http_parser_settings settings;
parser_data_t *data = malloc(sizeof(parser_data_t));
data->url = NULL;
if (nread < 0)
{
if (nread != UV_EOF)
{
fprintf(stderr, "Error on read: %s\n",
uv_strerror(nread));
}
uv_close((uv_handle_t *)client, on_close);
free(buf->base);
return;
}
http_parser_init(parser, HTTP_BOTH);
parser->data = client;
http_parser_settings_init(&settings);
settings.on_url = on_url;
parsed = http_parser_execute(parser, &settings,
buf->base, nread);
if (parsed != nread)
{
fprintf(stderr, "Error parsing HTTP request\n");
uv_close((uv_handle_t *)client, on_close);
free(buf->base);
return;
}
if (parser->method == HTTP_GET)
{
file_request = malloc(sizeof(file_request_t));
file_request->client = (uv_tcp_t *)client;
snprintf(file_request->path, 1024, "%s", ((parser_data_t *)parser->data)->url);
serve_file(file_request);
}
free(data->url);
free(data);
free(parser);
free(buf->base);
}
/**
* on_url - extracts request path and its length into struct
* @parser: the parser containing request
* @at: pointer to buffer containing path
* @length: size of the buffer
* return: 0 on success
*/
int on_url(http_parser* parser, const char* at, size_t length)
{
parser_data_t *data = (parser_data_t *)parser->data;
data->url = (char*)malloc(length + 1);
snprintf(data->url, length + 1, "%s", at);
return 0;
}