-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_response_decode.c
164 lines (146 loc) · 4.79 KB
/
http_response_decode.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
#include "httpkit/http_retcode.h"
#include "httpkit/http_response_decode.h"
#include "http_header_decode.h"
#include "misc.h"
/* values of http_response_decode_context::state */
enum {
HTTP_RES_EXPECT_STATUS_LINE,
HTTP_RES_EXPECT_HEADER,
HTTP_RES_EXPECT_CONTENT,
HTTP_RES_EXPECT_END,
};
static void __http_response_status_line_reset(struct http_response_status_line* st) {
st->code = 0;
offlen_reset(&st->text);
offlen_reset(&st->version);
}
void http_response_decode_context_init(struct http_response_decode_context* ctx) {
ctx->content_offset = 0;
ctx->content_length = 0;
__http_response_status_line_reset(&ctx->status_line);
cvector_init(&ctx->header_list, sizeof(struct kvpair));
ctx->state = HTTP_RES_EXPECT_STATUS_LINE;
ctx->offset = 0;
}
void http_response_decode_context_clear(struct http_response_decode_context* ctx) {
ctx->offset = 0;
ctx->state = HTTP_RES_EXPECT_STATUS_LINE;
cvector_clear(&ctx->header_list);
__http_response_status_line_reset(&ctx->status_line);
ctx->content_length = 0;
ctx->content_offset = 0;
}
void http_response_decode_context_destroy(struct http_response_decode_context* ctx) {
cvector_destroy(&ctx->header_list);
__http_response_status_line_reset(&ctx->status_line);
}
/* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
static int __status_line_decode(const char* base, unsigned long len,
struct http_response_status_line* l,
unsigned long* offset) {
const char* cursor = base;
const char* end = base + len;
l->version.off = 0;
while (*cursor != ' ') {
++cursor;
if (cursor == end) {
return HRC_MORE_DATA;
}
}
l->version.len = cursor - base;
++cursor; /* skips ' ' */
l->code = 0;
while (1) {
if (cursor == end) {
return HRC_MORE_DATA;
}
if (*cursor == ' ') {
break;
}
if (*cursor < '0' || *cursor > '9') {
return HRC_RES_LINE;
}
l->code = l->code * 10 + (*cursor - '0');
++cursor;
}
l->text.off = cursor + 1 - base;
while (1) {
++cursor;
if (cursor == end) {
return HRC_MORE_DATA;
}
if (*cursor == '\r') {
l->text.len = cursor - base - l->text.off;
++cursor;
if (cursor == end) {
return HRC_MORE_DATA;
}
if (*cursor == '\n') {
(*offset) += (cursor + 1 - base);
return HRC_OK;
}
}
}
return HRC_RES_LINE; /* unreachable */
}
int http_response_decode(struct http_response_decode_context* ctx, const void* base,
unsigned long len) {
const char* data = (const char*)base;
if (ctx->state == HTTP_RES_EXPECT_END) {
return HRC_OK;
}
data += ctx->offset;
len -= ctx->offset;
switch (ctx->state) {
case HTTP_RES_EXPECT_STATUS_LINE: {
unsigned long parsed_len = 0;
int rc = __status_line_decode(data, len, &ctx->status_line, &parsed_len);
len -= parsed_len;
data += parsed_len;
ctx->offset += parsed_len;
if (rc != HRC_OK) {
return rc;
}
ctx->state = HTTP_RES_EXPECT_HEADER;
}
case HTTP_RES_EXPECT_HEADER: {
unsigned long parsed_len = 0;
int rc = http_header_decode(data, len, base, &ctx->header_list, &parsed_len);
len -= parsed_len;
ctx->offset += parsed_len;
if (rc != HRC_OK) {
return rc;
}
set_content_len(base, &ctx->header_list, &ctx->content_length);
ctx->state = HTTP_RES_EXPECT_CONTENT;
}
case HTTP_RES_EXPECT_CONTENT: {
if (len < ctx->content_length) {
return HRC_MORE_DATA;
}
ctx->content_offset = ctx->offset;
ctx->offset += ctx->content_length;
ctx->state = HTTP_RES_EXPECT_END;
}
}
return HRC_OK;
}
void http_response_get_header(struct http_response_decode_context* ctx, unsigned int idx,
struct offlen* key, struct offlen* value) {
struct kvpair* item = (struct kvpair*)cvector_at(&ctx->header_list, idx);
if (key) {
*key = item->key;
}
if (value) {
*value = item->value;
}
}
void http_response_find_header(struct http_response_decode_context* ctx, const void* base,
const char* key, unsigned int klen, struct offlen* value) {
struct kvpair* item = kvpair_vector_lookup(&ctx->header_list, base, key, klen);
if (item) {
*value = item->value;
} else {
offlen_reset(value);
}
}