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