unicorn-heroku-wait 4.8.0.1.g0ed2.dirty
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.CHANGELOG.old +25 -0
- data/.document +29 -0
- data/.gitignore +25 -0
- data/.mailmap +26 -0
- data/.manifest +166 -0
- data/.wrongdoc.yml +10 -0
- data/Application_Timeouts +77 -0
- data/CONTRIBUTORS +35 -0
- data/COPYING +674 -0
- data/ChangeLog +4861 -0
- data/DESIGN +97 -0
- data/Documentation/.gitignore +5 -0
- data/Documentation/GNUmakefile +30 -0
- data/Documentation/unicorn.1.txt +178 -0
- data/Documentation/unicorn_rails.1.txt +175 -0
- data/FAQ +53 -0
- data/GIT-VERSION-FILE +1 -0
- data/GIT-VERSION-GEN +39 -0
- data/GNUmakefile +267 -0
- data/HACKING +134 -0
- data/ISSUES +36 -0
- data/KNOWN_ISSUES +79 -0
- data/LATEST +28 -0
- data/LICENSE +67 -0
- data/Links +56 -0
- data/NEWS +2067 -0
- data/PHILOSOPHY +145 -0
- data/README +150 -0
- data/Rakefile +60 -0
- data/SIGNALS +123 -0
- data/Sandbox +103 -0
- data/TODO +5 -0
- data/TUNING +98 -0
- data/bin/unicorn +126 -0
- data/bin/unicorn_rails +209 -0
- data/examples/big_app_gc.rb +2 -0
- data/examples/echo.ru +27 -0
- data/examples/git.ru +13 -0
- data/examples/init.sh +74 -0
- data/examples/logger_mp_safe.rb +25 -0
- data/examples/logrotate.conf +29 -0
- data/examples/nginx.conf +156 -0
- data/examples/unicorn.conf.minimal.rb +13 -0
- data/examples/unicorn.conf.rb +102 -0
- data/ext/unicorn_http/CFLAGS +13 -0
- data/ext/unicorn_http/c_util.h +124 -0
- data/ext/unicorn_http/common_field_optimization.h +111 -0
- data/ext/unicorn_http/ext_help.h +82 -0
- data/ext/unicorn_http/extconf.rb +10 -0
- data/ext/unicorn_http/global_variables.h +97 -0
- data/ext/unicorn_http/httpdate.c +78 -0
- data/ext/unicorn_http/unicorn_http.c +4031 -0
- data/ext/unicorn_http/unicorn_http.rl +1036 -0
- data/ext/unicorn_http/unicorn_http_common.rl +76 -0
- data/lib/unicorn/app/exec_cgi.rb +154 -0
- data/lib/unicorn/app/inetd.rb +109 -0
- data/lib/unicorn/app/old_rails/static.rb +59 -0
- data/lib/unicorn/app/old_rails.rb +35 -0
- data/lib/unicorn/cgi_wrapper.rb +147 -0
- data/lib/unicorn/configurator.rb +679 -0
- data/lib/unicorn/const.rb +44 -0
- data/lib/unicorn/http_request.rb +122 -0
- data/lib/unicorn/http_response.rb +75 -0
- data/lib/unicorn/http_server.rb +803 -0
- data/lib/unicorn/launcher.rb +62 -0
- data/lib/unicorn/oob_gc.rb +71 -0
- data/lib/unicorn/preread_input.rb +33 -0
- data/lib/unicorn/socket_helper.rb +231 -0
- data/lib/unicorn/ssl_client.rb +11 -0
- data/lib/unicorn/ssl_configurator.rb +104 -0
- data/lib/unicorn/ssl_server.rb +42 -0
- data/lib/unicorn/stream_input.rb +146 -0
- data/lib/unicorn/tee_input.rb +126 -0
- data/lib/unicorn/tmpio.rb +29 -0
- data/lib/unicorn/util.rb +89 -0
- data/lib/unicorn/version.rb +1 -0
- data/lib/unicorn/worker.rb +152 -0
- data/lib/unicorn.rb +118 -0
- data/local.mk.sample +59 -0
- data/man/man1/unicorn.1 +211 -0
- data/man/man1/unicorn_rails.1 +210 -0
- data/script/isolate_for_tests +32 -0
- data/setup.rb +1586 -0
- data/t/.gitignore +5 -0
- data/t/GNUmakefile +82 -0
- data/t/README +42 -0
- data/t/bin/content-md5-put +36 -0
- data/t/bin/sha1sum.rb +17 -0
- data/t/bin/unused_listen +40 -0
- data/t/broken-app.ru +12 -0
- data/t/detach.ru +11 -0
- data/t/env.ru +3 -0
- data/t/fails-rack-lint.ru +5 -0
- data/t/heartbeat-timeout.ru +12 -0
- data/t/hijack.ru +42 -0
- data/t/listener_names.ru +4 -0
- data/t/my-tap-lib.sh +201 -0
- data/t/oob_gc.ru +20 -0
- data/t/oob_gc_path.ru +20 -0
- data/t/pid.ru +3 -0
- data/t/preread_input.ru +17 -0
- data/t/rack-input-tests.ru +21 -0
- data/t/sslgen.sh +71 -0
- data/t/t0000-http-basic.sh +50 -0
- data/t/t0001-reload-bad-config.sh +53 -0
- data/t/t0002-config-conflict.sh +49 -0
- data/t/t0002-parser-error.sh +94 -0
- data/t/t0003-working_directory.sh +51 -0
- data/t/t0004-heartbeat-timeout.sh +69 -0
- data/t/t0004-working_directory_broken.sh +24 -0
- data/t/t0005-working_directory_app.rb.sh +40 -0
- data/t/t0006-reopen-logs.sh +83 -0
- data/t/t0006.ru +13 -0
- data/t/t0007-working_directory_no_embed_cli.sh +44 -0
- data/t/t0008-back_out_of_upgrade.sh +110 -0
- data/t/t0009-broken-app.sh +56 -0
- data/t/t0009-winch_ttin.sh +59 -0
- data/t/t0010-reap-logging.sh +55 -0
- data/t/t0011-active-unix-socket.sh +79 -0
- data/t/t0012-reload-empty-config.sh +85 -0
- data/t/t0013-rewindable-input-false.sh +24 -0
- data/t/t0013.ru +12 -0
- data/t/t0014-rewindable-input-true.sh +24 -0
- data/t/t0014.ru +12 -0
- data/t/t0015-configurator-internals.sh +25 -0
- data/t/t0016-trust-x-forwarded-false.sh +30 -0
- data/t/t0017-trust-x-forwarded-true.sh +30 -0
- data/t/t0018-write-on-close.sh +23 -0
- data/t/t0019-max_header_len.sh +49 -0
- data/t/t0020-at_exit-handler.sh +49 -0
- data/t/t0021-process_detach.sh +29 -0
- data/t/t0022-listener_names-preload_app.sh +32 -0
- data/t/t0100-rack-input-tests.sh +124 -0
- data/t/t0116-client_body_buffer_size.sh +80 -0
- data/t/t0116.ru +16 -0
- data/t/t0200-rack-hijack.sh +27 -0
- data/t/t0300-no-default-middleware.sh +20 -0
- data/t/t0600-https-server-basic.sh +48 -0
- data/t/t9000-preread-input.sh +48 -0
- data/t/t9001-oob_gc.sh +47 -0
- data/t/t9002-oob_gc-path.sh +75 -0
- data/t/test-lib.sh +128 -0
- data/t/write-on-close.ru +11 -0
- data/test/aggregate.rb +15 -0
- data/test/benchmark/README +50 -0
- data/test/benchmark/dd.ru +18 -0
- data/test/benchmark/stack.ru +8 -0
- data/test/exec/README +5 -0
- data/test/exec/test_exec.rb +1047 -0
- data/test/test_helper.rb +297 -0
- data/test/unit/test_configurator.rb +175 -0
- data/test/unit/test_droplet.rb +28 -0
- data/test/unit/test_http_parser.rb +854 -0
- data/test/unit/test_http_parser_ng.rb +731 -0
- data/test/unit/test_http_parser_xftrust.rb +38 -0
- data/test/unit/test_request.rb +182 -0
- data/test/unit/test_response.rb +99 -0
- data/test/unit/test_server.rb +268 -0
- data/test/unit/test_signals.rb +188 -0
- data/test/unit/test_sni_hostnames.rb +47 -0
- data/test/unit/test_socket_helper.rb +197 -0
- data/test/unit/test_stream_input.rb +203 -0
- data/test/unit/test_tee_input.rb +294 -0
- data/test/unit/test_upload.rb +306 -0
- data/test/unit/test_util.rb +105 -0
- data/unicorn.gemspec +44 -0
- metadata +328 -0
@@ -0,0 +1,1036 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2009 Eric Wong (all bugs are Eric's fault)
|
3
|
+
* Copyright (c) 2005 Zed A. Shaw
|
4
|
+
* You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
|
5
|
+
* the GPLv2+ (GPLv3+ preferred)
|
6
|
+
*/
|
7
|
+
#include "ruby.h"
|
8
|
+
#include "ext_help.h"
|
9
|
+
#include <assert.h>
|
10
|
+
#include <string.h>
|
11
|
+
#include <sys/types.h>
|
12
|
+
#include "common_field_optimization.h"
|
13
|
+
#include "global_variables.h"
|
14
|
+
#include "c_util.h"
|
15
|
+
|
16
|
+
void init_unicorn_httpdate(void);
|
17
|
+
|
18
|
+
#define UH_FL_CHUNKED 0x1
|
19
|
+
#define UH_FL_HASBODY 0x2
|
20
|
+
#define UH_FL_INBODY 0x4
|
21
|
+
#define UH_FL_HASTRAILER 0x8
|
22
|
+
#define UH_FL_INTRAILER 0x10
|
23
|
+
#define UH_FL_INCHUNK 0x20
|
24
|
+
#define UH_FL_REQEOF 0x40
|
25
|
+
#define UH_FL_KAVERSION 0x80
|
26
|
+
#define UH_FL_HASHEADER 0x100
|
27
|
+
#define UH_FL_TO_CLEAR 0x200
|
28
|
+
|
29
|
+
/* all of these flags need to be set for keepalive to be supported */
|
30
|
+
#define UH_FL_KEEPALIVE (UH_FL_KAVERSION | UH_FL_REQEOF | UH_FL_HASHEADER)
|
31
|
+
|
32
|
+
/*
|
33
|
+
* whether or not to trust X-Forwarded-Proto and X-Forwarded-SSL when
|
34
|
+
* setting rack.url_scheme
|
35
|
+
*/
|
36
|
+
static VALUE trust_x_forward = Qtrue;
|
37
|
+
|
38
|
+
static unsigned long keepalive_requests = 100; /* same as nginx */
|
39
|
+
|
40
|
+
/*
|
41
|
+
* Returns the maximum number of keepalive requests a client may make
|
42
|
+
* before the parser refuses to continue.
|
43
|
+
*/
|
44
|
+
static VALUE ka_req(VALUE self)
|
45
|
+
{
|
46
|
+
return ULONG2NUM(keepalive_requests);
|
47
|
+
}
|
48
|
+
|
49
|
+
/*
|
50
|
+
* Sets the maximum number of keepalive requests a client may make.
|
51
|
+
* A special value of +nil+ causes this to be the maximum value
|
52
|
+
* possible (this is architecture-dependent).
|
53
|
+
*/
|
54
|
+
static VALUE set_ka_req(VALUE self, VALUE val)
|
55
|
+
{
|
56
|
+
keepalive_requests = NIL_P(val) ? ULONG_MAX : NUM2ULONG(val);
|
57
|
+
|
58
|
+
return ka_req(self);
|
59
|
+
}
|
60
|
+
|
61
|
+
/*
|
62
|
+
* Sets whether or not the parser will trust X-Forwarded-Proto and
|
63
|
+
* X-Forwarded-SSL headers and set "rack.url_scheme" to "https" accordingly.
|
64
|
+
* Rainbows!/Zbatery installations facing untrusted clients directly
|
65
|
+
* should set this to +false+
|
66
|
+
*/
|
67
|
+
static VALUE set_xftrust(VALUE self, VALUE val)
|
68
|
+
{
|
69
|
+
if (Qtrue == val || Qfalse == val)
|
70
|
+
trust_x_forward = val;
|
71
|
+
else
|
72
|
+
rb_raise(rb_eTypeError, "must be true or false");
|
73
|
+
|
74
|
+
return val;
|
75
|
+
}
|
76
|
+
|
77
|
+
/*
|
78
|
+
* returns whether or not the parser will trust X-Forwarded-Proto and
|
79
|
+
* X-Forwarded-SSL headers and set "rack.url_scheme" to "https" accordingly
|
80
|
+
*/
|
81
|
+
static VALUE xftrust(VALUE self)
|
82
|
+
{
|
83
|
+
return trust_x_forward;
|
84
|
+
}
|
85
|
+
|
86
|
+
static size_t MAX_HEADER_LEN = 1024 * (80 + 32); /* same as Mongrel */
|
87
|
+
|
88
|
+
/* this is only intended for use with Rainbows! */
|
89
|
+
static VALUE set_maxhdrlen(VALUE self, VALUE len)
|
90
|
+
{
|
91
|
+
return SIZET2NUM(MAX_HEADER_LEN = NUM2SIZET(len));
|
92
|
+
}
|
93
|
+
|
94
|
+
/* keep this small for Rainbows! since every client has one */
|
95
|
+
struct http_parser {
|
96
|
+
int cs; /* Ragel internal state */
|
97
|
+
unsigned int flags;
|
98
|
+
unsigned long nr_requests;
|
99
|
+
size_t mark;
|
100
|
+
size_t offset;
|
101
|
+
union { /* these 2 fields don't nest */
|
102
|
+
size_t field;
|
103
|
+
size_t query;
|
104
|
+
} start;
|
105
|
+
union {
|
106
|
+
size_t field_len; /* only used during header processing */
|
107
|
+
size_t dest_offset; /* only used during body processing */
|
108
|
+
} s;
|
109
|
+
VALUE buf;
|
110
|
+
VALUE env;
|
111
|
+
VALUE cont; /* Qfalse: unset, Qnil: ignored header, T_STRING: append */
|
112
|
+
union {
|
113
|
+
off_t content;
|
114
|
+
off_t chunk;
|
115
|
+
} len;
|
116
|
+
};
|
117
|
+
|
118
|
+
static ID id_clear, id_set_backtrace, id_response_start_sent;
|
119
|
+
|
120
|
+
static void finalize_header(struct http_parser *hp);
|
121
|
+
|
122
|
+
static void parser_raise(VALUE klass, const char *msg)
|
123
|
+
{
|
124
|
+
VALUE exc = rb_exc_new2(klass, msg);
|
125
|
+
VALUE bt = rb_ary_new();
|
126
|
+
|
127
|
+
rb_funcall(exc, id_set_backtrace, 1, bt);
|
128
|
+
rb_exc_raise(exc);
|
129
|
+
}
|
130
|
+
|
131
|
+
#define REMAINING (unsigned long)(pe - p)
|
132
|
+
#define LEN(AT, FPC) (FPC - buffer - hp->AT)
|
133
|
+
#define MARK(M,FPC) (hp->M = (FPC) - buffer)
|
134
|
+
#define PTR_TO(F) (buffer + hp->F)
|
135
|
+
#define STR_NEW(M,FPC) rb_str_new(PTR_TO(M), LEN(M, FPC))
|
136
|
+
#define STRIPPED_STR_NEW(M,FPC) stripped_str_new(PTR_TO(M), LEN(M, FPC))
|
137
|
+
|
138
|
+
#define HP_FL_TEST(hp,fl) ((hp)->flags & (UH_FL_##fl))
|
139
|
+
#define HP_FL_SET(hp,fl) ((hp)->flags |= (UH_FL_##fl))
|
140
|
+
#define HP_FL_UNSET(hp,fl) ((hp)->flags &= ~(UH_FL_##fl))
|
141
|
+
#define HP_FL_ALL(hp,fl) (HP_FL_TEST(hp, fl) == (UH_FL_##fl))
|
142
|
+
|
143
|
+
static int is_lws(char c)
|
144
|
+
{
|
145
|
+
return (c == ' ' || c == '\t');
|
146
|
+
}
|
147
|
+
|
148
|
+
static VALUE stripped_str_new(const char *str, long len)
|
149
|
+
{
|
150
|
+
long end;
|
151
|
+
|
152
|
+
for (end = len - 1; end >= 0 && is_lws(str[end]); end--);
|
153
|
+
|
154
|
+
return rb_str_new(str, end + 1);
|
155
|
+
}
|
156
|
+
|
157
|
+
/*
|
158
|
+
* handles values of the "Connection:" header, keepalive is implied
|
159
|
+
* for HTTP/1.1 but needs to be explicitly enabled with HTTP/1.0
|
160
|
+
* Additionally, we require GET/HEAD requests to support keepalive.
|
161
|
+
*/
|
162
|
+
static void hp_keepalive_connection(struct http_parser *hp, VALUE val)
|
163
|
+
{
|
164
|
+
if (STR_CSTR_CASE_EQ(val, "keep-alive")) {
|
165
|
+
/* basically have HTTP/1.0 masquerade as HTTP/1.1+ */
|
166
|
+
HP_FL_SET(hp, KAVERSION);
|
167
|
+
} else if (STR_CSTR_CASE_EQ(val, "close")) {
|
168
|
+
/*
|
169
|
+
* it doesn't matter what HTTP version or request method we have,
|
170
|
+
* if a client says "Connection: close", we disable keepalive
|
171
|
+
*/
|
172
|
+
HP_FL_UNSET(hp, KAVERSION);
|
173
|
+
} else {
|
174
|
+
/*
|
175
|
+
* client could've sent anything, ignore it for now. Maybe
|
176
|
+
* "HP_FL_UNSET(hp, KAVERSION);" just in case?
|
177
|
+
* Raising an exception might be too mean...
|
178
|
+
*/
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
static void
|
183
|
+
request_method(struct http_parser *hp, const char *ptr, size_t len)
|
184
|
+
{
|
185
|
+
VALUE v = rb_str_new(ptr, len);
|
186
|
+
|
187
|
+
rb_hash_aset(hp->env, g_request_method, v);
|
188
|
+
}
|
189
|
+
|
190
|
+
static void
|
191
|
+
http_version(struct http_parser *hp, const char *ptr, size_t len)
|
192
|
+
{
|
193
|
+
VALUE v;
|
194
|
+
|
195
|
+
HP_FL_SET(hp, HASHEADER);
|
196
|
+
|
197
|
+
if (CONST_MEM_EQ("HTTP/1.1", ptr, len)) {
|
198
|
+
/* HTTP/1.1 implies keepalive unless "Connection: close" is set */
|
199
|
+
HP_FL_SET(hp, KAVERSION);
|
200
|
+
v = g_http_11;
|
201
|
+
} else if (CONST_MEM_EQ("HTTP/1.0", ptr, len)) {
|
202
|
+
v = g_http_10;
|
203
|
+
} else {
|
204
|
+
v = rb_str_new(ptr, len);
|
205
|
+
}
|
206
|
+
rb_hash_aset(hp->env, g_server_protocol, v);
|
207
|
+
rb_hash_aset(hp->env, g_http_version, v);
|
208
|
+
}
|
209
|
+
|
210
|
+
static inline void hp_invalid_if_trailer(struct http_parser *hp)
|
211
|
+
{
|
212
|
+
if (HP_FL_TEST(hp, INTRAILER))
|
213
|
+
parser_raise(eHttpParserError, "invalid Trailer");
|
214
|
+
}
|
215
|
+
|
216
|
+
static void write_cont_value(struct http_parser *hp,
|
217
|
+
char *buffer, const char *p)
|
218
|
+
{
|
219
|
+
char *vptr;
|
220
|
+
long end;
|
221
|
+
long len = LEN(mark, p);
|
222
|
+
long cont_len;
|
223
|
+
|
224
|
+
if (hp->cont == Qfalse)
|
225
|
+
parser_raise(eHttpParserError, "invalid continuation line");
|
226
|
+
if (NIL_P(hp->cont))
|
227
|
+
return; /* we're ignoring this header (probably Host:) */
|
228
|
+
|
229
|
+
assert(TYPE(hp->cont) == T_STRING && "continuation line is not a string");
|
230
|
+
assert(hp->mark > 0 && "impossible continuation line offset");
|
231
|
+
|
232
|
+
if (len == 0)
|
233
|
+
return;
|
234
|
+
|
235
|
+
cont_len = RSTRING_LEN(hp->cont);
|
236
|
+
if (cont_len > 0) {
|
237
|
+
--hp->mark;
|
238
|
+
len = LEN(mark, p);
|
239
|
+
}
|
240
|
+
vptr = PTR_TO(mark);
|
241
|
+
|
242
|
+
/* normalize tab to space */
|
243
|
+
if (cont_len > 0) {
|
244
|
+
assert((' ' == *vptr || '\t' == *vptr) && "invalid leading white space");
|
245
|
+
*vptr = ' ';
|
246
|
+
}
|
247
|
+
|
248
|
+
for (end = len - 1; end >= 0 && is_lws(vptr[end]); end--);
|
249
|
+
rb_str_buf_cat(hp->cont, vptr, end + 1);
|
250
|
+
}
|
251
|
+
|
252
|
+
static void write_value(struct http_parser *hp,
|
253
|
+
const char *buffer, const char *p)
|
254
|
+
{
|
255
|
+
VALUE f = find_common_field(PTR_TO(start.field), hp->s.field_len);
|
256
|
+
VALUE v;
|
257
|
+
VALUE e;
|
258
|
+
|
259
|
+
VALIDATE_MAX_LENGTH(LEN(mark, p), FIELD_VALUE);
|
260
|
+
v = LEN(mark, p) == 0 ? rb_str_buf_new(128) : STRIPPED_STR_NEW(mark, p);
|
261
|
+
if (NIL_P(f)) {
|
262
|
+
const char *field = PTR_TO(start.field);
|
263
|
+
size_t flen = hp->s.field_len;
|
264
|
+
|
265
|
+
VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
|
266
|
+
|
267
|
+
/*
|
268
|
+
* ignore "Version" headers since they conflict with the HTTP_VERSION
|
269
|
+
* rack env variable.
|
270
|
+
*/
|
271
|
+
if (CONST_MEM_EQ("VERSION", field, flen)) {
|
272
|
+
hp->cont = Qnil;
|
273
|
+
return;
|
274
|
+
}
|
275
|
+
f = uncommon_field(field, flen);
|
276
|
+
} else if (f == g_http_connection) {
|
277
|
+
hp_keepalive_connection(hp, v);
|
278
|
+
} else if (f == g_content_length) {
|
279
|
+
hp->len.content = parse_length(RSTRING_PTR(v), RSTRING_LEN(v));
|
280
|
+
if (hp->len.content < 0)
|
281
|
+
parser_raise(eHttpParserError, "invalid Content-Length");
|
282
|
+
if (hp->len.content != 0)
|
283
|
+
HP_FL_SET(hp, HASBODY);
|
284
|
+
hp_invalid_if_trailer(hp);
|
285
|
+
} else if (f == g_http_transfer_encoding) {
|
286
|
+
if (STR_CSTR_CASE_EQ(v, "chunked")) {
|
287
|
+
HP_FL_SET(hp, CHUNKED);
|
288
|
+
HP_FL_SET(hp, HASBODY);
|
289
|
+
}
|
290
|
+
hp_invalid_if_trailer(hp);
|
291
|
+
} else if (f == g_http_trailer) {
|
292
|
+
HP_FL_SET(hp, HASTRAILER);
|
293
|
+
hp_invalid_if_trailer(hp);
|
294
|
+
} else {
|
295
|
+
assert(TYPE(f) == T_STRING && "memoized object is not a string");
|
296
|
+
assert_frozen(f);
|
297
|
+
}
|
298
|
+
|
299
|
+
e = rb_hash_aref(hp->env, f);
|
300
|
+
if (NIL_P(e)) {
|
301
|
+
hp->cont = rb_hash_aset(hp->env, f, v);
|
302
|
+
} else if (f == g_http_host) {
|
303
|
+
/*
|
304
|
+
* ignored, absolute URLs in REQUEST_URI take precedence over
|
305
|
+
* the Host: header (ref: rfc 2616, section 5.2.1)
|
306
|
+
*/
|
307
|
+
hp->cont = Qnil;
|
308
|
+
} else {
|
309
|
+
rb_str_buf_cat(e, ",", 1);
|
310
|
+
hp->cont = rb_str_buf_append(e, v);
|
311
|
+
}
|
312
|
+
}
|
313
|
+
|
314
|
+
/** Machine **/
|
315
|
+
|
316
|
+
%%{
|
317
|
+
machine http_parser;
|
318
|
+
|
319
|
+
action mark {MARK(mark, fpc); }
|
320
|
+
|
321
|
+
action start_field { MARK(start.field, fpc); }
|
322
|
+
action snake_upcase_field { snake_upcase_char(deconst(fpc)); }
|
323
|
+
action downcase_char { downcase_char(deconst(fpc)); }
|
324
|
+
action write_field { hp->s.field_len = LEN(start.field, fpc); }
|
325
|
+
action start_value { MARK(mark, fpc); }
|
326
|
+
action write_value { write_value(hp, buffer, fpc); }
|
327
|
+
action write_cont_value { write_cont_value(hp, buffer, fpc); }
|
328
|
+
action request_method { request_method(hp, PTR_TO(mark), LEN(mark, fpc)); }
|
329
|
+
action scheme {
|
330
|
+
rb_hash_aset(hp->env, g_rack_url_scheme, STR_NEW(mark, fpc));
|
331
|
+
}
|
332
|
+
action host { rb_hash_aset(hp->env, g_http_host, STR_NEW(mark, fpc)); }
|
333
|
+
action request_uri {
|
334
|
+
VALUE str;
|
335
|
+
|
336
|
+
VALIDATE_MAX_URI_LENGTH(LEN(mark, fpc), REQUEST_URI);
|
337
|
+
str = rb_hash_aset(hp->env, g_request_uri, STR_NEW(mark, fpc));
|
338
|
+
/*
|
339
|
+
* "OPTIONS * HTTP/1.1\r\n" is a valid request, but we can't have '*'
|
340
|
+
* in REQUEST_PATH or PATH_INFO or else Rack::Lint will complain
|
341
|
+
*/
|
342
|
+
if (STR_CSTR_EQ(str, "*")) {
|
343
|
+
str = rb_str_new(NULL, 0);
|
344
|
+
rb_hash_aset(hp->env, g_path_info, str);
|
345
|
+
rb_hash_aset(hp->env, g_request_path, str);
|
346
|
+
}
|
347
|
+
}
|
348
|
+
action fragment {
|
349
|
+
VALIDATE_MAX_URI_LENGTH(LEN(mark, fpc), FRAGMENT);
|
350
|
+
rb_hash_aset(hp->env, g_fragment, STR_NEW(mark, fpc));
|
351
|
+
}
|
352
|
+
action start_query {MARK(start.query, fpc); }
|
353
|
+
action query_string {
|
354
|
+
VALIDATE_MAX_URI_LENGTH(LEN(start.query, fpc), QUERY_STRING);
|
355
|
+
rb_hash_aset(hp->env, g_query_string, STR_NEW(start.query, fpc));
|
356
|
+
}
|
357
|
+
action http_version { http_version(hp, PTR_TO(mark), LEN(mark, fpc)); }
|
358
|
+
action request_path {
|
359
|
+
VALUE val;
|
360
|
+
|
361
|
+
VALIDATE_MAX_URI_LENGTH(LEN(mark, fpc), REQUEST_PATH);
|
362
|
+
val = rb_hash_aset(hp->env, g_request_path, STR_NEW(mark, fpc));
|
363
|
+
|
364
|
+
/* rack says PATH_INFO must start with "/" or be empty */
|
365
|
+
if (!STR_CSTR_EQ(val, "*"))
|
366
|
+
rb_hash_aset(hp->env, g_path_info, val);
|
367
|
+
}
|
368
|
+
action add_to_chunk_size {
|
369
|
+
hp->len.chunk = step_incr(hp->len.chunk, fc, 16);
|
370
|
+
if (hp->len.chunk < 0)
|
371
|
+
parser_raise(eHttpParserError, "invalid chunk size");
|
372
|
+
}
|
373
|
+
action header_done {
|
374
|
+
finalize_header(hp);
|
375
|
+
|
376
|
+
cs = http_parser_first_final;
|
377
|
+
if (HP_FL_TEST(hp, HASBODY)) {
|
378
|
+
HP_FL_SET(hp, INBODY);
|
379
|
+
if (HP_FL_TEST(hp, CHUNKED))
|
380
|
+
cs = http_parser_en_ChunkedBody;
|
381
|
+
} else {
|
382
|
+
HP_FL_SET(hp, REQEOF);
|
383
|
+
assert(!HP_FL_TEST(hp, CHUNKED) && "chunked encoding without body!");
|
384
|
+
}
|
385
|
+
/*
|
386
|
+
* go back to Ruby so we can call the Rack application, we'll reenter
|
387
|
+
* the parser iff the body needs to be processed.
|
388
|
+
*/
|
389
|
+
goto post_exec;
|
390
|
+
}
|
391
|
+
|
392
|
+
action end_trailers {
|
393
|
+
cs = http_parser_first_final;
|
394
|
+
goto post_exec;
|
395
|
+
}
|
396
|
+
|
397
|
+
action end_chunked_body {
|
398
|
+
HP_FL_SET(hp, INTRAILER);
|
399
|
+
cs = http_parser_en_Trailers;
|
400
|
+
++p;
|
401
|
+
assert(p <= pe && "buffer overflow after chunked body");
|
402
|
+
goto post_exec;
|
403
|
+
}
|
404
|
+
|
405
|
+
action skip_chunk_data {
|
406
|
+
skip_chunk_data_hack: {
|
407
|
+
size_t nr = MIN((size_t)hp->len.chunk, REMAINING);
|
408
|
+
memcpy(RSTRING_PTR(hp->cont) + hp->s.dest_offset, fpc, nr);
|
409
|
+
hp->s.dest_offset += nr;
|
410
|
+
hp->len.chunk -= nr;
|
411
|
+
p += nr;
|
412
|
+
assert(hp->len.chunk >= 0 && "negative chunk length");
|
413
|
+
if ((size_t)hp->len.chunk > REMAINING) {
|
414
|
+
HP_FL_SET(hp, INCHUNK);
|
415
|
+
goto post_exec;
|
416
|
+
} else {
|
417
|
+
fhold;
|
418
|
+
fgoto chunk_end;
|
419
|
+
}
|
420
|
+
}}
|
421
|
+
|
422
|
+
include unicorn_http_common "unicorn_http_common.rl";
|
423
|
+
}%%
|
424
|
+
|
425
|
+
/** Data **/
|
426
|
+
%% write data;
|
427
|
+
|
428
|
+
static void http_parser_init(struct http_parser *hp)
|
429
|
+
{
|
430
|
+
int cs = 0;
|
431
|
+
hp->flags = 0;
|
432
|
+
hp->mark = 0;
|
433
|
+
hp->offset = 0;
|
434
|
+
hp->start.field = 0;
|
435
|
+
hp->s.field_len = 0;
|
436
|
+
hp->len.content = 0;
|
437
|
+
hp->cont = Qfalse; /* zero on MRI, should be optimized away by above */
|
438
|
+
%% write init;
|
439
|
+
hp->cs = cs;
|
440
|
+
}
|
441
|
+
|
442
|
+
/** exec **/
|
443
|
+
static void
|
444
|
+
http_parser_execute(struct http_parser *hp, char *buffer, size_t len)
|
445
|
+
{
|
446
|
+
const char *p, *pe;
|
447
|
+
int cs = hp->cs;
|
448
|
+
size_t off = hp->offset;
|
449
|
+
|
450
|
+
if (cs == http_parser_first_final)
|
451
|
+
return;
|
452
|
+
|
453
|
+
assert(off <= len && "offset past end of buffer");
|
454
|
+
|
455
|
+
p = buffer+off;
|
456
|
+
pe = buffer+len;
|
457
|
+
|
458
|
+
assert((void *)(pe - p) == (void *)(len - off) &&
|
459
|
+
"pointers aren't same distance");
|
460
|
+
|
461
|
+
if (HP_FL_TEST(hp, INCHUNK)) {
|
462
|
+
HP_FL_UNSET(hp, INCHUNK);
|
463
|
+
goto skip_chunk_data_hack;
|
464
|
+
}
|
465
|
+
%% write exec;
|
466
|
+
post_exec: /* "_out:" also goes here */
|
467
|
+
if (hp->cs != http_parser_error)
|
468
|
+
hp->cs = cs;
|
469
|
+
hp->offset = p - buffer;
|
470
|
+
|
471
|
+
assert(p <= pe && "buffer overflow after parsing execute");
|
472
|
+
assert(hp->offset <= len && "offset longer than length");
|
473
|
+
}
|
474
|
+
|
475
|
+
static struct http_parser *data_get(VALUE self)
|
476
|
+
{
|
477
|
+
struct http_parser *hp;
|
478
|
+
|
479
|
+
Data_Get_Struct(self, struct http_parser, hp);
|
480
|
+
assert(hp && "failed to extract http_parser struct");
|
481
|
+
return hp;
|
482
|
+
}
|
483
|
+
|
484
|
+
/*
|
485
|
+
* set rack.url_scheme to "https" or "http", no others are allowed by Rack
|
486
|
+
* this resembles the Rack::Request#scheme method as of rack commit
|
487
|
+
* 35bb5ba6746b5d346de9202c004cc926039650c7
|
488
|
+
*/
|
489
|
+
static void set_url_scheme(VALUE env, VALUE *server_port)
|
490
|
+
{
|
491
|
+
VALUE scheme = rb_hash_aref(env, g_rack_url_scheme);
|
492
|
+
|
493
|
+
if (NIL_P(scheme)) {
|
494
|
+
if (trust_x_forward == Qfalse) {
|
495
|
+
scheme = g_http;
|
496
|
+
} else {
|
497
|
+
scheme = rb_hash_aref(env, g_http_x_forwarded_ssl);
|
498
|
+
if (!NIL_P(scheme) && STR_CSTR_EQ(scheme, "on")) {
|
499
|
+
*server_port = g_port_443;
|
500
|
+
scheme = g_https;
|
501
|
+
} else {
|
502
|
+
scheme = rb_hash_aref(env, g_http_x_forwarded_proto);
|
503
|
+
if (NIL_P(scheme)) {
|
504
|
+
scheme = g_http;
|
505
|
+
} else {
|
506
|
+
long len = RSTRING_LEN(scheme);
|
507
|
+
if (len >= 5 && !memcmp(RSTRING_PTR(scheme), "https", 5)) {
|
508
|
+
if (len != 5)
|
509
|
+
scheme = g_https;
|
510
|
+
*server_port = g_port_443;
|
511
|
+
} else {
|
512
|
+
scheme = g_http;
|
513
|
+
}
|
514
|
+
}
|
515
|
+
}
|
516
|
+
}
|
517
|
+
rb_hash_aset(env, g_rack_url_scheme, scheme);
|
518
|
+
} else if (STR_CSTR_EQ(scheme, "https")) {
|
519
|
+
*server_port = g_port_443;
|
520
|
+
} else {
|
521
|
+
assert(*server_port == g_port_80 && "server_port not set");
|
522
|
+
}
|
523
|
+
}
|
524
|
+
|
525
|
+
/*
|
526
|
+
* Parse and set the SERVER_NAME and SERVER_PORT variables
|
527
|
+
* Not supporting X-Forwarded-Host/X-Forwarded-Port in here since
|
528
|
+
* anybody who needs them is using an unsupported configuration and/or
|
529
|
+
* incompetent. Rack::Request will handle X-Forwarded-{Port,Host} just
|
530
|
+
* fine.
|
531
|
+
*/
|
532
|
+
static void set_server_vars(VALUE env, VALUE *server_port)
|
533
|
+
{
|
534
|
+
VALUE server_name = g_localhost;
|
535
|
+
VALUE host = rb_hash_aref(env, g_http_host);
|
536
|
+
|
537
|
+
if (!NIL_P(host)) {
|
538
|
+
char *host_ptr = RSTRING_PTR(host);
|
539
|
+
long host_len = RSTRING_LEN(host);
|
540
|
+
char *colon;
|
541
|
+
|
542
|
+
if (*host_ptr == '[') { /* ipv6 address format */
|
543
|
+
char *rbracket = memchr(host_ptr + 1, ']', host_len - 1);
|
544
|
+
|
545
|
+
if (rbracket)
|
546
|
+
colon = (rbracket[1] == ':') ? rbracket + 1 : NULL;
|
547
|
+
else
|
548
|
+
colon = memchr(host_ptr + 1, ':', host_len - 1);
|
549
|
+
} else {
|
550
|
+
colon = memchr(host_ptr, ':', host_len);
|
551
|
+
}
|
552
|
+
|
553
|
+
if (colon) {
|
554
|
+
long port_start = colon - host_ptr + 1;
|
555
|
+
|
556
|
+
server_name = rb_str_substr(host, 0, colon - host_ptr);
|
557
|
+
if ((host_len - port_start) > 0)
|
558
|
+
*server_port = rb_str_substr(host, port_start, host_len);
|
559
|
+
} else {
|
560
|
+
server_name = host;
|
561
|
+
}
|
562
|
+
}
|
563
|
+
rb_hash_aset(env, g_server_name, server_name);
|
564
|
+
rb_hash_aset(env, g_server_port, *server_port);
|
565
|
+
}
|
566
|
+
|
567
|
+
static void finalize_header(struct http_parser *hp)
|
568
|
+
{
|
569
|
+
VALUE server_port = g_port_80;
|
570
|
+
|
571
|
+
set_url_scheme(hp->env, &server_port);
|
572
|
+
set_server_vars(hp->env, &server_port);
|
573
|
+
|
574
|
+
if (!HP_FL_TEST(hp, HASHEADER))
|
575
|
+
rb_hash_aset(hp->env, g_server_protocol, g_http_09);
|
576
|
+
|
577
|
+
/* rack requires QUERY_STRING */
|
578
|
+
if (NIL_P(rb_hash_aref(hp->env, g_query_string)))
|
579
|
+
rb_hash_aset(hp->env, g_query_string, rb_str_new(NULL, 0));
|
580
|
+
}
|
581
|
+
|
582
|
+
static void hp_mark(void *ptr)
|
583
|
+
{
|
584
|
+
struct http_parser *hp = ptr;
|
585
|
+
|
586
|
+
rb_gc_mark(hp->buf);
|
587
|
+
rb_gc_mark(hp->env);
|
588
|
+
rb_gc_mark(hp->cont);
|
589
|
+
}
|
590
|
+
|
591
|
+
static VALUE HttpParser_alloc(VALUE klass)
|
592
|
+
{
|
593
|
+
struct http_parser *hp;
|
594
|
+
return Data_Make_Struct(klass, struct http_parser, hp_mark, -1, hp);
|
595
|
+
}
|
596
|
+
|
597
|
+
|
598
|
+
/**
|
599
|
+
* call-seq:
|
600
|
+
* parser.new => parser
|
601
|
+
*
|
602
|
+
* Creates a new parser.
|
603
|
+
*/
|
604
|
+
static VALUE HttpParser_init(VALUE self)
|
605
|
+
{
|
606
|
+
struct http_parser *hp = data_get(self);
|
607
|
+
|
608
|
+
http_parser_init(hp);
|
609
|
+
hp->buf = rb_str_new(NULL, 0);
|
610
|
+
hp->env = rb_hash_new();
|
611
|
+
hp->nr_requests = keepalive_requests;
|
612
|
+
|
613
|
+
return self;
|
614
|
+
}
|
615
|
+
|
616
|
+
/**
|
617
|
+
* call-seq:
|
618
|
+
* parser.clear => parser
|
619
|
+
*
|
620
|
+
* Resets the parser to it's initial state so that you can reuse it
|
621
|
+
* rather than making new ones.
|
622
|
+
*/
|
623
|
+
static VALUE HttpParser_clear(VALUE self)
|
624
|
+
{
|
625
|
+
struct http_parser *hp = data_get(self);
|
626
|
+
|
627
|
+
http_parser_init(hp);
|
628
|
+
rb_funcall(hp->env, id_clear, 0);
|
629
|
+
rb_ivar_set(self, id_response_start_sent, Qfalse);
|
630
|
+
|
631
|
+
return self;
|
632
|
+
}
|
633
|
+
|
634
|
+
/**
|
635
|
+
* call-seq:
|
636
|
+
* parser.dechunk! => parser
|
637
|
+
*
|
638
|
+
* Resets the parser to a state suitable for dechunking response bodies
|
639
|
+
*
|
640
|
+
*/
|
641
|
+
static VALUE HttpParser_dechunk_bang(VALUE self)
|
642
|
+
{
|
643
|
+
struct http_parser *hp = data_get(self);
|
644
|
+
|
645
|
+
http_parser_init(hp);
|
646
|
+
|
647
|
+
/*
|
648
|
+
* we don't care about trailers in dechunk-only mode,
|
649
|
+
* but if we did we'd set UH_FL_HASTRAILER and clear hp->env
|
650
|
+
*/
|
651
|
+
if (0) {
|
652
|
+
rb_funcall(hp->env, id_clear, 0);
|
653
|
+
hp->flags = UH_FL_HASTRAILER;
|
654
|
+
}
|
655
|
+
|
656
|
+
hp->flags |= UH_FL_HASBODY | UH_FL_INBODY | UH_FL_CHUNKED;
|
657
|
+
hp->cs = http_parser_en_ChunkedBody;
|
658
|
+
|
659
|
+
return self;
|
660
|
+
}
|
661
|
+
|
662
|
+
/**
|
663
|
+
* call-seq:
|
664
|
+
* parser.reset => nil
|
665
|
+
*
|
666
|
+
* Resets the parser to it's initial state so that you can reuse it
|
667
|
+
* rather than making new ones.
|
668
|
+
*
|
669
|
+
* This method is deprecated and to be removed in Unicorn 4.x
|
670
|
+
*/
|
671
|
+
static VALUE HttpParser_reset(VALUE self)
|
672
|
+
{
|
673
|
+
static int warned;
|
674
|
+
|
675
|
+
if (!warned) {
|
676
|
+
rb_warn("Unicorn::HttpParser#reset is deprecated; "
|
677
|
+
"use Unicorn::HttpParser#clear instead");
|
678
|
+
}
|
679
|
+
HttpParser_clear(self);
|
680
|
+
return Qnil;
|
681
|
+
}
|
682
|
+
|
683
|
+
static void advance_str(VALUE str, off_t nr)
|
684
|
+
{
|
685
|
+
long len = RSTRING_LEN(str);
|
686
|
+
|
687
|
+
if (len == 0)
|
688
|
+
return;
|
689
|
+
|
690
|
+
rb_str_modify(str);
|
691
|
+
|
692
|
+
assert(nr <= len && "trying to advance past end of buffer");
|
693
|
+
len -= nr;
|
694
|
+
if (len > 0) /* unlikely, len is usually 0 */
|
695
|
+
memmove(RSTRING_PTR(str), RSTRING_PTR(str) + nr, len);
|
696
|
+
rb_str_set_len(str, len);
|
697
|
+
}
|
698
|
+
|
699
|
+
/**
|
700
|
+
* call-seq:
|
701
|
+
* parser.content_length => nil or Integer
|
702
|
+
*
|
703
|
+
* Returns the number of bytes left to run through HttpParser#filter_body.
|
704
|
+
* This will initially be the value of the "Content-Length" HTTP header
|
705
|
+
* after header parsing is complete and will decrease in value as
|
706
|
+
* HttpParser#filter_body is called for each chunk. This should return
|
707
|
+
* zero for requests with no body.
|
708
|
+
*
|
709
|
+
* This will return nil on "Transfer-Encoding: chunked" requests.
|
710
|
+
*/
|
711
|
+
static VALUE HttpParser_content_length(VALUE self)
|
712
|
+
{
|
713
|
+
struct http_parser *hp = data_get(self);
|
714
|
+
|
715
|
+
return HP_FL_TEST(hp, CHUNKED) ? Qnil : OFFT2NUM(hp->len.content);
|
716
|
+
}
|
717
|
+
|
718
|
+
/**
|
719
|
+
* Document-method: parse
|
720
|
+
* call-seq:
|
721
|
+
* parser.parse => env or nil
|
722
|
+
*
|
723
|
+
* Takes a Hash and a String of data, parses the String of data filling
|
724
|
+
* in the Hash returning the Hash if parsing is finished, nil otherwise
|
725
|
+
* When returning the env Hash, it may modify data to point to where
|
726
|
+
* body processing should begin.
|
727
|
+
*
|
728
|
+
* Raises HttpParserError if there are parsing errors.
|
729
|
+
*/
|
730
|
+
static VALUE HttpParser_parse(VALUE self)
|
731
|
+
{
|
732
|
+
struct http_parser *hp = data_get(self);
|
733
|
+
VALUE data = hp->buf;
|
734
|
+
|
735
|
+
if (HP_FL_TEST(hp, TO_CLEAR))
|
736
|
+
HttpParser_clear(self);
|
737
|
+
|
738
|
+
http_parser_execute(hp, RSTRING_PTR(data), RSTRING_LEN(data));
|
739
|
+
if (hp->offset > MAX_HEADER_LEN)
|
740
|
+
parser_raise(e413, "HTTP header is too large");
|
741
|
+
|
742
|
+
if (hp->cs == http_parser_first_final ||
|
743
|
+
hp->cs == http_parser_en_ChunkedBody) {
|
744
|
+
advance_str(data, hp->offset + 1);
|
745
|
+
hp->offset = 0;
|
746
|
+
if (HP_FL_TEST(hp, INTRAILER))
|
747
|
+
HP_FL_SET(hp, REQEOF);
|
748
|
+
|
749
|
+
return hp->env;
|
750
|
+
}
|
751
|
+
|
752
|
+
if (hp->cs == http_parser_error)
|
753
|
+
parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
|
754
|
+
|
755
|
+
return Qnil;
|
756
|
+
}
|
757
|
+
|
758
|
+
/**
|
759
|
+
* Document-method: parse
|
760
|
+
* call-seq:
|
761
|
+
* parser.add_parse(buffer) => env or nil
|
762
|
+
*
|
763
|
+
* adds the contents of +buffer+ to the internal buffer and attempts to
|
764
|
+
* continue parsing. Returns the +env+ Hash on success or nil if more
|
765
|
+
* data is needed.
|
766
|
+
*
|
767
|
+
* Raises HttpParserError if there are parsing errors.
|
768
|
+
*/
|
769
|
+
static VALUE HttpParser_add_parse(VALUE self, VALUE buffer)
|
770
|
+
{
|
771
|
+
struct http_parser *hp = data_get(self);
|
772
|
+
|
773
|
+
Check_Type(buffer, T_STRING);
|
774
|
+
rb_str_buf_append(hp->buf, buffer);
|
775
|
+
|
776
|
+
return HttpParser_parse(self);
|
777
|
+
}
|
778
|
+
|
779
|
+
/**
|
780
|
+
* Document-method: trailers
|
781
|
+
* call-seq:
|
782
|
+
* parser.trailers(req, data) => req or nil
|
783
|
+
*
|
784
|
+
* This is an alias for HttpParser#headers
|
785
|
+
*/
|
786
|
+
|
787
|
+
/**
|
788
|
+
* Document-method: headers
|
789
|
+
*/
|
790
|
+
static VALUE HttpParser_headers(VALUE self, VALUE env, VALUE buf)
|
791
|
+
{
|
792
|
+
struct http_parser *hp = data_get(self);
|
793
|
+
|
794
|
+
hp->env = env;
|
795
|
+
hp->buf = buf;
|
796
|
+
|
797
|
+
return HttpParser_parse(self);
|
798
|
+
}
|
799
|
+
|
800
|
+
static int chunked_eof(struct http_parser *hp)
|
801
|
+
{
|
802
|
+
return ((hp->cs == http_parser_first_final) || HP_FL_TEST(hp, INTRAILER));
|
803
|
+
}
|
804
|
+
|
805
|
+
/**
|
806
|
+
* call-seq:
|
807
|
+
* parser.body_eof? => true or false
|
808
|
+
*
|
809
|
+
* Detects if we're done filtering the body or not. This can be used
|
810
|
+
* to detect when to stop calling HttpParser#filter_body.
|
811
|
+
*/
|
812
|
+
static VALUE HttpParser_body_eof(VALUE self)
|
813
|
+
{
|
814
|
+
struct http_parser *hp = data_get(self);
|
815
|
+
|
816
|
+
if (HP_FL_TEST(hp, CHUNKED))
|
817
|
+
return chunked_eof(hp) ? Qtrue : Qfalse;
|
818
|
+
|
819
|
+
return hp->len.content == 0 ? Qtrue : Qfalse;
|
820
|
+
}
|
821
|
+
|
822
|
+
/**
|
823
|
+
* call-seq:
|
824
|
+
* parser.keepalive? => true or false
|
825
|
+
*
|
826
|
+
* This should be used to detect if a request can really handle
|
827
|
+
* keepalives and pipelining. Currently, the rules are:
|
828
|
+
*
|
829
|
+
* 1. MUST be a GET or HEAD request
|
830
|
+
* 2. MUST be HTTP/1.1 +or+ HTTP/1.0 with "Connection: keep-alive"
|
831
|
+
* 3. MUST NOT have "Connection: close" set
|
832
|
+
*/
|
833
|
+
static VALUE HttpParser_keepalive(VALUE self)
|
834
|
+
{
|
835
|
+
struct http_parser *hp = data_get(self);
|
836
|
+
|
837
|
+
return HP_FL_ALL(hp, KEEPALIVE) ? Qtrue : Qfalse;
|
838
|
+
}
|
839
|
+
|
840
|
+
/**
|
841
|
+
* call-seq:
|
842
|
+
* parser.next? => true or false
|
843
|
+
*
|
844
|
+
* Exactly like HttpParser#keepalive?, except it will reset the internal
|
845
|
+
* parser state on next parse if it returns true. It will also respect
|
846
|
+
* the maximum *keepalive_requests* value and return false if that is
|
847
|
+
* reached.
|
848
|
+
*/
|
849
|
+
static VALUE HttpParser_next(VALUE self)
|
850
|
+
{
|
851
|
+
struct http_parser *hp = data_get(self);
|
852
|
+
|
853
|
+
if ((HP_FL_ALL(hp, KEEPALIVE)) && (hp->nr_requests-- != 0)) {
|
854
|
+
HP_FL_SET(hp, TO_CLEAR);
|
855
|
+
return Qtrue;
|
856
|
+
}
|
857
|
+
return Qfalse;
|
858
|
+
}
|
859
|
+
|
860
|
+
/**
|
861
|
+
* call-seq:
|
862
|
+
* parser.headers? => true or false
|
863
|
+
*
|
864
|
+
* This should be used to detect if a request has headers (and if
|
865
|
+
* the response will have headers as well). HTTP/0.9 requests
|
866
|
+
* should return false, all subsequent HTTP versions will return true
|
867
|
+
*/
|
868
|
+
static VALUE HttpParser_has_headers(VALUE self)
|
869
|
+
{
|
870
|
+
struct http_parser *hp = data_get(self);
|
871
|
+
|
872
|
+
return HP_FL_TEST(hp, HASHEADER) ? Qtrue : Qfalse;
|
873
|
+
}
|
874
|
+
|
875
|
+
static VALUE HttpParser_buf(VALUE self)
|
876
|
+
{
|
877
|
+
return data_get(self)->buf;
|
878
|
+
}
|
879
|
+
|
880
|
+
static VALUE HttpParser_env(VALUE self)
|
881
|
+
{
|
882
|
+
return data_get(self)->env;
|
883
|
+
}
|
884
|
+
|
885
|
+
/**
|
886
|
+
* call-seq:
|
887
|
+
* parser.filter_body(dst, src) => nil/src
|
888
|
+
*
|
889
|
+
* Takes a String of +src+, will modify data if dechunking is done.
|
890
|
+
* Returns +nil+ if there is more data left to process. Returns
|
891
|
+
* +src+ if body processing is complete. When returning +src+,
|
892
|
+
* it may modify +src+ so the start of the string points to where
|
893
|
+
* the body ended so that trailer processing can begin.
|
894
|
+
*
|
895
|
+
* Raises HttpParserError if there are dechunking errors.
|
896
|
+
* Basically this is a glorified memcpy(3) that copies +src+
|
897
|
+
* into +buf+ while filtering it through the dechunker.
|
898
|
+
*/
|
899
|
+
static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src)
|
900
|
+
{
|
901
|
+
struct http_parser *hp = data_get(self);
|
902
|
+
char *srcptr;
|
903
|
+
long srclen;
|
904
|
+
|
905
|
+
srcptr = RSTRING_PTR(src);
|
906
|
+
srclen = RSTRING_LEN(src);
|
907
|
+
|
908
|
+
StringValue(dst);
|
909
|
+
|
910
|
+
if (HP_FL_TEST(hp, CHUNKED)) {
|
911
|
+
if (!chunked_eof(hp)) {
|
912
|
+
rb_str_modify(dst);
|
913
|
+
rb_str_resize(dst, srclen); /* we can never copy more than srclen bytes */
|
914
|
+
|
915
|
+
hp->s.dest_offset = 0;
|
916
|
+
hp->cont = dst;
|
917
|
+
hp->buf = src;
|
918
|
+
http_parser_execute(hp, srcptr, srclen);
|
919
|
+
if (hp->cs == http_parser_error)
|
920
|
+
parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
|
921
|
+
|
922
|
+
assert(hp->s.dest_offset <= hp->offset &&
|
923
|
+
"destination buffer overflow");
|
924
|
+
advance_str(src, hp->offset);
|
925
|
+
rb_str_set_len(dst, hp->s.dest_offset);
|
926
|
+
|
927
|
+
if (RSTRING_LEN(dst) == 0 && chunked_eof(hp)) {
|
928
|
+
assert(hp->len.chunk == 0 && "chunk at EOF but more to parse");
|
929
|
+
} else {
|
930
|
+
src = Qnil;
|
931
|
+
}
|
932
|
+
}
|
933
|
+
} else {
|
934
|
+
/* no need to enter the Ragel machine for unchunked transfers */
|
935
|
+
assert(hp->len.content >= 0 && "negative Content-Length");
|
936
|
+
if (hp->len.content > 0) {
|
937
|
+
long nr = MIN(srclen, hp->len.content);
|
938
|
+
|
939
|
+
rb_str_modify(dst);
|
940
|
+
rb_str_resize(dst, nr);
|
941
|
+
/*
|
942
|
+
* using rb_str_replace() to avoid memcpy() doesn't help in
|
943
|
+
* most cases because a GC-aware programmer will pass an explicit
|
944
|
+
* buffer to env["rack.input"].read and reuse the buffer in a loop.
|
945
|
+
* This causes copy-on-write behavior to be triggered anyways
|
946
|
+
* when the +src+ buffer is modified (when reading off the socket).
|
947
|
+
*/
|
948
|
+
hp->buf = src;
|
949
|
+
memcpy(RSTRING_PTR(dst), srcptr, nr);
|
950
|
+
hp->len.content -= nr;
|
951
|
+
if (hp->len.content == 0) {
|
952
|
+
HP_FL_SET(hp, REQEOF);
|
953
|
+
hp->cs = http_parser_first_final;
|
954
|
+
}
|
955
|
+
advance_str(src, nr);
|
956
|
+
src = Qnil;
|
957
|
+
}
|
958
|
+
}
|
959
|
+
hp->offset = 0; /* for trailer parsing */
|
960
|
+
return src;
|
961
|
+
}
|
962
|
+
|
963
|
+
#define SET_GLOBAL(var,str) do { \
|
964
|
+
var = find_common_field(str, sizeof(str) - 1); \
|
965
|
+
assert(!NIL_P(var) && "missed global field"); \
|
966
|
+
} while (0)
|
967
|
+
|
968
|
+
void Init_unicorn_http(void)
|
969
|
+
{
|
970
|
+
VALUE mUnicorn, cHttpParser;
|
971
|
+
|
972
|
+
mUnicorn = rb_const_get(rb_cObject, rb_intern("Unicorn"));
|
973
|
+
cHttpParser = rb_define_class_under(mUnicorn, "HttpParser", rb_cObject);
|
974
|
+
eHttpParserError =
|
975
|
+
rb_define_class_under(mUnicorn, "HttpParserError", rb_eIOError);
|
976
|
+
e413 = rb_define_class_under(mUnicorn, "RequestEntityTooLargeError",
|
977
|
+
eHttpParserError);
|
978
|
+
e414 = rb_define_class_under(mUnicorn, "RequestURITooLongError",
|
979
|
+
eHttpParserError);
|
980
|
+
|
981
|
+
init_globals();
|
982
|
+
rb_define_alloc_func(cHttpParser, HttpParser_alloc);
|
983
|
+
rb_define_method(cHttpParser, "initialize", HttpParser_init, 0);
|
984
|
+
rb_define_method(cHttpParser, "clear", HttpParser_clear, 0);
|
985
|
+
rb_define_method(cHttpParser, "reset", HttpParser_reset, 0);
|
986
|
+
rb_define_method(cHttpParser, "dechunk!", HttpParser_dechunk_bang, 0);
|
987
|
+
rb_define_method(cHttpParser, "parse", HttpParser_parse, 0);
|
988
|
+
rb_define_method(cHttpParser, "add_parse", HttpParser_add_parse, 1);
|
989
|
+
rb_define_method(cHttpParser, "headers", HttpParser_headers, 2);
|
990
|
+
rb_define_method(cHttpParser, "trailers", HttpParser_headers, 2);
|
991
|
+
rb_define_method(cHttpParser, "filter_body", HttpParser_filter_body, 2);
|
992
|
+
rb_define_method(cHttpParser, "content_length", HttpParser_content_length, 0);
|
993
|
+
rb_define_method(cHttpParser, "body_eof?", HttpParser_body_eof, 0);
|
994
|
+
rb_define_method(cHttpParser, "keepalive?", HttpParser_keepalive, 0);
|
995
|
+
rb_define_method(cHttpParser, "headers?", HttpParser_has_headers, 0);
|
996
|
+
rb_define_method(cHttpParser, "next?", HttpParser_next, 0);
|
997
|
+
rb_define_method(cHttpParser, "buf", HttpParser_buf, 0);
|
998
|
+
rb_define_method(cHttpParser, "env", HttpParser_env, 0);
|
999
|
+
|
1000
|
+
/*
|
1001
|
+
* The maximum size a single chunk when using chunked transfer encoding.
|
1002
|
+
* This is only a theoretical maximum used to detect errors in clients,
|
1003
|
+
* it is highly unlikely to encounter clients that send more than
|
1004
|
+
* several kilobytes at once.
|
1005
|
+
*/
|
1006
|
+
rb_define_const(cHttpParser, "CHUNK_MAX", OFFT2NUM(UH_OFF_T_MAX));
|
1007
|
+
|
1008
|
+
/*
|
1009
|
+
* The maximum size of the body as specified by Content-Length.
|
1010
|
+
* This is only a theoretical maximum, the actual limit is subject
|
1011
|
+
* to the limits of the file system used for +Dir.tmpdir+.
|
1012
|
+
*/
|
1013
|
+
rb_define_const(cHttpParser, "LENGTH_MAX", OFFT2NUM(UH_OFF_T_MAX));
|
1014
|
+
|
1015
|
+
/* default value for keepalive_requests */
|
1016
|
+
rb_define_const(cHttpParser, "KEEPALIVE_REQUESTS_DEFAULT",
|
1017
|
+
ULONG2NUM(keepalive_requests));
|
1018
|
+
|
1019
|
+
rb_define_singleton_method(cHttpParser, "keepalive_requests", ka_req, 0);
|
1020
|
+
rb_define_singleton_method(cHttpParser, "keepalive_requests=", set_ka_req, 1);
|
1021
|
+
rb_define_singleton_method(cHttpParser, "trust_x_forwarded=", set_xftrust, 1);
|
1022
|
+
rb_define_singleton_method(cHttpParser, "trust_x_forwarded?", xftrust, 0);
|
1023
|
+
rb_define_singleton_method(cHttpParser, "max_header_len=", set_maxhdrlen, 1);
|
1024
|
+
|
1025
|
+
init_common_fields();
|
1026
|
+
SET_GLOBAL(g_http_host, "HOST");
|
1027
|
+
SET_GLOBAL(g_http_trailer, "TRAILER");
|
1028
|
+
SET_GLOBAL(g_http_transfer_encoding, "TRANSFER_ENCODING");
|
1029
|
+
SET_GLOBAL(g_content_length, "CONTENT_LENGTH");
|
1030
|
+
SET_GLOBAL(g_http_connection, "CONNECTION");
|
1031
|
+
id_clear = rb_intern("clear");
|
1032
|
+
id_set_backtrace = rb_intern("set_backtrace");
|
1033
|
+
id_response_start_sent = rb_intern("@response_start_sent");
|
1034
|
+
init_unicorn_httpdate();
|
1035
|
+
}
|
1036
|
+
#undef SET_GLOBAL
|