puma 0.8.2-java

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of puma might be problematic. Click here for more details.

Files changed (63) hide show
  1. data/.gemtest +0 -0
  2. data/COPYING +55 -0
  3. data/Gemfile +6 -0
  4. data/History.txt +69 -0
  5. data/LICENSE +26 -0
  6. data/Manifest.txt +60 -0
  7. data/README.md +60 -0
  8. data/Rakefile +12 -0
  9. data/TODO +5 -0
  10. data/bin/puma +15 -0
  11. data/examples/builder.rb +29 -0
  12. data/examples/camping/README +3 -0
  13. data/examples/camping/blog.rb +294 -0
  14. data/examples/camping/tepee.rb +149 -0
  15. data/examples/httpd.conf +474 -0
  16. data/examples/mime.yaml +3 -0
  17. data/examples/mongrel.conf +9 -0
  18. data/examples/monitrc +57 -0
  19. data/examples/random_thrash.rb +19 -0
  20. data/examples/simpletest.rb +52 -0
  21. data/examples/webrick_compare.rb +20 -0
  22. data/ext/puma_http11/PumaHttp11Service.java +13 -0
  23. data/ext/puma_http11/ext_help.h +15 -0
  24. data/ext/puma_http11/extconf.rb +5 -0
  25. data/ext/puma_http11/http11_parser.c +1225 -0
  26. data/ext/puma_http11/http11_parser.h +63 -0
  27. data/ext/puma_http11/http11_parser.java.rl +161 -0
  28. data/ext/puma_http11/http11_parser.rl +146 -0
  29. data/ext/puma_http11/http11_parser_common.rl +54 -0
  30. data/ext/puma_http11/org/jruby/puma/Http11.java +225 -0
  31. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +488 -0
  32. data/ext/puma_http11/puma_http11.c +482 -0
  33. data/lib/puma.rb +18 -0
  34. data/lib/puma/cli.rb +164 -0
  35. data/lib/puma/const.rb +132 -0
  36. data/lib/puma/events.rb +36 -0
  37. data/lib/puma/gems.rb +20 -0
  38. data/lib/puma/mime_types.yml +616 -0
  39. data/lib/puma/rack_patch.rb +22 -0
  40. data/lib/puma/server.rb +429 -0
  41. data/lib/puma/thread_pool.rb +95 -0
  42. data/lib/puma/utils.rb +44 -0
  43. data/lib/puma_http11.jar +0 -0
  44. data/lib/rack/handler/puma.rb +48 -0
  45. data/puma.gemspec +40 -0
  46. data/tasks/gem.rake +24 -0
  47. data/tasks/java.rake +12 -0
  48. data/tasks/native.rake +36 -0
  49. data/tasks/ragel.rake +24 -0
  50. data/test/lobster.ru +4 -0
  51. data/test/mime.yaml +3 -0
  52. data/test/test_cli.rb +19 -0
  53. data/test/test_http10.rb +27 -0
  54. data/test/test_http11.rb +151 -0
  55. data/test/test_persistent.rb +205 -0
  56. data/test/test_rack_handler.rb +10 -0
  57. data/test/test_rack_server.rb +122 -0
  58. data/test/test_thread_pool.rb +102 -0
  59. data/test/test_unix_socket.rb +37 -0
  60. data/test/test_ws.rb +97 -0
  61. data/test/testhelp.rb +41 -0
  62. data/tools/trickletest.rb +45 -0
  63. metadata +163 -0
