-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_http_parser.c
More file actions
230 lines (153 loc) · 5.3 KB
/
server_http_parser.c
File metadata and controls
230 lines (153 loc) · 5.3 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Created by simone_mancini on 19/10/16.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "server_http_parser.h"
#include "http-parser-master/http_parser.h"
#include "error_management.h"
#include "typedef.h"
#define MAX_HEADER_LINES 10
#define MAX_ELEMENT_SIZE 2048
#define MAX_CHUNKS 16
#define HOST "Host"
/*SETTINGS ONLY FOR DEBUG*/
/*http_parser_settings settings_null =
{.on_message_begin = 0
,.on_header_field = 0
,.on_header_value = 0
,.on_url = 0
,.on_status = 0
,.on_body = 0
,.on_headers_complete = 0
,.on_message_complete = 0
,.on_chunk_header = 0
,.on_chunk_complete = 0
};*/
/*struct message{
char url[MAX_ELEMENT_SIZE];
};
struct message *mess;*/
//char url[MAX_ELEMENT_SIZE];
void initialize_request_parser(http_parser *parser){
parser = malloc(sizeof(http_parser));
if(parser == NULL)
die_with_error("error malloc() for parser");
http_parser_init(parser, HTTP_REQUEST);
}
int header_field_compare(rcv_msg *msg, const char *value, size_t length){
if(strncmp(msg->field, "Host", length) == 0){
//printf("Host parsing\n");
msg->host = malloc(length*sizeof(char));
if (msg->host == NULL)
die_with_error("error catching host");
strncpy(msg->host, value, length);
msg->host_length = length;
return 0;
}
if(strncmp(msg->field, "User-Agent", length) == 0){
msg->user_agent = malloc(length*sizeof(char));
if (msg->user_agent == NULL)
die_with_error("error catching host");
strncpy(msg->user_agent, value, length);
msg->user_agent_length = length;
return 0;
}
if(strncmp(msg->field, "Accept", length) == 0){
msg->accept = malloc(length*sizeof(char));
if (msg->accept == NULL)
die_with_error("error catching host");
strncpy(msg->accept, value, length);
msg->accept_length = length;
return 0;
}
if(strncmp(msg->field, "Accept-Language", length) == 0){
msg->accept_language = malloc(length*sizeof(char));
if (msg->accept_language == NULL)
die_with_error("error catching host");
strncpy(msg->accept_language, value, length);
msg->accept_language_length = length;
return 0;
}
if(strncmp(msg->field, "Accept-Encoding", length) == 0){
msg->accept_encoding = malloc(length*sizeof(char));
if (msg->accept_encoding == NULL)
die_with_error("error catching host");
strncpy(msg->accept_encoding, value, length);
msg->accept_encoding_length = length;
return 0;
}
if(strncmp(msg->field, "Referer", length) == 0){
msg->referer = malloc(length*sizeof(char));
if (msg->referer == NULL)
die_with_error("error catching host");
strncpy(msg->referer, value, length);
msg->referer_length = length;
return 0;
}
if(strncmp(msg->field, "Connection", length) == 0){
msg->connection = malloc(length*sizeof(char));
if (msg->connection == NULL)
die_with_error("error catching host");
strncpy(msg->connection, value, length);
msg->connection_length = length;
return 0;
}
return -1;
}
int url_reader(http_parser *p, const char *buf, size_t length){
//printf("Url: %.*s\n", (int)length, buf);
rcv_msg *msg = (rcv_msg *) p->data;
msg->url = malloc(length*sizeof(char));
strncpy(msg->url, buf, length);
msg->url_length = length;
//printf("msg->url: %.*s\n", (int) length, msg->url);
return 0;
}
int on_message_begin(http_parser* _) {
(void)_;
printf("\n***MESSAGE BEGIN***\n\n");
return 0;
}
int on_message_complete(http_parser* _) {
(void)_;
printf("\n***MESSAGE COMPLETE***\n\n");
return 0;
}
int on_header_field(http_parser *p, const char* at, size_t length) {
rcv_msg *msg = (rcv_msg *) p->data;
free(msg->field);
msg->field = malloc(length*sizeof(char));
memset(msg->field, '\0', length);
strncpy(msg->field, at, length);
//printf("Field: %.*s with length %d\n", (int) length, msg->field, (int) length);
return 0;
}
int on_header_value(http_parser *p, const char* at, size_t length) {
rcv_msg *msg = (rcv_msg *) p->data;
//printf("Field: %s\n", msg->field);
//printf("Value: %.*s\n", (int) length, at);
if(header_field_compare(msg, at, length) == -1)
printf("problems in field reading\n");
return 0;
}
void parse_data(rcv_msg *msg, char *buf, int rcvd){
size_t nparsed;
http_parser_settings settings;
http_parser *parser;
//printf("parser\n");
memset(&settings, 0, sizeof(settings));
settings.on_message_begin = on_message_begin;
settings.on_url = url_reader;
settings.on_message_complete = on_message_complete;
settings.on_header_value = on_header_value;
settings.on_header_field = on_header_field;
parser = malloc(sizeof(http_parser));
http_parser_init(parser, HTTP_REQUEST);
parser->data = msg;
nparsed = http_parser_execute(parser, &settings, buf, (size_t) rcvd);
if(nparsed != (size_t) rcvd)
die_with_error("nparsed != rcv");
}