unicorn-shopify 4.8.2.5.23

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.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/.CHANGELOG.old +25 -0
  3. data/.document +28 -0
  4. data/.gitignore +25 -0
  5. data/.mailmap +26 -0
  6. data/.olddoc.yml +15 -0
  7. data/Application_Timeouts +77 -0
  8. data/CONTRIBUTORS +35 -0
  9. data/COPYING +674 -0
  10. data/DESIGN +97 -0
  11. data/Documentation/.gitignore +5 -0
  12. data/Documentation/GNUmakefile +30 -0
  13. data/Documentation/unicorn.1.txt +185 -0
  14. data/Documentation/unicorn_rails.1.txt +175 -0
  15. data/FAQ +61 -0
  16. data/GIT-VERSION-GEN +39 -0
  17. data/GNUmakefile +252 -0
  18. data/HACKING +120 -0
  19. data/ISSUES +100 -0
  20. data/KNOWN_ISSUES +79 -0
  21. data/LICENSE +67 -0
  22. data/Links +59 -0
  23. data/PHILOSOPHY +145 -0
  24. data/README +145 -0
  25. data/Rakefile +16 -0
  26. data/SIGNALS +123 -0
  27. data/Sandbox +103 -0
  28. data/TODO +5 -0
  29. data/TUNING +101 -0
  30. data/archive/.gitignore +3 -0
  31. data/archive/slrnpull.conf +4 -0
  32. data/bin/unicorn +126 -0
  33. data/bin/unicorn_rails +209 -0
  34. data/examples/big_app_gc.rb +2 -0
  35. data/examples/echo.ru +27 -0
  36. data/examples/init.sh +74 -0
  37. data/examples/logger_mp_safe.rb +25 -0
  38. data/examples/logrotate.conf +29 -0
  39. data/examples/nginx.conf +156 -0
  40. data/examples/unicorn.conf.minimal.rb +13 -0
  41. data/examples/unicorn.conf.rb +113 -0
  42. data/ext/unicorn_http/CFLAGS +13 -0
  43. data/ext/unicorn_http/c_util.h +124 -0
  44. data/ext/unicorn_http/common_field_optimization.h +111 -0
  45. data/ext/unicorn_http/ext_help.h +82 -0
  46. data/ext/unicorn_http/extconf.rb +10 -0
  47. data/ext/unicorn_http/global_variables.h +97 -0
  48. data/ext/unicorn_http/httpdate.c +78 -0
  49. data/ext/unicorn_http/unicorn_http.rl +934 -0
  50. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  51. data/lib/unicorn.rb +112 -0
  52. data/lib/unicorn/app/old_rails.rb +35 -0
  53. data/lib/unicorn/app/old_rails/static.rb +59 -0
  54. data/lib/unicorn/cgi_wrapper.rb +147 -0
  55. data/lib/unicorn/configurator.rb +686 -0
  56. data/lib/unicorn/const.rb +21 -0
  57. data/lib/unicorn/http_request.rb +125 -0
  58. data/lib/unicorn/http_response.rb +73 -0
  59. data/lib/unicorn/http_server.rb +816 -0
  60. data/lib/unicorn/launcher.rb +62 -0
  61. data/lib/unicorn/oob_gc.rb +81 -0
  62. data/lib/unicorn/preread_input.rb +33 -0
  63. data/lib/unicorn/socket_helper.rb +197 -0
  64. data/lib/unicorn/stream_input.rb +146 -0
  65. data/lib/unicorn/tee_input.rb +133 -0
  66. data/lib/unicorn/tmpio.rb +27 -0
  67. data/lib/unicorn/util.rb +90 -0
  68. data/lib/unicorn/worker.rb +140 -0
  69. data/setup.rb +1586 -0
  70. data/t/.gitignore +4 -0
  71. data/t/GNUmakefile +74 -0
  72. data/t/README +42 -0
  73. data/t/before_murder.ru +7 -0
  74. data/t/bin/content-md5-put +36 -0
  75. data/t/bin/sha1sum.rb +17 -0
  76. data/t/bin/unused_listen +40 -0
  77. data/t/broken-app.ru +12 -0
  78. data/t/detach.ru +11 -0
  79. data/t/env.ru +3 -0
  80. data/t/fails-rack-lint.ru +5 -0
  81. data/t/heartbeat-timeout.ru +12 -0
  82. data/t/hijack.ru +42 -0
  83. data/t/listener_names.ru +4 -0
  84. data/t/my-tap-lib.sh +201 -0
  85. data/t/oob_gc.ru +20 -0
  86. data/t/oob_gc_path.ru +20 -0
  87. data/t/pid.ru +3 -0
  88. data/t/preread_input.ru +17 -0
  89. data/t/rack-input-tests.ru +21 -0
  90. data/t/t0000-http-basic.sh +50 -0
  91. data/t/t0001-reload-bad-config.sh +53 -0
  92. data/t/t0002-config-conflict.sh +49 -0
  93. data/t/t0002-parser-error.sh +94 -0
  94. data/t/t0003-working_directory.sh +51 -0
  95. data/t/t0004-heartbeat-timeout.sh +69 -0
  96. data/t/t0004-working_directory_broken.sh +24 -0
  97. data/t/t0005-working_directory_app.rb.sh +40 -0
  98. data/t/t0006-reopen-logs.sh +83 -0
  99. data/t/t0006.ru +13 -0
  100. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  101. data/t/t0008-back_out_of_upgrade.sh +110 -0
  102. data/t/t0009-broken-app.sh +56 -0
  103. data/t/t0009-winch_ttin.sh +59 -0
  104. data/t/t0010-reap-logging.sh +55 -0
  105. data/t/t0011-active-unix-socket.sh +79 -0
  106. data/t/t0012-reload-empty-config.sh +85 -0
  107. data/t/t0013-rewindable-input-false.sh +24 -0
  108. data/t/t0013.ru +12 -0
  109. data/t/t0014-rewindable-input-true.sh +24 -0
  110. data/t/t0014.ru +12 -0
  111. data/t/t0015-configurator-internals.sh +25 -0
  112. data/t/t0018-write-on-close.sh +23 -0
  113. data/t/t0019-max_header_len.sh +49 -0
  114. data/t/t0020-at_exit-handler.sh +49 -0
  115. data/t/t0021-process_detach.sh +29 -0
  116. data/t/t0022-listener_names-preload_app.sh +32 -0
  117. data/t/t0023-before-murder.sh +40 -0
  118. data/t/t0024-before-murder_once.sh +52 -0
  119. data/t/t0100-rack-input-tests.sh +124 -0
  120. data/t/t0116-client_body_buffer_size.sh +80 -0
  121. data/t/t0116.ru +16 -0
  122. data/t/t0200-rack-hijack.sh +27 -0
  123. data/t/t0300-no-default-middleware.sh +20 -0
  124. data/t/t9000-preread-input.sh +48 -0
  125. data/t/t9001-oob_gc.sh +47 -0
  126. data/t/t9002-oob_gc-path.sh +75 -0
  127. data/t/test-lib.sh +128 -0
  128. data/t/write-on-close.ru +11 -0
  129. data/test/aggregate.rb +15 -0
  130. data/test/benchmark/README +50 -0
  131. data/test/benchmark/dd.ru +18 -0
  132. data/test/benchmark/stack.ru +8 -0
  133. data/test/exec/README +5 -0
  134. data/test/exec/test_exec.rb +1047 -0
  135. data/test/test_helper.rb +297 -0
  136. data/test/unit/test_configurator.rb +175 -0
  137. data/test/unit/test_droplet.rb +28 -0
  138. data/test/unit/test_http_parser.rb +854 -0
  139. data/test/unit/test_http_parser_ng.rb +622 -0
  140. data/test/unit/test_request.rb +182 -0
  141. data/test/unit/test_response.rb +93 -0
  142. data/test/unit/test_server.rb +268 -0
  143. data/test/unit/test_signals.rb +188 -0
  144. data/test/unit/test_socket_helper.rb +197 -0
  145. data/test/unit/test_stream_input.rb +203 -0
  146. data/test/unit/test_tee_input.rb +304 -0
  147. data/test/unit/test_upload.rb +306 -0
  148. data/test/unit/test_util.rb +105 -0
  149. data/unicorn.gemspec +41 -0
  150. metadata +311 -0
