engineyard-mongrel 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/CHANGELOG +16 -0
  2. data/CONTRIBUTORS +17 -0
  3. data/COPYING +55 -0
  4. data/LICENSE +55 -0
  5. data/Manifest +71 -0
  6. data/README +74 -0
  7. data/bin/mongrel_rails +285 -0
  8. data/examples/builder.rb +29 -0
  9. data/examples/camping/README +3 -0
  10. data/examples/camping/blog.rb +294 -0
  11. data/examples/camping/tepee.rb +149 -0
  12. data/examples/httpd.conf +474 -0
  13. data/examples/mime.yaml +3 -0
  14. data/examples/mongrel.conf +9 -0
  15. data/examples/mongrel_simple_ctrl.rb +92 -0
  16. data/examples/mongrel_simple_service.rb +116 -0
  17. data/examples/monitrc +57 -0
  18. data/examples/random_thrash.rb +19 -0
  19. data/examples/simpletest.rb +52 -0
  20. data/examples/webrick_compare.rb +20 -0
  21. data/ext/http11/ext_help.h +15 -0
  22. data/ext/http11/extconf.rb +6 -0
  23. data/ext/http11/http11.c +527 -0
  24. data/ext/http11/http11_parser.c +1243 -0
  25. data/ext/http11/http11_parser.h +49 -0
  26. data/ext/http11/http11_parser.java.rl +171 -0
  27. data/ext/http11/http11_parser.rl +152 -0
  28. data/ext/http11/http11_parser_common.rl +54 -0
  29. data/ext/http11_java/Http11Service.java +13 -0
  30. data/ext/http11_java/org/jruby/mongrel/Http11.java +266 -0
  31. data/ext/http11_java/org/jruby/mongrel/Http11Parser.java +474 -0
  32. data/lib/mongrel.rb +360 -0
  33. data/lib/mongrel/camping.rb +107 -0
  34. data/lib/mongrel/cgi.rb +181 -0
  35. data/lib/mongrel/command.rb +222 -0
  36. data/lib/mongrel/configurator.rb +389 -0
  37. data/lib/mongrel/const.rb +110 -0
  38. data/lib/mongrel/debug.rb +203 -0
  39. data/lib/mongrel/gems.rb +22 -0
  40. data/lib/mongrel/handlers.rb +468 -0
  41. data/lib/mongrel/header_out.rb +28 -0
  42. data/lib/mongrel/http_request.rb +155 -0
  43. data/lib/mongrel/http_response.rb +166 -0
  44. data/lib/mongrel/init.rb +10 -0
  45. data/lib/mongrel/mime_types.yml +616 -0
  46. data/lib/mongrel/rails.rb +214 -0
  47. data/lib/mongrel/stats.rb +89 -0
  48. data/lib/mongrel/tcphack.rb +18 -0
  49. data/lib/mongrel/uri_classifier.rb +76 -0
  50. data/mongrel-public_cert.pem +20 -0
  51. data/setup.rb +1585 -0
  52. data/test/benchmark/previous.rb +11 -0
  53. data/test/benchmark/simple.rb +11 -0
  54. data/test/benchmark/utils.rb +82 -0
  55. data/test/mime.yaml +3 -0
  56. data/test/mongrel.conf +1 -0
  57. data/test/test_helper.rb +79 -0
  58. data/test/tools/trickletest.rb +45 -0
  59. data/test/unit/test_cgi_wrapper.rb +26 -0
  60. data/test/unit/test_command.rb +86 -0
  61. data/test/unit/test_conditional.rb +107 -0
  62. data/test/unit/test_configurator.rb +88 -0
  63. data/test/unit/test_debug.rb +25 -0
  64. data/test/unit/test_handlers.rb +136 -0
  65. data/test/unit/test_http_parser.rb +156 -0
  66. data/test/unit/test_redirect_handler.rb +45 -0
  67. data/test/unit/test_request_progress.rb +100 -0
  68. data/test/unit/test_response.rb +127 -0
  69. data/test/unit/test_stats.rb +35 -0
  70. data/test/unit/test_uriclassifier.rb +261 -0
  71. data/test/unit/test_ws.rb +116 -0
  72. metadata +158 -0
