thin 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of thin might be problematic. Click here for more details.

Files changed (91) hide show
  1. data/COPYING +18 -0
  2. data/README +32 -45
  3. data/Rakefile +66 -23
  4. data/bin/thin +76 -45
  5. data/doc/benchmarks.txt +64 -249
  6. data/doc/rdoc/created.rid +1 -1
  7. data/doc/rdoc/files/README.html +37 -100
  8. data/doc/rdoc/rdoc-style.css +5 -0
  9. data/example/config.ru +9 -0
  10. data/ext/thin_parser/common.rl +54 -0
  11. data/ext/thin_parser/ext_help.h +14 -0
  12. data/ext/thin_parser/extconf.rb +6 -0
  13. data/ext/thin_parser/parser.c +1199 -0
  14. data/ext/thin_parser/parser.h +49 -0
  15. data/ext/thin_parser/parser.rl +143 -0
  16. data/ext/thin_parser/thin.c +424 -0
  17. data/lib/rack/adapter/rails.rb +136 -0
  18. data/lib/rack/handler/thin.rb +13 -0
  19. data/lib/thin.rb +28 -12
  20. data/lib/thin/connection.rb +47 -0
  21. data/lib/thin/daemonizing.rb +5 -1
  22. data/lib/thin/headers.rb +3 -2
  23. data/lib/thin/logging.rb +6 -13
  24. data/lib/thin/request.rb +53 -133
  25. data/lib/thin/response.rb +21 -25
  26. data/lib/thin/server.rb +30 -94
  27. data/lib/thin/version.rb +2 -2
  28. data/lib/thin_parser.bundle +0 -0
  29. data/spec/daemonizing_spec.rb +94 -0
  30. data/spec/headers_spec.rb +35 -0
  31. data/spec/request_spec.rb +258 -0
  32. data/spec/response_spec.rb +40 -0
  33. data/spec/server_spec.rb +75 -0
  34. data/spec/spec_helper.rb +126 -0
  35. metadata +79 -99
  36. data/bin/thin_cluster +0 -53
  37. data/doc/rdoc/classes/Kernel.html +0 -182
  38. data/doc/rdoc/classes/Process.html +0 -175
  39. data/doc/rdoc/classes/Thin.html +0 -184
  40. data/doc/rdoc/classes/Thin/CGIWrapper.html +0 -438
  41. data/doc/rdoc/classes/Thin/Cluster.html +0 -392
  42. data/doc/rdoc/classes/Thin/Command.html +0 -221
  43. data/doc/rdoc/classes/Thin/CommandError.html +0 -154
  44. data/doc/rdoc/classes/Thin/Commands.html +0 -145
  45. data/doc/rdoc/classes/Thin/Daemonizable.html +0 -250
  46. data/doc/rdoc/classes/Thin/Daemonizable/ClassMethods.html +0 -203
  47. data/doc/rdoc/classes/Thin/DirHandler.html +0 -250
  48. data/doc/rdoc/classes/Thin/Handler.html +0 -195
  49. data/doc/rdoc/classes/Thin/Headers.html +0 -244
  50. data/doc/rdoc/classes/Thin/InvalidRequest.html +0 -150
  51. data/doc/rdoc/classes/Thin/Logging.html +0 -214
  52. data/doc/rdoc/classes/Thin/RailsHandler.html +0 -234
  53. data/doc/rdoc/classes/Thin/RailsServer.html +0 -175
  54. data/doc/rdoc/classes/Thin/Request.html +0 -379
  55. data/doc/rdoc/classes/Thin/Response.html +0 -311
  56. data/doc/rdoc/classes/Thin/Server.html +0 -381
  57. data/doc/rdoc/files/bin/thin.html +0 -188
  58. data/doc/rdoc/files/bin/thin_cluster.html +0 -175
  59. data/doc/rdoc/files/lib/thin/cgi_rb.html +0 -263
  60. data/doc/rdoc/files/lib/thin/cluster_rb.html +0 -263
  61. data/doc/rdoc/files/lib/thin/command_rb.html +0 -263
  62. data/doc/rdoc/files/lib/thin/consts_rb.html +0 -263
  63. data/doc/rdoc/files/lib/thin/daemonizing_rb.html +0 -263
  64. data/doc/rdoc/files/lib/thin/handler_rb.html +0 -263
  65. data/doc/rdoc/files/lib/thin/headers_rb.html +0 -263
  66. data/doc/rdoc/files/lib/thin/logging_rb.html +0 -263
  67. data/doc/rdoc/files/lib/thin/mime_types_rb.html +0 -263
  68. data/doc/rdoc/files/lib/thin/rails_rb.html +0 -263
  69. data/doc/rdoc/files/lib/thin/recipes_rb.html +0 -171
  70. data/doc/rdoc/files/lib/thin/request_rb.html +0 -171
  71. data/doc/rdoc/files/lib/thin/response_rb.html +0 -171
  72. data/doc/rdoc/files/lib/thin/server_rb.html +0 -171
  73. data/doc/rdoc/files/lib/thin/statuses_rb.html +0 -171
  74. data/doc/rdoc/files/lib/thin/version_rb.html +0 -171
  75. data/lib/thin/cgi.rb +0 -159
  76. data/lib/thin/cluster.rb +0 -147
  77. data/lib/thin/command.rb +0 -49
  78. data/lib/thin/commands/cluster/base.rb +0 -24
  79. data/lib/thin/commands/cluster/config.rb +0 -36
  80. data/lib/thin/commands/cluster/restart.rb +0 -35
  81. data/lib/thin/commands/cluster/start.rb +0 -40
  82. data/lib/thin/commands/cluster/stop.rb +0 -28
  83. data/lib/thin/commands/server/base.rb +0 -7
  84. data/lib/thin/commands/server/start.rb +0 -33
  85. data/lib/thin/commands/server/stop.rb +0 -29
  86. data/lib/thin/consts.rb +0 -37
  87. data/lib/thin/handler.rb +0 -57
  88. data/lib/thin/mime_types.rb +0 -619
  89. data/lib/thin/rails.rb +0 -44
  90. data/lib/thin/recipes.rb +0 -36
  91. data/lib/transat/parser.rb +0 -247
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Copyright (c) 2005 Zed A. Shaw
3
+ * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ */
5
+
6
+ #ifndef http11_parser_h
7
+ #define http11_parser_h
8
+
9
+ #include <sys/types.h>
10
+
11
+ #if defined(_WIN32)
12
+ #include <stddef.h>
13
+ #endif
14
+
15
+ typedef void (*element_cb)(void *data, const char *at, size_t length);
16
+ typedef void (*field_cb)(void *data, const char *field, size_t flen, const char *value, size_t vlen);
17
+
18
+ typedef struct http_parser {
19
+ int cs;
20
+ size_t body_start;
21
+ int content_len;
22
+ size_t nread;
23
+ size_t mark;
24
+ size_t field_start;
25
+ size_t field_len;
26
+ size_t query_start;
27
+
28
+ void *data;
29
+
30
+ field_cb http_field;
31
+ element_cb request_method;
32
+ element_cb request_uri;
33
+ element_cb fragment;
34
+ element_cb request_path;
35
+ element_cb query_string;
36
+ element_cb http_version;
37
+ element_cb header_done;
38
+
39
+ } http_parser;
40
+
41
+ int http_parser_init(http_parser *parser);
42
+ int http_parser_finish(http_parser *parser);
43
+ size_t http_parser_execute(http_parser *parser, const char *data, size_t len, size_t off);
44
+ int http_parser_has_error(http_parser *parser);
45
+ int http_parser_is_finished(http_parser *parser);
46
+
47
+ #define http_parser_nread(parser) (parser)->nread
48
+
49
+ #endif
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Copyright (c) 2005 Zed A. Shaw
3
+ * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ */
5
+ #include "parser.h"
6
+ #include <stdio.h>
7
+ #include <assert.h>
8
+ #include <stdlib.h>
9
+ #include <ctype.h>
10
+ #include <string.h>
11
+
12
+ #define LEN(AT, FPC) (FPC - buffer - parser->AT)
13
+ #define MARK(M,FPC) (parser->M = (FPC) - buffer)
14
+ #define PTR_TO(F) (buffer + parser->F)
15
+
16
+ /** Machine **/
17
+
18
+ %%{
19
+
20
+ machine http_parser;
21
+
22
+ action mark {MARK(mark, fpc); }
23
+
24
+
25
+ action start_field { MARK(field_start, fpc); }
26
+ action write_field {
27
+ parser->field_len = LEN(field_start, fpc);
28
+ }
29
+
30
+ action start_value { MARK(mark, fpc); }
31
+ action write_value {
32
+ parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
33
+ }
34
+ action request_method {
35
+ parser->request_method(parser->data, PTR_TO(mark), LEN(mark, fpc));
36
+ }
37
+ action request_uri {
38
+ parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, fpc));
39
+ }
40
+ action fragment {
41
+ parser->fragment(parser->data, PTR_TO(mark), LEN(mark, fpc));
42
+ }
43
+
44
+ action start_query {MARK(query_start, fpc); }
45
+ action query_string {
46
+ parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, fpc));
47
+ }
48
+
49
+ action http_version {
50
+ parser->http_version(parser->data, PTR_TO(mark), LEN(mark, fpc));
51
+ }
52
+
53
+ action request_path {
54
+ parser->request_path(parser->data, PTR_TO(mark), LEN(mark,fpc));
55
+ }
56
+
57
+ action done {
58
+ parser->body_start = fpc - buffer + 1;
59
+ parser->header_done(parser->data, fpc + 1, pe - fpc - 1);
60
+ fbreak;
61
+ }
62
+
63
+ include http_parser_common "common.rl";
64
+
65
+ }%%
66
+
67
+ /** Data **/
68
+ %% write data;
69
+
70
+ int http_parser_init(http_parser *parser) {
71
+ int cs = 0;
72
+ %% write init;
73
+ parser->cs = cs;
74
+ parser->body_start = 0;
75
+ parser->content_len = 0;
76
+ parser->mark = 0;
77
+ parser->nread = 0;
78
+ parser->field_len = 0;
79
+ parser->field_start = 0;
80
+
81
+ return(1);
82
+ }
83
+
84
+
85
+ /** exec **/
86
+ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, size_t off) {
87
+ const char *p, *pe;
88
+ int cs = parser->cs;
89
+
90
+ assert(off <= len && "offset past end of buffer");
91
+
92
+ p = buffer+off;
93
+ pe = buffer+len;
94
+
95
+ assert(*pe == '\0' && "pointer does not end on NUL");
96
+ assert(pe - p == len - off && "pointers aren't same distance");
97
+
98
+
99
+ %% write exec;
100
+
101
+ parser->cs = cs;
102
+ parser->nread += p - (buffer + off);
103
+
104
+ assert(p <= pe && "buffer overflow after parsing execute");
105
+ assert(parser->nread <= len && "nread longer than length");
106
+ assert(parser->body_start <= len && "body starts after buffer end");
107
+ assert(parser->mark < len && "mark is after buffer end");
108
+ assert(parser->field_len <= len && "field has length longer than whole buffer");
109
+ assert(parser->field_start < len && "field starts after buffer end");
110
+
111
+ if(parser->body_start) {
112
+ /* final \r\n combo encountered so stop right here */
113
+ %%write eof;
114
+ parser->nread++;
115
+ }
116
+
117
+ return(parser->nread);
118
+ }
119
+
120
+ int http_parser_finish(http_parser *parser)
121
+ {
122
+ int cs = parser->cs;
123
+
124
+ %%write eof;
125
+
126
+ parser->cs = cs;
127
+
128
+ if (http_parser_has_error(parser) ) {
129
+ return -1;
130
+ } else if (http_parser_is_finished(parser) ) {
131
+ return 1;
132
+ } else {
133
+ return 0;
134
+ }
135
+ }
136
+
137
+ int http_parser_has_error(http_parser *parser) {
138
+ return parser->cs == http_parser_error;
139
+ }
140
+
141
+ int http_parser_is_finished(http_parser *parser) {
142
+ return parser->cs == http_parser_first_final;
143
+ }
@@ -0,0 +1,424 @@
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
+
56
+ /* Defines the maximum allowed lengths for various input elements.*/
57
+ DEF_MAX_LENGTH(FIELD_NAME, 256);
58
+ DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
59
+ DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12);
60
+ DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
61
+ DEF_MAX_LENGTH(REQUEST_PATH, 1024);
62
+ DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
63
+ DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
64
+
65
+
66
+ void http_field(void *data, const char *field, size_t flen, const char *value, size_t vlen)
67
+ {
68
+ char *ch, *end;
69
+ VALUE req = (VALUE)data;
70
+ VALUE v = Qnil;
71
+ VALUE f = Qnil;
72
+
73
+ VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
74
+ VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
75
+
76
+ v = rb_str_new(value, vlen);
77
+ f = rb_str_dup(global_http_prefix);
78
+ f = rb_str_buf_cat(f, field, flen);
79
+
80
+ for(ch = RSTRING(f)->ptr, end = ch + RSTRING(f)->len; ch < end; ch++) {
81
+ if(*ch == '-') {
82
+ *ch = '_';
83
+ } else {
84
+ *ch = toupper(*ch);
85
+ }
86
+ }
87
+
88
+ rb_hash_aset(req, f, v);
89
+ }
90
+
91
+ void request_method(void *data, const char *at, size_t length)
92
+ {
93
+ VALUE req = (VALUE)data;
94
+ VALUE val = Qnil;
95
+
96
+ val = rb_str_new(at, length);
97
+ rb_hash_aset(req, global_request_method, val);
98
+ }
99
+
100
+ void request_uri(void *data, const char *at, size_t length)
101
+ {
102
+ VALUE req = (VALUE)data;
103
+ VALUE val = Qnil;
104
+
105
+ VALIDATE_MAX_LENGTH(length, REQUEST_URI);
106
+
107
+ val = rb_str_new(at, length);
108
+ rb_hash_aset(req, global_request_uri, val);
109
+ }
110
+
111
+ void fragment(void *data, const char *at, size_t length)
112
+ {
113
+ VALUE req = (VALUE)data;
114
+ VALUE val = Qnil;
115
+
116
+ VALIDATE_MAX_LENGTH(length, FRAGMENT);
117
+
118
+ val = rb_str_new(at, length);
119
+ rb_hash_aset(req, global_fragment, val);
120
+ }
121
+
122
+ void request_path(void *data, const char *at, size_t length)
123
+ {
124
+ VALUE req = (VALUE)data;
125
+ VALUE val = Qnil;
126
+
127
+ VALIDATE_MAX_LENGTH(length, REQUEST_PATH);
128
+
129
+ val = rb_str_new(at, length);
130
+ rb_hash_aset(req, global_request_path, val);
131
+ rb_hash_aset(req, global_path_info, val);
132
+ }
133
+
134
+ void query_string(void *data, const char *at, size_t length)
135
+ {
136
+ VALUE req = (VALUE)data;
137
+ VALUE val = Qnil;
138
+
139
+ VALIDATE_MAX_LENGTH(length, QUERY_STRING);
140
+
141
+ val = rb_str_new(at, length);
142
+ rb_hash_aset(req, global_query_string, val);
143
+ }
144
+
145
+ void http_version(void *data, const char *at, size_t length)
146
+ {
147
+ VALUE req = (VALUE)data;
148
+ VALUE val = rb_str_new(at, length);
149
+ rb_hash_aset(req, global_http_version, val);
150
+ }
151
+
152
+ /** Finalizes the request header to have a bunch of stuff that's
153
+ needed. */
154
+
155
+ void header_done(void *data, const char *at, size_t length)
156
+ {
157
+ VALUE req = (VALUE)data;
158
+ VALUE temp = Qnil;
159
+ VALUE ctype = Qnil;
160
+ VALUE clen = Qnil;
161
+ VALUE body = Qnil;
162
+ char *colon = NULL;
163
+
164
+ clen = rb_hash_aref(req, global_http_content_length);
165
+ if(clen != Qnil) {
166
+ rb_hash_aset(req, global_content_length, clen);
167
+ rb_hash_delete(req, global_http_content_length);
168
+ }
169
+
170
+ ctype = rb_hash_aref(req, global_http_content_type);
171
+ if(ctype != Qnil) {
172
+ rb_hash_aset(req, global_content_type, ctype);
173
+ rb_hash_delete(req, global_http_content_type);
174
+ }
175
+
176
+ rb_hash_aset(req, global_gateway_interface, global_gateway_interface_value);
177
+ if((temp = rb_hash_aref(req, global_http_host)) != Qnil) {
178
+ /* ruby better close strings off with a '\0' dammit */
179
+ colon = strchr(RSTRING(temp)->ptr, ':');
180
+ if(colon != NULL) {
181
+ rb_hash_aset(req, global_server_name, rb_str_substr(temp, 0, colon - RSTRING(temp)->ptr));
182
+ rb_hash_aset(req, global_server_port,
183
+ rb_str_substr(temp, colon - RSTRING(temp)->ptr+1,
184
+ RSTRING(temp)->len));
185
+ } else {
186
+ rb_hash_aset(req, global_server_name, temp);
187
+ rb_hash_aset(req, global_server_port, global_port_80);
188
+ }
189
+ }
190
+
191
+ /* grab the initial body and stuff it into the hash */
192
+ if(length > 0) {
193
+ body = rb_hash_aref(req, global_http_body);
194
+ rb_io_write(body, rb_str_new(at, length));
195
+ }
196
+
197
+ /* according to Rack specs, query string must be empty string if none */
198
+ if (rb_hash_aref(req, global_query_string) == Qnil) {
199
+ rb_hash_aset(req, global_query_string, global_empty);
200
+ }
201
+
202
+ /* set some constants */
203
+ rb_hash_aset(req, global_server_protocol, global_server_protocol_value);
204
+ rb_hash_aset(req, global_url_scheme, global_url_scheme_value);
205
+ rb_hash_aset(req, global_script_name, global_empty);
206
+ }
207
+
208
+
209
+ void HttpParser_free(void *data) {
210
+ TRACE();
211
+
212
+ if(data) {
213
+ free(data);
214
+ }
215
+ }
216
+
217
+
218
+ VALUE HttpParser_alloc(VALUE klass)
219
+ {
220
+ VALUE obj;
221
+ http_parser *hp = ALLOC_N(http_parser, 1);
222
+ TRACE();
223
+ hp->http_field = http_field;
224
+ hp->request_method = request_method;
225
+ hp->request_uri = request_uri;
226
+ hp->fragment = fragment;
227
+ hp->request_path = request_path;
228
+ hp->query_string = query_string;
229
+ hp->http_version = http_version;
230
+ hp->header_done = header_done;
231
+ http_parser_init(hp);
232
+
233
+ obj = Data_Wrap_Struct(klass, NULL, HttpParser_free, hp);
234
+
235
+ return obj;
236
+ }
237
+
238
+
239
+ /**
240
+ * call-seq:
241
+ * parser.new -> parser
242
+ *
243
+ * Creates a new parser.
244
+ */
245
+ VALUE HttpParser_init(VALUE self)
246
+ {
247
+ http_parser *http = NULL;
248
+ DATA_GET(self, http_parser, http);
249
+ http_parser_init(http);
250
+
251
+ return self;
252
+ }
253
+
254
+
255
+ /**
256
+ * call-seq:
257
+ * parser.reset -> nil
258
+ *
259
+ * Resets the parser to it's initial state so that you can reuse it
260
+ * rather than making new ones.
261
+ */
262
+ VALUE HttpParser_reset(VALUE self)
263
+ {
264
+ http_parser *http = NULL;
265
+ DATA_GET(self, http_parser, http);
266
+ http_parser_init(http);
267
+
268
+ return Qnil;
269
+ }
270
+
271
+
272
+ /**
273
+ * call-seq:
274
+ * parser.finish -> true/false
275
+ *
276
+ * Finishes a parser early which could put in a "good" or bad state.
277
+ * You should call reset after finish it or bad things will happen.
278
+ */
279
+ VALUE HttpParser_finish(VALUE self)
280
+ {
281
+ http_parser *http = NULL;
282
+ DATA_GET(self, http_parser, http);
283
+ http_parser_finish(http);
284
+
285
+ return http_parser_is_finished(http) ? Qtrue : Qfalse;
286
+ }
287
+
288
+
289
+ /**
290
+ * call-seq:
291
+ * parser.execute(req_hash, data, start) -> Integer
292
+ *
293
+ * Takes a Hash and a String of data, parses the String of data filling in the Hash
294
+ * returning an Integer to indicate how much of the data has been read. No matter
295
+ * what the return value, you should call HttpParser#finished? and HttpParser#error?
296
+ * to figure out if it's done parsing or there was an error.
297
+ *
298
+ * This function now throws an exception when there is a parsing error. This makes
299
+ * the logic for working with the parser much easier. You can still test for an
300
+ * error, but now you need to wrap the parser with an exception handling block.
301
+ *
302
+ * The third argument allows for parsing a partial request and then continuing
303
+ * the parsing from that position. It needs all of the original data as well
304
+ * so you have to append to the data buffer as you read.
305
+ */
306
+ VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
307
+ {
308
+ http_parser *http = NULL;
309
+ int from = 0;
310
+ char *dptr = NULL;
311
+ long dlen = 0;
312
+
313
+ DATA_GET(self, http_parser, http);
314
+
315
+ from = FIX2INT(start);
316
+ dptr = RSTRING(data)->ptr;
317
+ dlen = RSTRING(data)->len;
318
+
319
+ if(from >= dlen) {
320
+ rb_raise(eHttpParserError, "Requested start is after data buffer end.");
321
+ } else {
322
+ http->data = (void *)req_hash;
323
+ http_parser_execute(http, dptr, dlen, from);
324
+
325
+ VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
326
+
327
+ if(http_parser_has_error(http)) {
328
+ rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
329
+ } else {
330
+ return INT2FIX(http_parser_nread(http));
331
+ }
332
+ }
333
+ }
334
+
335
+
336
+
337
+ /**
338
+ * call-seq:
339
+ * parser.error? -> true/false
340
+ *
341
+ * Tells you whether the parser is in an error state.
342
+ */
343
+ VALUE HttpParser_has_error(VALUE self)
344
+ {
345
+ http_parser *http = NULL;
346
+ DATA_GET(self, http_parser, http);
347
+
348
+ return http_parser_has_error(http) ? Qtrue : Qfalse;
349
+ }
350
+
351
+
352
+ /**
353
+ * call-seq:
354
+ * parser.finished? -> true/false
355
+ *
356
+ * Tells you whether the parser is finished or not and in a good state.
357
+ */
358
+ VALUE HttpParser_is_finished(VALUE self)
359
+ {
360
+ http_parser *http = NULL;
361
+ DATA_GET(self, http_parser, http);
362
+
363
+ return http_parser_is_finished(http) ? Qtrue : Qfalse;
364
+ }
365
+
366
+
367
+ /**
368
+ * call-seq:
369
+ * parser.nread -> Integer
370
+ *
371
+ * Returns the amount of data processed so far during this processing cycle. It is
372
+ * set to 0 on initialize or reset calls and is incremented each time execute is called.
373
+ */
374
+ VALUE HttpParser_nread(VALUE self)
375
+ {
376
+ http_parser *http = NULL;
377
+ DATA_GET(self, http_parser, http);
378
+
379
+ return INT2FIX(http->nread);
380
+ }
381
+
382
+ void Init_thin_parser()
383
+ {
384
+
385
+ mThin = rb_define_module("Thin");
386
+
387
+ DEF_GLOBAL(empty, "");
388
+ DEF_GLOBAL(http_prefix, "HTTP_");
389
+ DEF_GLOBAL(request_method, "REQUEST_METHOD");
390
+ DEF_GLOBAL(request_uri, "REQUEST_URI");
391
+ DEF_GLOBAL(fragment, "FRAGMENT");
392
+ DEF_GLOBAL(query_string, "QUERY_STRING");
393
+ DEF_GLOBAL(http_version, "HTTP_VERSION");
394
+ DEF_GLOBAL(request_path, "REQUEST_PATH");
395
+ DEF_GLOBAL(content_length, "CONTENT_LENGTH");
396
+ DEF_GLOBAL(http_content_length, "HTTP_CONTENT_LENGTH");
397
+ DEF_GLOBAL(content_type, "CONTENT_TYPE");
398
+ DEF_GLOBAL(http_content_type, "HTTP_CONTENT_TYPE");
399
+ DEF_GLOBAL(gateway_interface, "GATEWAY_INTERFACE");
400
+ DEF_GLOBAL(gateway_interface_value, "CGI/1.2");
401
+ DEF_GLOBAL(server_name, "SERVER_NAME");
402
+ DEF_GLOBAL(server_port, "SERVER_PORT");
403
+ DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL");
404
+ DEF_GLOBAL(server_protocol_value, "HTTP/1.1");
405
+ DEF_GLOBAL(http_host, "HTTP_HOST");
406
+ DEF_GLOBAL(port_80, "80");
407
+ DEF_GLOBAL(http_body, "rack.input");
408
+ DEF_GLOBAL(url_scheme, "rack.url_scheme");
409
+ DEF_GLOBAL(url_scheme_value, "http");
410
+ DEF_GLOBAL(script_name, "SCRIPT_NAME");
411
+ DEF_GLOBAL(path_info, "PATH_INFO");
412
+
413
+ eHttpParserError = rb_define_class_under(mThin, "InvalidRequest", rb_eIOError);
414
+
415
+ cHttpParser = rb_define_class_under(mThin, "HttpParser", rb_cObject);
416
+ rb_define_alloc_func(cHttpParser, HttpParser_alloc);
417
+ rb_define_method(cHttpParser, "initialize", HttpParser_init,0);
418
+ rb_define_method(cHttpParser, "reset", HttpParser_reset,0);
419
+ rb_define_method(cHttpParser, "finish", HttpParser_finish,0);
420
+ rb_define_method(cHttpParser, "execute", HttpParser_execute,3);
421
+ rb_define_method(cHttpParser, "error?", HttpParser_has_error,0);
422
+ rb_define_method(cHttpParser, "finished?", HttpParser_is_finished,0);
423
+ rb_define_method(cHttpParser, "nread", HttpParser_nread,0);
424
+ }