-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.c
142 lines (117 loc) · 3.42 KB
/
http.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
#include "http.h"
#include "map.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#define koko fprintf(stderr, "ライン %d: %s() ここまで\n", __LINE__, __func__)
int request_on_message_begin(http_parser *);
int request_on_url(http_parser *, const char *at, size_t length);
int request_on_status(http_parser *, const char *at, size_t length);
int request_on_header_field(http_parser *, const char *at, size_t length);
int request_on_header_value(http_parser *, const char *at, size_t length);
int request_on_headers_complete(http_parser *);
int request_on_body(http_parser *, const char *at, size_t length);
int request_on_message_complete(http_parser *);
int request_on_chunk_header(http_parser *);
int request_on_chunk_complete(http_parser *);
struct http_parser_settings request_settings = {
.on_message_begin = request_on_message_begin,
.on_url = request_on_url,
.on_status = request_on_status,
.on_header_field = request_on_header_field,
.on_header_value = request_on_header_value,
.on_headers_complete = request_on_headers_complete,
.on_body = request_on_body,
.on_message_complete = request_on_message_complete,
.on_chunk_header = request_on_chunk_header,
.on_chunk_complete = request_on_chunk_complete
};
http_data *http_data_new()
{
http_data *hp = malloc(sizeof(http_data));
hp->parser = malloc(sizeof(struct http_parser));
http_parser_init(hp->parser, HTTP_REQUEST);
hp->parser->data = hp;
hp->head = malloc(sizeof(map_str_t));
map_init(hp->head);
hp->url = string_new();
hp->body = string_new();
hp->settings = &request_settings;
hp->state = NONE;
return hp;
}
void http_data_free(http_data *hp)
{
if (!hp) return;
free(hp->parser);
map_deinit(hp->head);
string_free(hp->url);
string_free(hp->body);
free(hp);
hp = NULL;
}
int request_on_message_begin(http_parser *p)
{
http_data *hp = (http_data *)p->data;
hp->state = BEGIN;
return 0;
}
int request_on_url(http_parser *p, const char *at, size_t length)
{
http_data *hp = (http_data *)p->data;
hp->state = URL;
string_ncat(hp->url, at, length);
return 0;
}
int request_on_status(http_parser *p, const char *at, size_t length)
{
http_data *hp = (http_data *)p->data;
hp->state = STATUS;
return 0;
}
int request_on_header_field(http_parser *p, const char *at, size_t length)
{
http_data *hp = (http_data *)p->data;
hp->state = FIELD;
hp->_last_key = strndup(at, length);
return 0;
}
int request_on_header_value(http_parser *p, const char *at, size_t length)
{
http_data *hp = (http_data *)p->data;
hp->state = VALUE;
char *value = strndup(at, length);
map_set(hp->head, hp->_last_key, value);
return 0;
}
int request_on_headers_complete(http_parser *p)
{
http_data *hp = (http_data *)p->data;
hp->state = HEADER_END;
return 0;
}
int request_on_body(http_parser *p, const char *at, size_t length)
{
http_data *hp = (http_data *)p->data;
hp->state = BODY;
string_ncat(hp->body, at, length);
return 0;
}
int request_on_message_complete(http_parser *p)
{
http_data *hp = (http_data *)p->data;
hp->state = END;
return 0;
}
int request_on_chunk_header(http_parser *p)
{
http_data *hp = (http_data *)p->data;
hp->state = CHUNK;
return 0;
}
int request_on_chunk_complete(http_parser *p)
{
http_data *hp = (http_data *)p->data;
hp->state = END;
return 0;
}