unicorn-maintained 6.2.0

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