midori_http_parser 0.6.1.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.gitmodules +6 -0
- data/.travis.yml +33 -0
- data/Gemfile +2 -0
- data/LICENSE-MIT +20 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/bench/standalone.rb +23 -0
- data/bench/thin.rb +58 -0
- data/ext/ruby_http_parser/.gitignore +1 -0
- data/ext/ruby_http_parser/RubyHttpParserService.java +18 -0
- data/ext/ruby_http_parser/ext_help.h +18 -0
- data/ext/ruby_http_parser/extconf.rb +24 -0
- data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +495 -0
- data/ext/ruby_http_parser/ruby_http_parser.c +516 -0
- data/ext/ruby_http_parser/vendor/.gitkeep +0 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/AUTHORS +32 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +48 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/README.md +183 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/TODO +28 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/build.xml +74 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +2175 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +304 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPCallback.java +8 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPDataCallback.java +34 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPErrorCallback.java +12 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPException.java +9 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +113 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParser.java +36 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +256 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserType.java +13 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/Util.java +111 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPCallback.java +5 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPDataCallback.java +25 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPErrorCallback.java +7 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +2171 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +83 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +374 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/ParseUrl.java +51 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +69 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +52 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +16 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +48 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +212 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +62 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +117 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +27 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Url.java +127 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +236 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +59 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/test.c +3425 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +845 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tests.utf8 +17 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tools/byte_constants.rb +6 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tools/const_char.rb +13 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tools/lowcase.rb +15 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tools/parse_tests.rb +33 -0
- data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +68 -0
- data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +23 -0
- data/ext/ruby_http_parser/vendor/http-parser/README.md +246 -0
- data/ext/ruby_http_parser/vendor/http-parser/bench.c +111 -0
- data/ext/ruby_http_parser/vendor/http-parser/contrib/parsertrace.c +160 -0
- data/ext/ruby_http_parser/vendor/http-parser/contrib/url_parser.c +47 -0
- data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +2470 -0
- data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +111 -0
- data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +432 -0
- data/ext/ruby_http_parser/vendor/http-parser/test.c +4226 -0
- data/ext/ruby_http_parser/vendor/http-parser/test_fast +0 -0
- data/ext/ruby_http_parser/vendor/http-parser/test_g +0 -0
- data/lib/http/parser.rb +1 -0
- data/lib/http_parser.rb +21 -0
- data/midori_http_parser.gemspec +24 -0
- data/spec/parser_spec.rb +376 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/requests.json +631 -0
- data/spec/support/responses.json +375 -0
- data/tasks/compile.rake +42 -0
- data/tasks/fixtures.rake +71 -0
- data/tasks/spec.rake +5 -0
- data/tasks/submodules.rake +7 -0
- metadata +206 -0
@@ -0,0 +1,516 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "ext_help.h"
|
3
|
+
#include "ryah_http_parser.h"
|
4
|
+
|
5
|
+
#define GET_WRAPPER(N, from) ParserWrapper *N = (ParserWrapper *)(from)->data;
|
6
|
+
#define HASH_CAT(h, k, ptr, len) \
|
7
|
+
do { \
|
8
|
+
VALUE __v = rb_hash_aref(h, k); \
|
9
|
+
if (__v != Qnil) { \
|
10
|
+
rb_str_cat(__v, ptr, len); \
|
11
|
+
} else { \
|
12
|
+
rb_hash_aset(h, k, rb_str_new(ptr, len)); \
|
13
|
+
} \
|
14
|
+
} while(0)
|
15
|
+
|
16
|
+
typedef struct ParserWrapper {
|
17
|
+
ryah_http_parser parser;
|
18
|
+
|
19
|
+
VALUE request_url;
|
20
|
+
|
21
|
+
VALUE headers;
|
22
|
+
|
23
|
+
VALUE upgrade_data;
|
24
|
+
|
25
|
+
VALUE on_message_begin;
|
26
|
+
VALUE on_headers_complete;
|
27
|
+
VALUE on_body;
|
28
|
+
VALUE on_message_complete;
|
29
|
+
|
30
|
+
VALUE callback_object;
|
31
|
+
VALUE stopped;
|
32
|
+
VALUE completed;
|
33
|
+
|
34
|
+
VALUE header_value_type;
|
35
|
+
|
36
|
+
VALUE last_field_name;
|
37
|
+
VALUE curr_field_name;
|
38
|
+
|
39
|
+
enum ryah_http_parser_type type;
|
40
|
+
} ParserWrapper;
|
41
|
+
|
42
|
+
void ParserWrapper_init(ParserWrapper *wrapper) {
|
43
|
+
ryah_http_parser_init(&wrapper->parser, wrapper->type);
|
44
|
+
wrapper->parser.status_code = 0;
|
45
|
+
wrapper->parser.http_major = 0;
|
46
|
+
wrapper->parser.http_minor = 0;
|
47
|
+
|
48
|
+
wrapper->request_url = Qnil;
|
49
|
+
|
50
|
+
wrapper->upgrade_data = Qnil;
|
51
|
+
|
52
|
+
wrapper->headers = Qnil;
|
53
|
+
wrapper->completed = Qfalse;
|
54
|
+
|
55
|
+
wrapper->last_field_name = Qnil;
|
56
|
+
wrapper->curr_field_name = Qnil;
|
57
|
+
}
|
58
|
+
|
59
|
+
void ParserWrapper_mark(void *data) {
|
60
|
+
if(data) {
|
61
|
+
ParserWrapper *wrapper = (ParserWrapper *) data;
|
62
|
+
rb_gc_mark_maybe(wrapper->request_url);
|
63
|
+
rb_gc_mark_maybe(wrapper->upgrade_data);
|
64
|
+
rb_gc_mark_maybe(wrapper->headers);
|
65
|
+
rb_gc_mark_maybe(wrapper->on_message_begin);
|
66
|
+
rb_gc_mark_maybe(wrapper->on_headers_complete);
|
67
|
+
rb_gc_mark_maybe(wrapper->on_body);
|
68
|
+
rb_gc_mark_maybe(wrapper->on_message_complete);
|
69
|
+
rb_gc_mark_maybe(wrapper->callback_object);
|
70
|
+
rb_gc_mark_maybe(wrapper->last_field_name);
|
71
|
+
rb_gc_mark_maybe(wrapper->curr_field_name);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
void ParserWrapper_free(void *data) {
|
76
|
+
if(data) {
|
77
|
+
free(data);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
static VALUE cParser;
|
82
|
+
static VALUE cRequestParser;
|
83
|
+
static VALUE cResponseParser;
|
84
|
+
|
85
|
+
static VALUE eParserError;
|
86
|
+
|
87
|
+
static ID Icall;
|
88
|
+
static ID Ion_message_begin;
|
89
|
+
static ID Ion_headers_complete;
|
90
|
+
static ID Ion_body;
|
91
|
+
static ID Ion_message_complete;
|
92
|
+
|
93
|
+
static VALUE Sstop;
|
94
|
+
static VALUE Sreset;
|
95
|
+
static VALUE Sarrays;
|
96
|
+
static VALUE Sstrings;
|
97
|
+
static VALUE Smixed;
|
98
|
+
|
99
|
+
/** Callbacks **/
|
100
|
+
|
101
|
+
int on_message_begin(ryah_http_parser *parser) {
|
102
|
+
GET_WRAPPER(wrapper, parser);
|
103
|
+
|
104
|
+
wrapper->request_url = rb_str_new2("");
|
105
|
+
wrapper->headers = rb_hash_new();
|
106
|
+
wrapper->upgrade_data = rb_str_new2("");
|
107
|
+
|
108
|
+
VALUE ret = Qnil;
|
109
|
+
|
110
|
+
if (wrapper->callback_object != Qnil && rb_respond_to(wrapper->callback_object, Ion_message_begin)) {
|
111
|
+
ret = rb_funcall(wrapper->callback_object, Ion_message_begin, 0);
|
112
|
+
} else if (wrapper->on_message_begin != Qnil) {
|
113
|
+
ret = rb_funcall(wrapper->on_message_begin, Icall, 0);
|
114
|
+
}
|
115
|
+
|
116
|
+
if (ret == Sstop) {
|
117
|
+
wrapper->stopped = Qtrue;
|
118
|
+
return -1;
|
119
|
+
} else {
|
120
|
+
return 0;
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
int on_url(ryah_http_parser *parser, const char *at, size_t length) {
|
125
|
+
GET_WRAPPER(wrapper, parser);
|
126
|
+
rb_str_cat(wrapper->request_url, at, length);
|
127
|
+
return 0;
|
128
|
+
}
|
129
|
+
|
130
|
+
int on_header_field(ryah_http_parser *parser, const char *at, size_t length) {
|
131
|
+
GET_WRAPPER(wrapper, parser);
|
132
|
+
|
133
|
+
if (wrapper->curr_field_name == Qnil) {
|
134
|
+
wrapper->last_field_name = Qnil;
|
135
|
+
wrapper->curr_field_name = rb_str_new(at, length);
|
136
|
+
} else {
|
137
|
+
rb_str_cat(wrapper->curr_field_name, at, length);
|
138
|
+
}
|
139
|
+
|
140
|
+
return 0;
|
141
|
+
}
|
142
|
+
|
143
|
+
int on_header_value(ryah_http_parser *parser, const char *at, size_t length) {
|
144
|
+
GET_WRAPPER(wrapper, parser);
|
145
|
+
|
146
|
+
int new_field = 0;
|
147
|
+
VALUE current_value;
|
148
|
+
|
149
|
+
if (wrapper->last_field_name == Qnil) {
|
150
|
+
new_field = 1;
|
151
|
+
wrapper->last_field_name = wrapper->curr_field_name;
|
152
|
+
wrapper->curr_field_name = Qnil;
|
153
|
+
}
|
154
|
+
|
155
|
+
current_value = rb_hash_aref(wrapper->headers, wrapper->last_field_name);
|
156
|
+
|
157
|
+
if (new_field == 1) {
|
158
|
+
if (current_value == Qnil) {
|
159
|
+
if (wrapper->header_value_type == Sarrays) {
|
160
|
+
rb_hash_aset(wrapper->headers, wrapper->last_field_name, rb_ary_new3(1, rb_str_new2("")));
|
161
|
+
} else {
|
162
|
+
rb_hash_aset(wrapper->headers, wrapper->last_field_name, rb_str_new2(""));
|
163
|
+
}
|
164
|
+
} else {
|
165
|
+
if (wrapper->header_value_type == Smixed) {
|
166
|
+
if (TYPE(current_value) == T_STRING) {
|
167
|
+
rb_hash_aset(wrapper->headers, wrapper->last_field_name, rb_ary_new3(2, current_value, rb_str_new2("")));
|
168
|
+
} else {
|
169
|
+
rb_ary_push(current_value, rb_str_new2(""));
|
170
|
+
}
|
171
|
+
} else if (wrapper->header_value_type == Sarrays) {
|
172
|
+
rb_ary_push(current_value, rb_str_new2(""));
|
173
|
+
} else {
|
174
|
+
rb_str_cat(current_value, ", ", 2);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
current_value = rb_hash_aref(wrapper->headers, wrapper->last_field_name);
|
178
|
+
}
|
179
|
+
|
180
|
+
if (TYPE(current_value) == T_ARRAY) {
|
181
|
+
current_value = rb_ary_entry(current_value, -1);
|
182
|
+
}
|
183
|
+
|
184
|
+
rb_str_cat(current_value, at, length);
|
185
|
+
|
186
|
+
return 0;
|
187
|
+
}
|
188
|
+
|
189
|
+
int on_headers_complete(ryah_http_parser *parser) {
|
190
|
+
GET_WRAPPER(wrapper, parser);
|
191
|
+
|
192
|
+
VALUE ret = Qnil;
|
193
|
+
|
194
|
+
if (wrapper->callback_object != Qnil && rb_respond_to(wrapper->callback_object, Ion_headers_complete)) {
|
195
|
+
ret = rb_funcall(wrapper->callback_object, Ion_headers_complete, 1, wrapper->headers);
|
196
|
+
} else if (wrapper->on_headers_complete != Qnil) {
|
197
|
+
ret = rb_funcall(wrapper->on_headers_complete, Icall, 1, wrapper->headers);
|
198
|
+
}
|
199
|
+
|
200
|
+
if (ret == Sstop) {
|
201
|
+
wrapper->stopped = Qtrue;
|
202
|
+
return -1;
|
203
|
+
} else if (ret == Sreset){
|
204
|
+
return 1;
|
205
|
+
} else {
|
206
|
+
return 0;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
int on_body(ryah_http_parser *parser, const char *at, size_t length) {
|
211
|
+
GET_WRAPPER(wrapper, parser);
|
212
|
+
|
213
|
+
VALUE ret = Qnil;
|
214
|
+
|
215
|
+
if (wrapper->callback_object != Qnil && rb_respond_to(wrapper->callback_object, Ion_body)) {
|
216
|
+
ret = rb_funcall(wrapper->callback_object, Ion_body, 1, rb_str_new(at, length));
|
217
|
+
} else if (wrapper->on_body != Qnil) {
|
218
|
+
ret = rb_funcall(wrapper->on_body, Icall, 1, rb_str_new(at, length));
|
219
|
+
}
|
220
|
+
|
221
|
+
if (ret == Sstop) {
|
222
|
+
wrapper->stopped = Qtrue;
|
223
|
+
return -1;
|
224
|
+
} else {
|
225
|
+
return 0;
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
int on_message_complete(ryah_http_parser *parser) {
|
230
|
+
GET_WRAPPER(wrapper, parser);
|
231
|
+
|
232
|
+
VALUE ret = Qnil;
|
233
|
+
wrapper->completed = Qtrue;
|
234
|
+
|
235
|
+
if (wrapper->callback_object != Qnil && rb_respond_to(wrapper->callback_object, Ion_message_complete)) {
|
236
|
+
ret = rb_funcall(wrapper->callback_object, Ion_message_complete, 0);
|
237
|
+
} else if (wrapper->on_message_complete != Qnil) {
|
238
|
+
ret = rb_funcall(wrapper->on_message_complete, Icall, 0);
|
239
|
+
}
|
240
|
+
|
241
|
+
if (ret == Sstop) {
|
242
|
+
wrapper->stopped = Qtrue;
|
243
|
+
return -1;
|
244
|
+
} else {
|
245
|
+
return 0;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
static ryah_http_parser_settings settings = {
|
250
|
+
.on_message_begin = on_message_begin,
|
251
|
+
.on_url = on_url,
|
252
|
+
.on_header_field = on_header_field,
|
253
|
+
.on_header_value = on_header_value,
|
254
|
+
.on_headers_complete = on_headers_complete,
|
255
|
+
.on_body = on_body,
|
256
|
+
.on_message_complete = on_message_complete
|
257
|
+
};
|
258
|
+
|
259
|
+
VALUE Parser_alloc_by_type(VALUE klass, enum ryah_http_parser_type type) {
|
260
|
+
ParserWrapper *wrapper = ALLOC_N(ParserWrapper, 1);
|
261
|
+
wrapper->type = type;
|
262
|
+
wrapper->parser.data = wrapper;
|
263
|
+
|
264
|
+
wrapper->on_message_begin = Qnil;
|
265
|
+
wrapper->on_headers_complete = Qnil;
|
266
|
+
wrapper->on_body = Qnil;
|
267
|
+
wrapper->on_message_complete = Qnil;
|
268
|
+
|
269
|
+
wrapper->callback_object = Qnil;
|
270
|
+
|
271
|
+
ParserWrapper_init(wrapper);
|
272
|
+
|
273
|
+
return Data_Wrap_Struct(klass, ParserWrapper_mark, ParserWrapper_free, wrapper);
|
274
|
+
}
|
275
|
+
|
276
|
+
VALUE Parser_alloc(VALUE klass) {
|
277
|
+
return Parser_alloc_by_type(klass, HTTP_BOTH);
|
278
|
+
}
|
279
|
+
|
280
|
+
VALUE RequestParser_alloc(VALUE klass) {
|
281
|
+
return Parser_alloc_by_type(klass, HTTP_REQUEST);
|
282
|
+
}
|
283
|
+
|
284
|
+
VALUE ResponseParser_alloc(VALUE klass) {
|
285
|
+
return Parser_alloc_by_type(klass, HTTP_RESPONSE);
|
286
|
+
}
|
287
|
+
|
288
|
+
VALUE Parser_strict_p(VALUE klass) {
|
289
|
+
return HTTP_PARSER_STRICT == 1 ? Qtrue : Qfalse;
|
290
|
+
}
|
291
|
+
|
292
|
+
VALUE Parser_initialize(int argc, VALUE *argv, VALUE self) {
|
293
|
+
ParserWrapper *wrapper = NULL;
|
294
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
295
|
+
|
296
|
+
wrapper->header_value_type = rb_iv_get(CLASS_OF(self), "@default_header_value_type");
|
297
|
+
|
298
|
+
if (argc == 1) {
|
299
|
+
wrapper->callback_object = argv[0];
|
300
|
+
}
|
301
|
+
|
302
|
+
if (argc == 2) {
|
303
|
+
wrapper->callback_object = argv[0];
|
304
|
+
wrapper->header_value_type = argv[1];
|
305
|
+
}
|
306
|
+
|
307
|
+
return self;
|
308
|
+
}
|
309
|
+
|
310
|
+
VALUE Parser_execute(VALUE self, VALUE data) {
|
311
|
+
ParserWrapper *wrapper = NULL;
|
312
|
+
|
313
|
+
Check_Type(data, T_STRING);
|
314
|
+
char *ptr = RSTRING_PTR(data);
|
315
|
+
long len = RSTRING_LEN(data);
|
316
|
+
|
317
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
318
|
+
|
319
|
+
wrapper->stopped = Qfalse;
|
320
|
+
size_t nparsed = ryah_http_parser_execute(&wrapper->parser, &settings, ptr, len);
|
321
|
+
|
322
|
+
if (wrapper->parser.upgrade) {
|
323
|
+
if (RTEST(wrapper->stopped) && !RTEST(wrapper->completed))
|
324
|
+
nparsed += 1;
|
325
|
+
|
326
|
+
if (nparsed < len)
|
327
|
+
rb_str_cat(wrapper->upgrade_data, ptr + nparsed, len - nparsed);
|
328
|
+
|
329
|
+
} else if (nparsed != (size_t)len) {
|
330
|
+
if (!RTEST(wrapper->stopped) && !RTEST(wrapper->completed))
|
331
|
+
rb_raise(eParserError, "Could not parse data entirely (%zu != %zu)", nparsed, len);
|
332
|
+
else
|
333
|
+
nparsed += 1; // error states fail on the current character
|
334
|
+
}
|
335
|
+
|
336
|
+
return INT2FIX(nparsed);
|
337
|
+
}
|
338
|
+
|
339
|
+
VALUE Parser_set_on_message_begin(VALUE self, VALUE callback) {
|
340
|
+
ParserWrapper *wrapper = NULL;
|
341
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
342
|
+
|
343
|
+
wrapper->on_message_begin = callback;
|
344
|
+
return callback;
|
345
|
+
}
|
346
|
+
|
347
|
+
VALUE Parser_set_on_headers_complete(VALUE self, VALUE callback) {
|
348
|
+
ParserWrapper *wrapper = NULL;
|
349
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
350
|
+
|
351
|
+
wrapper->on_headers_complete = callback;
|
352
|
+
return callback;
|
353
|
+
}
|
354
|
+
|
355
|
+
VALUE Parser_set_on_body(VALUE self, VALUE callback) {
|
356
|
+
ParserWrapper *wrapper = NULL;
|
357
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
358
|
+
|
359
|
+
wrapper->on_body = callback;
|
360
|
+
return callback;
|
361
|
+
}
|
362
|
+
|
363
|
+
VALUE Parser_set_on_message_complete(VALUE self, VALUE callback) {
|
364
|
+
ParserWrapper *wrapper = NULL;
|
365
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
366
|
+
|
367
|
+
wrapper->on_message_complete = callback;
|
368
|
+
return callback;
|
369
|
+
}
|
370
|
+
|
371
|
+
VALUE Parser_keep_alive_p(VALUE self) {
|
372
|
+
ParserWrapper *wrapper = NULL;
|
373
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
374
|
+
|
375
|
+
return http_should_keep_alive(&wrapper->parser) == 1 ? Qtrue : Qfalse;
|
376
|
+
}
|
377
|
+
|
378
|
+
VALUE Parser_upgrade_p(VALUE self) {
|
379
|
+
ParserWrapper *wrapper = NULL;
|
380
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
381
|
+
|
382
|
+
return wrapper->parser.upgrade ? Qtrue : Qfalse;
|
383
|
+
}
|
384
|
+
|
385
|
+
VALUE Parser_http_version(VALUE self) {
|
386
|
+
ParserWrapper *wrapper = NULL;
|
387
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
388
|
+
|
389
|
+
if (wrapper->parser.http_major == 0 && wrapper->parser.http_minor == 0)
|
390
|
+
return Qnil;
|
391
|
+
else
|
392
|
+
return rb_ary_new3(2, INT2FIX(wrapper->parser.http_major), INT2FIX(wrapper->parser.http_minor));
|
393
|
+
}
|
394
|
+
|
395
|
+
VALUE Parser_http_major(VALUE self) {
|
396
|
+
ParserWrapper *wrapper = NULL;
|
397
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
398
|
+
|
399
|
+
if (wrapper->parser.http_major == 0 && wrapper->parser.http_minor == 0)
|
400
|
+
return Qnil;
|
401
|
+
else
|
402
|
+
return INT2FIX(wrapper->parser.http_major);
|
403
|
+
}
|
404
|
+
|
405
|
+
VALUE Parser_http_minor(VALUE self) {
|
406
|
+
ParserWrapper *wrapper = NULL;
|
407
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
408
|
+
|
409
|
+
if (wrapper->parser.http_major == 0 && wrapper->parser.http_minor == 0)
|
410
|
+
return Qnil;
|
411
|
+
else
|
412
|
+
return INT2FIX(wrapper->parser.http_minor);
|
413
|
+
}
|
414
|
+
|
415
|
+
VALUE Parser_http_method(VALUE self) {
|
416
|
+
ParserWrapper *wrapper = NULL;
|
417
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
418
|
+
|
419
|
+
if (wrapper->parser.type == HTTP_REQUEST)
|
420
|
+
return rb_str_new2(http_method_str(wrapper->parser.method));
|
421
|
+
else
|
422
|
+
return Qnil;
|
423
|
+
}
|
424
|
+
|
425
|
+
VALUE Parser_status_code(VALUE self) {
|
426
|
+
ParserWrapper *wrapper = NULL;
|
427
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
428
|
+
|
429
|
+
if (wrapper->parser.status_code)
|
430
|
+
return INT2FIX(wrapper->parser.status_code);
|
431
|
+
else
|
432
|
+
return Qnil;
|
433
|
+
}
|
434
|
+
|
435
|
+
#define DEFINE_GETTER(name) \
|
436
|
+
VALUE Parser_##name(VALUE self) { \
|
437
|
+
ParserWrapper *wrapper = NULL; \
|
438
|
+
DATA_GET(self, ParserWrapper, wrapper); \
|
439
|
+
return wrapper->name; \
|
440
|
+
}
|
441
|
+
|
442
|
+
DEFINE_GETTER(request_url);
|
443
|
+
DEFINE_GETTER(headers);
|
444
|
+
DEFINE_GETTER(upgrade_data);
|
445
|
+
DEFINE_GETTER(header_value_type);
|
446
|
+
|
447
|
+
VALUE Parser_set_header_value_type(VALUE self, VALUE val) {
|
448
|
+
if (val != Sarrays && val != Sstrings && val != Smixed) {
|
449
|
+
rb_raise(rb_eArgError, "Invalid header value type");
|
450
|
+
}
|
451
|
+
|
452
|
+
ParserWrapper *wrapper = NULL;
|
453
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
454
|
+
wrapper->header_value_type = val;
|
455
|
+
return wrapper->header_value_type;
|
456
|
+
}
|
457
|
+
|
458
|
+
VALUE Parser_reset(VALUE self) {
|
459
|
+
ParserWrapper *wrapper = NULL;
|
460
|
+
DATA_GET(self, ParserWrapper, wrapper);
|
461
|
+
|
462
|
+
ParserWrapper_init(wrapper);
|
463
|
+
|
464
|
+
return Qtrue;
|
465
|
+
}
|
466
|
+
|
467
|
+
void Init_ruby_http_parser() {
|
468
|
+
VALUE mHTTP = rb_define_module("HTTP");
|
469
|
+
cParser = rb_define_class_under(mHTTP, "Parser", rb_cObject);
|
470
|
+
cRequestParser = rb_define_class_under(mHTTP, "RequestParser", cParser);
|
471
|
+
cResponseParser = rb_define_class_under(mHTTP, "ResponseParser", cParser);
|
472
|
+
|
473
|
+
eParserError = rb_define_class_under(cParser, "Error", rb_eIOError);
|
474
|
+
Icall = rb_intern("call");
|
475
|
+
Ion_message_begin = rb_intern("on_message_begin");
|
476
|
+
Ion_headers_complete = rb_intern("on_headers_complete");
|
477
|
+
Ion_body = rb_intern("on_body");
|
478
|
+
Ion_message_complete = rb_intern("on_message_complete");
|
479
|
+
Sstop = ID2SYM(rb_intern("stop"));
|
480
|
+
Sreset = ID2SYM(rb_intern("reset"));
|
481
|
+
|
482
|
+
Sarrays = ID2SYM(rb_intern("arrays"));
|
483
|
+
Sstrings = ID2SYM(rb_intern("strings"));
|
484
|
+
Smixed = ID2SYM(rb_intern("mixed"));
|
485
|
+
|
486
|
+
rb_define_alloc_func(cParser, Parser_alloc);
|
487
|
+
rb_define_alloc_func(cRequestParser, RequestParser_alloc);
|
488
|
+
rb_define_alloc_func(cResponseParser, ResponseParser_alloc);
|
489
|
+
|
490
|
+
rb_define_singleton_method(cParser, "strict?", Parser_strict_p, 0);
|
491
|
+
rb_define_method(cParser, "initialize", Parser_initialize, -1);
|
492
|
+
|
493
|
+
rb_define_method(cParser, "on_message_begin=", Parser_set_on_message_begin, 1);
|
494
|
+
rb_define_method(cParser, "on_headers_complete=", Parser_set_on_headers_complete, 1);
|
495
|
+
rb_define_method(cParser, "on_body=", Parser_set_on_body, 1);
|
496
|
+
rb_define_method(cParser, "on_message_complete=", Parser_set_on_message_complete, 1);
|
497
|
+
rb_define_method(cParser, "<<", Parser_execute, 1);
|
498
|
+
|
499
|
+
rb_define_method(cParser, "keep_alive?", Parser_keep_alive_p, 0);
|
500
|
+
rb_define_method(cParser, "upgrade?", Parser_upgrade_p, 0);
|
501
|
+
|
502
|
+
rb_define_method(cParser, "http_version", Parser_http_version, 0);
|
503
|
+
rb_define_method(cParser, "http_major", Parser_http_major, 0);
|
504
|
+
rb_define_method(cParser, "http_minor", Parser_http_minor, 0);
|
505
|
+
|
506
|
+
rb_define_method(cParser, "http_method", Parser_http_method, 0);
|
507
|
+
rb_define_method(cParser, "status_code", Parser_status_code, 0);
|
508
|
+
|
509
|
+
rb_define_method(cParser, "request_url", Parser_request_url, 0);
|
510
|
+
rb_define_method(cParser, "headers", Parser_headers, 0);
|
511
|
+
rb_define_method(cParser, "upgrade_data", Parser_upgrade_data, 0);
|
512
|
+
rb_define_method(cParser, "header_value_type", Parser_header_value_type, 0);
|
513
|
+
rb_define_method(cParser, "header_value_type=", Parser_set_header_value_type, 1);
|
514
|
+
|
515
|
+
rb_define_method(cParser, "reset!", Parser_reset, 0);
|
516
|
+
}
|