headius-mongrel 1.1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +21 -0
- data/COPYING +55 -0
- data/LICENSE +55 -0
- data/Manifest +69 -0
- data/README +74 -0
- data/Rakefile +202 -0
- data/TODO +5 -0
- data/bin/mongrel_rails +283 -0
- data/examples/builder.rb +29 -0
- data/examples/camping/README +3 -0
- data/examples/camping/blog.rb +294 -0
- data/examples/camping/tepee.rb +149 -0
- data/examples/httpd.conf +474 -0
- data/examples/mime.yaml +3 -0
- data/examples/mongrel.conf +9 -0
- data/examples/mongrel_simple_ctrl.rb +92 -0
- data/examples/mongrel_simple_service.rb +116 -0
- data/examples/monitrc +57 -0
- data/examples/random_thrash.rb +19 -0
- data/examples/simpletest.rb +52 -0
- data/examples/webrick_compare.rb +20 -0
- data/ext/http11/ext_help.h +14 -0
- data/ext/http11/extconf.rb +6 -0
- data/ext/http11/http11.c +402 -0
- data/ext/http11/http11_parser.c +1221 -0
- data/ext/http11/http11_parser.h +49 -0
- data/ext/http11/http11_parser.java.rl +170 -0
- data/ext/http11/http11_parser.rl +152 -0
- data/ext/http11/http11_parser_common.rl +54 -0
- data/ext/http11_java/Http11Service.java +13 -0
- data/ext/http11_java/org/jruby/mongrel/Http11.java +353 -0
- data/ext/http11_java/org/jruby/mongrel/Http11Parser.java +572 -0
- data/lib/mongrel.rb +364 -0
- data/lib/mongrel/camping.rb +107 -0
- data/lib/mongrel/cgi.rb +181 -0
- data/lib/mongrel/command.rb +222 -0
- data/lib/mongrel/configurator.rb +388 -0
- data/lib/mongrel/const.rb +110 -0
- data/lib/mongrel/debug.rb +203 -0
- data/lib/mongrel/gems.rb +22 -0
- data/lib/mongrel/handlers.rb +468 -0
- data/lib/mongrel/header_out.rb +28 -0
- data/lib/mongrel/http_request.rb +155 -0
- data/lib/mongrel/http_response.rb +163 -0
- data/lib/mongrel/init.rb +10 -0
- data/lib/mongrel/mime_types.yml +616 -0
- data/lib/mongrel/rails.rb +192 -0
- data/lib/mongrel/stats.rb +89 -0
- data/lib/mongrel/tcphack.rb +18 -0
- data/lib/mongrel/uri_classifier.rb +76 -0
- data/mongrel-public_cert.pem +20 -0
- data/mongrel.gemspec +47 -0
- data/setup.rb +1585 -0
- data/test/mime.yaml +3 -0
- data/test/mongrel.conf +1 -0
- data/test/test_cgi_wrapper.rb +26 -0
- data/test/test_command.rb +86 -0
- data/test/test_conditional.rb +107 -0
- data/test/test_configurator.rb +88 -0
- data/test/test_debug.rb +25 -0
- data/test/test_handlers.rb +126 -0
- data/test/test_http11.rb +156 -0
- data/test/test_redirect_handler.rb +45 -0
- data/test/test_request_progress.rb +100 -0
- data/test/test_response.rb +127 -0
- data/test/test_stats.rb +35 -0
- data/test/test_uriclassifier.rb +261 -0
- data/test/test_ws.rb +115 -0
- data/test/testhelp.rb +79 -0
- data/tools/trickletest.rb +45 -0
- metadata +199 -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,353 @@
|
|
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
|
+
// Pre-constructed bytelists to share for all constructed header strings
|
66
|
+
public static final ByteList REQUEST_METHOD_BL = ByteList.create("REQUEST_METHOD");
|
67
|
+
public static final ByteList REQUEST_URI_BL = ByteList.create("REQUEST_URI");
|
68
|
+
public static final ByteList FRAGMENT_BL = ByteList.create("FRAGMENT");
|
69
|
+
public static final ByteList REQUEST_PATH_BL = ByteList.create("REQUEST_PATH");
|
70
|
+
public static final ByteList QUERY_STRING_BL = ByteList.create("QUERY_STRING");
|
71
|
+
public static final ByteList HTTP_VERSION_BL = ByteList.create("HTTP_VERSION");
|
72
|
+
public static final ByteList HTTP_CONTENT_LENGTH_BL = ByteList.create("HTTP_CONTENT_LENGTH");
|
73
|
+
public static final ByteList CONTENT_LENGTH_BL = ByteList.create("CONTENT_LENGTH");
|
74
|
+
public static final ByteList HTTP_CONTENT_TYPE_BL = ByteList.create("HTTP_CONTENT_TYPE");
|
75
|
+
public static final ByteList CONTENT_TYPE_BL = ByteList.create("CONTENT_TYPE");
|
76
|
+
public static final ByteList GATEWAY_INTERFACE_BL = ByteList.create("GATEWAY_INTERFACE");
|
77
|
+
public static final ByteList CGI_1_2_BL = ByteList.create("CGI/1.2");
|
78
|
+
public static final ByteList HTTP_HOST_BL = ByteList.create("HTTP_HOST");
|
79
|
+
public static final ByteList SERVER_NAME_BL = ByteList.create("SERVER_NAME");
|
80
|
+
public static final ByteList SERVER_PORT_BL = ByteList.create("SERVER_PORT");
|
81
|
+
public static final ByteList EIGHTY_BL = ByteList.create("80");
|
82
|
+
public static final ByteList SERVER_PROTOCOL_BL = ByteList.create("SERVER_PROTOCOL");
|
83
|
+
public static final ByteList HTTP_1_1_BL = ByteList.create("HTTP/1.1");
|
84
|
+
public static final ByteList SERVER_SOFTWARE_BL = ByteList.create("SERVER_SOFTWARE");
|
85
|
+
public static final ByteList MONGREL_1_1_6_BL = ByteList.create("Mongrel 1.1.6");
|
86
|
+
|
87
|
+
public final RubyString REQUEST_METHOD;
|
88
|
+
public final RubyString REQUEST_URI;
|
89
|
+
public final RubyString FRAGMENT;
|
90
|
+
public final RubyString REQUEST_PATH;
|
91
|
+
public final RubyString QUERY_STRING;
|
92
|
+
public final RubyString HTTP_VERSION;
|
93
|
+
public final RubyString HTTP_CONTENT_LENGTH;
|
94
|
+
public final RubyString CONTENT_LENGTH;
|
95
|
+
public final RubyString HTTP_CONTENT_TYPE;
|
96
|
+
public final RubyString CONTENT_TYPE;
|
97
|
+
public final RubyString GATEWAY_INTERFACE;
|
98
|
+
public final RubyString CGI_1_2;
|
99
|
+
public final RubyString HTTP_HOST;
|
100
|
+
public final RubyString SERVER_NAME;
|
101
|
+
public final RubyString SERVER_PORT;
|
102
|
+
public final RubyString EIGHTY;
|
103
|
+
public final RubyString SERVER_PROTOCOL;
|
104
|
+
public final RubyString HTTP_1_1;
|
105
|
+
public final RubyString SERVER_SOFTWARE;
|
106
|
+
public final RubyString MONGREL_1_1_6;
|
107
|
+
|
108
|
+
|
109
|
+
private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
|
110
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
|
111
|
+
return new Http11(runtime, klass);
|
112
|
+
}
|
113
|
+
};
|
114
|
+
|
115
|
+
public static void createHttp11(Ruby runtime) {
|
116
|
+
RubyModule mMongrel = runtime.defineModule("Mongrel");
|
117
|
+
mMongrel.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator());
|
118
|
+
|
119
|
+
CallbackFactory cf = runtime.callbackFactory(Http11.class);
|
120
|
+
|
121
|
+
RubyClass cHttpParser = mMongrel.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR);
|
122
|
+
cHttpParser.defineFastMethod("initialize",cf.getFastMethod("initialize"));
|
123
|
+
cHttpParser.defineFastMethod("reset",cf.getFastMethod("reset"));
|
124
|
+
cHttpParser.defineFastMethod("finish",cf.getFastMethod("finish"));
|
125
|
+
cHttpParser.defineFastMethod("execute",cf.getFastMethod("execute", IRubyObject.class, IRubyObject.class, IRubyObject.class));
|
126
|
+
cHttpParser.defineFastMethod("error?",cf.getFastMethod("has_error"));
|
127
|
+
cHttpParser.defineFastMethod("finished?",cf.getFastMethod("is_finished"));
|
128
|
+
cHttpParser.defineFastMethod("nread",cf.getFastMethod("nread"));
|
129
|
+
}
|
130
|
+
|
131
|
+
private Ruby runtime;
|
132
|
+
private RubyClass eHttpParserError;
|
133
|
+
private Http11Parser hp;
|
134
|
+
|
135
|
+
public Http11(Ruby runtime, RubyClass clazz) {
|
136
|
+
super(runtime,clazz);
|
137
|
+
this.runtime = runtime;
|
138
|
+
this.eHttpParserError = (RubyClass)runtime.getModule("Mongrel").getConstant("HttpParserError");
|
139
|
+
this.hp = new Http11Parser();
|
140
|
+
this.hp.parser.http_field = http_field;
|
141
|
+
this.hp.parser.request_method = request_method;
|
142
|
+
this.hp.parser.request_uri = request_uri;
|
143
|
+
this.hp.parser.fragment = fragment;
|
144
|
+
this.hp.parser.request_path = request_path;
|
145
|
+
this.hp.parser.query_string = query_string;
|
146
|
+
this.hp.parser.http_version = http_version;
|
147
|
+
this.hp.parser.header_done = header_done;
|
148
|
+
this.hp.parser.init();
|
149
|
+
|
150
|
+
// prepare all hash keys ahead of time
|
151
|
+
REQUEST_METHOD = RubyString.newStringShared(runtime, REQUEST_METHOD_BL);
|
152
|
+
REQUEST_METHOD.setFrozen(true);
|
153
|
+
REQUEST_URI = RubyString.newStringShared(runtime, REQUEST_URI_BL);
|
154
|
+
REQUEST_URI.setFrozen(true);
|
155
|
+
FRAGMENT = RubyString.newStringShared(runtime, FRAGMENT_BL);
|
156
|
+
FRAGMENT.setFrozen(true);
|
157
|
+
REQUEST_PATH = RubyString.newStringShared(runtime, REQUEST_PATH_BL);
|
158
|
+
REQUEST_PATH.setFrozen(true);
|
159
|
+
QUERY_STRING = RubyString.newStringShared(runtime, QUERY_STRING_BL);
|
160
|
+
QUERY_STRING.setFrozen(true);
|
161
|
+
HTTP_VERSION = RubyString.newStringShared(runtime, HTTP_VERSION_BL);
|
162
|
+
HTTP_VERSION.setFrozen(true);
|
163
|
+
HTTP_CONTENT_LENGTH = RubyString.newStringShared(runtime, HTTP_CONTENT_LENGTH_BL);
|
164
|
+
HTTP_CONTENT_LENGTH.setFrozen(true);
|
165
|
+
CONTENT_LENGTH = RubyString.newStringShared(runtime, CONTENT_LENGTH_BL);
|
166
|
+
CONTENT_LENGTH.setFrozen(true);
|
167
|
+
HTTP_CONTENT_TYPE = RubyString.newStringShared(runtime, HTTP_CONTENT_TYPE_BL);
|
168
|
+
HTTP_CONTENT_TYPE.setFrozen(true);
|
169
|
+
CONTENT_TYPE = RubyString.newStringShared(runtime, CONTENT_TYPE_BL);
|
170
|
+
CONTENT_TYPE.setFrozen(true);
|
171
|
+
GATEWAY_INTERFACE = RubyString.newStringShared(runtime, GATEWAY_INTERFACE_BL);
|
172
|
+
GATEWAY_INTERFACE.setFrozen(true);
|
173
|
+
CGI_1_2 = RubyString.newStringShared(runtime, CGI_1_2_BL);
|
174
|
+
CGI_1_2.setFrozen(true);
|
175
|
+
HTTP_HOST = RubyString.newStringShared(runtime, HTTP_HOST_BL);
|
176
|
+
HTTP_HOST.setFrozen(true);
|
177
|
+
SERVER_NAME = RubyString.newStringShared(runtime, SERVER_NAME_BL);
|
178
|
+
SERVER_NAME.setFrozen(true);
|
179
|
+
SERVER_PORT = RubyString.newStringShared(runtime, SERVER_PORT_BL);
|
180
|
+
SERVER_PORT.setFrozen(true);
|
181
|
+
EIGHTY = RubyString.newStringShared(runtime, EIGHTY_BL);
|
182
|
+
EIGHTY.setFrozen(true);
|
183
|
+
SERVER_PROTOCOL = RubyString.newStringShared(runtime, SERVER_PROTOCOL_BL);
|
184
|
+
SERVER_PROTOCOL.setFrozen(true);
|
185
|
+
HTTP_1_1 = RubyString.newStringShared(runtime, HTTP_1_1_BL);
|
186
|
+
HTTP_1_1.setFrozen(true);
|
187
|
+
SERVER_SOFTWARE = RubyString.newStringShared(runtime, SERVER_SOFTWARE_BL);
|
188
|
+
SERVER_SOFTWARE.setFrozen(true);
|
189
|
+
MONGREL_1_1_6 = RubyString.newStringShared(runtime, MONGREL_1_1_6_BL);
|
190
|
+
MONGREL_1_1_6.setFrozen(true);
|
191
|
+
}
|
192
|
+
|
193
|
+
public void validateMaxLength(int len, int max, String msg) {
|
194
|
+
if(len>max) {
|
195
|
+
throw new RaiseException(runtime, eHttpParserError, msg, true);
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
private Http11Parser.FieldCB http_field = new Http11Parser.FieldCB() {
|
200
|
+
public void call(Object data, int field, int flen, int value, int vlen) {
|
201
|
+
RubyHash req = (RubyHash)data;
|
202
|
+
RubyString v,f;
|
203
|
+
validateMaxLength(flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR);
|
204
|
+
validateMaxLength(vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR);
|
205
|
+
|
206
|
+
v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen));
|
207
|
+
// FIXME: There should be a way to preallocate these headers
|
208
|
+
f = RubyString.newString(runtime, "HTTP_");
|
209
|
+
ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen);
|
210
|
+
for(int i=0,j=b.realSize;i<j;i++) {
|
211
|
+
if((b.bytes[i]&0xFF) == '-') {
|
212
|
+
b.bytes[i] = (byte)'_';
|
213
|
+
} else {
|
214
|
+
b.bytes[i] = (byte)Character.toUpperCase((char)b.bytes[i]);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
f.cat(b);
|
218
|
+
f.setFrozen(true);
|
219
|
+
req.fastASet(f,v);
|
220
|
+
}
|
221
|
+
};
|
222
|
+
|
223
|
+
private Http11Parser.ElementCB request_method = new Http11Parser.ElementCB() {
|
224
|
+
public void call(Object data, int at, int length) {
|
225
|
+
RubyHash req = (RubyHash)data;
|
226
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
227
|
+
req.fastASet(REQUEST_METHOD,val);
|
228
|
+
}
|
229
|
+
};
|
230
|
+
|
231
|
+
private Http11Parser.ElementCB request_uri = new Http11Parser.ElementCB() {
|
232
|
+
public void call(Object data, int at, int length) {
|
233
|
+
RubyHash req = (RubyHash)data;
|
234
|
+
validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR);
|
235
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
236
|
+
req.fastASet(REQUEST_URI,val);
|
237
|
+
}
|
238
|
+
};
|
239
|
+
|
240
|
+
private Http11Parser.ElementCB fragment = new Http11Parser.ElementCB() {
|
241
|
+
public void call(Object data, int at, int length) {
|
242
|
+
RubyHash req = (RubyHash)data;
|
243
|
+
validateMaxLength(length, MAX_FRAGMENT_LENGTH, MAX_FRAGMENT_LENGTH_ERR);
|
244
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
245
|
+
req.fastASet(FRAGMENT,val);
|
246
|
+
}
|
247
|
+
};
|
248
|
+
|
249
|
+
private Http11Parser.ElementCB request_path = new Http11Parser.ElementCB() {
|
250
|
+
public void call(Object data, int at, int length) {
|
251
|
+
RubyHash req = (RubyHash)data;
|
252
|
+
validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR);
|
253
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
254
|
+
req.fastASet(REQUEST_PATH,val);
|
255
|
+
}
|
256
|
+
};
|
257
|
+
|
258
|
+
private Http11Parser.ElementCB query_string = new Http11Parser.ElementCB() {
|
259
|
+
public void call(Object data, int at, int length) {
|
260
|
+
RubyHash req = (RubyHash)data;
|
261
|
+
validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR);
|
262
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
263
|
+
req.fastASet(QUERY_STRING,val);
|
264
|
+
}
|
265
|
+
};
|
266
|
+
|
267
|
+
private Http11Parser.ElementCB http_version = new Http11Parser.ElementCB() {
|
268
|
+
public void call(Object data, int at, int length) {
|
269
|
+
RubyHash req = (RubyHash)data;
|
270
|
+
RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length));
|
271
|
+
req.fastASet(HTTP_VERSION,val);
|
272
|
+
}
|
273
|
+
};
|
274
|
+
|
275
|
+
private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() {
|
276
|
+
public void call(Object data, int at, int length) {
|
277
|
+
RubyHash req = (RubyHash)data;
|
278
|
+
IRubyObject temp,ctype,clen;
|
279
|
+
|
280
|
+
clen = req.fastARef(HTTP_CONTENT_LENGTH);
|
281
|
+
if(clen != null) {
|
282
|
+
req.fastASet(CONTENT_LENGTH,clen);
|
283
|
+
}
|
284
|
+
|
285
|
+
ctype = req.fastARef(HTTP_CONTENT_TYPE);
|
286
|
+
if(ctype != null) {
|
287
|
+
req.fastASet(CONTENT_TYPE,ctype);
|
288
|
+
}
|
289
|
+
|
290
|
+
req.fastASet(GATEWAY_INTERFACE,CGI_1_2);
|
291
|
+
if((temp = req.fastARef(HTTP_HOST)) != null) {
|
292
|
+
String s = temp.toString();
|
293
|
+
int colon = s.indexOf(':');
|
294
|
+
if(colon != -1) {
|
295
|
+
req.fastASet(SERVER_NAME,runtime.newString(s.substring(0,colon)));
|
296
|
+
req.fastASet(SERVER_PORT,runtime.newString(s.substring(colon+1)));
|
297
|
+
} else {
|
298
|
+
req.fastASet(SERVER_NAME,temp);
|
299
|
+
req.fastASet(SERVER_PORT,EIGHTY);
|
300
|
+
}
|
301
|
+
}
|
302
|
+
|
303
|
+
req.setInstanceVariable("@http_body", RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length)));
|
304
|
+
req.fastASet(SERVER_PROTOCOL,HTTP_1_1);
|
305
|
+
req.fastASet(SERVER_SOFTWARE,MONGREL_1_1_6);
|
306
|
+
}
|
307
|
+
};
|
308
|
+
|
309
|
+
public IRubyObject initialize() {
|
310
|
+
this.hp.parser.init();
|
311
|
+
return this;
|
312
|
+
}
|
313
|
+
|
314
|
+
public IRubyObject reset() {
|
315
|
+
this.hp.parser.init();
|
316
|
+
return runtime.getNil();
|
317
|
+
}
|
318
|
+
|
319
|
+
public IRubyObject finish() {
|
320
|
+
this.hp.finish();
|
321
|
+
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
|
322
|
+
}
|
323
|
+
|
324
|
+
public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) {
|
325
|
+
int from = 0;
|
326
|
+
from = RubyNumeric.fix2int(start);
|
327
|
+
ByteList d = ((RubyString)data).getByteList();
|
328
|
+
if(from >= d.realSize) {
|
329
|
+
throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true);
|
330
|
+
} else {
|
331
|
+
this.hp.parser.data = req_hash;
|
332
|
+
this.hp.execute(d,from);
|
333
|
+
validateMaxLength(this.hp.parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR);
|
334
|
+
if(this.hp.has_error()) {
|
335
|
+
throw new RaiseException(runtime, eHttpParserError, "Invalid HTTP format, parsing fails.", true);
|
336
|
+
} else {
|
337
|
+
return runtime.newFixnum((long)this.hp.parser.nread);
|
338
|
+
}
|
339
|
+
}
|
340
|
+
}
|
341
|
+
|
342
|
+
public IRubyObject has_error() {
|
343
|
+
return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse();
|
344
|
+
}
|
345
|
+
|
346
|
+
public IRubyObject is_finished() {
|
347
|
+
return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse();
|
348
|
+
}
|
349
|
+
|
350
|
+
public IRubyObject nread() {
|
351
|
+
return runtime.newFixnum(this.hp.parser.nread);
|
352
|
+
}
|
353
|
+
}// Http11
|