@@ -0,0 +1,482 @@
1
+ /**
2
+ * Copyright (c) 2005 Zed A. Shaw
3
+ * You can redistribute it and/or modify it under the same terms as Ruby.
4
+ */
5
+ #include "ruby.h"
6
+ #include "ext_help.h"
7
+ #include <assert.h>
8
+ #include <string.h>
9
+ #include "http11_parser.h"
10
+
11
+ #ifndef MANAGED_STRINGS
12
+
13
+ #ifndef RSTRING_PTR
14
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
15
+ #endif
16
+ #ifndef RSTRING_LEN
17
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
18
+ #endif
19
+
20
+ #define rb_extract_chars(e, sz) (*sz = RSTRING_LEN(e), RSTRING_PTR(e))
21
+ #define rb_free_chars(e) /* nothing */
22
+
23
+ #endif
24
+
25
+ static VALUE eHttpParserError;
26
+
27
+ #define HTTP_PREFIX "HTTP_"
28
+ #define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1)
29
+
30
+ static VALUE global_request_method;
31
+ static VALUE global_request_uri;
32
+ static VALUE global_fragment;
33
+ static VALUE global_query_string;
34
+ static VALUE global_http_version;
35
+ static VALUE global_request_path;
36
+
37
+ /** Defines common length and error messages for input length validation. */
38
+ #define DEF_MAX_LENGTH(N,length) const size_t MAX_##N##_LENGTH = length; const char *MAX_##N##_LENGTH_ERR = "HTTP element " # N " is longer than the " # length " allowed length."
39
+
40
+ /** Validates the max length of given input and throws an HttpParserError exception if over. */
41
+ #define VALIDATE_MAX_LENGTH(len, N) if(len > MAX_##N##_LENGTH) { rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); }
42
+
43
+ /** Defines global strings in the init method. */
44
+ #define DEF_GLOBAL(N, val) global_##N = rb_str_new2(val); rb_global_variable(&global_##N)
45
+
46
+
47
+ /* Defines the maximum allowed lengths for various input elements.*/
48
+ DEF_MAX_LENGTH(FIELD_NAME, 256);
49
+ DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
50
+ DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12);
51
+ DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
52
+ DEF_MAX_LENGTH(REQUEST_PATH, 1024);
53
+ DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
54
+ DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
55
+
56
+ struct common_field {
57
+ const signed long len;
58
+ const char *name;
59
+ int raw;
60
+ VALUE value;
61
+ };
62
+
63
+ /*
64
+ * A list of common HTTP headers we expect to receive.
65
+ * This allows us to avoid repeatedly creating identical string
66
+ * objects to be used with rb_hash_aset().
67
+ */
68
+ static struct common_field common_http_fields[] = {
69
+ # define f(N) { (sizeof(N) - 1), N, 0, Qnil }
70
+ # define fr(N) { (sizeof(N) - 1), N, 1, Qnil }
71
+ f("ACCEPT"),
72
+ f("ACCEPT_CHARSET"),
73
+ f("ACCEPT_ENCODING"),
74
+ f("ACCEPT_LANGUAGE"),
75
+ f("ALLOW"),
76
+ f("AUTHORIZATION"),
77
+ f("CACHE_CONTROL"),
78
+ f("CONNECTION"),
79
+ f("CONTENT_ENCODING"),
80
+ fr("CONTENT_LENGTH"),
81
+ fr("CONTENT_TYPE"),
82
+ f("COOKIE"),
83
+ f("DATE"),
84
+ f("EXPECT"),
85
+ f("FROM"),
86
+ f("HOST"),
87
+ f("IF_MATCH"),
88
+ f("IF_MODIFIED_SINCE"),
89
+ f("IF_NONE_MATCH"),
90
+ f("IF_RANGE"),
91
+ f("IF_UNMODIFIED_SINCE"),
92
+ f("KEEP_ALIVE"), /* Firefox sends this */
93
+ f("MAX_FORWARDS"),
94
+ f("PRAGMA"),
95
+ f("PROXY_AUTHORIZATION"),
96
+ f("RANGE"),
97
+ f("REFERER"),
98
+ f("TE"),
99
+ f("TRAILER"),
100
+ f("TRANSFER_ENCODING"),
101
+ f("UPGRADE"),
102
+ f("USER_AGENT"),
103
+ f("VIA"),
104
+ f("X_FORWARDED_FOR"), /* common for proxies */
105
+ f("X_REAL_IP"), /* common for proxies */
106
+ f("WARNING")
107
+ # undef f
108
+ };
109
+
110
+ /*
111
+ * qsort(3) and bsearch(3) improve average performance slightly, but may
112
+ * not be worth it for lack of portability to certain platforms...
113
+ */
114
+ #if defined(HAVE_QSORT_BSEARCH)
115
+ /* sort by length, then by name if there's a tie */
116
+ static int common_field_cmp(const void *a, const void *b)
117
+ {
118
+ struct common_field *cfa = (struct common_field *)a;
119
+ struct common_field *cfb = (struct common_field *)b;
120
+ signed long diff = cfa->len - cfb->len;
121
+ return diff ? diff : memcmp(cfa->name, cfb->name, cfa->len);
122
+ }
123
+ #endif /* HAVE_QSORT_BSEARCH */
124
+
125
+ static void init_common_fields(void)
126
+ {
127
+ int i;
128
+ struct common_field *cf = common_http_fields;
129
+ char tmp[256]; /* MAX_FIELD_NAME_LENGTH */
130
+ memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN);
131
+
132
+ for(i = 0; i < ARRAY_SIZE(common_http_fields); cf++, i++) {
133
+ if(cf->raw) {
134
+ cf->value = rb_str_new(cf->name, cf->len);
135
+ } else {
136
+ memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1);
137
+ cf->value = rb_str_new(tmp, HTTP_PREFIX_LEN + cf->len);
138
+ }
139
+ rb_global_variable(&cf->value);
140
+ }
141
+
142
+ #if defined(HAVE_QSORT_BSEARCH)
143
+ qsort(common_http_fields,
144
+ ARRAY_SIZE(common_http_fields),
145
+ sizeof(struct common_field),
146
+ common_field_cmp);
147
+ #endif /* HAVE_QSORT_BSEARCH */
148
+ }
149
+
150
+ static VALUE find_common_field_value(const char *field, size_t flen)
151
+ {
152
+ #if defined(HAVE_QSORT_BSEARCH)
153
+ struct common_field key;
154
+ struct common_field *found;
155
+ key.name = field;
156
+ key.len = (signed long)flen;
157
+ found = (struct common_field *)bsearch(&key, common_http_fields,
158
+ ARRAY_SIZE(common_http_fields),
159
+ sizeof(struct common_field),
160
+ common_field_cmp);
161
+ return found ? found->value : Qnil;
162
+ #else /* !HAVE_QSORT_BSEARCH */
163
+ int i;
164
+ struct common_field *cf = common_http_fields;
165
+ for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) {
166
+ if (cf->len == flen && !memcmp(cf->name, field, flen))
167
+ return cf->value;
168
+ }
169
+ return Qnil;
170
+ #endif /* !HAVE_QSORT_BSEARCH */
171
+ }
172
+
173
+ void http_field(http_parser* hp, const char *field, size_t flen,
174
+ const char *value, size_t vlen)
175
+ {
176
+ VALUE v = Qnil;
177
+ VALUE f = Qnil;
178
+
179
+ VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
180
+ VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE);
181
+
182
+ v = rb_str_new(value, vlen);
183
+
184
+ f = find_common_field_value(field, flen);
185
+
186
+ if (f == Qnil) {
187
+ /*
188
+ * We got a strange header that we don't have a memoized value for.
189
+ * Fallback to creating a new string to use as a hash key.
190
+ */
191
+
192
+ size_t new_size = HTTP_PREFIX_LEN + flen;
193
+ assert(new_size < BUFFER_LEN);
194
+
195
+ memcpy(hp->buf, HTTP_PREFIX, HTTP_PREFIX_LEN);
196
+ memcpy(hp->buf + HTTP_PREFIX_LEN, field, flen);
197
+
198
+ f = rb_str_new(hp->buf, new_size);
199
+ }
200
+
201
+ rb_hash_aset(hp->request, f, v);
202
+ }
203
+
204
+ void request_method(http_parser* hp, const char *at, size_t length)
205
+ {
206
+ VALUE val = Qnil;
207
+
208
+ val = rb_str_new(at, length);
209
+ rb_hash_aset(hp->request, global_request_method, val);
210
+ }
211
+
212
+ void request_uri(http_parser* hp, const char *at, size_t length)
213
+ {
214
+ VALUE val = Qnil;
215
+
216
+ VALIDATE_MAX_LENGTH(length, REQUEST_URI);
217
+
218
+ val = rb_str_new(at, length);
219
+ rb_hash_aset(hp->request, global_request_uri, val);
220
+ }
221
+
222
+ void fragment(http_parser* hp, const char *at, size_t length)
223
+ {
224
+ VALUE val = Qnil;
225
+
226
+ VALIDATE_MAX_LENGTH(length, FRAGMENT);
227
+
228
+ val = rb_str_new(at, length);
229
+ rb_hash_aset(hp->request, global_fragment, val);
230
+ }
231
+
232
+ void request_path(http_parser* hp, const char *at, size_t length)
233
+ {
234
+ VALUE val = Qnil;
235
+
236
+ VALIDATE_MAX_LENGTH(length, REQUEST_PATH);
237
+
238
+ val = rb_str_new(at, length);
239
+ rb_hash_aset(hp->request, global_request_path, val);
240
+ }
241
+
242
+ void query_string(http_parser* hp, const char *at, size_t length)
243
+ {
244
+ VALUE val = Qnil;
245
+
246
+ VALIDATE_MAX_LENGTH(length, QUERY_STRING);
247
+
248
+ val = rb_str_new(at, length);
249
+ rb_hash_aset(hp->request, global_query_string, val);
250
+ }
251
+
252
+ void http_version(http_parser* hp, const char *at, size_t length)
253
+ {
254
+ VALUE val = rb_str_new(at, length);
255
+ rb_hash_aset(hp->request, global_http_version, val);
256
+ }
257
+
258
+ /** Finalizes the request header to have a bunch of stuff that's
259
+ needed. */
260
+
261
+ void header_done(http_parser* hp, const char *at, size_t length)
262
+ {
263
+ hp->body = rb_str_new(at, length);
264
+ }
265
+
266
+
267
+ void HttpParser_free(void *data) {
268
+ TRACE();
269
+
270
+ if(data) {
271
+ free(data);
272
+ }
273
+ }
274
+
275
+ void HttpParser_mark(http_parser* hp) {
276
+ if(hp->request) rb_gc_mark(hp->request);
277
+ if(hp->body) rb_gc_mark(hp->body);
278
+ }
279
+
280
+ VALUE HttpParser_alloc(VALUE klass)
281
+ {
282
+ http_parser *hp = ALLOC_N(http_parser, 1);
283
+ TRACE();
284
+ hp->http_field = http_field;
285
+ hp->request_method = request_method;
286
+ hp->request_uri = request_uri;
287
+ hp->fragment = fragment;
288
+ hp->request_path = request_path;
289
+ hp->query_string = query_string;
290
+ hp->http_version = http_version;
291
+ hp->header_done = header_done;
292
+ hp->request = Qnil;
293
+
294
+ http_parser_init(hp);
295
+
296
+ return Data_Wrap_Struct(klass, HttpParser_mark, HttpParser_free, hp);
297
+ }
298
+
299
+ /**
300
+ * call-seq:
301
+ * parser.new -> parser
302
+ *
303
+ * Creates a new parser.
304
+ */
305
+ VALUE HttpParser_init(VALUE self)
306
+ {
307
+ http_parser *http = NULL;
308
+ DATA_GET(self, http_parser, http);
309
+ http_parser_init(http);
310
+
311
+ return self;
312
+ }
313
+
314
+
315
+ /**
316
+ * call-seq:
317
+ * parser.reset -> nil
318
+ *
319
+ * Resets the parser to it's initial state so that you can reuse it
320
+ * rather than making new ones.
321
+ */
322
+ VALUE HttpParser_reset(VALUE self)
323
+ {
324
+ http_parser *http = NULL;
325
+ DATA_GET(self, http_parser, http);
326
+ http_parser_init(http);
327
+
328
+ return Qnil;
329
+ }
330
+
331
+
332
+ /**
333
+ * call-seq:
334
+ * parser.finish -> true/false
335
+ *
336
+ * Finishes a parser early which could put in a "good" or bad state.
337
+ * You should call reset after finish it or bad things will happen.
338
+ */
339
+ VALUE HttpParser_finish(VALUE self)
340
+ {
341
+ http_parser *http = NULL;
342
+ DATA_GET(self, http_parser, http);
343
+ http_parser_finish(http);
344
+
345
+ return http_parser_is_finished(http) ? Qtrue : Qfalse;
346
+ }
347
+
348
+
349
+ /**
350
+ * call-seq:
351
+ * parser.execute(req_hash, data, start) -> Integer
352
+ *
353
+ * Takes a Hash and a String of data, parses the String of data filling in the Hash
354
+ * returning an Integer to indicate how much of the data has been read. No matter
355
+ * what the return value, you should call HttpParser#finished? and HttpParser#error?
356
+ * to figure out if it's done parsing or there was an error.
357
+ *
358
+ * This function now throws an exception when there is a parsing error. This makes
359
+ * the logic for working with the parser much easier. You can still test for an
360
+ * error, but now you need to wrap the parser with an exception handling block.
361
+ *
362
+ * The third argument allows for parsing a partial request and then continuing
363
+ * the parsing from that position. It needs all of the original data as well
364
+ * so you have to append to the data buffer as you read.
365
+ */
366
+ VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start)
367
+ {
368
+ http_parser *http = NULL;
369
+ int from = 0;
370
+ char *dptr = NULL;
371
+ long dlen = 0;
372
+
373
+ DATA_GET(self, http_parser, http);
374
+
375
+ from = FIX2INT(start);
376
+ dptr = rb_extract_chars(data, &dlen);
377
+
378
+ if(from >= dlen) {
379
+ rb_free_chars(dptr);
380
+ rb_raise(eHttpParserError, "Requested start is after data buffer end.");
381
+ } else {
382
+ http->request = req_hash;
383
+ http_parser_execute(http, dptr, dlen, from);
384
+
385
+ rb_free_chars(dptr);
386
+ VALIDATE_MAX_LENGTH(http_parser_nread(http), HEADER);
387
+
388
+ if(http_parser_has_error(http)) {
389
+ rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
390
+ } else {
391
+ return INT2FIX(http_parser_nread(http));
392
+ }
393
+ }
394
+ }
395
+
396
+
397
+
398
+ /**
399
+ * call-seq:
400
+ * parser.error? -> true/false
401
+ *
402
+ * Tells you whether the parser is in an error state.
403
+ */
404
+ VALUE HttpParser_has_error(VALUE self)
405
+ {
406
+ http_parser *http = NULL;
407
+ DATA_GET(self, http_parser, http);
408
+
409
+ return http_parser_has_error(http) ? Qtrue : Qfalse;
410
+ }
411
+
412
+
413
+ /**
414
+ * call-seq:
415
+ * parser.finished? -> true/false
416
+ *
417
+ * Tells you whether the parser is finished or not and in a good state.
418
+ */
419
+ VALUE HttpParser_is_finished(VALUE self)
420
+ {
421
+ http_parser *http = NULL;
422
+ DATA_GET(self, http_parser, http);
423
+
424
+ return http_parser_is_finished(http) ? Qtrue : Qfalse;
425
+ }
426
+
427
+
428
+ /**
429
+ * call-seq:
430
+ * parser.nread -> Integer
431
+ *
432
+ * Returns the amount of data processed so far during this processing cycle. It is
433
+ * set to 0 on initialize or reset calls and is incremented each time execute is called.
434
+ */
435
+ VALUE HttpParser_nread(VALUE self)
436
+ {
437
+ http_parser *http = NULL;
438
+ DATA_GET(self, http_parser, http);
439
+
440
+ return INT2FIX(http->nread);
441
+ }
442
+
443
+ /**
444
+ * call-seq:
445
+ * parser.body -> nil or String
446
+ *
447
+ * If the request included a body, returns it.
448
+ */
449
+ VALUE HttpParser_body(VALUE self) {
450
+ http_parser *http = NULL;
451
+ DATA_GET(self, http_parser, http);
452
+
453
+ return http->body;
454
+ }
455
+
456
+ void Init_puma_http11()
457
+ {
458
+
459
+ VALUE mPuma = rb_define_module("Puma");
460
+
461
+ DEF_GLOBAL(request_method, "REQUEST_METHOD");
462
+ DEF_GLOBAL(request_uri, "REQUEST_URI");
463
+ DEF_GLOBAL(fragment, "FRAGMENT");
464
+ DEF_GLOBAL(query_string, "QUERY_STRING");
465
+ DEF_GLOBAL(http_version, "HTTP_VERSION");
466
+ DEF_GLOBAL(request_path, "REQUEST_PATH");
467
+
468
+ eHttpParserError = rb_define_class_under(mPuma, "HttpParserError", rb_eIOError);
469
+ rb_global_variable(&eHttpParserError);
470
+
471
+ VALUE cHttpParser = rb_define_class_under(mPuma, "HttpParser", rb_cObject);
472
+ rb_define_alloc_func(cHttpParser, HttpParser_alloc);
473
+ rb_define_method(cHttpParser, "initialize", HttpParser_init, 0);
474
+ rb_define_method(cHttpParser, "reset", HttpParser_reset, 0);
475
+ rb_define_method(cHttpParser, "finish", HttpParser_finish, 0);
476
+ rb_define_method(cHttpParser, "execute", HttpParser_execute, 3);
477
+ rb_define_method(cHttpParser, "error?", HttpParser_has_error, 0);
478
+ rb_define_method(cHttpParser, "finished?", HttpParser_is_finished, 0);
479
+ rb_define_method(cHttpParser, "nread", HttpParser_nread, 0);
480
+ rb_define_method(cHttpParser, "body", HttpParser_body, 0);
481
+ init_common_fields();
482
+ }