mongrel 1.1.5-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data.tar.gz.sig +4 -0
  2. data/CHANGELOG +18 -0
  3. data/COPYING +55 -0
  4. data/LICENSE +55 -0
  5. data/Manifest +69 -0
  6. data/README +74 -0
  7. data/TODO +5 -0
  8. data/bin/mongrel_rails +283 -0
  9. data/examples/builder.rb +29 -0
  10. data/examples/camping/README +3 -0
  11. data/examples/camping/blog.rb +294 -0
  12. data/examples/camping/tepee.rb +149 -0
  13. data/examples/httpd.conf +474 -0
  14. data/examples/mime.yaml +3 -0
  15. data/examples/mongrel.conf +9 -0
  16. data/examples/mongrel_simple_ctrl.rb +92 -0
  17. data/examples/mongrel_simple_service.rb +116 -0
  18. data/examples/monitrc +57 -0
  19. data/examples/random_thrash.rb +19 -0
  20. data/examples/simpletest.rb +52 -0
  21. data/examples/webrick_compare.rb +20 -0
  22. data/ext/http11/ext_help.h +14 -0
  23. data/ext/http11/extconf.rb +6 -0
  24. data/ext/http11/http11.c +402 -0
  25. data/ext/http11/http11_parser.c +1221 -0
  26. data/ext/http11/http11_parser.h +49 -0
  27. data/ext/http11/http11_parser.java.rl +170 -0
  28. data/ext/http11/http11_parser.rl +152 -0
  29. data/ext/http11/http11_parser_common.rl +54 -0
  30. data/ext/http11_java/Http11Service.java +13 -0
  31. data/ext/http11_java/org/jruby/mongrel/Http11.java +266 -0
  32. data/ext/http11_java/org/jruby/mongrel/Http11Parser.java +572 -0
  33. data/lib/http11.so +0 -0
  34. data/lib/mongrel.rb +355 -0
  35. data/lib/mongrel/camping.rb +107 -0
  36. data/lib/mongrel/cgi.rb +181 -0
  37. data/lib/mongrel/command.rb +222 -0
  38. data/lib/mongrel/configurator.rb +388 -0
  39. data/lib/mongrel/const.rb +110 -0
  40. data/lib/mongrel/debug.rb +203 -0
  41. data/lib/mongrel/gems.rb +22 -0
  42. data/lib/mongrel/handlers.rb +468 -0
  43. data/lib/mongrel/header_out.rb +28 -0
  44. data/lib/mongrel/http_request.rb +155 -0
  45. data/lib/mongrel/http_response.rb +163 -0
  46. data/lib/mongrel/init.rb +10 -0
  47. data/lib/mongrel/mime_types.yml +616 -0
  48. data/lib/mongrel/rails.rb +185 -0
  49. data/lib/mongrel/stats.rb +89 -0
  50. data/lib/mongrel/tcphack.rb +18 -0
  51. data/lib/mongrel/uri_classifier.rb +76 -0
  52. data/mongrel-public_cert.pem +20 -0
  53. data/mongrel.gemspec +242 -0
  54. data/setup.rb +1585 -0
  55. data/test/mime.yaml +3 -0
  56. data/test/mongrel.conf +1 -0
  57. data/test/test_cgi_wrapper.rb +26 -0
  58. data/test/test_command.rb +86 -0
  59. data/test/test_conditional.rb +107 -0
  60. data/test/test_configurator.rb +88 -0
  61. data/test/test_debug.rb +25 -0
  62. data/test/test_handlers.rb +126 -0
  63. data/test/test_http11.rb +156 -0
  64. data/test/test_redirect_handler.rb +45 -0
  65. data/test/test_request_progress.rb +100 -0
  66. data/test/test_response.rb +127 -0
  67. data/test/test_stats.rb +35 -0
  68. data/test/test_uriclassifier.rb +261 -0
  69. data/test/test_ws.rb +115 -0
  70. data/test/testhelp.rb +79 -0
  71. data/tools/trickletest.rb +45 -0
  72. metadata +197 -0
  73. metadata.gz.sig +1 -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,170 @@
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 write_field {
17
+ parser.field_len = fpc-parser.field_start;
18
+ }
19
+
20
+ action start_value { parser.mark = fpc; }
21
+ action write_value {
22
+ if(parser.http_field != null) {
23
+ parser.http_field.call(parser.data, parser.field_start, parser.field_len, parser.mark, fpc-parser.mark);
24
+ }
25
+ }
26
+ action request_method {
27
+ if(parser.request_method != null)
28
+ parser.request_method.call(parser.data, parser.mark, fpc-parser.mark);
29
+ }
30
+ action request_uri {
31
+ if(parser.request_uri != null)
32
+ parser.request_uri.call(parser.data, parser.mark, fpc-parser.mark);
33
+ }
34
+ action fragment {
35
+ if(parser.fragment != null)
36
+ parser.fragment.call(parser.data, parser.mark, fpc-parser.mark);
37
+ }
38
+
39
+ action start_query {parser.query_start = fpc; }
40
+ action query_string {
41
+ if(parser.query_string != null)
42
+ parser.query_string.call(parser.data, parser.query_start, fpc-parser.query_start);
43
+ }
44
+
45
+ action http_version {
46
+ if(parser.http_version != null)
47
+ parser.http_version.call(parser.data, parser.mark, fpc-parser.mark);
48
+ }
49
+
50
+ action request_path {
51
+ if(parser.request_path != null)
52
+ parser.request_path.call(parser.data, parser.mark, fpc-parser.mark);
53
+ }
54
+
55
+ action done {
56
+ parser.body_start = fpc + 1;
57
+ if(parser.header_done != null)
58
+ parser.header_done.call(parser.data, fpc + 1, pe - fpc - 1);
59
+ fbreak;
60
+ }
61
+
62
+ include http_parser_common "http11_parser_common.rl";
63
+
64
+ }%%
65
+
66
+ /** Data **/
67
+ %% write data;
68
+
69
+ public static interface ElementCB {
70
+ public void call(Object data, int at, int length);
71
+ }
72
+
73
+ public static interface FieldCB {
74
+ public void call(Object data, int field, int flen, int value, int vlen);
75
+ }
76
+
77
+ public static class HttpParser {
78
+ int cs;
79
+ int body_start;
80
+ int content_len;
81
+ int nread;
82
+ int mark;
83
+ int field_start;
84
+ int field_len;
85
+ int query_start;
86
+
87
+ Object data;
88
+ ByteList buffer;
89
+
90
+ public FieldCB http_field;
91
+ public ElementCB request_method;
92
+ public ElementCB request_uri;
93
+ public ElementCB fragment;
94
+ public ElementCB request_path;
95
+ public ElementCB query_string;
96
+ public ElementCB http_version;
97
+ public ElementCB header_done;
98
+
99
+ public void init() {
100
+ cs = 0;
101
+
102
+ %% write init;
103
+
104
+ body_start = 0;
105
+ content_len = 0;
106
+ mark = 0;
107
+ nread = 0;
108
+ field_len = 0;
109
+ field_start = 0;
110
+ }
111
+ }
112
+
113
+ public final HttpParser parser = new HttpParser();
114
+
115
+ public int execute(ByteList buffer, int off) {
116
+ int p, pe;
117
+ int cs = parser.cs;
118
+ int len = buffer.realSize;
119
+ assert off<=len : "offset past end of buffer";
120
+
121
+ p = off;
122
+ pe = len;
123
+ byte[] data = buffer.bytes;
124
+ parser.buffer = buffer;
125
+
126
+ %% write exec;
127
+
128
+ parser.cs = cs;
129
+ parser.nread += (p - off);
130
+
131
+ assert p <= pe : "buffer overflow after parsing execute";
132
+ assert parser.nread <= len : "nread longer than length";
133
+ assert parser.body_start <= len : "body starts after buffer end";
134
+ assert parser.mark < len : "mark is after buffer end";
135
+ assert parser.field_len <= len : "field has length longer than whole buffer";
136
+ assert parser.field_start < len : "field starts after buffer end";
137
+
138
+ if(parser.body_start>0) {
139
+ /* final \r\n combo encountered so stop right here */
140
+ %%write eof;
141
+ parser.nread++;
142
+ }
143
+
144
+ return parser.nread;
145
+ }
146
+
147
+ public int finish() {
148
+ int cs = parser.cs;
149
+
150
+ %%write eof;
151
+
152
+ parser.cs = cs;
153
+
154
+ if(has_error()) {
155
+ return -1;
156
+ } else if(is_finished()) {
157
+ return 1;
158
+ } else {
159
+ return 0;
160
+ }
161
+ }
162
+
163
+ public boolean has_error() {
164
+ return parser.cs == http_parser_error;
165
+ }
166
+
167
+ public boolean is_finished() {
168
+ return parser.cs == http_parser_first_final;
169
+ }
170
+ }
@@ -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
+ #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
+ if(parser->http_field != NULL) {
33
+ parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, fpc));
34
+ }
35
+ }
36
+ action request_method {
37
+ if(parser->request_method != NULL)
38
+ parser->request_method(parser->data, PTR_TO(mark), LEN(mark, fpc));
39
+ }
40
+ action request_uri {
41
+ if(parser->request_uri != NULL)
42
+ parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, fpc));
43
+ }
44
+ action fragment {
45
+ if(parser->fragment != NULL)
46
+ parser->fragment(parser->data, PTR_TO(mark), LEN(mark, fpc));
47
+ }
48
+
49
+ action start_query {MARK(query_start, fpc); }
50
+ action query_string {
51
+ if(parser->query_string != NULL)
52
+ parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, fpc));
53
+ }
54
+
55
+ action http_version {
56
+ if(parser->http_version != NULL)
57
+ parser->http_version(parser->data, PTR_TO(mark), LEN(mark, fpc));
58
+ }
59
+
60
+ action request_path {
61
+ if(parser->request_path != NULL)
62
+ parser->request_path(parser->data, PTR_TO(mark), LEN(mark,fpc));
63
+ }
64
+
65
+ action done {
66
+ parser->body_start = fpc - buffer + 1;
67
+ if(parser->header_done != NULL)
68
+ parser->header_done(parser->data, fpc + 1, pe - fpc - 1);
69
+ fbreak;
70
+ }
71
+
72
+ include http_parser_common "http11_parser_common.rl";
73
+
74
+ }%%
75
+
76
+ /** Data **/
77
+ %% write data;
78
+
79
+ int http_parser_init(http_parser *parser) {
80
+ int cs = 0;
81
+ %% write init;
82
+ parser->cs = cs;
83
+ parser->body_start = 0;
84
+ parser->content_len = 0;
85
+ parser->mark = 0;
86
+ parser->nread = 0;
87
+ parser->field_len = 0;
88
+ parser->field_start = 0;
89
+
90
+ return(1);
91
+ }
92
+
93
+
94
+ /** exec **/
95
+ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, size_t off) {
96
+ const char *p, *pe;
97
+ int cs = parser->cs;
98
+
99
+ assert(off <= len && "offset past end of buffer");
100
+
101
+ p = buffer+off;
102
+ pe = buffer+len;
103
+
104
+ assert(*pe == '\0' && "pointer does not end on NUL");
105
+ assert(pe - p == len - off && "pointers aren't same distance");
106
+
107
+
108
+ %% write exec;
109
+
110
+ parser->cs = cs;
111
+ parser->nread += p - (buffer + off);
112
+
113
+ assert(p <= pe && "buffer overflow after parsing execute");
114
+ assert(parser->nread <= len && "nread longer than length");
115
+ assert(parser->body_start <= len && "body starts after buffer end");
116
+ assert(parser->mark < len && "mark is after buffer end");
117
+ assert(parser->field_len <= len && "field has length longer than whole buffer");
118
+ assert(parser->field_start < len && "field starts after buffer end");
119
+
120
+ if(parser->body_start) {
121
+ /* final \r\n combo encountered so stop right here */
122
+ %%write eof;
123
+ parser->nread++;
124
+ }
125
+
126
+ return(parser->nread);
127
+ }
128
+
129
+ int http_parser_finish(http_parser *parser)
130
+ {
131
+ int cs = parser->cs;
132
+
133
+ %%write eof;
134
+
135
+ parser->cs = cs;
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 %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.1.5"));
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