OSDN Git Service

ActiveLdap 1.2.4
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / thin-1.2.11-x86-mswin32 / ext / thin_parser / thin.c
1 /**
2  * Mongrel Parser adpated to Thin and to play more nicely with Rack specs.
3  * 
4  * Orignal version Copyright (c) 2005 Zed A. Shaw
5  * You can redistribute it and/or modify it under the same terms as Ruby.
6  */
7 #include "ruby.h"
8 #include "ext_help.h"
9 #include <assert.h>
10 #include <string.h>
11 #include "parser.h"
12 #include <ctype.h>
13
14 static VALUE mThin;
15 static VALUE cHttpParser;
16 static VALUE eHttpParserError;
17
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;
43
44 #define TRIE_INCREASE 30
45
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."
48
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); }
51
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)
54
55 /* for compatibility with Ruby 1.8.5, which doesn't declare RSTRING_PTR */
56 #ifndef RSTRING_PTR
57 #define RSTRING_PTR(s) (RSTRING(s)->ptr)
58 #endif
59
60 /* for compatibility with Ruby 1.8.5, which doesn't declare RSTRING_LEN */
61 #ifndef RSTRING_LEN
62 #define RSTRING_LEN(s) (RSTRING(s)->len)
63 #endif
64
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)));
73
74
75 static void http_field(void *data, const char *field, size_t flen, const char *value, size_t vlen)
76 {
77   char *ch, *end;
78   VALUE req = (VALUE)data;
79   VALUE v = Qnil;
80   VALUE f = Qnil;
81
82   VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
83   VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
84
85   v = rb_str_new(value, vlen);
86   f = rb_str_dup(global_http_prefix);
87   f = rb_str_buf_cat(f, field, flen); 
88
89   for(ch = RSTRING_PTR(f) + RSTRING_LEN(global_http_prefix), end = RSTRING_PTR(f) + RSTRING_LEN(f); ch < end; ch++) {
90     if(*ch == '-') {
91       *ch = '_';
92     } else {
93       *ch = toupper(*ch);
94     }
95   }
96
97   rb_hash_aset(req, f, v);
98 }
99
100 static void request_method(void *data, const char *at, size_t length)
101 {
102   VALUE req = (VALUE)data;
103   VALUE val = Qnil;
104
105   val = rb_str_new(at, length);
106   rb_hash_aset(req, global_request_method, val);
107 }
108
109 static void request_uri(void *data, const char *at, size_t length)
110 {
111   VALUE req = (VALUE)data;
112   VALUE val = Qnil;
113
114   VALIDATE_MAX_LENGTH(length, REQUEST_URI);
115
116   val = rb_str_new(at, length);
117   rb_hash_aset(req, global_request_uri, val);
118 }
119
120 static void fragment(void *data, const char *at, size_t length)
121 {
122   VALUE req = (VALUE)data;
123   VALUE val = Qnil;
124
125   VALIDATE_MAX_LENGTH(length, FRAGMENT);
126
127   val = rb_str_new(at, length);
128   rb_hash_aset(req, global_fragment, val);
129 }
130
131 static void request_path(void *data, const char *at, size_t length)
132 {
133   VALUE req = (VALUE)data;
134   VALUE val = Qnil;
135
136   VALIDATE_MAX_LENGTH(length, REQUEST_PATH);
137
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);
141 }
142
143 static void query_string(void *data, const char *at, size_t length)
144 {
145   VALUE req = (VALUE)data;
146   VALUE val = Qnil;
147
148   VALIDATE_MAX_LENGTH(length, QUERY_STRING);
149
150   val = rb_str_new(at, length);
151   rb_hash_aset(req, global_query_string, val);
152 }
153
154 static void http_version(void *data, const char *at, size_t length)
155 {
156   VALUE req = (VALUE)data;
157   VALUE val = rb_str_new(at, length);
158   rb_hash_aset(req, global_http_version, val);
159 }
160
161 /** Finalizes the request header to have a bunch of stuff that's
162   needed. */
163
164 static void header_done(void *data, const char *at, size_t length)
165 {
166   VALUE req = (VALUE)data;
167   VALUE temp = Qnil;
168   VALUE ctype = Qnil;
169   VALUE clen = Qnil;
170   VALUE body = Qnil;
171   char *colon = NULL;
172
173   clen = rb_hash_aref(req, global_http_content_length);
174   if(clen != Qnil) {
175     rb_hash_aset(req, global_content_length, clen);
176     rb_hash_delete(req, global_http_content_length);
177   }
178
179   ctype = rb_hash_aref(req, global_http_content_type);
180   if(ctype != Qnil) {
181     rb_hash_aset(req, global_content_type, ctype);
182     rb_hash_delete(req, global_http_content_type);
183   }
184
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), ':');
189     if(colon != NULL) {
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, 
193             RSTRING_LEN(temp)));
194     } else {
195       rb_hash_aset(req, global_server_name, temp);
196       rb_hash_aset(req, global_server_port, global_port_80);
197     }
198   }
199
200   /* grab the initial body and stuff it into the hash */
201   if(length > 0) {
202     body = rb_hash_aref(req, global_http_body);
203     rb_io_write(body, rb_str_new(at, length));
204   }
205   
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);
209   }
210   if (rb_hash_aref(req, global_path_info) == Qnil) {
211     rb_hash_aset(req, global_path_info, global_empty);
212   }
213   
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);
218 }
219
220
221 void Thin_HttpParser_free(void *data) {
222   TRACE();
223
224   if(data) {
225     free(data);
226   }
227 }
228
229
230 VALUE Thin_HttpParser_alloc(VALUE klass)
231 {
232   VALUE obj;
233   http_parser *hp = ALLOC_N(http_parser, 1);
234   TRACE();
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);
244
245   obj = Data_Wrap_Struct(klass, NULL, Thin_HttpParser_free, hp);
246
247   return obj;
248 }
249
250
251 /**
252  * call-seq:
253  *    parser.new -> parser
254  *
255  * Creates a new parser.
256  */
257 VALUE Thin_HttpParser_init(VALUE self)
258 {
259   http_parser *http = NULL;
260   DATA_GET(self, http_parser, http);
261   thin_http_parser_init(http);
262
263   return self;
264 }
265
266
267 /**
268  * call-seq:
269  *    parser.reset -> nil
270  *
271  * Resets the parser to it's initial state so that you can reuse it
272  * rather than making new ones.
273  */
274 VALUE Thin_HttpParser_reset(VALUE self)
275 {
276   http_parser *http = NULL;
277   DATA_GET(self, http_parser, http);
278   thin_http_parser_init(http);
279
280   return Qnil;
281 }
282
283
284 /**
285  * call-seq:
286  *    parser.finish -> true/false
287  *
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.
290  */
291 VALUE Thin_HttpParser_finish(VALUE self)
292 {
293   http_parser *http = NULL;
294   DATA_GET(self, http_parser, http);
295   thin_http_parser_finish(http);
296
297   return thin_http_parser_is_finished(http) ? Qtrue : Qfalse;
298 }
299
300
301 /**
302  * call-seq:
303  *    parser.execute(req_hash, data, start) -> Integer
304  *
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.
309  * 
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.
313  *
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.
317  */
318 VALUE Thin_HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
319 {
320   http_parser *http = NULL;
321   int from = 0;
322   char *dptr = NULL;
323   long dlen = 0;
324
325   DATA_GET(self, http_parser, http);
326
327   from = FIX2INT(start);
328   dptr = RSTRING_PTR(data);
329   dlen = RSTRING_LEN(data);
330
331   if(from >= dlen) {
332     rb_raise(eHttpParserError, "Requested start is after data buffer end.");
333   } else {
334     http->data = (void *)req_hash;
335     thin_http_parser_execute(http, dptr, dlen, from);
336
337     VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
338
339     if(thin_http_parser_has_error(http)) {
340       rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
341     } else {
342       return INT2FIX(http_parser_nread(http));
343     }
344   }
345 }
346
347
348
349 /**
350  * call-seq:
351  *    parser.error? -> true/false
352  *
353  * Tells you whether the parser is in an error state.
354  */
355 VALUE Thin_HttpParser_has_error(VALUE self)
356 {
357   http_parser *http = NULL;
358   DATA_GET(self, http_parser, http);
359
360   return thin_http_parser_has_error(http) ? Qtrue : Qfalse;
361 }
362
363
364 /**
365  * call-seq:
366  *    parser.finished? -> true/false
367  *
368  * Tells you whether the parser is finished or not and in a good state.
369  */
370 VALUE Thin_HttpParser_is_finished(VALUE self)
371 {
372   http_parser *http = NULL;
373   DATA_GET(self, http_parser, http);
374
375   return thin_http_parser_is_finished(http) ? Qtrue : Qfalse;
376 }
377
378
379 /**
380  * call-seq:
381  *    parser.nread -> Integer
382  *
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.
385  */
386 VALUE Thin_HttpParser_nread(VALUE self)
387 {
388   http_parser *http = NULL;
389   DATA_GET(self, http_parser, http);
390
391   return INT2FIX(http->nread);
392 }
393
394 void Init_thin_parser()
395 {
396
397   mThin = rb_define_module("Thin");
398
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");
424
425   eHttpParserError = rb_define_class_under(mThin, "InvalidRequest", rb_eIOError);
426
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);
436 }