unicorn-lb-patch 4.3.1.11.g21b8.dirty

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