2 * Mongrel Parser adpated to Thin and to play more nicely with Rack specs.
4 * Orignal version Copyright (c) 2005 Zed A. Shaw
5 * You can redistribute it and/or modify it under the same terms as Ruby.
15 static VALUE cHttpParser;
16 static VALUE eHttpParserError;
18 static VALUE global_empty;
19 static VALUE global_http_prefix;
20 static VALUE global_request_method;
21 static VALUE global_request_uri;
22 static VALUE global_fragment;
23 static VALUE global_query_string;
24 static VALUE global_http_version;
25 static VALUE global_content_length;
26 static VALUE global_http_content_length;
27 static VALUE global_request_path;
28 static VALUE global_content_type;
29 static VALUE global_http_content_type;
30 static VALUE global_gateway_interface;
31 static VALUE global_gateway_interface_value;
32 static VALUE global_server_name;
33 static VALUE global_server_port;
34 static VALUE global_server_protocol;
35 static VALUE global_server_protocol_value;
36 static VALUE global_http_host;
37 static VALUE global_port_80;
38 static VALUE global_http_body;
39 static VALUE global_url_scheme;
40 static VALUE global_url_scheme_value;
41 static VALUE global_script_name;
42 static VALUE global_path_info;
44 #define TRIE_INCREASE 30
46 /** Defines common length and error messages for input length validation. */
47 #define DEF_MAX_LENGTH(N,length) const size_t MAX_##N##_LENGTH = length; const char *MAX_##N##_LENGTH_ERR = "HTTP element " # N " is longer than the " # length " allowed length."
49 /** Validates the max length of given input and throws an HttpParserError exception if over. */
50 #define VALIDATE_MAX_LENGTH(len, N) if(len > MAX_##N##_LENGTH) { rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); }
52 /** Defines global strings in the init method. */
53 #define DEF_GLOBAL(N, val) global_##N = rb_obj_freeze(rb_str_new2(val)); rb_global_variable(&global_##N)
55 /* for compatibility with Ruby 1.8.5, which doesn't declare RSTRING_PTR */
57 #define RSTRING_PTR(s) (RSTRING(s)->ptr)
60 /* for compatibility with Ruby 1.8.5, which doesn't declare RSTRING_LEN */
62 #define RSTRING_LEN(s) (RSTRING(s)->len)
65 /* Defines the maximum allowed lengths for various input elements.*/
66 DEF_MAX_LENGTH(FIELD_NAME, 256);
67 DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
68 DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12);
69 DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
70 DEF_MAX_LENGTH(REQUEST_PATH, 1024);
71 DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
72 DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
75 static void http_field(void *data, const char *field, size_t flen, const char *value, size_t vlen)
78 VALUE req = (VALUE)data;
82 VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
83 VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
85 v = rb_str_new(value, vlen);
86 f = rb_str_dup(global_http_prefix);
87 f = rb_str_buf_cat(f, field, flen);
89 for(ch = RSTRING_PTR(f) + RSTRING_LEN(global_http_prefix), end = RSTRING_PTR(f) + RSTRING_LEN(f); ch < end; ch++) {
97 rb_hash_aset(req, f, v);
100 static void request_method(void *data, const char *at, size_t length)
102 VALUE req = (VALUE)data;
105 val = rb_str_new(at, length);
106 rb_hash_aset(req, global_request_method, val);
109 static void request_uri(void *data, const char *at, size_t length)
111 VALUE req = (VALUE)data;
114 VALIDATE_MAX_LENGTH(length, REQUEST_URI);
116 val = rb_str_new(at, length);
117 rb_hash_aset(req, global_request_uri, val);
120 static void fragment(void *data, const char *at, size_t length)
122 VALUE req = (VALUE)data;
125 VALIDATE_MAX_LENGTH(length, FRAGMENT);
127 val = rb_str_new(at, length);
128 rb_hash_aset(req, global_fragment, val);
131 static void request_path(void *data, const char *at, size_t length)
133 VALUE req = (VALUE)data;
136 VALIDATE_MAX_LENGTH(length, REQUEST_PATH);
138 val = rb_str_new(at, length);
139 rb_hash_aset(req, global_request_path, val);
140 rb_hash_aset(req, global_path_info, val);
143 static void query_string(void *data, const char *at, size_t length)
145 VALUE req = (VALUE)data;
148 VALIDATE_MAX_LENGTH(length, QUERY_STRING);
150 val = rb_str_new(at, length);
151 rb_hash_aset(req, global_query_string, val);
154 static void http_version(void *data, const char *at, size_t length)
156 VALUE req = (VALUE)data;
157 VALUE val = rb_str_new(at, length);
158 rb_hash_aset(req, global_http_version, val);
161 /** Finalizes the request header to have a bunch of stuff that's
164 static void header_done(void *data, const char *at, size_t length)
166 VALUE req = (VALUE)data;
173 clen = rb_hash_aref(req, global_http_content_length);
175 rb_hash_aset(req, global_content_length, clen);
176 rb_hash_delete(req, global_http_content_length);
179 ctype = rb_hash_aref(req, global_http_content_type);
181 rb_hash_aset(req, global_content_type, ctype);
182 rb_hash_delete(req, global_http_content_type);
185 rb_hash_aset(req, global_gateway_interface, global_gateway_interface_value);
186 if((temp = rb_hash_aref(req, global_http_host)) != Qnil) {
187 /* ruby better close strings off with a '\0' dammit */
188 colon = strchr(RSTRING_PTR(temp), ':');
190 rb_hash_aset(req, global_server_name, rb_str_substr(temp, 0, colon - RSTRING_PTR(temp)));
191 rb_hash_aset(req, global_server_port,
192 rb_str_substr(temp, colon - RSTRING_PTR(temp)+1,
195 rb_hash_aset(req, global_server_name, temp);
196 rb_hash_aset(req, global_server_port, global_port_80);
200 /* grab the initial body and stuff it into the hash */
202 body = rb_hash_aref(req, global_http_body);
203 rb_io_write(body, rb_str_new(at, length));
206 /* according to Rack specs, query string must be empty string if none */
207 if (rb_hash_aref(req, global_query_string) == Qnil) {
208 rb_hash_aset(req, global_query_string, global_empty);
210 if (rb_hash_aref(req, global_path_info) == Qnil) {
211 rb_hash_aset(req, global_path_info, global_empty);
214 /* set some constants */
215 rb_hash_aset(req, global_server_protocol, global_server_protocol_value);
216 rb_hash_aset(req, global_url_scheme, global_url_scheme_value);
217 rb_hash_aset(req, global_script_name, global_empty);
221 void Thin_HttpParser_free(void *data) {
230 VALUE Thin_HttpParser_alloc(VALUE klass)
233 http_parser *hp = ALLOC_N(http_parser, 1);
235 hp->http_field = http_field;
236 hp->request_method = request_method;
237 hp->request_uri = request_uri;
238 hp->fragment = fragment;
239 hp->request_path = request_path;
240 hp->query_string = query_string;
241 hp->http_version = http_version;
242 hp->header_done = header_done;
243 thin_http_parser_init(hp);
245 obj = Data_Wrap_Struct(klass, NULL, Thin_HttpParser_free, hp);
253 * parser.new -> parser
255 * Creates a new parser.
257 VALUE Thin_HttpParser_init(VALUE self)
259 http_parser *http = NULL;
260 DATA_GET(self, http_parser, http);
261 thin_http_parser_init(http);
269 * parser.reset -> nil
271 * Resets the parser to it's initial state so that you can reuse it
272 * rather than making new ones.
274 VALUE Thin_HttpParser_reset(VALUE self)
276 http_parser *http = NULL;
277 DATA_GET(self, http_parser, http);
278 thin_http_parser_init(http);
286 * parser.finish -> true/false
288 * Finishes a parser early which could put in a "good" or bad state.
289 * You should call reset after finish it or bad things will happen.
291 VALUE Thin_HttpParser_finish(VALUE self)
293 http_parser *http = NULL;
294 DATA_GET(self, http_parser, http);
295 thin_http_parser_finish(http);
297 return thin_http_parser_is_finished(http) ? Qtrue : Qfalse;
303 * parser.execute(req_hash, data, start) -> Integer
305 * Takes a Hash and a String of data, parses the String of data filling in the Hash
306 * returning an Integer to indicate how much of the data has been read. No matter
307 * what the return value, you should call HttpParser#finished? and HttpParser#error?
308 * to figure out if it's done parsing or there was an error.
310 * This function now throws an exception when there is a parsing error. This makes
311 * the logic for working with the parser much easier. You can still test for an
312 * error, but now you need to wrap the parser with an exception handling block.
314 * The third argument allows for parsing a partial request and then continuing
315 * the parsing from that position. It needs all of the original data as well
316 * so you have to append to the data buffer as you read.
318 VALUE Thin_HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
320 http_parser *http = NULL;
325 DATA_GET(self, http_parser, http);
327 from = FIX2INT(start);
328 dptr = RSTRING_PTR(data);
329 dlen = RSTRING_LEN(data);
332 rb_raise(eHttpParserError, "Requested start is after data buffer end.");
334 http->data = (void *)req_hash;
335 thin_http_parser_execute(http, dptr, dlen, from);
337 VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
339 if(thin_http_parser_has_error(http)) {
340 rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
342 return INT2FIX(http_parser_nread(http));
351 * parser.error? -> true/false
353 * Tells you whether the parser is in an error state.
355 VALUE Thin_HttpParser_has_error(VALUE self)
357 http_parser *http = NULL;
358 DATA_GET(self, http_parser, http);
360 return thin_http_parser_has_error(http) ? Qtrue : Qfalse;
366 * parser.finished? -> true/false
368 * Tells you whether the parser is finished or not and in a good state.
370 VALUE Thin_HttpParser_is_finished(VALUE self)
372 http_parser *http = NULL;
373 DATA_GET(self, http_parser, http);
375 return thin_http_parser_is_finished(http) ? Qtrue : Qfalse;
381 * parser.nread -> Integer
383 * Returns the amount of data processed so far during this processing cycle. It is
384 * set to 0 on initialize or reset calls and is incremented each time execute is called.
386 VALUE Thin_HttpParser_nread(VALUE self)
388 http_parser *http = NULL;
389 DATA_GET(self, http_parser, http);
391 return INT2FIX(http->nread);
394 void Init_thin_parser()
397 mThin = rb_define_module("Thin");
399 DEF_GLOBAL(empty, "");
400 DEF_GLOBAL(http_prefix, "HTTP_");
401 DEF_GLOBAL(request_method, "REQUEST_METHOD");
402 DEF_GLOBAL(request_uri, "REQUEST_URI");
403 DEF_GLOBAL(fragment, "FRAGMENT");
404 DEF_GLOBAL(query_string, "QUERY_STRING");
405 DEF_GLOBAL(http_version, "HTTP_VERSION");
406 DEF_GLOBAL(request_path, "REQUEST_PATH");
407 DEF_GLOBAL(content_length, "CONTENT_LENGTH");
408 DEF_GLOBAL(http_content_length, "HTTP_CONTENT_LENGTH");
409 DEF_GLOBAL(content_type, "CONTENT_TYPE");
410 DEF_GLOBAL(http_content_type, "HTTP_CONTENT_TYPE");
411 DEF_GLOBAL(gateway_interface, "GATEWAY_INTERFACE");
412 DEF_GLOBAL(gateway_interface_value, "CGI/1.2");
413 DEF_GLOBAL(server_name, "SERVER_NAME");
414 DEF_GLOBAL(server_port, "SERVER_PORT");
415 DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL");
416 DEF_GLOBAL(server_protocol_value, "HTTP/1.1");
417 DEF_GLOBAL(http_host, "HTTP_HOST");
418 DEF_GLOBAL(port_80, "80");
419 DEF_GLOBAL(http_body, "rack.input");
420 DEF_GLOBAL(url_scheme, "rack.url_scheme");
421 DEF_GLOBAL(url_scheme_value, "http");
422 DEF_GLOBAL(script_name, "SCRIPT_NAME");
423 DEF_GLOBAL(path_info, "PATH_INFO");
425 eHttpParserError = rb_define_class_under(mThin, "InvalidRequest", rb_eIOError);
427 cHttpParser = rb_define_class_under(mThin, "HttpParser", rb_cObject);
428 rb_define_alloc_func(cHttpParser, Thin_HttpParser_alloc);
429 rb_define_method(cHttpParser, "initialize", Thin_HttpParser_init,0);
430 rb_define_method(cHttpParser, "reset", Thin_HttpParser_reset,0);
431 rb_define_method(cHttpParser, "finish", Thin_HttpParser_finish,0);
432 rb_define_method(cHttpParser, "execute", Thin_HttpParser_execute,3);
433 rb_define_method(cHttpParser, "error?", Thin_HttpParser_has_error,0);
434 rb_define_method(cHttpParser, "finished?", Thin_HttpParser_is_finished,0);
435 rb_define_method(cHttpParser, "nread", Thin_HttpParser_nread,0);