@@ -0,0 +1,111 @@
1
+ #ifndef common_field_optimization
2
+ #define common_field_optimization
3
+ #include "ruby.h"
4
+ #include "c_util.h"
5
+
6
+ struct common_field {
7
+ const signed long len;
8
+ const char *name;
9
+ VALUE value;
10
+ };
11
+
12
+ /*
13
+ * A list of common HTTP headers we expect to receive.
14
+ * This allows us to avoid repeatedly creating identical string
15
+ * objects to be used with rb_hash_aset().
16
+ */
17
+ static struct common_field common_http_fields[] = {
18
+ # define f(N) { (sizeof(N) - 1), N, Qnil }
19
+ f("ACCEPT"),
20
+ f("ACCEPT_CHARSET"),
21
+ f("ACCEPT_ENCODING"),
22
+ f("ACCEPT_LANGUAGE"),
23
+ f("ALLOW"),
24
+ f("AUTHORIZATION"),
25
+ f("CACHE_CONTROL"),
26
+ f("CONNECTION"),
27
+ f("CONTENT_ENCODING"),
28
+ f("CONTENT_LENGTH"),
29
+ f("CONTENT_TYPE"),
30
+ f("COOKIE"),
31
+ f("DATE"),
32
+ f("EXPECT"),
33
+ f("FROM"),
34
+ f("HOST"),
35
+ f("IF_MATCH"),
36
+ f("IF_MODIFIED_SINCE"),
37
+ f("IF_NONE_MATCH"),
38
+ f("IF_RANGE"),
39
+ f("IF_UNMODIFIED_SINCE"),
40
+ f("KEEP_ALIVE"), /* Firefox sends this */
41
+ f("MAX_FORWARDS"),
42
+ f("PRAGMA"),
43
+ f("PROXY_AUTHORIZATION"),
44
+ f("RANGE"),
45
+ f("REFERER"),
46
+ f("TE"),
47
+ f("TRAILER"),
48
+ f("TRANSFER_ENCODING"),
49
+ f("UPGRADE"),
50
+ f("USER_AGENT"),
51
+ f("VIA"),
52
+ f("X_FORWARDED_FOR"), /* common for proxies */
53
+ f("X_FORWARDED_PROTO"), /* common for proxies */
54
+ f("X_REAL_IP"), /* common for proxies */
55
+ f("WARNING")
56
+ # undef f
57
+ };
58
+
59
+ #define HTTP_PREFIX "HTTP_"
60
+ #define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1)
61
+
62
+ /* this function is not performance-critical, called only at load time */
63
+ static void init_common_fields(void)
64
+ {
65
+ int i;
66
+ struct common_field *cf = common_http_fields;
67
+ char tmp[64];
68
+ memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN);
69
+
70
+ for(i = ARRAY_SIZE(common_http_fields); --i >= 0; cf++) {
71
+ /* Rack doesn't like certain headers prefixed with "HTTP_" */
72
+ if (!strcmp("CONTENT_LENGTH", cf->name) ||
73
+ !strcmp("CONTENT_TYPE", cf->name)) {
74
+ cf->value = rb_str_new(cf->name, cf->len);
75
+ } else {
76
+ memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1);
77
+ cf->value = rb_str_new(tmp, HTTP_PREFIX_LEN + cf->len);
78
+ }
79
+ cf->value = rb_obj_freeze(cf->value);
80
+ rb_global_variable(&cf->value);
81
+ }
82
+ }
83
+
84
+ /* this function is called for every header set */
85
+ static VALUE find_common_field(const char *field, size_t flen)
86
+ {
87
+ int i;
88
+ struct common_field *cf = common_http_fields;
89
+
90
+ for(i = ARRAY_SIZE(common_http_fields); --i >= 0; cf++) {
91
+ if (cf->len == (long)flen && !memcmp(cf->name, field, flen))
92
+ return cf->value;
93
+ }
94
+ return Qnil;
95
+ }
96
+
97
+ /*
98
+ * We got a strange header that we don't have a memoized value for.
99
+ * Fallback to creating a new string to use as a hash key.
100
+ */
101
+ static VALUE uncommon_field(const char *field, size_t flen)
102
+ {
103
+ VALUE f = rb_str_new(NULL, HTTP_PREFIX_LEN + flen);
104
+ memcpy(RSTRING_PTR(f), HTTP_PREFIX, HTTP_PREFIX_LEN);
105
+ memcpy(RSTRING_PTR(f) + HTTP_PREFIX_LEN, field, flen);
106
+ assert(*(RSTRING_PTR(f) + RSTRING_LEN(f)) == '\0' &&
107
+ "string didn't end with \\0"); /* paranoia */
108
+ return rb_obj_freeze(f);
109
+ }
110
+
111
+ #endif /* common_field_optimization_h */
@@ -0,0 +1,82 @@
1
+ #ifndef ext_help_h
2
+ #define ext_help_h
3
+
4
+ #ifndef RSTRING_PTR
5
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
6
+ #endif /* !defined(RSTRING_PTR) */
7
+ #ifndef RSTRING_LEN
8
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
9
+ #endif /* !defined(RSTRING_LEN) */
10
+
11
+ #ifndef HAVE_RB_STR_SET_LEN
12
+ # ifdef RUBINIUS
13
+ # error we should never get here with current Rubinius (1.x)
14
+ # endif
15
+ /* this is taken from Ruby 1.8.7, 1.8.6 may not have it */
16
+ static void rb_18_str_set_len(VALUE str, long len)
17
+ {
18
+ RSTRING(str)->len = len;
19
+ RSTRING(str)->ptr[len] = '\0';
20
+ }
21
+ # define rb_str_set_len(str,len) rb_18_str_set_len(str,len)
22
+ #endif /* !defined(HAVE_RB_STR_SET_LEN) */
23
+
24
+ /* not all Ruby implementations support frozen objects (Rubinius does not) */
25
+ #if defined(OBJ_FROZEN)
26
+ # define assert_frozen(f) assert(OBJ_FROZEN(f) && "unfrozen object")
27
+ #else
28
+ # define assert_frozen(f) do {} while (0)
29
+ #endif /* !defined(OBJ_FROZEN) */
30
+
31
+ #if !defined(OFFT2NUM)
32
+ # if SIZEOF_OFF_T == SIZEOF_LONG
33
+ # define OFFT2NUM(n) LONG2NUM(n)
34
+ # else
35
+ # define OFFT2NUM(n) LL2NUM(n)
36
+ # endif
37
+ #endif /* ! defined(OFFT2NUM) */
38
+
39
+ #if !defined(SIZET2NUM)
40
+ # if SIZEOF_SIZE_T == SIZEOF_LONG
41
+ # define SIZET2NUM(n) ULONG2NUM(n)
42
+ # else
43
+ # define SIZET2NUM(n) ULL2NUM(n)
44
+ # endif
45
+ #endif /* ! defined(SIZET2NUM) */
46
+
47
+ #if !defined(NUM2SIZET)
48
+ # if SIZEOF_SIZE_T == SIZEOF_LONG
49
+ # define NUM2SIZET(n) ((size_t)NUM2ULONG(n))
50
+ # else
51
+ # define NUM2SIZET(n) ((size_t)NUM2ULL(n))
52
+ # endif
53
+ #endif /* ! defined(NUM2SIZET) */
54
+
55
+ static inline int str_cstr_eq(VALUE val, const char *ptr, long len)
56
+ {
57
+ return (RSTRING_LEN(val) == len && !memcmp(ptr, RSTRING_PTR(val), len));
58
+ }
59
+
60
+ #define STR_CSTR_EQ(val, const_str) \
61
+ str_cstr_eq(val, const_str, sizeof(const_str) - 1)
62
+
63
+ /* strcasecmp isn't locale independent */
64
+ static int str_cstr_case_eq(VALUE val, const char *ptr, long len)
65
+ {
66
+ if (RSTRING_LEN(val) == len) {
67
+ const char *v = RSTRING_PTR(val);
68
+
69
+ for (; len--; ++ptr, ++v) {
70
+ if ((*ptr == *v) || (*v >= 'A' && *v <= 'Z' && (*v | 0x20) == *ptr))
71
+ continue;
72
+ return 0;
73
+ }
74
+ return 1;
75
+ }
76
+ return 0;
77
+ }
78
+
79
+ #define STR_CSTR_CASE_EQ(val, const_str) \
80
+ str_cstr_case_eq(val, const_str, sizeof(const_str) - 1)
81
+
82
+ #endif /* ext_help_h */
@@ -0,0 +1,10 @@
1
+ # -*- encoding: binary -*-
2
+ require 'mkmf'
3
+
4
+ have_macro("SIZEOF_OFF_T", "ruby.h") or check_sizeof("off_t", "sys/types.h")
5
+ have_macro("SIZEOF_SIZE_T", "ruby.h") or check_sizeof("size_t", "sys/types.h")
6
+ have_macro("SIZEOF_LONG", "ruby.h") or check_sizeof("long", "sys/types.h")
7
+ have_func("rb_str_set_len", "ruby.h")
8
+ have_func("gmtime_r", "time.h")
9
+
10
+ create_makefile("unicorn_http")
@@ -0,0 +1,97 @@
1
+ #ifndef global_variables_h
2
+ #define global_variables_h
3
+ static VALUE eHttpParserError;
4
+ static VALUE e413;
5
+ static VALUE e414;
6
+
7
+ static VALUE g_rack_url_scheme;
8
+ static VALUE g_request_method;
9
+ static VALUE g_request_uri;
10
+ static VALUE g_fragment;
11
+ static VALUE g_query_string;
12
+ static VALUE g_http_version;
13
+ static VALUE g_request_path;
14
+ static VALUE g_path_info;
15
+ static VALUE g_server_name;
16
+ static VALUE g_server_port;
17
+ static VALUE g_server_protocol;
18
+ static VALUE g_http_host;
19
+ static VALUE g_http_x_forwarded_proto;
20
+ static VALUE g_http_x_forwarded_ssl;
21
+ static VALUE g_http_transfer_encoding;
22
+ static VALUE g_content_length;
23
+ static VALUE g_http_trailer;
24
+ static VALUE g_http_connection;
25
+ static VALUE g_port_80;
26
+ static VALUE g_port_443;
27
+ static VALUE g_localhost;
28
+ static VALUE g_http;
29
+ static VALUE g_https;
30
+ static VALUE g_http_09;
31
+ static VALUE g_http_10;
32
+ static VALUE g_http_11;
33
+
34
+ /** Defines common length and error messages for input length validation. */
35
+ #define DEF_MAX_LENGTH(N, length) \
36
+ static const size_t MAX_##N##_LENGTH = length; \
37
+ static const char * const MAX_##N##_LENGTH_ERR = \
38
+ "HTTP element " # N " is longer than the " # length " allowed length."
39
+
40
+ NORETURN(static void parser_raise(VALUE klass, const char *));
41
+
42
+ /**
43
+ * Validates the max length of given input and throws an HttpParserError
44
+ * exception if over.
45
+ */
46
+ #define VALIDATE_MAX_LENGTH(len, N) do { \
47
+ if (len > MAX_##N##_LENGTH) \
48
+ parser_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); \
49
+ } while (0)
50
+
51
+ #define VALIDATE_MAX_URI_LENGTH(len, N) do { \
52
+ if (len > MAX_##N##_LENGTH) \
53
+ parser_raise(e414, MAX_##N##_LENGTH_ERR); \
54
+ } while (0)
55
+
56
+ /** Defines global strings in the init method. */
57
+ #define DEF_GLOBAL(N, val) do { \
58
+ g_##N = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \
59
+ rb_global_variable(&g_##N); \
60
+ } while (0)
61
+
62
+ /* Defines the maximum allowed lengths for various input elements.*/
63
+ DEF_MAX_LENGTH(FIELD_NAME, 256);
64
+ DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
65
+ DEF_MAX_LENGTH(REQUEST_URI, 1024 * 15);
66
+ DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
67
+ DEF_MAX_LENGTH(REQUEST_PATH, 4096); /* common PATH_MAX on modern systems */
68
+ DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
69
+
70
+ static void init_globals(void)
71
+ {
72
+ DEF_GLOBAL(rack_url_scheme, "rack.url_scheme");
73
+ DEF_GLOBAL(request_method, "REQUEST_METHOD");
74
+ DEF_GLOBAL(request_uri, "REQUEST_URI");
75
+ DEF_GLOBAL(fragment, "FRAGMENT");
76
+ DEF_GLOBAL(query_string, "QUERY_STRING");
77
+ DEF_GLOBAL(http_version, "HTTP_VERSION");
78
+ DEF_GLOBAL(request_path, "REQUEST_PATH");
79
+ DEF_GLOBAL(path_info, "PATH_INFO");
80
+ DEF_GLOBAL(server_name, "SERVER_NAME");
81
+ DEF_GLOBAL(server_port, "SERVER_PORT");
82
+ DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL");
83
+ DEF_GLOBAL(http_x_forwarded_proto, "HTTP_X_FORWARDED_PROTO");
84
+ DEF_GLOBAL(http_x_forwarded_ssl, "HTTP_X_FORWARDED_SSL");
85
+ DEF_GLOBAL(port_80, "80");
86
+ DEF_GLOBAL(port_443, "443");
87
+ DEF_GLOBAL(localhost, "localhost");
88
+ DEF_GLOBAL(http, "http");
89
+ DEF_GLOBAL(https, "https");
90
+ DEF_GLOBAL(http_11, "HTTP/1.1");
91
+ DEF_GLOBAL(http_10, "HTTP/1.0");
92
+ DEF_GLOBAL(http_09, "HTTP/0.9");
93
+ }
94
+
95
+ #undef DEF_GLOBAL
96
+
97
+ #endif /* global_variables_h */
@@ -0,0 +1,78 @@
1
+ #include <ruby.h>
2
+ #include <time.h>
3
+ #include <stdio.h>
4
+
5
+ static const size_t buf_capa = sizeof("Thu, 01 Jan 1970 00:00:00 GMT");
6
+ static VALUE buf;
7
+ static char *buf_ptr;
8
+ static const char week[] = "Sun\0Mon\0Tue\0Wed\0Thu\0Fri\0Sat";
9
+ static const char months[] = "Jan\0Feb\0Mar\0Apr\0May\0Jun\0"
10
+ "Jul\0Aug\0Sep\0Oct\0Nov\0Dec";
11
+
12
+ /* for people on wonky systems only */
13
+ #ifndef HAVE_GMTIME_R
14
+ static struct tm * my_gmtime_r(time_t *now, struct tm *tm)
15
+ {
16
+ struct tm *global = gmtime(now);
17
+ if (global)
18
+ *tm = *global;
19
+ return tm;
20
+ }
21
+ # define gmtime_r my_gmtime_r
22
+ #endif
23
+
24
+
25
+ /*
26
+ * Returns a string which represents the time as rfc1123-date of HTTP-date
27
+ * defined by RFC 2616:
28
+ *
29
+ * day-of-week, DD month-name CCYY hh:mm:ss GMT
30
+ *
31
+ * Note that the result is always GMT.
32
+ *
33
+ * This method is identical to Time#httpdate in the Ruby standard library,
34
+ * except it is implemented in C for performance. We always saw
35
+ * Time#httpdate at or near the top of the profiler output so we
36
+ * decided to rewrite this in C.
37
+ *
38
+ * Caveats: it relies on a Ruby implementation with the global VM lock,
39
+ * a thread-safe version will be provided when a Unix-only, GVL-free Ruby
40
+ * implementation becomes viable.
41
+ */
42
+ static VALUE httpdate(VALUE self)
43
+ {
44
+ static time_t last;
45
+ time_t now = time(NULL); /* not a syscall on modern 64-bit systems */
46
+ struct tm tm;
47
+
48
+ if (last == now)
49
+ return buf;
50
+ last = now;
51
+ gmtime_r(&now, &tm);
52
+
53
+ /* we can make this thread-safe later if our Ruby loses the GVL */
54
+ snprintf(buf_ptr, buf_capa,
55
+ "%s, %02d %s %4d %02d:%02d:%02d GMT",
56
+ week + (tm.tm_wday * 4),
57
+ tm.tm_mday,
58
+ months + (tm.tm_mon * 4),
59
+ tm.tm_year + 1900,
60
+ tm.tm_hour,
61
+ tm.tm_min,
62
+ tm.tm_sec);
63
+
64
+ return buf;
65
+ }
66
+
67
+ void init_unicorn_httpdate(void)
68
+ {
69
+ VALUE mod = rb_define_module("Unicorn");
70
+ mod = rb_define_module_under(mod, "HttpResponse");
71
+
72
+ buf = rb_str_new(0, buf_capa - 1);
73
+ rb_global_variable(&buf);
74
+ buf_ptr = RSTRING_PTR(buf);
75
+ httpdate(Qnil);
76
+
77
+ rb_define_method(mod, "httpdate", httpdate, 0);
78
+ }
@@ -0,0 +1,934 @@
1
+ /**
2
+ * Copyright (c) 2009 Eric Wong (all bugs are Eric's fault)
3
+ * Copyright (c) 2005 Zed A. Shaw
4
+ * You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
5
+ * the GPLv2+ (GPLv3+ preferred)
6
+ */
7
+ #include "ruby.h"
8
+ #include "ext_help.h"
9
+ #include <assert.h>
10
+ #include <string.h>
11
+ #include <sys/types.h>
12
+ #include "common_field_optimization.h"
13
+ #include "global_variables.h"
14
+ #include "c_util.h"
15
+
16
+ void init_unicorn_httpdate(void);
17
+
18
+ #define UH_FL_CHUNKED 0x1
19
+ #define UH_FL_HASBODY 0x2
20
+ #define UH_FL_INBODY 0x4
21
+ #define UH_FL_HASTRAILER 0x8
22
+ #define UH_FL_INTRAILER 0x10
23
+ #define UH_FL_INCHUNK 0x20
24
+ #define UH_FL_REQEOF 0x40
25
+ #define UH_FL_KAVERSION 0x80
26
+ #define UH_FL_HASHEADER 0x100
27
+ #define UH_FL_TO_CLEAR 0x200
28
+
29
+ /* all of these flags need to be set for keepalive to be supported */
30
+ #define UH_FL_KEEPALIVE (UH_FL_KAVERSION | UH_FL_REQEOF | UH_FL_HASHEADER)
31
+
32
+ static unsigned int MAX_HEADER_LEN = 1024 * (80 + 32); /* same as Mongrel */
33
+
34
+ /* this is only intended for use with Rainbows! */
35
+ static VALUE set_maxhdrlen(VALUE self, VALUE len)
36
+ {
37
+ return UINT2NUM(MAX_HEADER_LEN = NUM2UINT(len));
38
+ }
39
+
40
+ /* keep this small for Rainbows! since every client has one */
41
+ struct http_parser {
42
+ int cs; /* Ragel internal state */
43
+ unsigned int flags;
44
+ unsigned int mark;
45
+ unsigned int offset;
46
+ union { /* these 2 fields don't nest */
47
+ unsigned int field;
48
+ unsigned int query;
49
+ } start;
50
+ union {
51
+ unsigned int field_len; /* only used during header processing */
52
+ unsigned int dest_offset; /* only used during body processing */
53
+ } s;
54
+ VALUE buf;
55
+ VALUE env;
56
+ VALUE cont; /* Qfalse: unset, Qnil: ignored header, T_STRING: append */
57
+ union {
58
+ off_t content;
59
+ off_t chunk;
60
+ } len;
61
+ };
62
+
63
+ static ID id_clear, id_set_backtrace, id_response_start_sent;
64
+
65
+ static void finalize_header(struct http_parser *hp);
66
+
67
+ static void parser_raise(VALUE klass, const char *msg)
68
+ {
69
+ VALUE exc = rb_exc_new2(klass, msg);
70
+ VALUE bt = rb_ary_new();
71
+
72
+ rb_funcall(exc, id_set_backtrace, 1, bt);
73
+ rb_exc_raise(exc);
74
+ }
75
+
76
+ static inline unsigned int ulong2uint(unsigned long n)
77
+ {
78
+ unsigned int i = (unsigned int)n;
79
+
80
+ if (sizeof(unsigned int) != sizeof(unsigned long)) {
81
+ if ((unsigned long)i != n) {
82
+ rb_raise(rb_eRangeError, "too large to be 32-bit uint: %lu", n);
83
+ }
84
+ }
85
+ return i;
86
+ }
87
+
88
+ #define REMAINING (unsigned long)(pe - p)
89
+ #define LEN(AT, FPC) (ulong2uint(FPC - buffer) - hp->AT)
90
+ #define MARK(M,FPC) (hp->M = ulong2uint((FPC) - buffer))
91
+ #define PTR_TO(F) (buffer + hp->F)
92
+ #define STR_NEW(M,FPC) rb_str_new(PTR_TO(M), LEN(M, FPC))
93
+ #define STRIPPED_STR_NEW(M,FPC) stripped_str_new(PTR_TO(M), LEN(M, FPC))
94
+
95
+ #define HP_FL_TEST(hp,fl) ((hp)->flags & (UH_FL_##fl))
96
+ #define HP_FL_SET(hp,fl) ((hp)->flags |= (UH_FL_##fl))
97
+ #define HP_FL_UNSET(hp,fl) ((hp)->flags &= ~(UH_FL_##fl))
98
+ #define HP_FL_ALL(hp,fl) (HP_FL_TEST(hp, fl) == (UH_FL_##fl))
99
+
100
+ static int is_lws(char c)
101
+ {
102
+ return (c == ' ' || c == '\t');
103
+ }
104
+
105
+ static VALUE stripped_str_new(const char *str, long len)
106
+ {
107
+ long end;
108
+
109
+ for (end = len - 1; end >= 0 && is_lws(str[end]); end--);
110
+
111
+ return rb_str_new(str, end + 1);
112
+ }
113
+
114
+ /*
115
+ * handles values of the "Connection:" header, keepalive is implied
116
+ * for HTTP/1.1 but needs to be explicitly enabled with HTTP/1.0
117
+ * Additionally, we require GET/HEAD requests to support keepalive.
118
+ */
119
+ static void hp_keepalive_connection(struct http_parser *hp, VALUE val)
120
+ {
121
+ if (STR_CSTR_CASE_EQ(val, "keep-alive")) {
122
+ /* basically have HTTP/1.0 masquerade as HTTP/1.1+ */
123
+ HP_FL_SET(hp, KAVERSION);
124
+ } else if (STR_CSTR_CASE_EQ(val, "close")) {
125
+ /*
126
+ * it doesn't matter what HTTP version or request method we have,
127
+ * if a client says "Connection: close", we disable keepalive
128
+ */
129
+ HP_FL_UNSET(hp, KAVERSION);
130
+ } else {
131
+ /*
132
+ * client could've sent anything, ignore it for now. Maybe
133
+ * "HP_FL_UNSET(hp, KAVERSION);" just in case?
134
+ * Raising an exception might be too mean...
135
+ */
136
+ }
137
+ }
138
+
139
+ static void
140
+ request_method(struct http_parser *hp, const char *ptr, size_t len)
141
+ {
142
+ VALUE v = rb_str_new(ptr, len);
143
+
144
+ rb_hash_aset(hp->env, g_request_method, v);
145
+ }
146
+
147
+ static void
148
+ http_version(struct http_parser *hp, const char *ptr, size_t len)
149
+ {
150
+ VALUE v;
151
+
152
+ HP_FL_SET(hp, HASHEADER);
153
+
154
+ if (CONST_MEM_EQ("HTTP/1.1", ptr, len)) {
155
+ /* HTTP/1.1 implies keepalive unless "Connection: close" is set */
156
+ HP_FL_SET(hp, KAVERSION);
157
+ v = g_http_11;
158
+ } else if (CONST_MEM_EQ("HTTP/1.0", ptr, len)) {
159
+ v = g_http_10;
160
+ } else {
161
+ v = rb_str_new(ptr, len);
162
+ }
163
+ rb_hash_aset(hp->env, g_server_protocol, v);
164
+ rb_hash_aset(hp->env, g_http_version, v);
165
+ }
166
+
167
+ static inline void hp_invalid_if_trailer(struct http_parser *hp)
168
+ {
169
+ if (HP_FL_TEST(hp, INTRAILER))
170
+ parser_raise(eHttpParserError, "invalid Trailer");
171
+ }
172
+
173
+ static void write_cont_value(struct http_parser *hp,
174
+ char *buffer, const char *p)
175
+ {
176
+ char *vptr;
177
+ long end;
178
+ long len = LEN(mark, p);
179
+ long cont_len;
180
+
181
+ if (hp->cont == Qfalse)
182
+ parser_raise(eHttpParserError, "invalid continuation line");
183
+ if (NIL_P(hp->cont))
184
+ return; /* we're ignoring this header (probably Host:) */
185
+
186
+ assert(TYPE(hp->cont) == T_STRING && "continuation line is not a string");
187
+ assert(hp->mark > 0 && "impossible continuation line offset");
188
+
189
+ if (len == 0)
190
+ return;
191
+
192
+ cont_len = RSTRING_LEN(hp->cont);
193
+ if (cont_len > 0) {
194
+ --hp->mark;
195
+ len = LEN(mark, p);
196
+ }
197
+ vptr = PTR_TO(mark);
198
+
199
+ /* normalize tab to space */
200
+ if (cont_len > 0) {
201
+ assert((' ' == *vptr || '\t' == *vptr) && "invalid leading white space");
202
+ *vptr = ' ';
203
+ }
204
+
205
+ for (end = len - 1; end >= 0 && is_lws(vptr[end]); end--);
206
+ rb_str_buf_cat(hp->cont, vptr, end + 1);
207
+ }
208
+
209
+ static void write_value(struct http_parser *hp,
210
+ const char *buffer, const char *p)
211
+ {
212
+ VALUE f = find_common_field(PTR_TO(start.field), hp->s.field_len);
213
+ VALUE v;
214
+ VALUE e;
215
+
216
+ VALIDATE_MAX_LENGTH(LEN(mark, p), FIELD_VALUE);
217
+ v = LEN(mark, p) == 0 ? rb_str_buf_new(128) : STRIPPED_STR_NEW(mark, p);
218
+ if (NIL_P(f)) {
219
+ const char *field = PTR_TO(start.field);
220
+ size_t flen = hp->s.field_len;
221
+
222
+ VALIDATE_MAX_LENGTH(flen, FIELD_NAME);
223
+
224
+ /*
225
+ * ignore "Version" headers since they conflict with the HTTP_VERSION
226
+ * rack env variable.
227
+ */
228
+ if (CONST_MEM_EQ("VERSION", field, flen)) {
229
+ hp->cont = Qnil;
230
+ return;
231
+ }
232
+ f = uncommon_field(field, flen);
233
+ } else if (f == g_http_connection) {
234
+ hp_keepalive_connection(hp, v);
235
+ } else if (f == g_content_length) {
236
+ hp->len.content = parse_length(RSTRING_PTR(v), RSTRING_LEN(v));
237
+ if (hp->len.content < 0)
238
+ parser_raise(eHttpParserError, "invalid Content-Length");
239
+ if (hp->len.content != 0)
240
+ HP_FL_SET(hp, HASBODY);
241
+ hp_invalid_if_trailer(hp);
242
+ } else if (f == g_http_transfer_encoding) {
243
+ if (STR_CSTR_CASE_EQ(v, "chunked")) {
244
+ HP_FL_SET(hp, CHUNKED);
245
+ HP_FL_SET(hp, HASBODY);
246
+ }
247
+ hp_invalid_if_trailer(hp);
248
+ } else if (f == g_http_trailer) {
249
+ HP_FL_SET(hp, HASTRAILER);
250
+ hp_invalid_if_trailer(hp);
251
+ } else {
252
+ assert(TYPE(f) == T_STRING && "memoized object is not a string");
253
+ assert_frozen(f);
254
+ }
255
+
256
+ e = rb_hash_aref(hp->env, f);
257
+ if (NIL_P(e)) {
258
+ hp->cont = rb_hash_aset(hp->env, f, v);
259
+ } else if (f == g_http_host) {
260
+ /*
261
+ * ignored, absolute URLs in REQUEST_URI take precedence over
262
+ * the Host: header (ref: rfc 2616, section 5.2.1)
263
+ */
264
+ hp->cont = Qnil;
265
+ } else {
266
+ rb_str_buf_cat(e, ",", 1);
267
+ hp->cont = rb_str_buf_append(e, v);
268
+ }
269
+ }
270
+
271
+ /** Machine **/
272
+
273
+ %%{
274
+ machine http_parser;
275
+
276
+ action mark {MARK(mark, fpc); }
277
+
278
+ action start_field { MARK(start.field, fpc); }
279
+ action snake_upcase_field { snake_upcase_char(deconst(fpc)); }
280
+ action downcase_char { downcase_char(deconst(fpc)); }
281
+ action write_field { hp->s.field_len = LEN(start.field, fpc); }
282
+ action start_value { MARK(mark, fpc); }
283
+ action write_value { write_value(hp, buffer, fpc); }
284
+ action write_cont_value { write_cont_value(hp, buffer, fpc); }
285
+ action request_method { request_method(hp, PTR_TO(mark), LEN(mark, fpc)); }
286
+ action scheme {
287
+ rb_hash_aset(hp->env, g_rack_url_scheme, STR_NEW(mark, fpc));
288
+ }
289
+ action host { rb_hash_aset(hp->env, g_http_host, STR_NEW(mark, fpc)); }
290
+ action request_uri {
291
+ VALUE str;
292
+
293
+ VALIDATE_MAX_URI_LENGTH(LEN(mark, fpc), REQUEST_URI);
294
+ str = rb_hash_aset(hp->env, g_request_uri, STR_NEW(mark, fpc));
295
+ /*
296
+ * "OPTIONS * HTTP/1.1\r\n" is a valid request, but we can't have '*'
297
+ * in REQUEST_PATH or PATH_INFO or else Rack::Lint will complain
298
+ */
299
+ if (STR_CSTR_EQ(str, "*")) {
300
+ str = rb_str_new(NULL, 0);
301
+ rb_hash_aset(hp->env, g_path_info, str);
302
+ rb_hash_aset(hp->env, g_request_path, str);
303
+ }
304
+ }
305
+ action fragment {
306
+ VALIDATE_MAX_URI_LENGTH(LEN(mark, fpc), FRAGMENT);
307
+ rb_hash_aset(hp->env, g_fragment, STR_NEW(mark, fpc));
308
+ }
309
+ action start_query {MARK(start.query, fpc); }
310
+ action query_string {
311
+ VALIDATE_MAX_URI_LENGTH(LEN(start.query, fpc), QUERY_STRING);
312
+ rb_hash_aset(hp->env, g_query_string, STR_NEW(start.query, fpc));
313
+ }
314
+ action http_version { http_version(hp, PTR_TO(mark), LEN(mark, fpc)); }
315
+ action request_path {
316
+ VALUE val;
317
+
318
+ VALIDATE_MAX_URI_LENGTH(LEN(mark, fpc), REQUEST_PATH);
319
+ val = rb_hash_aset(hp->env, g_request_path, STR_NEW(mark, fpc));
320
+
321
+ /* rack says PATH_INFO must start with "/" or be empty */
322
+ if (!STR_CSTR_EQ(val, "*"))
323
+ rb_hash_aset(hp->env, g_path_info, val);
324
+ }
325
+ action add_to_chunk_size {
326
+ hp->len.chunk = step_incr(hp->len.chunk, fc, 16);
327
+ if (hp->len.chunk < 0)
328
+ parser_raise(eHttpParserError, "invalid chunk size");
329
+ }
330
+ action header_done {
331
+ finalize_header(hp);
332
+
333
+ cs = http_parser_first_final;
334
+ if (HP_FL_TEST(hp, HASBODY)) {
335
+ HP_FL_SET(hp, INBODY);
336
+ if (HP_FL_TEST(hp, CHUNKED))
337
+ cs = http_parser_en_ChunkedBody;
338
+ } else {
339
+ HP_FL_SET(hp, REQEOF);
340
+ assert(!HP_FL_TEST(hp, CHUNKED) && "chunked encoding without body!");
341
+ }
342
+ /*
343
+ * go back to Ruby so we can call the Rack application, we'll reenter
344
+ * the parser iff the body needs to be processed.
345
+ */
346
+ goto post_exec;
347
+ }
348
+
349
+ action end_trailers {
350
+ cs = http_parser_first_final;
351
+ goto post_exec;
352
+ }
353
+
354
+ action end_chunked_body {
355
+ HP_FL_SET(hp, INTRAILER);
356
+ cs = http_parser_en_Trailers;
357
+ ++p;
358
+ assert(p <= pe && "buffer overflow after chunked body");
359
+ goto post_exec;
360
+ }
361
+
362
+ action skip_chunk_data {
363
+ skip_chunk_data_hack: {
364
+ size_t nr = MIN((size_t)hp->len.chunk, REMAINING);
365
+ memcpy(RSTRING_PTR(hp->cont) + hp->s.dest_offset, fpc, nr);
366
+ hp->s.dest_offset += nr;
367
+ hp->len.chunk -= nr;
368
+ p += nr;
369
+ assert(hp->len.chunk >= 0 && "negative chunk length");
370
+ if ((size_t)hp->len.chunk > REMAINING) {
371
+ HP_FL_SET(hp, INCHUNK);
372
+ goto post_exec;
373
+ } else {
374
+ fhold;
375
+ fgoto chunk_end;
376
+ }
377
+ }}
378
+
379
+ include unicorn_http_common "unicorn_http_common.rl";
380
+ }%%
381
+
382
+ /** Data **/
383
+ %% write data;
384
+
385
+ static void http_parser_init(struct http_parser *hp)
386
+ {
387
+ int cs = 0;
388
+ hp->flags = 0;
389
+ hp->mark = 0;
390
+ hp->offset = 0;
391
+ hp->start.field = 0;
392
+ hp->s.field_len = 0;
393
+ hp->len.content = 0;
394
+ hp->cont = Qfalse; /* zero on MRI, should be optimized away by above */
395
+ %% write init;
396
+ hp->cs = cs;
397
+ }
398
+
399
+ /** exec **/
400
+ static void
401
+ http_parser_execute(struct http_parser *hp, char *buffer, size_t len)
402
+ {
403
+ const char *p, *pe;
404
+ int cs = hp->cs;
405
+ size_t off = hp->offset;
406
+
407
+ if (cs == http_parser_first_final)
408
+ return;
409
+
410
+ assert(off <= len && "offset past end of buffer");
411
+
412
+ p = buffer+off;
413
+ pe = buffer+len;
414
+
415
+ assert((void *)(pe - p) == (void *)(len - off) &&
416
+ "pointers aren't same distance");
417
+
418
+ if (HP_FL_TEST(hp, INCHUNK)) {
419
+ HP_FL_UNSET(hp, INCHUNK);
420
+ goto skip_chunk_data_hack;
421
+ }
422
+ %% write exec;
423
+ post_exec: /* "_out:" also goes here */
424
+ if (hp->cs != http_parser_error)
425
+ hp->cs = cs;
426
+ hp->offset = ulong2uint(p - buffer);
427
+
428
+ assert(p <= pe && "buffer overflow after parsing execute");
429
+ assert(hp->offset <= len && "offset longer than length");
430
+ }
431
+
432
+ static struct http_parser *data_get(VALUE self)
433
+ {
434
+ struct http_parser *hp;
435
+
436
+ Data_Get_Struct(self, struct http_parser, hp);
437
+ assert(hp && "failed to extract http_parser struct");
438
+ return hp;
439
+ }
440
+
441
+ /*
442
+ * set rack.url_scheme to "https" or "http", no others are allowed by Rack
443
+ * this resembles the Rack::Request#scheme method as of rack commit
444
+ * 35bb5ba6746b5d346de9202c004cc926039650c7
445
+ */
446
+ static void set_url_scheme(VALUE env, VALUE *server_port)
447
+ {
448
+ VALUE scheme = rb_hash_aref(env, g_rack_url_scheme);
449
+
450
+ if (NIL_P(scheme)) {
451
+ /*
452
+ * would anybody be horribly opposed to removing the X-Forwarded-SSL
453
+ * and X-Forwarded-Proto handling from this parser? We've had it
454
+ * forever and nobody has said anything against it, either.
455
+ * Anyways, please send comments to our public mailing list:
456
+ * unicorn-public@bogomips.org (no HTML mail, no subscription necessary)
457
+ */
458
+ scheme = rb_hash_aref(env, g_http_x_forwarded_ssl);
459
+ if (!NIL_P(scheme) && STR_CSTR_EQ(scheme, "on")) {
460
+ *server_port = g_port_443;
461
+ scheme = g_https;
462
+ } else {
463
+ scheme = rb_hash_aref(env, g_http_x_forwarded_proto);
464
+ if (NIL_P(scheme)) {
465
+ scheme = g_http;
466
+ } else {
467
+ long len = RSTRING_LEN(scheme);
468
+ if (len >= 5 && !memcmp(RSTRING_PTR(scheme), "https", 5)) {
469
+ if (len != 5)
470
+ scheme = g_https;
471
+ *server_port = g_port_443;
472
+ } else {
473
+ scheme = g_http;
474
+ }
475
+ }
476
+ }
477
+ rb_hash_aset(env, g_rack_url_scheme, scheme);
478
+ } else if (STR_CSTR_EQ(scheme, "https")) {
479
+ *server_port = g_port_443;
480
+ } else {
481
+ assert(*server_port == g_port_80 && "server_port not set");
482
+ }
483
+ }
484
+
485
+ /*
486
+ * Parse and set the SERVER_NAME and SERVER_PORT variables
487
+ * Not supporting X-Forwarded-Host/X-Forwarded-Port in here since
488
+ * anybody who needs them is using an unsupported configuration and/or
489
+ * incompetent. Rack::Request will handle X-Forwarded-{Port,Host} just
490
+ * fine.
491
+ */
492
+ static void set_server_vars(VALUE env, VALUE *server_port)
493
+ {
494
+ VALUE server_name = g_localhost;
495
+ VALUE host = rb_hash_aref(env, g_http_host);
496
+
497
+ if (!NIL_P(host)) {
498
+ char *host_ptr = RSTRING_PTR(host);
499
+ long host_len = RSTRING_LEN(host);
500
+ char *colon;
501
+
502
+ if (*host_ptr == '[') { /* ipv6 address format */
503
+ char *rbracket = memchr(host_ptr + 1, ']', host_len - 1);
504
+
505
+ if (rbracket)
506
+ colon = (rbracket[1] == ':') ? rbracket + 1 : NULL;
507
+ else
508
+ colon = memchr(host_ptr + 1, ':', host_len - 1);
509
+ } else {
510
+ colon = memchr(host_ptr, ':', host_len);
511
+ }
512
+
513
+ if (colon) {
514
+ long port_start = colon - host_ptr + 1;
515
+
516
+ server_name = rb_str_substr(host, 0, colon - host_ptr);
517
+ if ((host_len - port_start) > 0)
518
+ *server_port = rb_str_substr(host, port_start, host_len);
519
+ } else {
520
+ server_name = host;
521
+ }
522
+ }
523
+ rb_hash_aset(env, g_server_name, server_name);
524
+ rb_hash_aset(env, g_server_port, *server_port);
525
+ }
526
+
527
+ static void finalize_header(struct http_parser *hp)
528
+ {
529
+ VALUE server_port = g_port_80;
530
+
531
+ set_url_scheme(hp->env, &server_port);
532
+ set_server_vars(hp->env, &server_port);
533
+
534
+ if (!HP_FL_TEST(hp, HASHEADER))
535
+ rb_hash_aset(hp->env, g_server_protocol, g_http_09);
536
+
537
+ /* rack requires QUERY_STRING */
538
+ if (NIL_P(rb_hash_aref(hp->env, g_query_string)))
539
+ rb_hash_aset(hp->env, g_query_string, rb_str_new(NULL, 0));
540
+ }
541
+
542
+ static void hp_mark(void *ptr)
543
+ {
544
+ struct http_parser *hp = ptr;
545
+
546
+ rb_gc_mark(hp->buf);
547
+ rb_gc_mark(hp->env);
548
+ rb_gc_mark(hp->cont);
549
+ }
550
+
551
+ static VALUE HttpParser_alloc(VALUE klass)
552
+ {
553
+ struct http_parser *hp;
554
+ return Data_Make_Struct(klass, struct http_parser, hp_mark, -1, hp);
555
+ }
556
+
557
+
558
+ /**
559
+ * call-seq:
560
+ * parser.new => parser
561
+ *
562
+ * Creates a new parser.
563
+ */
564
+ static VALUE HttpParser_init(VALUE self)
565
+ {
566
+ struct http_parser *hp = data_get(self);
567
+
568
+ http_parser_init(hp);
569
+ hp->buf = rb_str_new(NULL, 0);
570
+ hp->env = rb_hash_new();
571
+
572
+ return self;
573
+ }
574
+
575
+ /**
576
+ * call-seq:
577
+ * parser.clear => parser
578
+ *
579
+ * Resets the parser to it's initial state so that you can reuse it
580
+ * rather than making new ones.
581
+ */
582
+ static VALUE HttpParser_clear(VALUE self)
583
+ {
584
+ struct http_parser *hp = data_get(self);
585
+
586
+ http_parser_init(hp);
587
+ rb_funcall(hp->env, id_clear, 0);
588
+ rb_ivar_set(self, id_response_start_sent, Qfalse);
589
+
590
+ return self;
591
+ }
592
+
593
+ static void advance_str(VALUE str, off_t nr)
594
+ {
595
+ long len = RSTRING_LEN(str);
596
+
597
+ if (len == 0)
598
+ return;
599
+
600
+ rb_str_modify(str);
601
+
602
+ assert(nr <= len && "trying to advance past end of buffer");
603
+ len -= nr;
604
+ if (len > 0) /* unlikely, len is usually 0 */
605
+ memmove(RSTRING_PTR(str), RSTRING_PTR(str) + nr, len);
606
+ rb_str_set_len(str, len);
607
+ }
608
+
609
+ /**
610
+ * call-seq:
611
+ * parser.content_length => nil or Integer
612
+ *
613
+ * Returns the number of bytes left to run through HttpParser#filter_body.
614
+ * This will initially be the value of the "Content-Length" HTTP header
615
+ * after header parsing is complete and will decrease in value as
616
+ * HttpParser#filter_body is called for each chunk. This should return
617
+ * zero for requests with no body.
618
+ *
619
+ * This will return nil on "Transfer-Encoding: chunked" requests.
620
+ */
621
+ static VALUE HttpParser_content_length(VALUE self)
622
+ {
623
+ struct http_parser *hp = data_get(self);
624
+
625
+ return HP_FL_TEST(hp, CHUNKED) ? Qnil : OFFT2NUM(hp->len.content);
626
+ }
627
+
628
+ /**
629
+ * Document-method: parse
630
+ * call-seq:
631
+ * parser.parse => env or nil
632
+ *
633
+ * Takes a Hash and a String of data, parses the String of data filling
634
+ * in the Hash returning the Hash if parsing is finished, nil otherwise
635
+ * When returning the env Hash, it may modify data to point to where
636
+ * body processing should begin.
637
+ *
638
+ * Raises HttpParserError if there are parsing errors.
639
+ */
640
+ static VALUE HttpParser_parse(VALUE self)
641
+ {
642
+ struct http_parser *hp = data_get(self);
643
+ VALUE data = hp->buf;
644
+
645
+ if (HP_FL_TEST(hp, TO_CLEAR))
646
+ HttpParser_clear(self);
647
+
648
+ http_parser_execute(hp, RSTRING_PTR(data), RSTRING_LEN(data));
649
+ if (hp->offset > MAX_HEADER_LEN)
650
+ parser_raise(e413, "HTTP header is too large");
651
+
652
+ if (hp->cs == http_parser_first_final ||
653
+ hp->cs == http_parser_en_ChunkedBody) {
654
+ advance_str(data, hp->offset + 1);
655
+ hp->offset = 0;
656
+ if (HP_FL_TEST(hp, INTRAILER))
657
+ HP_FL_SET(hp, REQEOF);
658
+
659
+ return hp->env;
660
+ }
661
+
662
+ if (hp->cs == http_parser_error)
663
+ parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
664
+
665
+ return Qnil;
666
+ }
667
+
668
+ /**
669
+ * Document-method: parse
670
+ * call-seq:
671
+ * parser.add_parse(buffer) => env or nil
672
+ *
673
+ * adds the contents of +buffer+ to the internal buffer and attempts to
674
+ * continue parsing. Returns the +env+ Hash on success or nil if more
675
+ * data is needed.
676
+ *
677
+ * Raises HttpParserError if there are parsing errors.
678
+ */
679
+ static VALUE HttpParser_add_parse(VALUE self, VALUE buffer)
680
+ {
681
+ struct http_parser *hp = data_get(self);
682
+
683
+ Check_Type(buffer, T_STRING);
684
+ rb_str_buf_append(hp->buf, buffer);
685
+
686
+ return HttpParser_parse(self);
687
+ }
688
+
689
+ /**
690
+ * Document-method: trailers
691
+ * call-seq:
692
+ * parser.trailers(req, data) => req or nil
693
+ *
694
+ * This is an alias for HttpParser#headers
695
+ */
696
+
697
+ /**
698
+ * Document-method: headers
699
+ */
700
+ static VALUE HttpParser_headers(VALUE self, VALUE env, VALUE buf)
701
+ {
702
+ struct http_parser *hp = data_get(self);
703
+
704
+ hp->env = env;
705
+ hp->buf = buf;
706
+
707
+ return HttpParser_parse(self);
708
+ }
709
+
710
+ static int chunked_eof(struct http_parser *hp)
711
+ {
712
+ return ((hp->cs == http_parser_first_final) || HP_FL_TEST(hp, INTRAILER));
713
+ }
714
+
715
+ /**
716
+ * call-seq:
717
+ * parser.body_eof? => true or false
718
+ *
719
+ * Detects if we're done filtering the body or not. This can be used
720
+ * to detect when to stop calling HttpParser#filter_body.
721
+ */
722
+ static VALUE HttpParser_body_eof(VALUE self)
723
+ {
724
+ struct http_parser *hp = data_get(self);
725
+
726
+ if (HP_FL_TEST(hp, CHUNKED))
727
+ return chunked_eof(hp) ? Qtrue : Qfalse;
728
+
729
+ return hp->len.content == 0 ? Qtrue : Qfalse;
730
+ }
731
+
732
+ /**
733
+ * call-seq:
734
+ * parser.keepalive? => true or false
735
+ *
736
+ * This should be used to detect if a request can really handle
737
+ * keepalives and pipelining. Currently, the rules are:
738
+ *
739
+ * 1. MUST be a GET or HEAD request
740
+ * 2. MUST be HTTP/1.1 +or+ HTTP/1.0 with "Connection: keep-alive"
741
+ * 3. MUST NOT have "Connection: close" set
742
+ */
743
+ static VALUE HttpParser_keepalive(VALUE self)
744
+ {
745
+ struct http_parser *hp = data_get(self);
746
+
747
+ return HP_FL_ALL(hp, KEEPALIVE) ? Qtrue : Qfalse;
748
+ }
749
+
750
+ /**
751
+ * call-seq:
752
+ * parser.next? => true or false
753
+ *
754
+ * Exactly like HttpParser#keepalive?, except it will reset the internal
755
+ * parser state on next parse if it returns true.
756
+ */
757
+ static VALUE HttpParser_next(VALUE self)
758
+ {
759
+ struct http_parser *hp = data_get(self);
760
+
761
+ if (HP_FL_ALL(hp, KEEPALIVE)) {
762
+ HP_FL_SET(hp, TO_CLEAR);
763
+ return Qtrue;
764
+ }
765
+ return Qfalse;
766
+ }
767
+
768
+ /**
769
+ * call-seq:
770
+ * parser.headers? => true or false
771
+ *
772
+ * This should be used to detect if a request has headers (and if
773
+ * the response will have headers as well). HTTP/0.9 requests
774
+ * should return false, all subsequent HTTP versions will return true
775
+ */
776
+ static VALUE HttpParser_has_headers(VALUE self)
777
+ {
778
+ struct http_parser *hp = data_get(self);
779
+
780
+ return HP_FL_TEST(hp, HASHEADER) ? Qtrue : Qfalse;
781
+ }
782
+
783
+ static VALUE HttpParser_buf(VALUE self)
784
+ {
785
+ return data_get(self)->buf;
786
+ }
787
+
788
+ static VALUE HttpParser_env(VALUE self)
789
+ {
790
+ return data_get(self)->env;
791
+ }
792
+
793
+ /**
794
+ * call-seq:
795
+ * parser.filter_body(dst, src) => nil/src
796
+ *
797
+ * Takes a String of +src+, will modify data if dechunking is done.
798
+ * Returns +nil+ if there is more data left to process. Returns
799
+ * +src+ if body processing is complete. When returning +src+,
800
+ * it may modify +src+ so the start of the string points to where
801
+ * the body ended so that trailer processing can begin.
802
+ *
803
+ * Raises HttpParserError if there are dechunking errors.
804
+ * Basically this is a glorified memcpy(3) that copies +src+
805
+ * into +buf+ while filtering it through the dechunker.
806
+ */
807
+ static VALUE HttpParser_filter_body(VALUE self, VALUE dst, VALUE src)
808
+ {
809
+ struct http_parser *hp = data_get(self);
810
+ char *srcptr;
811
+ long srclen;
812
+
813
+ srcptr = RSTRING_PTR(src);
814
+ srclen = RSTRING_LEN(src);
815
+
816
+ StringValue(dst);
817
+
818
+ if (HP_FL_TEST(hp, CHUNKED)) {
819
+ if (!chunked_eof(hp)) {
820
+ rb_str_modify(dst);
821
+ rb_str_resize(dst, srclen); /* we can never copy more than srclen bytes */
822
+
823
+ hp->s.dest_offset = 0;
824
+ hp->cont = dst;
825
+ hp->buf = src;
826
+ http_parser_execute(hp, srcptr, srclen);
827
+ if (hp->cs == http_parser_error)
828
+ parser_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
829
+
830
+ assert(hp->s.dest_offset <= hp->offset &&
831
+ "destination buffer overflow");
832
+ advance_str(src, hp->offset);
833
+ rb_str_set_len(dst, hp->s.dest_offset);
834
+
835
+ if (RSTRING_LEN(dst) == 0 && chunked_eof(hp)) {
836
+ assert(hp->len.chunk == 0 && "chunk at EOF but more to parse");
837
+ } else {
838
+ src = Qnil;
839
+ }
840
+ }
841
+ } else {
842
+ /* no need to enter the Ragel machine for unchunked transfers */
843
+ assert(hp->len.content >= 0 && "negative Content-Length");
844
+ if (hp->len.content > 0) {
845
+ long nr = MIN(srclen, hp->len.content);
846
+
847
+ rb_str_modify(dst);
848
+ rb_str_resize(dst, nr);
849
+ /*
850
+ * using rb_str_replace() to avoid memcpy() doesn't help in
851
+ * most cases because a GC-aware programmer will pass an explicit
852
+ * buffer to env["rack.input"].read and reuse the buffer in a loop.
853
+ * This causes copy-on-write behavior to be triggered anyways
854
+ * when the +src+ buffer is modified (when reading off the socket).
855
+ */
856
+ hp->buf = src;
857
+ memcpy(RSTRING_PTR(dst), srcptr, nr);
858
+ hp->len.content -= nr;
859
+ if (hp->len.content == 0) {
860
+ HP_FL_SET(hp, REQEOF);
861
+ hp->cs = http_parser_first_final;
862
+ }
863
+ advance_str(src, nr);
864
+ src = Qnil;
865
+ }
866
+ }
867
+ hp->offset = 0; /* for trailer parsing */
868
+ return src;
869
+ }
870
+
871
+ #define SET_GLOBAL(var,str) do { \
872
+ var = find_common_field(str, sizeof(str) - 1); \
873
+ assert(!NIL_P(var) && "missed global field"); \
874
+ } while (0)
875
+
876
+ void Init_unicorn_http(void)
877
+ {
878
+ VALUE mUnicorn, cHttpParser;
879
+
880
+ mUnicorn = rb_define_module("Unicorn");
881
+ cHttpParser = rb_define_class_under(mUnicorn, "HttpParser", rb_cObject);
882
+ eHttpParserError =
883
+ rb_define_class_under(mUnicorn, "HttpParserError", rb_eIOError);
884
+ e413 = rb_define_class_under(mUnicorn, "RequestEntityTooLargeError",
885
+ eHttpParserError);
886
+ e414 = rb_define_class_under(mUnicorn, "RequestURITooLongError",
887
+ eHttpParserError);
888
+
889
+ init_globals();
890
+ rb_define_alloc_func(cHttpParser, HttpParser_alloc);
891
+ rb_define_method(cHttpParser, "initialize", HttpParser_init, 0);
892
+ rb_define_method(cHttpParser, "clear", HttpParser_clear, 0);
893
+ rb_define_method(cHttpParser, "parse", HttpParser_parse, 0);
894
+ rb_define_method(cHttpParser, "add_parse", HttpParser_add_parse, 1);
895
+ rb_define_method(cHttpParser, "headers", HttpParser_headers, 2);
896
+ rb_define_method(cHttpParser, "trailers", HttpParser_headers, 2);
897
+ rb_define_method(cHttpParser, "filter_body", HttpParser_filter_body, 2);
898
+ rb_define_method(cHttpParser, "content_length", HttpParser_content_length, 0);
899
+ rb_define_method(cHttpParser, "body_eof?", HttpParser_body_eof, 0);
900
+ rb_define_method(cHttpParser, "keepalive?", HttpParser_keepalive, 0);
901
+ rb_define_method(cHttpParser, "headers?", HttpParser_has_headers, 0);
902
+ rb_define_method(cHttpParser, "next?", HttpParser_next, 0);
903
+ rb_define_method(cHttpParser, "buf", HttpParser_buf, 0);
904
+ rb_define_method(cHttpParser, "env", HttpParser_env, 0);
905
+
906
+ /*
907
+ * The maximum size a single chunk when using chunked transfer encoding.
908
+ * This is only a theoretical maximum used to detect errors in clients,
909
+ * it is highly unlikely to encounter clients that send more than
910
+ * several kilobytes at once.
911
+ */
912
+ rb_define_const(cHttpParser, "CHUNK_MAX", OFFT2NUM(UH_OFF_T_MAX));
913
+
914
+ /*
915
+ * The maximum size of the body as specified by Content-Length.
916
+ * This is only a theoretical maximum, the actual limit is subject
917
+ * to the limits of the file system used for +Dir.tmpdir+.
918
+ */
919
+ rb_define_const(cHttpParser, "LENGTH_MAX", OFFT2NUM(UH_OFF_T_MAX));
920
+
921
+ rb_define_singleton_method(cHttpParser, "max_header_len=", set_maxhdrlen, 1);
922
+
923
+ init_common_fields();
924
+ SET_GLOBAL(g_http_host, "HOST");
925
+ SET_GLOBAL(g_http_trailer, "TRAILER");
926
+ SET_GLOBAL(g_http_transfer_encoding, "TRANSFER_ENCODING");
927
+ SET_GLOBAL(g_content_length, "CONTENT_LENGTH");
928
+ SET_GLOBAL(g_http_connection, "CONNECTION");
929
+ id_clear = rb_intern("clear");
930
+ id_set_backtrace = rb_intern("set_backtrace");
931
+ id_response_start_sent = rb_intern("@response_start_sent");
932
+ init_unicorn_httpdate();
933
+ }
934
+ #undef SET_GLOBAL