-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.c
168 lines (133 loc) · 3.92 KB
/
request.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
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
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdlib.h>
#include "message.h"
#include "request.h"
#include "http_parser.h"
#include "route.h"
int handle_read_data(client_t *c, char* buf, int len)
{
return http_parser_execute(c->parser, c->parser_settings, buf, len);
}
int on_message_begin(http_parser *parser)
{
syslog(LOG_DEBUG, "%s()...", __func__);
return 0;
}
int on_message_complete(http_parser *parser)
{
syslog(LOG_DEBUG, "%s()...", __func__);
syslog(LOG_DEBUG, "-------- response should be generated at this point ---------");
route_request((client_t *)parser->data);
return 0;
}
int on_url(http_parser *parser, const char *at, size_t len)
{
char tok[256];
client_t *c;
message_t *msg;
c = parser->data;
msg = c->msg;
/* Append to the growing URL; we'll break it down later */
extend_message_url(msg, at, len);
/* debugging stuff...*/
strncpy(tok, at, len);
tok[len] = '\0';
syslog(LOG_DEBUG, "%s(): token is %s", __func__, tok);
return 0;
}
int on_status_complete(http_parser *parser)
{
syslog(LOG_DEBUG, "%s()...", __func__);
return 0;
}
int on_header_field(http_parser *parser, const char *at, size_t len)
{
char tok[256];
client_t *c;
message_t *msg;
c = parser->data;
msg = c->msg;
/* If we were last working on a value, we're in a new header now. */
if(msg->last_header_element == VALUE || msg->last_header_element == NONE)
add_message_header(msg, at, len, NULL, 0);
/* Append to the header field-name; this might be a continuation due to a partial read */
else
extend_message_header_fieldname(msg, msg->num_headers - 1, at, len);
msg->last_header_element = FIELD;
/* debugging stuff...*/
strncpy(tok, at, len);
tok[len] = '\0';
syslog(LOG_DEBUG, "%s(): token is %s", __func__, tok);
return 0;
}
int on_header_value(http_parser *parser, const char *at, size_t len)
{
char tok[256];
client_t *c;
message_t *msg;
c = parser->data;
msg = c->msg;
extend_message_header_value(msg, msg->num_headers - 1, at, len);
msg->last_header_element = VALUE;
/* debugging stuff...*/
strncpy(tok, at, len);
tok[len] = '\0';
syslog(LOG_DEBUG, "%s(): token is %s", __func__, tok);
return 0;
}
int on_headers_complete(http_parser *parser)
{
client_t *c;
message_t *msg;
c = parser->data;
msg = c->msg;
syslog(LOG_DEBUG, "%s()...", __func__);
/* Store HTTP major/minor codes */
msg->http_major = parser->http_major;
msg->http_minor = parser->http_minor;
/* Break down the url into the path and query-string */
msg->request_pathlen = strcspn(msg->request_url, "?");
if(msg->request_pathlen)
{
msg->request_path = malloc(msg->request_pathlen);
strncpy(msg->request_path, msg->request_url, msg->request_pathlen);
}
msg->query_stringlen = strlen(&msg->request_url[msg->request_pathlen+1]);
if(msg->query_stringlen)
{
msg->query_string = malloc(msg->query_stringlen);
strcpy(msg->query_string, &msg->request_url[msg->request_pathlen+1]);
}
msg->method = parser->method;
switch(parser->method)
{
case(HTTP_GET):
msg->method_name = "GET";
break;
case(HTTP_PUT):
msg->method_name = "PUT";
break;
case(HTTP_POST):
msg->method_name = "POST";
break;
case(HTTP_DELETE):
msg->method_name = "DELETE";
break;
default:
msg->method_name = "UNSUPPORTED";
break;
}
describe_message(msg);
return 0;
}
int on_body(http_parser *parser, const char *at, size_t len)
{
client_t *c;
c = parser->data;
extend_message_body(c->msg, at, len);
syslog(LOG_DEBUG, "%s():...", __func__);
return 0;
}