@@ -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,171 @@
1
+ package org.jruby.mongrel;
2
+
3
+ import org.jruby.util.ByteList;
4
+
5
+ public class Http11Parser {
6
+
7
+ /** Machine **/
8
+
9
+ %%{
10
+
11
+ machine http_parser;
12
+
13
+ action mark {parser.mark = fpc; }
14
+
15
+ action start_field { parser.field_start = fpc; }
16
+ action snake_upcase_field { /* FIXME stub */ }
17
+ action write_field {
18
+ parser.field_len = fpc-parser.field_start;
19
+ }
20
+
21
+ action start_value { parser.mark = fpc; }
22
+ action write_value {
23
+ if(parser.http_field != null) {
24
+ parser.http_field.call(parser.data, parser.field_start, parser.field_len, parser.mark, fpc-parser.mark);
25
+ }
26
+ }
27
+ action request_method {
28
+ if(parser.request_method != null)
29
+ parser.request_method.call(parser.data, parser.mark, fpc-parser.mark);
30
+ }
31
+ action request_uri {
32
+ if(parser.request_uri != null)
33
+ parser.request_uri.call(parser.data, parser.mark, fpc-parser.mark);
34
+ }
35
+ action fragment {
36
+ if(parser.fragment != null)
37
+ parser.fragment.call(parser.data, parser.mark, fpc-parser.mark);
38
+ }
39
+
40
+ action start_query {parser.query_start = fpc; }
41
+ action query_string {
42
+ if(parser.query_string != null)
43
+ parser.query_string.call(parser.data, parser.query_start, fpc-parser.query_start);
44
+ }
45
+
46
+ action http_version {
47
+ if(parser.http_version != null)
48
+ parser.http_version.call(parser.data, parser.mark, fpc-parser.mark);
49
+ }
50
+
51
+ action request_path {
52
+ if(parser.request_path != null)
53
+ parser.request_path.call(parser.data, parser.mark, fpc-parser.mark);
54
+ }
55
+
56
+ action done {
57
+ parser.body_start = fpc + 1;
58
+ if(parser.header_done != null)
59
+ parser.header_done.call(parser.data, fpc + 1, pe - fpc - 1);
60
+ fbreak;
61
+ }
62
+
63
+ include http_parser_common "http11_parser_common.rl";
64
+
65
+ }%%
66
+
67
+ /** Data **/
68
+ %% write data;
69
+
70
+ public static interface ElementCB {
71
+ public void call(Object data, int at, int length);
72
+ }
73
+
74
+ public static interface FieldCB {
75
+ public void call(Object data, int field, int flen, int value, int vlen);
76
+ }
77
+
78
+ public static class HttpParser {
79
+ int cs;
80
+ int body_start;
81
+ int content_len;
82
+ int nread;
83
+ int mark;
84
+ int field_start;
85
+ int field_len;
86
+ int query_start;
87
+
88
+ Object data;
89
+ ByteList buffer;
90
+
91
+ public FieldCB http_field;
92
+ public ElementCB request_method;
93
+ public ElementCB request_uri;
94
+ public ElementCB fragment;
95
+ public ElementCB request_path;
96
+ public ElementCB query_string;
97
+ public ElementCB http_version;
98
+ public ElementCB header_done;
99
+
100
+ public void init() {
101
+ cs = 0;
102
+
103
+ %% write init;
104
+
105
+ body_start = 0;
106
+ content_len = 0;
107
+ mark = 0;
108
+ nread = 0;
109
+ field_len = 0;
110
+ field_start = 0;
111
+ }
112
+ }
113
+
114
+ public final HttpParser parser = new HttpParser();
115
+
116
+ public int execute(ByteList buffer, int off) {
117
+ int p, pe;
118
+ int cs = parser.cs;
119
+ int len = buffer.realSize;
120
+ assert off<=len : "offset past end of buffer";
121
+
122
+ p = off;
123
+ pe = len;
124
+ byte[] data = buffer.bytes;
125
+ parser.buffer = buffer;
126
+
127
+ %% write exec;
128
+
129
+ parser.cs = cs;
130
+ parser.nread += (p - off);
131
+
132
+ assert p <= pe : "buffer overflow after parsing execute";
133
+ assert parser.nread <= len : "nread longer than length";
134
+ assert parser.body_start <= len : "body starts after buffer end";
135
+ assert parser.mark < len : "mark is after buffer end";
136
+ assert parser.field_len <= len : "field has length longer than whole buffer";
137
+ assert parser.field_start < len : "field starts after buffer end";
138
+
139
+ if(parser.body_start>0) {
140
+ /* final \r\n combo encountered so stop right here */
141
+ %%write eof;
142
+ parser.nread++;
143
+ }
144
+
145
+ return parser.nread;
146
+ }
147
+
148
+ public int finish() {
149
+ int cs = parser.cs;
150
+
151
+ %%write eof;
152
+
153
+ parser.cs = cs;
154
+
155
+ if(has_error()) {
156
+ return -1;
157
+ } else if(is_finished()) {
158
+ return 1;
159
+ } else {
160
+ return 0;
161
+ }
162
+ }
163
+
164
+ public boolean has_error() {
165
+ return parser.cs == http_parser_error;
166
+ }
167
+
168
+ public boolean is_finished() {
169
+ return parser.cs == http_parser_first_final;
170
+ }
171
+ }
@@ -0,0 +1,152 @@
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 "http11_parser.h"
6
+ #include <stdio.h>
7
+ #include <assert.h>
8
+ #include <stdlib.h>
9
+ #include <ctype.h>
10
+ #include <string.h>
11
+
12
+ /*
13
+ * capitalizes all lower-case ASCII characters,
14
+ * converts dashes to underscores.
15
+ */
16
+ static void snake_upcase_char(char *c)
17
+ {
18
+ if (*c >= 'a' && *c <= 'z')
19
+ *c &= ~0x20;
20
+ else if (*c == '-')
21
+ *c = '_';
22
+ }
23
+
24
+ #define LEN(AT, FPC) (FPC - buffer - parser->AT)
25
+ #define MARK(M,FPC) (parser->M = (FPC) - buffer)
26
+ #define PTR_TO(F) (buffer + parser->F)
27
+
28
+ /** Machine **/
29
+
30
+ %%{
31
+
32
+ machine http_parser;
33
+
34
+ action mark {MARK(mark, fpc); }
35
+
36
+
37
+ action start_field { MARK(field_start, fpc); }
38
+ action snake_upcase_field { snake_upcase_char((char *)fpc); }
39
+ action write_field {
40
+ parser->field_len = LEN(field_start, fpc);
41
+ }
42
+
43
+ action start_value { MARK(mark, fpc); }
44
+ action write_value {
45
+ if(parser->http_field != NULL) {
46
+ parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
47
+ }
48
+ }
49
+ action request_method {
50
+ if(parser->request_method != NULL)
51
+ parser->request_method(parser->data, PTR_TO(mark), LEN(mark, fpc));
52
+ }
53
+ action request_uri {
54
+ if(parser->request_uri != NULL)
55
+ parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, fpc));
56
+ }
57
+ action fragment {
58
+ if(parser->fragment != NULL)
59
+ parser->fragment(parser->data, PTR_TO(mark), LEN(mark, fpc));
60
+ }
61
+
62
+ action start_query {MARK(query_start, fpc); }
63
+ action query_string {
64
+ if(parser->query_string != NULL)
65
+ parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, fpc));
66
+ }
67
+
68
+ action http_version {
69
+ if(parser->http_version != NULL)
70
+ parser->http_version(parser->data, PTR_TO(mark), LEN(mark, fpc));
71
+ }
72
+
73
+ action request_path {
74
+ if(parser->request_path != NULL)
75
+ parser->request_path(parser->data, PTR_TO(mark), LEN(mark,fpc));
76
+ }
77
+
78
+ action done {
79
+ parser->body_start = fpc - buffer + 1;
80
+ if(parser->header_done != NULL)
81
+ parser->header_done(parser->data, fpc + 1, pe - fpc - 1);
82
+ fbreak;
83
+ }
84
+
85
+ include http_parser_common "http11_parser_common.rl";
86
+
87
+ }%%
88
+
89
+ /** Data **/
90
+ %% write data;
91
+
92
+ int http_parser_init(http_parser *parser) {
93
+ int cs = 0;
94
+ %% write init;
95
+ parser->cs = cs;
96
+ parser->body_start = 0;
97
+ parser->content_len = 0;
98
+ parser->mark = 0;
99
+ parser->nread = 0;
100
+ parser->field_len = 0;
101
+ parser->field_start = 0;
102
+
103
+ return(1);
104
+ }
105
+
106
+
107
+ /** exec **/
108
+ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, size_t off) {
109
+ const char *p, *pe;
110
+ int cs = parser->cs;
111
+
112
+ assert(off <= len && "offset past end of buffer");
113
+
114
+ p = buffer+off;
115
+ pe = buffer+len;
116
+
117
+ /* assert(*pe == '\0' && "pointer does not end on NUL"); */
118
+ assert(pe - p == len - off && "pointers aren't same distance");
119
+
120
+ %% write exec;
121
+
122
+ parser->cs = cs;
123
+ parser->nread += p - (buffer + off);
124
+
125
+ assert(p <= pe && "buffer overflow after parsing execute");
126
+ assert(parser->nread <= len && "nread longer than length");
127
+ assert(parser->body_start <= len && "body starts after buffer end");
128
+ assert(parser->mark < len && "mark is after buffer end");
129
+ assert(parser->field_len <= len && "field has length longer than whole buffer");
130
+ assert(parser->field_start < len && "field starts after buffer end");
131
+
132
+ return(parser->nread);
133
+ }
134
+
135
+ int http_parser_finish(http_parser *parser)
136
+ {
137
+ if (http_parser_has_error(parser) ) {
138
+ return -1;
139
+ } else if (http_parser_is_finished(parser) ) {
140
+ return 1;
141
+ } else {
142
+ return 0;
143
+ }
144
+ }
145
+
146
+ int http_parser_has_error(http_parser *parser) {
147
+ return parser->cs == http_parser_error;
148
+ }
149
+
150
+ int http_parser_is_finished(http_parser *parser) {
151
+ return parser->cs >= http_parser_first_final;
152
+ }
@@ -0,0 +1,54 @@
1
+ %%{
2
+
3
+ machine http_parser_common;
4
+
5
+ #### HTTP PROTOCOL GRAMMAR
6
+ # line endings
7
+ CRLF = "\r\n";
8
+
9
+ # character types
10
+ CTL = (cntrl | 127);
11
+ safe = ("$" | "-" | "_" | ".");
12
+ extra = ("!" | "*" | "'" | "(" | ")" | ",");
13
+ reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+");
14
+ unsafe = (CTL | " " | "\"" | "#" | "%" | "<" | ">");
15
+ national = any -- (alpha | digit | reserved | extra | safe | unsafe);
16
+ unreserved = (alpha | digit | safe | extra | national);
17
+ escape = ("%" xdigit xdigit);
18
+ uchar = (unreserved | escape);
19
+ pchar = (uchar | ":" | "@" | "&" | "=" | "+");
20
+ tspecials = ("(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\\" | "\"" | "/" | "[" | "]" | "?" | "=" | "{" | "}" | " " | "\t");
21
+
22
+ # elements
23
+ token = (ascii -- (CTL | tspecials));
24
+
25
+ # URI schemes and absolute paths
26
+ scheme = ( alpha | digit | "+" | "-" | "." )* ;
27
+ absolute_uri = (scheme ":" (uchar | reserved )*);
28
+
29
+ path = ( pchar+ ( "/" pchar* )* ) ;
30
+ query = ( uchar | reserved )* %query_string ;
31
+ param = ( pchar | "/" )* ;
32
+ params = ( param ( ";" param )* ) ;
33
+ rel_path = ( path? %request_path (";" params)? ) ("?" %start_query query)?;
34
+ absolute_path = ( "/"+ rel_path );
35
+
36
+ Request_URI = ( "*" | absolute_uri | absolute_path ) >mark %request_uri;
37
+ Fragment = ( uchar | reserved )* >mark %fragment;
38
+ Method = ( upper | digit | safe ){1,20} >mark %request_method;
39
+
40
+ http_number = ( digit+ "." digit+ ) ;
41
+ HTTP_Version = ( "HTTP/" http_number ) >mark %http_version ;
42
+ Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " HTTP_Version CRLF ) ;
43
+
44
+ field_name = ( token -- ":" )+ >start_field $snake_upcase_field %write_field;
45
+
46
+ field_value = any* >start_value %write_value;
47
+
48
+ message_header = field_name ":" " "* field_value :> CRLF;
49
+
50
+ Request = Request_Line ( message_header )* ( CRLF @done );
51
+
52
+ main := Request;
53
+
54
+ }%%
@@ -0,0 +1,13 @@
1
+ import java.io.IOException;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.runtime.load.BasicLibraryService;
5
+
6
+ import org.jruby.mongrel.Http11;
7
+
8
+ public class Http11Service implements BasicLibraryService {
9
+ public boolean basicLoad(final Ruby runtime) throws IOException {
10
+ Http11.createHttp11(runtime);
11
+ return true;
12
+ }
13
+ }
@@ -0,0 +1,266 @@
1
+ /***** BEGIN LICENSE BLOCK *****
2
+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3
+ *
4
+ * The contents of this file are subject to the Common Public
5
+ * License Version 1.0 (the "License"); you may not use this file
6
+ * except in compliance with the License. You may obtain a copy of
7
+ * the License at http://www.eclipse.org/legal/cpl-v10.html
8
+ *
9
+ * Software distributed under the License is distributed on an "AS
10
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
+ * implied. See the License for the specific language governing
12
+ * rights and limitations under the License.
13
+ *
14
+ * Copyright (C) 2007 Ola Bini <ola@ologix.com>
15
+ *
16
+ * Alternatively, the contents of this file may be used under the terms of
17
+ * either of the GNU General Public License Version 2 or later (the "GPL"),
18
+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19
+ * in which case the provisions of the GPL or the LGPL are applicable instead
20
+ * of those above. If you wish to allow use of your version of this file only
21
+ * under the terms of either the GPL or the LGPL, and not to allow others to
22
+ * use your version of this file under the terms of the CPL, indicate your
23
+ * decision by deleting the provisions above and replace them with the notice
24
+ * and other provisions required by the GPL or the LGPL. If you do not delete
25
+ * the provisions above, a recipient may use your version of this file under
26
+ * the terms of any one of the CPL, the GPL or the LGPL.
27
+ ***** END LICENSE BLOCK *****/
28
+ package org.jruby.mongrel;
29
+
30
+ import org.jruby.Ruby;
31
+ import org.jruby.RubyClass;
32
+ import org.jruby.RubyHash;
33
+ import org.jruby.RubyModule;
34
+ import org.jruby.RubyNumeric;
35
+ import org.jruby.RubyObject;
36
+ import org.jruby.RubyString;
37
+
38
+ import org.jruby.runtime.CallbackFactory;
39
+ import org.jruby.runtime.ObjectAllocator;
40
+ import org.jruby.runtime.builtin.IRubyObject;
41
+
42
+ import org.jruby.exceptions.RaiseException;
43
+
44
+ import org.jruby.util.ByteList;
45
+
46
+ /**
47
+ * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a>
48
+ */
49
+ public class Http11 extends RubyObject {
50
+ public final static int MAX_FIELD_NAME_LENGTH = 256;
51
+ public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length.";
52
+ public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024;
53
+ public final static String MAX_FIELD_VALUE_LENGTH_ERR = "HTTP element FIELD_VALUE is longer than the 81920 allowed length.";
54
+ public final static int MAX_REQUEST_URI_LENGTH = 1024 * 12;
55
+ public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the 12288 allowed length.";
56
+ public final static int MAX_FRAGMENT_LENGTH = 1024;
57
+ public final static String MAX_FRAGMENT_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length.";
58
+ public final static int MAX_REQUEST_PATH_LENGTH = 1024;
59
+ public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length.";
60
+ public final static int MAX_QUERY_STRING_LENGTH = 1024 * 10;
61
+ public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the 10240 allowed length.";
62
+ public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32);
63
+ public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length.";
64
+
65
+
66
+ private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
67
+ public IRubyObject allocate(Ruby runtime, RubyClass klass) {
68
+ return new Http11(runtime, klass);
69
+ }
70
+ };
71
+
72
+ public static void createHttp11(Ruby runtime) {
73
+ RubyModule mMongrel = runtime.defineModule("Mongrel");
74
+ mMongrel.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
75
+
76
+ CallbackFactory cf = runtime.callbackFactory(Http11.class);
77
+
78
+ RubyClass cHttpParser = mMongrel.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
79
+ cHttpParser.defineFastMethod("initialize",cf.getFastMethod("initialize"));
80
+ cHttpParser.defineFastMethod("reset",cf.getFastMethod("reset"));
81
+ cHttpParser.defineFastMethod("finish",cf.getFastMethod("finish"));
82
+ cHttpParser.defineFastMethod("execute",cf.getFastMethod("execute", IRubyObject.class, IRubyObject.class, IRubyObject.class));
83
+ cHttpParser.defineFastMethod("error?",cf.getFastMethod("has_error"));
84
+ cHttpParser.defineFastMethod("finished?",cf.getFastMethod("is_finished"));
85
+ cHttpParser.defineFastMethod("nread",cf.getFastMethod("nread"));
86
+ }
87
+
88
+ private Ruby runtime;
89
+ private RubyClass eHttpParserError;
90
+ private Http11Parser hp;
91
+
92
+ public Http11(Ruby runtime, RubyClass clazz) {
93
+ super(runtime,clazz);
94
+ this.runtime = runtime;
95
+ this.eHttpParserError = (RubyClass)runtime.getModule("Mongrel").getConstant("HttpParserError");
96
+ this.hp = new Http11Parser();
97
+ this.hp.parser.http_field = http_field;
98
+ this.hp.parser.request_method = request_method;
99
+ this.hp.parser.request_uri = request_uri;
100
+ this.hp.parser.fragment = fragment;
101
+ this.hp.parser.request_path = request_path;
102
+ this.hp.parser.query_string = query_string;
103
+ this.hp.parser.http_version = http_version;
104
+ this.hp.parser.header_done = header_done;
105
+ this.hp.parser.init();
106
+ }
107
+
108
+ public void validateMaxLength(int len, int max, String msg) {
109
+ if(len>max) {
110
+ throw new RaiseException(runtime, eHttpParserError, msg, true);
111
+ }
112
+ }
113
+
114
+ private Http11Parser.FieldCB http_field = new Http11Parser.FieldCB() {
115
+ public void call(Object data, int field, int flen, int value, int vlen) {
116
+ RubyHash req = (RubyHash)data;
117
+ RubyString v,f;
118
+ validateMaxLength(flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
119
+ validateMaxLength(vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
120
+
121
+ v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen));
122
+ f = RubyString.newString(runtime, "HTTP_");
123
+ ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen);
124
+ for(int i=0,j=b.realSize;i<j;i++) {
125
+ if((b.bytes[i]&0xFF) == '-') {
126
+ b.bytes[i] = (byte)'_';
127
+ } else {
128
+ b.bytes[i] = (byte)Character.toUpperCase((char)b.bytes[i]);
129
+ }
130
+ }
131
+ f.cat(b);
132
+ req.aset(f,v);
133
+ }
134
+ };
135
+
136
+ private Http11Parser.ElementCB request_method = new Http11Parser.ElementCB() {
137
+ public void call(Object data, int at, int length) {
138
+ RubyHash req = (RubyHash)data;
139
+ RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
140
+ req.aset(runtime.newString("REQUEST_METHOD"),val);
141
+ }
142
+ };
143
+
144
+ private Http11Parser.ElementCB request_uri = new Http11Parser.ElementCB() {
145
+ public void call(Object data, int at, int length) {
146
+ RubyHash req = (RubyHash)data;
147
+ validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
148
+ RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
149
+ req.aset(runtime.newString("REQUEST_URI"),val);
150
+ }
151
+ };
152
+
153
+ private Http11Parser.ElementCB fragment = new Http11Parser.ElementCB() {
154
+ public void call(Object data, int at, int length) {
155
+ RubyHash req = (RubyHash)data;
156
+ validateMaxLength(length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
157
+ RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
158
+ req.aset(runtime.newString("FRAGMENT"),val);
159
+ }
160
+ };
161
+
162
+ private Http11Parser.ElementCB request_path = new Http11Parser.ElementCB() {
163
+ public void call(Object data, int at, int length) {
164
+ RubyHash req = (RubyHash)data;
165
+ validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
166
+ RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
167
+ req.aset(runtime.newString("REQUEST_PATH"),val);
168
+ }
169
+ };
170
+
171
+ private Http11Parser.ElementCB query_string = new Http11Parser.ElementCB() {
172
+ public void call(Object data, int at, int length) {
173
+ RubyHash req = (RubyHash)data;
174
+ validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
175
+ RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
176
+ req.aset(runtime.newString("QUERY_STRING"),val);
177
+ }
178
+ };
179
+
180
+ private Http11Parser.ElementCB http_version = new Http11Parser.ElementCB() {
181
+ public void call(Object data, int at, int length) {
182
+ RubyHash req = (RubyHash)data;
183
+ RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
184
+ req.aset(runtime.newString("HTTP_VERSION"),val);
185
+ }
186
+ };
187
+
188
+ private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() {
189
+ public void call(Object data, int at, int length) {
190
+ RubyHash req = (RubyHash)data;
191
+ IRubyObject temp,ctype,clen;
192
+
193
+ clen = req.aref(runtime.newString("HTTP_CONTENT_LENGTH"));
194
+ if(!clen.isNil()) {
195
+ req.aset(runtime.newString("CONTENT_LENGTH"),clen);
196
+ }
197
+
198
+ ctype = req.aref(runtime.newString("HTTP_CONTENT_TYPE"));
199
+ if(!ctype.isNil()) {
200
+ req.aset(runtime.newString("CONTENT_TYPE"),ctype);
201
+ }
202
+
203
+ req.aset(runtime.newString("GATEWAY_INTERFACE"),runtime.newString("CGI/1.2"));
204
+ if(!(temp = req.aref(runtime.newString("HTTP_HOST"))).isNil()) {
205
+ String s = temp.toString();
206
+ int colon = s.indexOf(':');
207
+ if(colon != -1) {
208
+ req.aset(runtime.newString("SERVER_NAME"),runtime.newString(s.substring(0,colon)));
209
+ req.aset(runtime.newString("SERVER_PORT"),runtime.newString(s.substring(colon+1)));
210
+ } else {
211
+ req.aset(runtime.newString("SERVER_NAME"),temp);
212
+ req.aset(runtime.newString("SERVER_PORT"),runtime.newString("80"));
213
+ }
214
+ }
215
+
216
+ req.setInstanceVariable("@http_body", RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length)));
217
+ req.aset(runtime.newString("SERVER_PROTOCOL"),runtime.newString("HTTP/1.1"));
218
+ req.aset(runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.2.0"));
219
+ }
220
+ };
221
+
222
+ public IRubyObject initialize() {
223
+ this.hp.parser.init();
224
+ return this;
225
+ }
226
+
227
+ public IRubyObject reset() {
228
+ this.hp.parser.init();
229
+ return runtime.getNil();
230
+ }
231
+
232
+ public IRubyObject finish() {
233
+ this.hp.finish();
234
+ return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
235
+ }
236
+
237
+ public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
238
+ int from = 0;
239
+ from = RubyNumeric.fix2int(start);
240
+ ByteList d = ((RubyString)data).getByteList();
241
+ if(from >= d.realSize) {
242
+ throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true);
243
+ } else {
244
+ this.hp.parser.data = req_hash;
245
+ this.hp.execute(d,from);
246
+ validateMaxLength(this.hp.parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
247
+ if(this.hp.has_error()) {
248
+ throw new RaiseException(runtime, eHttpParserError, "Invalid HTTP format, parsing fails.", true);
249
+ } else {
250
+ return runtime.newFixnum((long)this.hp.parser.nread);
251
+ }
252
+ }
253
+ }
254
+
255
+ public IRubyObject has_error() {
256
+ return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
257
+ }
258
+
259
+ public IRubyObject is_finished() {
260
+ return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
261
+ }
262
+
263
+ public IRubyObject nread() {
264
+ return runtime.newFixnum(this.hp.parser.nread);
265
+ }
266
+ }// Http11