unicorn-fotopedia 0.99.1

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 (163) hide show
  1. data/.CHANGELOG.old +25 -0
  2. data/.document +19 -0
  3. data/.gitignore +21 -0
  4. data/.mailmap +26 -0
  5. data/CONTRIBUTORS +32 -0
  6. data/COPYING +339 -0
  7. data/DESIGN +105 -0
  8. data/Documentation/.gitignore +5 -0
  9. data/Documentation/GNUmakefile +30 -0
  10. data/Documentation/unicorn.1.txt +171 -0
  11. data/Documentation/unicorn_rails.1.txt +172 -0
  12. data/FAQ +52 -0
  13. data/GIT-VERSION-GEN +40 -0
  14. data/GNUmakefile +292 -0
  15. data/HACKING +116 -0
  16. data/ISSUES +36 -0
  17. data/KNOWN_ISSUES +50 -0
  18. data/LICENSE +55 -0
  19. data/PHILOSOPHY +145 -0
  20. data/README +149 -0
  21. data/Rakefile +191 -0
  22. data/SIGNALS +109 -0
  23. data/Sandbox +78 -0
  24. data/TODO +5 -0
  25. data/TUNING +70 -0
  26. data/bin/unicorn +126 -0
  27. data/bin/unicorn_rails +203 -0
  28. data/examples/big_app_gc.rb +33 -0
  29. data/examples/echo.ru +27 -0
  30. data/examples/git.ru +13 -0
  31. data/examples/init.sh +58 -0
  32. data/examples/logger_mp_safe.rb +25 -0
  33. data/examples/nginx.conf +139 -0
  34. data/examples/unicorn.conf.rb +78 -0
  35. data/ext/unicorn_http/CFLAGS +13 -0
  36. data/ext/unicorn_http/c_util.h +124 -0
  37. data/ext/unicorn_http/common_field_optimization.h +111 -0
  38. data/ext/unicorn_http/ext_help.h +77 -0
  39. data/ext/unicorn_http/extconf.rb +14 -0
  40. data/ext/unicorn_http/global_variables.h +89 -0
  41. data/ext/unicorn_http/unicorn_http.rl +714 -0
  42. data/ext/unicorn_http/unicorn_http_common.rl +75 -0
  43. data/lib/unicorn.rb +847 -0
  44. data/lib/unicorn/app/exec_cgi.rb +150 -0
  45. data/lib/unicorn/app/inetd.rb +109 -0
  46. data/lib/unicorn/app/old_rails.rb +33 -0
  47. data/lib/unicorn/app/old_rails/static.rb +58 -0
  48. data/lib/unicorn/cgi_wrapper.rb +145 -0
  49. data/lib/unicorn/configurator.rb +421 -0
  50. data/lib/unicorn/const.rb +34 -0
  51. data/lib/unicorn/http_request.rb +72 -0
  52. data/lib/unicorn/http_response.rb +75 -0
  53. data/lib/unicorn/launcher.rb +65 -0
  54. data/lib/unicorn/oob_gc.rb +58 -0
  55. data/lib/unicorn/socket_helper.rb +152 -0
  56. data/lib/unicorn/tee_input.rb +217 -0
  57. data/lib/unicorn/util.rb +90 -0
  58. data/local.mk.sample +62 -0
  59. data/setup.rb +1586 -0
  60. data/t/.gitignore +2 -0
  61. data/t/GNUmakefile +67 -0
  62. data/t/README +42 -0
  63. data/t/bin/content-md5-put +36 -0
  64. data/t/bin/sha1sum.rb +23 -0
  65. data/t/bin/unused_listen +40 -0
  66. data/t/bin/utee +12 -0
  67. data/t/env.ru +3 -0
  68. data/t/my-tap-lib.sh +200 -0
  69. data/t/t0000-http-basic.sh +50 -0
  70. data/t/t0001-reload-bad-config.sh +52 -0
  71. data/t/t0002-config-conflict.sh +49 -0
  72. data/t/test-lib.sh +100 -0
  73. data/test/aggregate.rb +15 -0
  74. data/test/benchmark/README +50 -0
  75. data/test/benchmark/dd.ru +18 -0
  76. data/test/exec/README +5 -0
  77. data/test/exec/test_exec.rb +1038 -0
  78. data/test/rails/app-1.2.3/.gitignore +2 -0
  79. data/test/rails/app-1.2.3/Rakefile +7 -0
  80. data/test/rails/app-1.2.3/app/controllers/application.rb +6 -0
  81. data/test/rails/app-1.2.3/app/controllers/foo_controller.rb +36 -0
  82. data/test/rails/app-1.2.3/app/helpers/application_helper.rb +4 -0
  83. data/test/rails/app-1.2.3/config/boot.rb +11 -0
  84. data/test/rails/app-1.2.3/config/database.yml +12 -0
  85. data/test/rails/app-1.2.3/config/environment.rb +13 -0
  86. data/test/rails/app-1.2.3/config/environments/development.rb +9 -0
  87. data/test/rails/app-1.2.3/config/environments/production.rb +5 -0
  88. data/test/rails/app-1.2.3/config/routes.rb +6 -0
  89. data/test/rails/app-1.2.3/db/.gitignore +0 -0
  90. data/test/rails/app-1.2.3/public/404.html +1 -0
  91. data/test/rails/app-1.2.3/public/500.html +1 -0
  92. data/test/rails/app-2.0.2/.gitignore +2 -0
  93. data/test/rails/app-2.0.2/Rakefile +7 -0
  94. data/test/rails/app-2.0.2/app/controllers/application.rb +4 -0
  95. data/test/rails/app-2.0.2/app/controllers/foo_controller.rb +36 -0
  96. data/test/rails/app-2.0.2/app/helpers/application_helper.rb +4 -0
  97. data/test/rails/app-2.0.2/config/boot.rb +11 -0
  98. data/test/rails/app-2.0.2/config/database.yml +12 -0
  99. data/test/rails/app-2.0.2/config/environment.rb +17 -0
  100. data/test/rails/app-2.0.2/config/environments/development.rb +8 -0
  101. data/test/rails/app-2.0.2/config/environments/production.rb +5 -0
  102. data/test/rails/app-2.0.2/config/routes.rb +6 -0
  103. data/test/rails/app-2.0.2/db/.gitignore +0 -0
  104. data/test/rails/app-2.0.2/public/404.html +1 -0
  105. data/test/rails/app-2.0.2/public/500.html +1 -0
  106. data/test/rails/app-2.1.2/.gitignore +2 -0
  107. data/test/rails/app-2.1.2/Rakefile +7 -0
  108. data/test/rails/app-2.1.2/app/controllers/application.rb +4 -0
  109. data/test/rails/app-2.1.2/app/controllers/foo_controller.rb +36 -0
  110. data/test/rails/app-2.1.2/app/helpers/application_helper.rb +4 -0
  111. data/test/rails/app-2.1.2/config/boot.rb +111 -0
  112. data/test/rails/app-2.1.2/config/database.yml +12 -0
  113. data/test/rails/app-2.1.2/config/environment.rb +17 -0
  114. data/test/rails/app-2.1.2/config/environments/development.rb +7 -0
  115. data/test/rails/app-2.1.2/config/environments/production.rb +5 -0
  116. data/test/rails/app-2.1.2/config/routes.rb +6 -0
  117. data/test/rails/app-2.1.2/db/.gitignore +0 -0
  118. data/test/rails/app-2.1.2/public/404.html +1 -0
  119. data/test/rails/app-2.1.2/public/500.html +1 -0
  120. data/test/rails/app-2.2.2/.gitignore +2 -0
  121. data/test/rails/app-2.2.2/Rakefile +7 -0
  122. data/test/rails/app-2.2.2/app/controllers/application.rb +4 -0
  123. data/test/rails/app-2.2.2/app/controllers/foo_controller.rb +36 -0
  124. data/test/rails/app-2.2.2/app/helpers/application_helper.rb +4 -0
  125. data/test/rails/app-2.2.2/config/boot.rb +111 -0
  126. data/test/rails/app-2.2.2/config/database.yml +12 -0
  127. data/test/rails/app-2.2.2/config/environment.rb +17 -0
  128. data/test/rails/app-2.2.2/config/environments/development.rb +7 -0
  129. data/test/rails/app-2.2.2/config/environments/production.rb +5 -0
  130. data/test/rails/app-2.2.2/config/routes.rb +6 -0
  131. data/test/rails/app-2.2.2/db/.gitignore +0 -0
  132. data/test/rails/app-2.2.2/public/404.html +1 -0
  133. data/test/rails/app-2.2.2/public/500.html +1 -0
  134. data/test/rails/app-2.3.5/.gitignore +2 -0
  135. data/test/rails/app-2.3.5/Rakefile +7 -0
  136. data/test/rails/app-2.3.5/app/controllers/application_controller.rb +5 -0
  137. data/test/rails/app-2.3.5/app/controllers/foo_controller.rb +36 -0
  138. data/test/rails/app-2.3.5/app/helpers/application_helper.rb +4 -0
  139. data/test/rails/app-2.3.5/config/boot.rb +109 -0
  140. data/test/rails/app-2.3.5/config/database.yml +12 -0
  141. data/test/rails/app-2.3.5/config/environment.rb +17 -0
  142. data/test/rails/app-2.3.5/config/environments/development.rb +7 -0
  143. data/test/rails/app-2.3.5/config/environments/production.rb +6 -0
  144. data/test/rails/app-2.3.5/config/routes.rb +6 -0
  145. data/test/rails/app-2.3.5/db/.gitignore +0 -0
  146. data/test/rails/app-2.3.5/public/404.html +1 -0
  147. data/test/rails/app-2.3.5/public/500.html +1 -0
  148. data/test/rails/app-2.3.5/public/x.txt +1 -0
  149. data/test/rails/test_rails.rb +280 -0
  150. data/test/test_helper.rb +301 -0
  151. data/test/unit/test_configurator.rb +150 -0
  152. data/test/unit/test_http_parser.rb +555 -0
  153. data/test/unit/test_http_parser_ng.rb +443 -0
  154. data/test/unit/test_request.rb +184 -0
  155. data/test/unit/test_response.rb +110 -0
  156. data/test/unit/test_server.rb +291 -0
  157. data/test/unit/test_signals.rb +206 -0
  158. data/test/unit/test_socket_helper.rb +147 -0
  159. data/test/unit/test_tee_input.rb +257 -0
  160. data/test/unit/test_upload.rb +298 -0
  161. data/test/unit/test_util.rb +96 -0
  162. data/unicorn.gemspec +52 -0
  163. metadata +283 -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,77 @@
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 RUBINIUS
12
+ # define rb_str_update(x) do {} while (0)
13
+ # define rb_str_flush(x) do {} while (0)
14
+ #endif /* !RUBINIUS */
15
+
16
+ #ifndef HAVE_RB_STR_SET_LEN
17
+ # ifdef RUBINIUS
18
+ # define rb_str_set_len(str,len) rb_str_resize(str,len)
19
+ # else /* 1.8.6 optimized version */
20
+ /* this is taken from Ruby 1.8.7, 1.8.6 may not have it */
21
+ static void rb_18_str_set_len(VALUE str, long len)
22
+ {
23
+ RSTRING(str)->len = len;
24
+ RSTRING(str)->ptr[len] = '\0';
25
+ rb_str_flush(str);
26
+ }
27
+ # define rb_str_set_len(str,len) rb_18_str_set_len(str,len)
28
+ # endif /* ! RUBINIUS */
29
+ #endif /* !defined(HAVE_RB_STR_SET_LEN) */
30
+
31
+ /* not all Ruby implementations support frozen objects (Rubinius does not) */
32
+ #if defined(OBJ_FROZEN)
33
+ # define assert_frozen(f) assert(OBJ_FROZEN(f) && "unfrozen object")
34
+ #else
35
+ # define assert_frozen(f) do {} while (0)
36
+ #endif /* !defined(OBJ_FROZEN) */
37
+
38
+ #if !defined(OFFT2NUM)
39
+ # if SIZEOF_OFF_T == SIZEOF_LONG
40
+ # define OFFT2NUM(n) LONG2NUM(n)
41
+ # else
42
+ # define OFFT2NUM(n) LL2NUM(n)
43
+ # endif
44
+ #endif /* ! defined(OFFT2NUM) */
45
+
46
+ #ifndef HAVE_RB_STR_MODIFY
47
+ # define rb_str_modify(x) do {} while (0)
48
+ #endif /* ! defined(HAVE_RB_STR_MODIFY) */
49
+
50
+ static inline int str_cstr_eq(VALUE val, const char *ptr, long len)
51
+ {
52
+ return (RSTRING_LEN(val) == len && !memcmp(ptr, RSTRING_PTR(val), len));
53
+ }
54
+
55
+ #define STR_CSTR_EQ(val, const_str) \
56
+ str_cstr_eq(val, const_str, sizeof(const_str) - 1)
57
+
58
+ /* strcasecmp isn't locale independent */
59
+ static int str_cstr_case_eq(VALUE val, const char *ptr, long len)
60
+ {
61
+ if (RSTRING_LEN(val) == len) {
62
+ const char *v = RSTRING_PTR(val);
63
+
64
+ for (; len--; ++ptr, ++v) {
65
+ if ((*ptr == *v) || (*v >= 'A' && *v <= 'Z' && (*v | 0x20) == *ptr))
66
+ continue;
67
+ return 0;
68
+ }
69
+ return 1;
70
+ }
71
+ return 0;
72
+ }
73
+
74
+ #define STR_CSTR_CASE_EQ(val, const_str) \
75
+ str_cstr_case_eq(val, const_str, sizeof(const_str) - 1)
76
+
77
+ #endif /* ext_help_h */
@@ -0,0 +1,14 @@
1
+ # -*- encoding: binary -*-
2
+ require 'mkmf'
3
+
4
+ dir_config("unicorn_http")
5
+
6
+ have_macro("SIZEOF_OFF_T", "ruby.h") or check_sizeof("off_t", "sys/types.h")
7
+ have_macro("SIZEOF_LONG", "ruby.h") or check_sizeof("long", "sys/types.h")
8
+ have_func("rb_str_set_len", "ruby.h")
9
+ have_func("rb_str_modify", "ruby.h")
10
+
11
+ # -fPIC is needed for Rubinius, MRI already uses it regardless
12
+ with_cflags($CFLAGS + " -fPIC ") do
13
+ create_makefile("unicorn_http")
14
+ end
@@ -0,0 +1,89 @@
1
+ #ifndef global_variables_h
2
+ #define global_variables_h
3
+ static VALUE eHttpParserError;
4
+
5
+ static VALUE g_rack_url_scheme;
6
+ static VALUE g_request_method;
7
+ static VALUE g_request_uri;
8
+ static VALUE g_fragment;
9
+ static VALUE g_query_string;
10
+ static VALUE g_http_version;
11
+ static VALUE g_request_path;
12
+ static VALUE g_path_info;
13
+ static VALUE g_server_name;
14
+ static VALUE g_server_port;
15
+ static VALUE g_server_protocol;
16
+ static VALUE g_http_host;
17
+ static VALUE g_http_x_forwarded_proto;
18
+ static VALUE g_http_transfer_encoding;
19
+ static VALUE g_content_length;
20
+ static VALUE g_http_trailer;
21
+ static VALUE g_http_connection;
22
+ static VALUE g_port_80;
23
+ static VALUE g_port_443;
24
+ static VALUE g_localhost;
25
+ static VALUE g_http;
26
+ static VALUE g_http_09;
27
+ static VALUE g_http_10;
28
+ static VALUE g_http_11;
29
+ static VALUE g_GET;
30
+ static VALUE g_HEAD;
31
+
32
+ /** Defines common length and error messages for input length validation. */
33
+ #define DEF_MAX_LENGTH(N, length) \
34
+ static const size_t MAX_##N##_LENGTH = length; \
35
+ static const char * const MAX_##N##_LENGTH_ERR = \
36
+ "HTTP element " # N " is longer than the " # length " allowed length."
37
+
38
+ /**
39
+ * Validates the max length of given input and throws an HttpParserError
40
+ * exception if over.
41
+ */
42
+ #define VALIDATE_MAX_LENGTH(len, N) do { \
43
+ if (len > MAX_##N##_LENGTH) \
44
+ rb_raise(eHttpParserError, MAX_##N##_LENGTH_ERR); \
45
+ } while (0)
46
+
47
+ /** Defines global strings in the init method. */
48
+ #define DEF_GLOBAL(N, val) do { \
49
+ g_##N = rb_obj_freeze(rb_str_new(val, sizeof(val) - 1)); \
50
+ rb_global_variable(&g_##N); \
51
+ } while (0)
52
+
53
+ /* Defines the maximum allowed lengths for various input elements.*/
54
+ DEF_MAX_LENGTH(FIELD_NAME, 256);
55
+ DEF_MAX_LENGTH(FIELD_VALUE, 80 * 1024);
56
+ DEF_MAX_LENGTH(REQUEST_URI, 1024 * 12);
57
+ DEF_MAX_LENGTH(FRAGMENT, 1024); /* Don't know if this length is specified somewhere or not */
58
+ DEF_MAX_LENGTH(REQUEST_PATH, 1024);
59
+ DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10));
60
+ DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32)));
61
+
62
+ static void init_globals(void)
63
+ {
64
+ DEF_GLOBAL(rack_url_scheme, "rack.url_scheme");
65
+ DEF_GLOBAL(request_method, "REQUEST_METHOD");
66
+ DEF_GLOBAL(request_uri, "REQUEST_URI");
67
+ DEF_GLOBAL(fragment, "FRAGMENT");
68
+ DEF_GLOBAL(query_string, "QUERY_STRING");
69
+ DEF_GLOBAL(http_version, "HTTP_VERSION");
70
+ DEF_GLOBAL(request_path, "REQUEST_PATH");
71
+ DEF_GLOBAL(path_info, "PATH_INFO");
72
+ DEF_GLOBAL(server_name, "SERVER_NAME");
73
+ DEF_GLOBAL(server_port, "SERVER_PORT");
74
+ DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL");
75
+ DEF_GLOBAL(http_x_forwarded_proto, "HTTP_X_FORWARDED_PROTO");
76
+ DEF_GLOBAL(port_80, "80");
77
+ DEF_GLOBAL(port_443, "443");
78
+ DEF_GLOBAL(localhost, "localhost");
79
+ DEF_GLOBAL(http, "http");
80
+ DEF_GLOBAL(http_11, "HTTP/1.1");
81
+ DEF_GLOBAL(http_10, "HTTP/1.0");
82
+ DEF_GLOBAL(http_09, "HTTP/0.9");
83
+ DEF_GLOBAL(GET, "GET");
84
+ DEF_GLOBAL(HEAD, "HEAD");
85
+ }
86
+
87
+ #undef DEF_GLOBAL
88
+
89
+ #endif /* global_variables_h */
@@ -0,0 +1,714 @@
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.
5
+ */
6
+ #include "ruby.h"
7
+ #include "ext_help.h"
8
+ #include <assert.h>
9
+ #include <string.h>
10
+ #include <sys/types.h>
11
+ #include "common_field_optimization.h"
12
+ #include "global_variables.h"
13
+ #include "c_util.h"
14
+
15
+ #define UH_FL_CHUNKED 0x1
16
+ #define UH_FL_HASBODY 0x2
17
+ #define UH_FL_INBODY 0x4
18
+ #define UH_FL_HASTRAILER 0x8
19
+ #define UH_FL_INTRAILER 0x10
20
+ #define UH_FL_INCHUNK 0x20
21
+ #define UH_FL_KAMETHOD 0x40
22
+ #define UH_FL_KAVERSION 0x80
23
+ #define UH_FL_HASHEADER 0x100
24
+
25
+ /* both of these flags need to be set for keepalive to be supported */
26
+ #define UH_FL_KEEPALIVE (UH_FL_KAMETHOD | UH_FL_KAVERSION)
27
+
28
+ /* keep this small for Rainbows! since every client has one */
29
+ struct http_parser {
30
+ int cs; /* Ragel internal state */
31
+ unsigned int flags;
32
+ size_t mark;
33
+ size_t offset;
34
+ union { /* these 2 fields don't nest */
35
+ size_t field;
36
+ size_t query;
37
+ } start;
38
+ union {
39
+ size_t field_len; /* only used during header processing */
40
+ size_t dest_offset; /* only used during body processing */
41
+ } s;
42
+ VALUE cont; /* Qfalse: unset, Qnil: ignored header, T_STRING: append */
43
+ union {
44
+ off_t content;
45
+ off_t chunk;
46
+ } len;
47
+ };
48
+
49
+ static void finalize_header(struct http_parser *hp, VALUE req);
50
+
51
+ #define REMAINING (unsigned long)(pe - p)
52
+ #define LEN(AT, FPC) (FPC - buffer - hp->AT)
53
+ #define MARK(M,FPC) (hp->M = (FPC) - buffer)
54
+ #define PTR_TO(F) (buffer + hp->F)
55
+ #define STR_NEW(M,FPC) rb_str_new(PTR_TO(M), LEN(M, FPC))
56
+
57
+ #define HP_FL_TEST(hp,fl) ((hp)->flags & (UH_FL_##fl))
58
+ #define HP_FL_SET(hp,fl) ((hp)->flags |= (UH_FL_##fl))
59
+ #define HP_FL_UNSET(hp,fl) ((hp)->flags &= ~(UH_FL_##fl))
60
+ #define HP_FL_ALL(hp,fl) (HP_FL_TEST(hp, fl) == (UH_FL_##fl))
61
+
62
+ /*
63
+ * handles values of the "Connection:" header, keepalive is implied
64
+ * for HTTP/1.1 but needs to be explicitly enabled with HTTP/1.0
65
+ * Additionally, we require GET/HEAD requests to support keepalive.
66
+ */
67
+ static void hp_keepalive_connection(struct http_parser *hp, VALUE val)
68
+ {
69
+ /* REQUEST_METHOD is always set before any headers */
70
+ if (HP_FL_TEST(hp, KAMETHOD)) {
71
+ if (STR_CSTR_CASE_EQ(val, "keep-alive")) {
72
+ /* basically have HTTP/1.0 masquerade as HTTP/1.1+ */
73
+ HP_FL_SET(hp, KAVERSION);
74
+ } else if (STR_CSTR_CASE_EQ(val, "close")) {
75
+ /*
76
+ * it doesn't matter what HTTP version or request method we have,
77
+ * if a client says "Connection: close", we disable keepalive
78
+ */
79
+ HP_FL_UNSET(hp, KEEPALIVE);
80
+ } else {
81
+ /*
82
+ * client could've sent anything, ignore it for now. Maybe
83
+ * "HP_FL_UNSET(hp, KEEPALIVE);" just in case?
84
+ * Raising an exception might be too mean...
85
+ */
86
+ }
87
+ }
88
+ }
89
+
90
+ static void
91
+ request_method(struct http_parser *hp, VALUE req, const char *ptr, size_t len)
92
+ {
93
+ VALUE v;
94
+
95
+ /*
96
+ * we only support keepalive for GET and HEAD requests for now other
97
+ * methods are too rarely seen to be worth optimizing. POST is unsafe
98
+ * since some clients send extra bytes after POST bodies.
99
+ */
100
+ if (CONST_MEM_EQ("GET", ptr, len)) {
101
+ HP_FL_SET(hp, KAMETHOD);
102
+ v = g_GET;
103
+ } else if (CONST_MEM_EQ("HEAD", ptr, len)) {
104
+ HP_FL_SET(hp, KAMETHOD);
105
+ v = g_HEAD;
106
+ } else {
107
+ v = rb_str_new(ptr, len);
108
+ }
109
+ rb_hash_aset(req, g_request_method, v);
110
+ }
111
+
112
+ static void
113
+ http_version(struct http_parser *hp, VALUE req, const char *ptr, size_t len)
114
+ {
115
+ VALUE v;
116
+
117
+ HP_FL_SET(hp, HASHEADER);
118
+
119
+ if (CONST_MEM_EQ("HTTP/1.1", ptr, len)) {
120
+ /* HTTP/1.1 implies keepalive unless "Connection: close" is set */
121
+ HP_FL_SET(hp, KAVERSION);
122
+ v = g_http_11;
123
+ } else if (CONST_MEM_EQ("HTTP/1.0", ptr, len)) {
124
+ v = g_http_10;
125
+ } else {
126
+ v = rb_str_new(ptr, len);
127
+ }
128
+ rb_hash_aset(req, g_server_protocol, v);
129
+ rb_hash_aset(req, g_http_version, v);
130
+ }
131
+
132
+ static inline void hp_invalid_if_trailer(struct http_parser *hp)
133
+ {
134
+ if (HP_FL_TEST(hp, INTRAILER))
135
+ rb_raise(eHttpParserError, "invalid Trailer");
136
+ }
137
+
138
+ static void write_cont_value(struct http_parser *hp,
139
+ char *buffer, const char *p)
140
+ {
141
+ char *vptr;
142
+
143
+ if (hp->cont == Qfalse)
144
+ rb_raise(eHttpParserError, "invalid continuation line");
145
+ if (NIL_P(hp->cont))
146
+ return; /* we're ignoring this header (probably Host:) */
147
+
148
+ assert(TYPE(hp->cont) == T_STRING && "continuation line is not a string");
149
+ assert(hp->mark > 0 && "impossible continuation line offset");
150
+
151
+ if (LEN(mark, p) == 0)
152
+ return;
153
+
154
+ if (RSTRING_LEN(hp->cont) > 0)
155
+ --hp->mark;
156
+
157
+ vptr = PTR_TO(mark);
158
+
159
+ if (RSTRING_LEN(hp->cont) > 0) {
160
+ assert((' ' == *vptr || '\t' == *vptr) && "invalid leading white space");
161
+ *vptr = ' ';
162
+ }
163
+ rb_str_buf_cat(hp->cont, vptr, LEN(mark, p));
164
+ }
165
+
166
+ static void write_value(VALUE req, struct http_parser *hp,
167
+ const char *buffer, const char *p)
168
+ {
169
+ VALUE f = find_common_field(PTR_TO(start.field), hp->s.field_len);
170
+ VALUE v;
171
+ VALUE e;
172
+
173
+ VALIDATE_MAX_LENGTH(LEN(mark, p), FIELD_VALUE);
174
+ v = LEN(mark, p) == 0 ? rb_str_buf_new(128) : STR_NEW(mark, p);
175
+ if (NIL_P(f)) {
176
+ VALIDATE_MAX_LENGTH(hp->s.field_len, FIELD_NAME);
177
+ f = uncommon_field(PTR_TO(start.field), hp->s.field_len);
178
+ } else if (f == g_http_connection) {
179
+ hp_keepalive_connection(hp, v);
180
+ } else if (f == g_content_length) {
181
+ hp->len.content = parse_length(RSTRING_PTR(v), RSTRING_LEN(v));
182
+ if (hp->len.content < 0)
183
+ rb_raise(eHttpParserError, "invalid Content-Length");
184
+ HP_FL_SET(hp, HASBODY);
185
+ hp_invalid_if_trailer(hp);
186
+ } else if (f == g_http_transfer_encoding) {
187
+ if (STR_CSTR_CASE_EQ(v, "chunked")) {
188
+ HP_FL_SET(hp, CHUNKED);
189
+ HP_FL_SET(hp, HASBODY);
190
+ }
191
+ hp_invalid_if_trailer(hp);
192
+ } else if (f == g_http_trailer) {
193
+ HP_FL_SET(hp, HASTRAILER);
194
+ hp_invalid_if_trailer(hp);
195
+ } else {
196
+ assert(TYPE(f) == T_STRING && "memoized object is not a string");
197
+ assert_frozen(f);
198
+ }
199
+
200
+ e = rb_hash_aref(req, f);
201
+ if (NIL_P(e)) {
202
+ hp->cont = rb_hash_aset(req, f, v);
203
+ } else if (f == g_http_host) {
204
+ /*
205
+ * ignored, absolute URLs in REQUEST_URI take precedence over
206
+ * the Host: header (ref: rfc 2616, section 5.2.1)
207
+ */
208
+ hp->cont = Qnil;
209
+ } else {
210
+ rb_str_buf_cat(e, ",", 1);
211
+ hp->cont = rb_str_buf_append(e, v);
212
+ }
213
+ }
214
+
215
+ /** Machine **/
216
+
217
+ %%{
218
+ machine http_parser;
219
+
220
+ action mark {MARK(mark, fpc); }
221
+
222
+ action start_field { MARK(start.field, fpc); }
223
+ action snake_upcase_field { snake_upcase_char(deconst(fpc)); }
224
+ action downcase_char { downcase_char(deconst(fpc)); }
225
+ action write_field { hp->s.field_len = LEN(start.field, fpc); }
226
+ action start_value { MARK(mark, fpc); }
227
+ action write_value { write_value(req, hp, buffer, fpc); }
228
+ action write_cont_value { write_cont_value(hp, buffer, fpc); }
229
+ action request_method {
230
+ request_method(hp, req, PTR_TO(mark), LEN(mark, fpc));
231
+ }
232
+ action scheme {
233
+ rb_hash_aset(req, g_rack_url_scheme, STR_NEW(mark, fpc));
234
+ }
235
+ action host {
236
+ rb_hash_aset(req, g_http_host, STR_NEW(mark, fpc));
237
+ }
238
+ action request_uri {
239
+ VALUE str;
240
+
241
+ VALIDATE_MAX_LENGTH(LEN(mark, fpc), REQUEST_URI);
242
+ str = rb_hash_aset(req, g_request_uri, STR_NEW(mark, fpc));
243
+ /*
244
+ * "OPTIONS * HTTP/1.1\r\n" is a valid request, but we can't have '*'
245
+ * in REQUEST_PATH or PATH_INFO or else Rack::Lint will complain
246
+ */
247
+ if (STR_CSTR_EQ(str, "*")) {
248
+ str = rb_str_new(NULL, 0);
249
+ rb_hash_aset(req, g_path_info, str);
250
+ rb_hash_aset(req, g_request_path, str);
251
+ }
252
+ }
253
+ action fragment {
254
+ VALIDATE_MAX_LENGTH(LEN(mark, fpc), FRAGMENT);
255
+ rb_hash_aset(req, g_fragment, STR_NEW(mark, fpc));
256
+ }
257
+ action start_query {MARK(start.query, fpc); }
258
+ action query_string {
259
+ VALIDATE_MAX_LENGTH(LEN(start.query, fpc), QUERY_STRING);
260
+ rb_hash_aset(req, g_query_string, STR_NEW(start.query, fpc));
261
+ }
262
+ action http_version { http_version(hp, req, PTR_TO(mark), LEN(mark, fpc)); }
263
+ action request_path {
264
+ VALUE val;
265
+
266
+ VALIDATE_MAX_LENGTH(LEN(mark, fpc), REQUEST_PATH);
267
+ val = rb_hash_aset(req, g_request_path, STR_NEW(mark, fpc));
268
+
269
+ /* rack says PATH_INFO must start with "/" or be empty */
270
+ if (!STR_CSTR_EQ(val, "*"))
271
+ rb_hash_aset(req, g_path_info, val);
272
+ }
273
+ action add_to_chunk_size {
274
+ hp->len.chunk = step_incr(hp->len.chunk, fc, 16);
275
+ if (hp->len.chunk < 0)
276
+ rb_raise(eHttpParserError, "invalid chunk size");
277
+ }
278
+ action header_done {
279
+ finalize_header(hp, req);
280
+
281
+ cs = http_parser_first_final;
282
+ if (HP_FL_TEST(hp, HASBODY)) {
283
+ HP_FL_SET(hp, INBODY);
284
+ if (HP_FL_TEST(hp, CHUNKED))
285
+ cs = http_parser_en_ChunkedBody;
286
+ } else {
287
+ assert(!HP_FL_TEST(hp, CHUNKED) && "chunked encoding without body!");
288
+ }
289
+ /*
290
+ * go back to Ruby so we can call the Rack application, we'll reenter
291
+ * the parser iff the body needs to be processed.
292
+ */
293
+ goto post_exec;
294
+ }
295
+
296
+ action end_trailers {
297
+ cs = http_parser_first_final;
298
+ goto post_exec;
299
+ }
300
+
301
+ action end_chunked_body {
302
+ HP_FL_SET(hp, INTRAILER);
303
+ cs = http_parser_en_Trailers;
304
+ ++p;
305
+ assert(p <= pe && "buffer overflow after chunked body");
306
+ goto post_exec;
307
+ }
308
+
309
+ action skip_chunk_data {
310
+ skip_chunk_data_hack: {
311
+ size_t nr = MIN((size_t)hp->len.chunk, REMAINING);
312
+ memcpy(RSTRING_PTR(req) + hp->s.dest_offset, fpc, nr);
313
+ hp->s.dest_offset += nr;
314
+ hp->len.chunk -= nr;
315
+ p += nr;
316
+ assert(hp->len.chunk >= 0 && "negative chunk length");
317
+ if ((size_t)hp->len.chunk > REMAINING) {
318
+ HP_FL_SET(hp, INCHUNK);
319
+ goto post_exec;
320
+ } else {
321
+ fhold;
322
+ fgoto chunk_end;
323
+ }
324
+ }}
325
+
326
+ include unicorn_http_common "unicorn_http_common.rl";
327
+ }%%
328
+
329
+ /** Data **/
330
+ %% write data;
331
+
332
+ static void http_parser_init(struct http_parser *hp)
333
+ {
334
+ int cs = 0;
335
+ memset(hp, 0, sizeof(struct http_parser));
336
+ hp->cont = Qfalse; /* zero on MRI, should be optimized away by above */
337
+ %% write init;
338
+ hp->cs = cs;
339
+ }
340
+
341
+ /** exec **/
342
+ static void http_parser_execute(struct http_parser *hp,
343
+ VALUE req, char *buffer, size_t len)
344
+ {
345
+ const char *p, *pe;
346
+ int cs = hp->cs;
347
+ size_t off = hp->offset;
348
+
349
+ if (cs == http_parser_first_final)
350
+ return;
351
+
352
+ assert(off <= len && "offset past end of buffer");
353
+
354
+ p = buffer+off;
355
+ pe = buffer+len;
356
+
357
+ assert((void *)(pe - p) == (void *)(len - off) &&
358
+ "pointers aren't same distance");
359
+
360
+ if (HP_FL_TEST(hp, INCHUNK)) {
361
+ HP_FL_UNSET(hp, INCHUNK);
362
+ goto skip_chunk_data_hack;
363
+ }
364
+ %% write exec;
365
+ post_exec: /* "_out:" also goes here */
366
+ if (hp->cs != http_parser_error)
367
+ hp->cs = cs;
368
+ hp->offset = p - buffer;
369
+
370
+ assert(p <= pe && "buffer overflow after parsing execute");
371
+ assert(hp->offset <= len && "offset longer than length");
372
+ }
373
+
374
+ static struct http_parser *data_get(VALUE self)
375
+ {
376
+ struct http_parser *hp;
377
+
378
+ Data_Get_Struct(self, struct http_parser, hp);
379
+ assert(hp && "failed to extract http_parser struct");
380
+ return hp;
381
+ }
382
+
383
+ static void finalize_header(struct http_parser *hp, VALUE req)
384
+ {
385
+ VALUE temp = rb_hash_aref(req, g_rack_url_scheme);
386
+ VALUE server_name = g_localhost;
387
+ VALUE server_port = g_port_80;
388
+
389
+ /* set rack.url_scheme to "https" or "http", no others are allowed by Rack */
390
+ if (NIL_P(temp)) {
391
+ temp = rb_hash_aref(req, g_http_x_forwarded_proto);
392
+ if (!NIL_P(temp) && STR_CSTR_EQ(temp, "https"))
393
+ server_port = g_port_443;
394
+ else
395
+ temp = g_http;
396
+ rb_hash_aset(req, g_rack_url_scheme, temp);
397
+ } else if (STR_CSTR_EQ(temp, "https")) {
398
+ server_port = g_port_443;
399
+ } else {
400
+ assert(server_port == g_port_80 && "server_port not set");
401
+ }
402
+
403
+ /* parse and set the SERVER_NAME and SERVER_PORT variables */
404
+ temp = rb_hash_aref(req, g_http_host);
405
+ if (!NIL_P(temp)) {
406
+ char *colon = memchr(RSTRING_PTR(temp), ':', RSTRING_LEN(temp));
407
+ if (colon) {
408
+ long port_start = colon - RSTRING_PTR(temp) + 1;
409
+
410
+ server_name = rb_str_substr(temp, 0, colon - RSTRING_PTR(temp));
411
+ if ((RSTRING_LEN(temp) - port_start) > 0)
412
+ server_port = rb_str_substr(temp, port_start, RSTRING_LEN(temp));
413
+ } else {
414
+ server_name = temp;
415
+ }
416
+ }
417
+ rb_hash_aset(req, g_server_name, server_name);
418
+ rb_hash_aset(req, g_server_port, server_port);
419
+ if (!HP_FL_TEST(hp, HASHEADER))
420
+ rb_hash_aset(req, g_server_protocol, g_http_09);
421
+
422
+ /* rack requires QUERY_STRING */
423
+ if (NIL_P(rb_hash_aref(req, g_query_string)))
424
+ rb_hash_aset(req, g_query_string, rb_str_new(NULL, 0));
425
+ }
426
+
427
+ static void hp_mark(void *ptr)
428
+ {
429
+ struct http_parser *hp = ptr;
430
+
431
+ rb_gc_mark(hp->cont);
432
+ }
433
+
434
+ static VALUE HttpParser_alloc(VALUE klass)
435
+ {
436
+ struct http_parser *hp;
437
+ return Data_Make_Struct(klass, struct http_parser, hp_mark, -1, hp);
438
+ }
439
+
440
+
441
+ /**
442
+ * call-seq:
443
+ * parser.new => parser
444
+ *
445
+ * Creates a new parser.
446
+ */
447
+ static VALUE HttpParser_init(VALUE self)
448
+ {
449
+ http_parser_init(data_get(self));
450
+
451
+ return self;
452
+ }
453
+
454
+ /**
455
+ * call-seq:
456
+ * parser.reset => nil
457
+ *
458
+ * Resets the parser to it's initial state so that you can reuse it
459
+ * rather than making new ones.
460
+ */
461
+ static VALUE HttpParser_reset(VALUE self)
462
+ {
463
+ http_parser_init(data_get(self));
464
+
465
+ return Qnil;
466
+ }
467
+
468
+ static void advance_str(VALUE str, off_t nr)
469
+ {
470
+ long len = RSTRING_LEN(str);
471
+
472
+ if (len == 0)
473
+ return;
474
+
475
+ rb_str_modify(str);
476
+
477
+ assert(nr <= len && "trying to advance past end of buffer");
478
+ len -= nr;
479
+ if (len > 0) /* unlikely, len is usually 0 */
480
+ memmove(RSTRING_PTR(str), RSTRING_PTR(str) + nr, len);
481
+ rb_str_set_len(str, len);
482
+ }
483
+
484
+ /**
485
+ * call-seq:
486
+ * parser.content_length => nil or Integer
487
+ *
488
+ * Returns the number of bytes left to run through HttpParser#filter_body.
489
+ * This will initially be the value of the "Content-Length" HTTP header
490
+ * after header parsing is complete and will decrease in value as
491
+ * HttpParser#filter_body is called for each chunk. This should return
492
+ * zero for requests with no body.
493
+ *
494
+ * This will return nil on "Transfer-Encoding: chunked" requests.
495
+ */
496
+ static VALUE HttpParser_content_length(VALUE self)
497
+ {
498
+ struct http_parser *hp = data_get(self);
499
+
500
+ return HP_FL_TEST(hp, CHUNKED) ? Qnil : OFFT2NUM(hp->len.content);
501
+ }
502
+
503
+ /**
504
+ * Document-method: trailers
505
+ * call-seq:
506
+ * parser.trailers(req, data) => req or nil
507
+ *
508
+ * This is an alias for HttpParser#headers
509
+ */
510
+
511
+ /**
512
+ * Document-method: headers
513
+ * call-seq:
514
+ * parser.headers(req, data) => req or nil
515
+ *
516
+ * Takes a Hash and a String of data, parses the String of data filling
517
+ * in the Hash returning the Hash if parsing is finished, nil otherwise
518
+ * When returning the req Hash, it may modify data to point to where
519
+ * body processing should begin.
520
+ *
521
+ * Raises HttpParserError if there are parsing errors.
522
+ */
523
+ static VALUE HttpParser_headers(VALUE self, VALUE req, VALUE data)
524
+ {
525
+ struct http_parser *hp = data_get(self);
526
+
527
+ rb_str_update(data);
528
+
529
+ http_parser_execute(hp, req, RSTRING_PTR(data), RSTRING_LEN(data));
530
+ VALIDATE_MAX_LENGTH(hp->offset, HEADER);
531
+
532
+ if (hp->cs == http_parser_first_final ||
533
+ hp->cs == http_parser_en_ChunkedBody) {
534
+ advance_str(data, hp->offset + 1);
535
+ hp->offset = 0;
536
+
537
+ return req;
538
+ }
539
+
540
+ if (hp->cs == http_parser_error)
541
+ rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
542
+
543
+ return Qnil;
544
+ }
545
+
546
+ static int chunked_eof(struct http_parser *hp)
547
+ {
548
+ return ((hp->cs == http_parser_first_final) || HP_FL_TEST(hp, INTRAILER));
549
+ }
550
+
551
+ /**
552
+ * call-seq:
553
+ * parser.body_eof? => true or false
554
+ *
555
+ * Detects if we're done filtering the body or not. This can be used
556
+ * to detect when to stop calling HttpParser#filter_body.
557
+ */
558
+ static VALUE HttpParser_body_eof(VALUE self)
559
+ {
560
+ struct http_parser *hp = data_get(self);
561
+
562
+ if (HP_FL_TEST(hp, CHUNKED))
563
+ return chunked_eof(hp) ? Qtrue : Qfalse;
564
+
565
+ return hp->len.content == 0 ? Qtrue : Qfalse;
566
+ }
567
+
568
+ /**
569
+ * call-seq:
570
+ * parser.keepalive? => true or false
571
+ *
572
+ * This should be used to detect if a request can really handle
573
+ * keepalives and pipelining. Currently, the rules are:
574
+ *
575
+ * 1. MUST be a GET or HEAD request
576
+ * 2. MUST be HTTP/1.1 +or+ HTTP/1.0 with "Connection: keep-alive"
577
+ * 3. MUST NOT have "Connection: close" set
578
+ */
579
+ static VALUE HttpParser_keepalive(VALUE self)
580
+ {
581
+ struct http_parser *hp = data_get(self);
582
+
583
+ return HP_FL_ALL(hp, KEEPALIVE) ? Qtrue : Qfalse;
584
+ }
585
+
586
+ /**
587
+ * call-seq:
588
+ * parser.headers? => true or false
589
+ *
590
+ * This should be used to detect if a request has headers (and if
591
+ * the response will have headers as well). HTTP/0.9 requests
592
+ * should return false, all subsequent HTTP versions will return true
593
+ */
594
+ static VALUE HttpParser_has_headers(VALUE self)
595
+ {
596
+ struct http_parser *hp = data_get(self);
597
+
598
+ return HP_FL_TEST(hp, HASHEADER) ? Qtrue : Qfalse;
599
+ }
600
+
601
+ /**
602
+ * call-seq:
603
+ * parser.filter_body(buf, data) => nil/data
604
+ *
605
+ * Takes a String of +data+, will modify data if dechunking is done.
606
+ * Returns +nil+ if there is more data left to process. Returns
607
+ * +data+ if body processing is complete. When returning +data+,
608
+ * it may modify +data+ so the start of the string points to where
609
+ * the body ended so that trailer processing can begin.
610
+ *
611
+ * Raises HttpParserError if there are dechunking errors.
612
+ * Basically this is a glorified memcpy(3) that copies +data+
613
+ * into +buf+ while filtering it through the dechunker.
614
+ */
615
+ static VALUE HttpParser_filter_body(VALUE self, VALUE buf, VALUE data)
616
+ {
617
+ struct http_parser *hp = data_get(self);
618
+ char *dptr;
619
+ long dlen;
620
+
621
+ rb_str_update(data);
622
+ dptr = RSTRING_PTR(data);
623
+ dlen = RSTRING_LEN(data);
624
+
625
+ StringValue(buf);
626
+ rb_str_resize(buf, dlen); /* we can never copy more than dlen bytes */
627
+ OBJ_TAINT(buf); /* keep weirdo $SAFE users happy */
628
+
629
+ if (HP_FL_TEST(hp, CHUNKED)) {
630
+ if (!chunked_eof(hp)) {
631
+ hp->s.dest_offset = 0;
632
+ http_parser_execute(hp, buf, dptr, dlen);
633
+ if (hp->cs == http_parser_error)
634
+ rb_raise(eHttpParserError, "Invalid HTTP format, parsing fails.");
635
+
636
+ assert(hp->s.dest_offset <= hp->offset &&
637
+ "destination buffer overflow");
638
+ advance_str(data, hp->offset);
639
+ rb_str_set_len(buf, hp->s.dest_offset);
640
+
641
+ if (RSTRING_LEN(buf) == 0 && chunked_eof(hp)) {
642
+ assert(hp->len.chunk == 0 && "chunk at EOF but more to parse");
643
+ } else {
644
+ data = Qnil;
645
+ }
646
+ }
647
+ } else {
648
+ /* no need to enter the Ragel machine for unchunked transfers */
649
+ assert(hp->len.content >= 0 && "negative Content-Length");
650
+ if (hp->len.content > 0) {
651
+ long nr = MIN(dlen, hp->len.content);
652
+
653
+ memcpy(RSTRING_PTR(buf), dptr, nr);
654
+ hp->len.content -= nr;
655
+ if (hp->len.content == 0)
656
+ hp->cs = http_parser_first_final;
657
+ advance_str(data, nr);
658
+ rb_str_set_len(buf, nr);
659
+ data = Qnil;
660
+ }
661
+ }
662
+ hp->offset = 0; /* for trailer parsing */
663
+ return data;
664
+ }
665
+
666
+ #define SET_GLOBAL(var,str) do { \
667
+ var = find_common_field(str, sizeof(str) - 1); \
668
+ assert(!NIL_P(var) && "missed global field"); \
669
+ } while (0)
670
+
671
+ void Init_unicorn_http(void)
672
+ {
673
+ VALUE mUnicorn, cHttpParser;
674
+
675
+ mUnicorn = rb_define_module("Unicorn");
676
+ cHttpParser = rb_define_class_under(mUnicorn, "HttpParser", rb_cObject);
677
+ eHttpParserError =
678
+ rb_define_class_under(mUnicorn, "HttpParserError", rb_eIOError);
679
+
680
+ init_globals();
681
+ rb_define_alloc_func(cHttpParser, HttpParser_alloc);
682
+ rb_define_method(cHttpParser, "initialize", HttpParser_init,0);
683
+ rb_define_method(cHttpParser, "reset", HttpParser_reset,0);
684
+ rb_define_method(cHttpParser, "headers", HttpParser_headers, 2);
685
+ rb_define_method(cHttpParser, "filter_body", HttpParser_filter_body, 2);
686
+ rb_define_method(cHttpParser, "trailers", HttpParser_headers, 2);
687
+ rb_define_method(cHttpParser, "content_length", HttpParser_content_length, 0);
688
+ rb_define_method(cHttpParser, "body_eof?", HttpParser_body_eof, 0);
689
+ rb_define_method(cHttpParser, "keepalive?", HttpParser_keepalive, 0);
690
+ rb_define_method(cHttpParser, "headers?", HttpParser_has_headers, 0);
691
+
692
+ /*
693
+ * The maximum size a single chunk when using chunked transfer encoding.
694
+ * This is only a theoretical maximum used to detect errors in clients,
695
+ * it is highly unlikely to encounter clients that send more than
696
+ * several kilobytes at once.
697
+ */
698
+ rb_define_const(cHttpParser, "CHUNK_MAX", OFFT2NUM(UH_OFF_T_MAX));
699
+
700
+ /*
701
+ * The maximum size of the body as specified by Content-Length.
702
+ * This is only a theoretical maximum, the actual limit is subject
703
+ * to the limits of the file system used for +Dir.tmpdir+.
704
+ */
705
+ rb_define_const(cHttpParser, "LENGTH_MAX", OFFT2NUM(UH_OFF_T_MAX));
706
+
707
+ init_common_fields();
708
+ SET_GLOBAL(g_http_host, "HOST");
709
+ SET_GLOBAL(g_http_trailer, "TRAILER");
710
+ SET_GLOBAL(g_http_transfer_encoding, "TRANSFER_ENCODING");
711
+ SET_GLOBAL(g_content_length, "CONTENT_LENGTH");
712
+ SET_GLOBAL(g_http_connection, "CONNECTION");
713
+ }
714
+ #undef SET_GLOBAL