/
http-parser_ext.c
513 lines (446 loc) · 18.1 KB
/
http-parser_ext.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
* Copyright (c) 2009 Jeremy Hinegardner
* All rights reserved. See LICENSE and/or COPYING for details.
*
* vim: shiftwidth=4 tw=72
*/
/* #include "http-parser_ext.h" */
#include "ruby.h"
#include "http_parser.h"
#include <string.h>
#include <stdio.h>
#define true 1
#define false 0
/* class defined here */
VALUE mHttp; /* module Http */
VALUE cHttpParser; /* class Http::Parser */
VALUE cHttpRequestParser; /* class Http::RequestParser */
VALUE cHttpResponseParser; /* class Http::ResponseParser */
VALUE eHttpParserError; /* class Http::Parser::Error */
/***
* Callback Error handling:
*
* Any time a callback is invoked, if it raises and exception then the
* parsing will stop and either an exception will be raised where the
* +parse_chunk+ method is called, or the +on_error+ callback will be
* called.
*
* Since we are registering ruby methods as the callbacks to C methods,
* we need to wrap the ruby method and capture the exception and store
* it away. We do this by using rb_protect and then getting hte
* exception by accessing $!.
*
* The exception caught is then stored in the @_exception instance
* variable in the Parser class which is passed to the +on_error+
* callback.
*
*/
/* wrapper struct around the information need to call a ruby method
* and have it be protected with rb_protect. This is used by
* hpe_funcall2 which is invoked by rb_protect
*/
typedef struct hpe_protected {
VALUE instance;
ID method;
int argc;
VALUE *argv;
} hpe_protected_t;
#define ERROR_INFO_MESSAGE() ( rb_obj_as_string( rb_gv_get("$!") ) )
/**
* invoke a ruby function, this is to be used by rb_protect
*/
VALUE hpe_wrap_funcall2( VALUE arg )
{
hpe_protected_t *protected = (hpe_protected_t*) arg;
return rb_funcall2( protected->instance, protected->method,
protected->argc, protected->argv );
}
/* generator for callback setter */
#define HPE_CALLBACK_SETTER(FOR,CB_TYPE) \
VALUE hpe_parser_##FOR( VALUE self, VALUE callable ) \
{ \
http_parser *parser; \
VALUE rb_parser; \
\
Data_Get_Struct( self, http_parser, parser ); \
rb_parser = (VALUE)parser->data; \
\
if ( Qnil == callable ) { \
parser->FOR = NULL; \
} else { \
rb_iv_set( rb_parser, "@" #FOR "_callback", callable ); \
parser->FOR = (http_##CB_TYPE)hpe_##FOR##_##CB_TYPE; \
} \
return callable; \
}
/* generator for callback methods */
#define HPE_CALLBACK(FOR) \
int hpe_##FOR##_cb( http_parser *parser ) \
{ \
VALUE rb_parser = (VALUE)parser->data; \
VALUE callable = rb_iv_get( rb_parser, "@" #FOR "_callback" ); \
int had_error = false; \
\
if ( Qnil != callable ) { \
hpe_protected_t protected; \
VALUE result; \
VALUE cb_exception = Qnil; \
\
protected.instance = (VALUE)callable; \
protected.method = rb_intern("call"); \
protected.argc = 1; \
protected.argv = &rb_parser; \
\
result = rb_protect( hpe_wrap_funcall2, (VALUE)&protected, \
&had_error); \
if (had_error) { \
cb_exception = rb_gv_get("$!"); \
} \
rb_iv_set( rb_parser, "@callback_exception", cb_exception); \
} \
return had_error; \
}; \
HPE_CALLBACK_SETTER(FOR,cb)
#define HPE_DATA_CALLBACK(FOR) \
int hpe_##FOR##_data_cb( http_parser *parser, \
const char *at, size_t length) \
{ \
VALUE rb_parser = (VALUE)parser->data; \
VALUE callable = rb_iv_get( rb_parser, "@" #FOR "_callback" ); \
int had_error = false; \
\
if ( Qnil != callable ) { \
hpe_protected_t protected; \
VALUE result; \
VALUE cb_exception = Qnil; \
VALUE args[2]; \
VALUE str = rb_str_new( at, length ); \
\
args[0] = rb_parser; \
args[1] = str; \
\
protected.instance = (VALUE)callable; \
protected.method = rb_intern("call"); \
protected.argc = 2; \
protected.argv = args; \
\
result = rb_protect( hpe_wrap_funcall2, (VALUE)&protected, \
&had_error); \
if (had_error) { \
cb_exception = rb_gv_get("$!"); \
} \
rb_iv_set( rb_parser, "@callback_exception", cb_exception); \
} \
return had_error; \
}; \
HPE_CALLBACK_SETTER(FOR,data_cb)
/* common callacks */
HPE_CALLBACK(on_message_begin);
HPE_DATA_CALLBACK(on_header_field);
HPE_DATA_CALLBACK(on_header_value);
HPE_CALLBACK(on_headers_complete);
HPE_DATA_CALLBACK(on_body);
HPE_CALLBACK(on_message_complete);
/* only used by Http::Request */
HPE_DATA_CALLBACK(on_path);
HPE_DATA_CALLBACK(on_query_string);
HPE_DATA_CALLBACK(on_uri);
HPE_DATA_CALLBACK(on_fragment);
/* free the http_parser memory */
void hpe_free( http_parser* parser )
{
parser->data = NULL;
xfree( parser );
return;
}
/*
* allocate the hpe_http_parser structure
*/
VALUE hpe_alloc( VALUE klass )
{
http_parser *parser = xcalloc(1, sizeof( http_parser ));
VALUE obj;
obj = Data_Wrap_Struct( klass, NULL, hpe_free, parser );
return obj;
}
/*
* call-seq:
* response_parser.status_code -> Integer
*
* Return the HTTP Status code of the response.
*
*/
VALUE hpe_parser_status_code( VALUE self )
{
http_parser *parser;
VALUE rc;
Data_Get_Struct( self, http_parser, parser );
rc = INT2FIX( parser->status_code );
return rc;
}
/*
* call-seq:
* request_parser.method -> String
*
* Return the HTTP Method used for the request.
*/
VALUE hpe_parser_method( VALUE self )
{
http_parser *parser;
ID const_get = rb_intern("const_get");
VALUE method;
Data_Get_Struct( self, http_parser, parser );
switch ( parser->method ) {
case HTTP_COPY:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("COPY"));
break;
case HTTP_DELETE:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("DELETE"));
break;
case HTTP_GET:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("GET" ));
break;
case HTTP_HEAD:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("HEAD" ));
break;
case HTTP_LOCK:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("LOCK" ));
break;
case HTTP_MKCOL:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("MKCOL"));
break;
case HTTP_MOVE:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("MOVE"));
break;
case HTTP_OPTIONS:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("OPTIONS"));
break;
case HTTP_POST:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("POST"));
break;
case HTTP_PROPFIND:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("PROPFIND"));
break;
case HTTP_PROPPATCH:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("PROPPATCH" ));
break;
case HTTP_PUT:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("PUT"));
break;
case HTTP_TRACE:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("TRACE"));
break;
case HTTP_UNLOCK:
method = rb_funcall( mHttp, const_get, 1, rb_str_new2("UNLOCK"));
break;
default:
rb_raise(eHttpParserError, "Invalid Request Method");
break;
}
}
/*
* call-seq:
* parser.chunked_encoding? -> true of false
*
* Return if the request/reponse is chunked_encoding.
*
*/
VALUE hpe_parser_chunked_encoding( VALUE self )
{
http_parser *parser;
VALUE rc = Qfalse;
Data_Get_Struct( self, http_parser, parser );
if ( parser->transfer_encoding == HTTP_CHUNKED ) {
rc = Qtrue;
}
return rc;
}
/*
* call-seq:
* parser.version -> Version string
*
* Return the version string of the request/response being parsed.
*
*/
VALUE hpe_parser_version( VALUE self )
{
http_parser *parser;
char v[4];
Data_Get_Struct( self, http_parser, parser );
snprintf(v, 4, "%d.%d", parser->version_major, parser->version_minor );
return rb_str_new2( v );
}
/*
* call-seq:
* parser.keep_alive? -> true or false
*
* Does this request/response support keep_alive?
*
*/
VALUE hpe_parser_keep_alive( VALUE self )
{
http_parser *parser;
int ka;
Data_Get_Struct( self, http_parser, parser );
if ( http_parser_should_keep_alive( parser ) ) {
return Qtrue;
} else {
return Qfalse;
}
}
/*
* call-seq:
* parser.content_length -> Integer
*
* The content length of the body of the request/response being parsed.
*
*/
VALUE hpe_parser_content_length( VALUE self )
{
http_parser *parser;
size_t content_length;
VALUE rc;
Data_Get_Struct( self, http_parser, parser );
content_length = parser->content_length;
rc = ULL2NUM( content_length );
return rc;
}
/*
* call-seq:
* parser.parse_chunk( String ) -> nil
*
* Parse the given hunk of data invoking the callbacks as appropriate.
*
* If an error is encountered, an exception is thrown. This could be
* one of two things:
*
* 1) The error happened in the callback, in that case that exception is
* reraised
* 2) Internal error in the parser, in this case an new excpetion is
* raised
*
* If there is an on_error callback, then it is invoked. In both cases
* the parser and the input chunk are passed to the +on_error+ callback.
* The receiver of the callback can interrogate the parser and see if
* there was an error from some other callback.
*
*/
VALUE hpe_parser_parse_chunk( VALUE self, VALUE chunk )
{
http_parser *parser;
VALUE str = StringValue( chunk );
char* chunk_p = RSTRING_PTR( str );
Data_Get_Struct( self, http_parser, parser );
http_parser_execute( parser, chunk_p, RSTRING_LEN(str) );
if ( http_parser_has_error( parser ) ) {
VALUE callback = rb_iv_get( self, "@on_error_callback" );
if ( Qnil == callback ) {
rb_raise(eHttpParserError, "Failure during parsing of chunk [%s]",
chunk_p);
} else {
rb_funcall( callback, rb_intern("call"), 2, self, chunk );
}
}
return Qnil;
}
/*
* call-seq:
* RequestParser.new
*
* Create a new HTTP Request parser.
*
*/
VALUE hpe_request_parser_initialize( VALUE self )
{
http_parser *parser;
Data_Get_Struct( self, http_parser, parser );
http_parser_init( parser, HTTP_REQUEST );
rb_call_super( 0, NULL );
/* we use the data field in the parser to point to the Ruby Object
* that wraps it
*/
parser->data = (void*)self;
rb_iv_set( self, "@on_path_callback", Qnil);
rb_iv_set( self, "@on_uri_callback", Qnil);
rb_iv_set( self, "@on_fragment_callback", Qnil);
rb_iv_set( self, "@on_query_string_callback", Qnil);
return self;
}
/*
* call-seq:
* ResponseParser.new
*
* Create a new HTTP Response parser.
*
*/
VALUE hpe_response_parser_initialize( VALUE self )
{
http_parser *parser;
Data_Get_Struct( self, http_parser, parser );
http_parser_init( parser, HTTP_RESPONSE );
rb_call_super( 0, NULL );
/* we use the data field in the parser to point to the Ruby Object
* that wraps it.
*/
parser->data = (void*)self;
return self;
}
void Init_http_parser_ext()
{
mHttp = rb_define_module( "Http" );
cHttpParser = rb_define_class_under( mHttp, "Parser", rb_cObject);
cHttpRequestParser = rb_define_class_under( mHttp, "RequestParser", cHttpParser );
cHttpResponseParser = rb_define_class_under( mHttp, "ResponseParser", cHttpParser );
eHttpParserError = rb_define_class_under( cHttpParser, "Error", rb_eStandardError );
/* Http:: Constants */
/* methods */
rb_define_const( mHttp, "COPY" ,rb_str_new2("COPY") );
rb_define_const( mHttp, "DELETE" ,rb_str_new2("DELETE") );
rb_define_const( mHttp, "GET" ,rb_str_new2("GET") );
rb_define_const( mHttp, "HEAD" ,rb_str_new2("HEAD") );
rb_define_const( mHttp, "LOCK" ,rb_str_new2("LOCK") );
rb_define_const( mHttp, "MKCOL" ,rb_str_new2("MKCOL") );
rb_define_const( mHttp, "MOVE" ,rb_str_new2("MOVE") );
rb_define_const( mHttp, "OPTIONS" ,rb_str_new2("OPTIONS") );
rb_define_const( mHttp, "POST" ,rb_str_new2("POST") );
rb_define_const( mHttp, "PROPFIND" ,rb_str_new2("PROPFIND") );
rb_define_const( mHttp, "PROPPATCH" ,rb_str_new2("PROPPATCH") );
rb_define_const( mHttp, "PUT" ,rb_str_new2("PUT") );
rb_define_const( mHttp, "TRACE" ,rb_str_new2("TRACE") );
rb_define_const( mHttp, "UNLOCK" ,rb_str_new2("UNLOCK") );
/* transer encodings */
rb_define_const( mHttp, "IDENTITY" ,rb_str_new2("IDENTITY") );
rb_define_const( mHttp, "CHUNKED" ,rb_str_new2("CHUNKED") );
/******************************************************************
* Http::Parser
******************************************************************/
rb_define_method( cHttpParser, "chunked_encoding?" ,hpe_parser_chunked_encoding , 0 );
rb_define_method( cHttpParser, "version" ,hpe_parser_version , 0 );
rb_define_method( cHttpParser, "keep_alive?" ,hpe_parser_keep_alive , 0 );
rb_define_method( cHttpParser, "content_length" ,hpe_parser_content_length , 0 );
rb_define_method( cHttpParser, "parse_chunk" ,hpe_parser_parse_chunk , 1 );
/* the common callbacks */
rb_define_method( cHttpParser, "on_message_begin=" ,hpe_parser_on_message_begin , 1 );
rb_define_method( cHttpParser, "on_header_field=" ,hpe_parser_on_header_field , 1 );
rb_define_method( cHttpParser, "on_header_value=" ,hpe_parser_on_header_value , 1 );
rb_define_method( cHttpParser, "on_headers_complete=" ,hpe_parser_on_headers_complete, 1 );
rb_define_method( cHttpParser, "on_body=" ,hpe_parser_on_body , 1 );
rb_define_method( cHttpParser, "on_message_complete=" ,hpe_parser_on_message_complete, 1 );
/******************************************************************
* Http::RequestParser
******************************************************************/
rb_define_alloc_func( cHttpRequestParser, hpe_alloc);
rb_define_method( cHttpRequestParser, "initialize",hpe_request_parser_initialize, 0 );
rb_define_method( cHttpRequestParser, "method" ,hpe_parser_method , 0 );
/* additional request callbacks */
rb_define_method( cHttpParser, "on_path=" ,hpe_parser_on_path , 1 );
rb_define_method( cHttpParser, "on_query_string=" ,hpe_parser_on_query_string , 1 );
rb_define_method( cHttpParser, "on_uri=" ,hpe_parser_on_uri , 1 );
rb_define_method( cHttpParser, "on_fragment=" ,hpe_parser_on_fragment , 1 );
/******************************************************************
* Http::ResponseParser
******************************************************************/
rb_define_alloc_func( cHttpResponseParser, hpe_alloc);
rb_define_method( cHttpResponseParser, "initialize" ,hpe_response_parser_initialize, 0 );
rb_define_method( cHttpResponseParser, "status_code" ,hpe_parser_status_code , 0 );
}