http_parser.rb 0.5.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/linux.yml +23 -0
  3. data/.github/workflows/windows.yml +23 -0
  4. data/.gitignore +5 -4
  5. data/.gitmodules +4 -4
  6. data/Gemfile +1 -1
  7. data/README.md +52 -47
  8. data/Rakefile +1 -0
  9. data/bench/standalone.rb +23 -0
  10. data/bench/thin.rb +1 -0
  11. data/ext/ruby_http_parser/extconf.rb +1 -1
  12. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +139 -83
  13. data/ext/ruby_http_parser/ruby_http_parser.c +40 -41
  14. data/ext/ruby_http_parser/vendor/http-parser-java/AUTHORS +32 -0
  15. data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +5 -1
  16. data/ext/ruby_http_parser/vendor/http-parser-java/README.md +133 -1
  17. data/ext/ruby_http_parser/vendor/http-parser-java/TODO +6 -0
  18. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +1202 -671
  19. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
  20. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +172 -51
  21. data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
  22. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
  23. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +8 -3
  24. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
  25. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +35 -102
  26. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/Util.java +6 -6
  27. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +775 -682
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +8 -4
  29. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +70 -20
  30. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/ParseUrl.java +51 -0
  31. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +1 -1
  32. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +1 -0
  33. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +2 -1
  34. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +1 -0
  35. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +6 -17
  36. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +1 -0
  37. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +1 -0
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +1 -0
  39. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Url.java +127 -0
  40. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +80 -9
  41. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +2 -1
  42. data/ext/ruby_http_parser/vendor/http-parser-java/test.c +1637 -280
  43. data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +230 -71
  44. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +68 -0
  45. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +1 -1
  46. data/ext/ruby_http_parser/vendor/http-parser/README.md +113 -38
  47. data/ext/ruby_http_parser/vendor/http-parser/bench.c +128 -0
  48. data/ext/ruby_http_parser/vendor/http-parser/contrib/parsertrace.c +157 -0
  49. data/ext/ruby_http_parser/vendor/http-parser/contrib/url_parser.c +47 -0
  50. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +1576 -780
  51. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +111 -0
  52. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +308 -58
  53. data/ext/ruby_http_parser/vendor/http-parser/test.c +2964 -460
  54. data/http_parser.rb.gemspec +14 -7
  55. data/spec/parser_spec.rb +196 -102
  56. data/spec/support/requests.json +236 -24
  57. data/spec/support/responses.json +202 -36
  58. data/tasks/compile.rake +2 -2
  59. data/tasks/fixtures.rake +8 -2
  60. data/tasks/spec.rake +1 -1
  61. metadata +141 -134
  62. data/Gemfile.lock +0 -32
  63. data/ext/ruby_http_parser/vendor/http-parser-java/compile +0 -1
  64. data/ext/ruby_http_parser/vendor/http-parser-java/test_permutations +0 -1
  65. data/ext/ruby_http_parser/vendor/http-parser-java/test_unit +0 -1
  66. data/ext/ruby_http_parser/vendor/http-parser-java/test_utf8 +0 -1
  67. data/ext/ruby_http_parser/vendor/http-parser/CONTRIBUTIONS +0 -4
@@ -0,0 +1,79 @@
1
+ # This file is used with the GYP meta build system.
2
+ # http://code.google.com/p/gyp/
3
+ # To build try this:
4
+ # svn co http://gyp.googlecode.com/svn/trunk gyp
5
+ # ./gyp/gyp -f make --depth=`pwd` http_parser.gyp
6
+ # ./out/Debug/test
7
+ {
8
+ 'target_defaults': {
9
+ 'default_configuration': 'Debug',
10
+ 'configurations': {
11
+ # TODO: hoist these out and put them somewhere common, because
12
+ # RuntimeLibrary MUST MATCH across the entire project
13
+ 'Debug': {
14
+ 'defines': [ 'DEBUG', '_DEBUG' ],
15
+ 'msvs_settings': {
16
+ 'VCCLCompilerTool': {
17
+ 'RuntimeLibrary': 1, # static debug
18
+ },
19
+ },
20
+ },
21
+ 'Release': {
22
+ 'defines': [ 'NDEBUG' ],
23
+ 'msvs_settings': {
24
+ 'VCCLCompilerTool': {
25
+ 'RuntimeLibrary': 0, # static release
26
+ },
27
+ },
28
+ }
29
+ },
30
+ 'msvs_settings': {
31
+ 'VCCLCompilerTool': {
32
+ },
33
+ 'VCLibrarianTool': {
34
+ },
35
+ 'VCLinkerTool': {
36
+ 'GenerateDebugInformation': 'true',
37
+ },
38
+ },
39
+ 'conditions': [
40
+ ['OS == "win"', {
41
+ 'defines': [
42
+ 'WIN32'
43
+ ],
44
+ }]
45
+ ],
46
+ },
47
+
48
+ 'targets': [
49
+ {
50
+ 'target_name': 'http_parser',
51
+ 'type': 'static_library',
52
+ 'include_dirs': [ '.' ],
53
+ 'direct_dependent_settings': {
54
+ 'include_dirs': [ '.' ],
55
+ },
56
+ 'defines': [ 'HTTP_PARSER_STRICT=0' ],
57
+ 'sources': [ './http_parser.c', ],
58
+ 'conditions': [
59
+ ['OS=="win"', {
60
+ 'msvs_settings': {
61
+ 'VCCLCompilerTool': {
62
+ # Compile as C++. http_parser.c is actually C99, but C++ is
63
+ # close enough in this case.
64
+ 'CompileAs': 2,
65
+ },
66
+ },
67
+ }]
68
+ ],
69
+ },
70
+
71
+ {
72
+ 'target_name': 'test',
73
+ 'type': 'executable',
74
+ 'dependencies': [ 'http_parser' ],
75
+ 'sources': [ 'test.c' ]
76
+ }
77
+ ]
78
+ }
79
+
@@ -24,11 +24,13 @@
24
24
  extern "C" {
25
25
  #endif
26
26
 
27
- #define HTTP_PARSER_VERSION_MAJOR 1
27
+ #define HTTP_PARSER_VERSION_MAJOR 2
28
28
  #define HTTP_PARSER_VERSION_MINOR 0
29
29
 
30
30
  #include <sys/types.h>
31
- #if defined(_WIN32) && !defined(__MINGW32__)
31
+ #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
32
+ #include <BaseTsd.h>
33
+ #include <stddef.h>
32
34
  typedef __int8 int8_t;
33
35
  typedef unsigned __int8 uint8_t;
34
36
  typedef __int16 int16_t;
@@ -37,9 +39,6 @@ typedef __int32 int32_t;
37
39
  typedef unsigned __int32 uint32_t;
38
40
  typedef __int64 int64_t;
39
41
  typedef unsigned __int64 uint64_t;
40
-
41
- typedef unsigned int size_t;
42
- typedef int ssize_t;
43
42
  #else
44
43
  #include <stdint.h>
45
44
  #endif
@@ -49,11 +48,8 @@ typedef int ssize_t;
49
48
  */
50
49
  #ifndef HTTP_PARSER_STRICT
51
50
  # define HTTP_PARSER_STRICT 1
52
- #else
53
- # define HTTP_PARSER_STRICT 0
54
51
  #endif
55
52
 
56
-
57
53
  /* Maximium header size allowed */
58
54
  #define HTTP_MAX_HEADER_SIZE (80*1024)
59
55
 
@@ -72,7 +68,7 @@ typedef struct http_parser_settings http_parser_settings;
72
68
  * chunked' headers that indicate the presence of a body.
73
69
  *
74
70
  * http_data_cb does not return data chunks. It will be call arbitrarally
75
- * many times for each string. E.G. you might get 10 callbacks for "on_path"
71
+ * many times for each string. E.G. you might get 10 callbacks for "on_url"
76
72
  * each providing just a few characters more data.
77
73
  */
78
74
  typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
@@ -80,63 +76,143 @@ typedef int (*http_cb) (http_parser*);
80
76
 
81
77
 
82
78
  /* Request Methods */
79
+ #define HTTP_METHOD_MAP(XX) \
80
+ XX(0, DELETE, DELETE) \
81
+ XX(1, GET, GET) \
82
+ XX(2, HEAD, HEAD) \
83
+ XX(3, POST, POST) \
84
+ XX(4, PUT, PUT) \
85
+ /* pathological */ \
86
+ XX(5, CONNECT, CONNECT) \
87
+ XX(6, OPTIONS, OPTIONS) \
88
+ XX(7, TRACE, TRACE) \
89
+ /* webdav */ \
90
+ XX(8, COPY, COPY) \
91
+ XX(9, LOCK, LOCK) \
92
+ XX(10, MKCOL, MKCOL) \
93
+ XX(11, MOVE, MOVE) \
94
+ XX(12, PROPFIND, PROPFIND) \
95
+ XX(13, PROPPATCH, PROPPATCH) \
96
+ XX(14, SEARCH, SEARCH) \
97
+ XX(15, UNLOCK, UNLOCK) \
98
+ /* subversion */ \
99
+ XX(16, REPORT, REPORT) \
100
+ XX(17, MKACTIVITY, MKACTIVITY) \
101
+ XX(18, CHECKOUT, CHECKOUT) \
102
+ XX(19, MERGE, MERGE) \
103
+ /* upnp */ \
104
+ XX(20, MSEARCH, M-SEARCH) \
105
+ XX(21, NOTIFY, NOTIFY) \
106
+ XX(22, SUBSCRIBE, SUBSCRIBE) \
107
+ XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
108
+ /* RFC-5789 */ \
109
+ XX(24, PATCH, PATCH) \
110
+ XX(25, PURGE, PURGE) \
111
+
83
112
  enum http_method
84
- { HTTP_DELETE = 0
85
- , HTTP_GET
86
- , HTTP_HEAD
87
- , HTTP_POST
88
- , HTTP_PUT
89
- /* pathological */
90
- , HTTP_CONNECT
91
- , HTTP_OPTIONS
92
- , HTTP_TRACE
93
- /* webdav */
94
- , HTTP_COPY
95
- , HTTP_LOCK
96
- , HTTP_MKCOL
97
- , HTTP_MOVE
98
- , HTTP_PROPFIND
99
- , HTTP_PROPPATCH
100
- , HTTP_UNLOCK
101
- /* subversion */
102
- , HTTP_REPORT
103
- , HTTP_MKACTIVITY
104
- , HTTP_CHECKOUT
105
- , HTTP_MERGE
106
- /* upnp */
107
- , HTTP_MSEARCH
108
- , HTTP_NOTIFY
109
- , HTTP_SUBSCRIBE
110
- , HTTP_UNSUBSCRIBE
113
+ {
114
+ #define XX(num, name, string) HTTP_##name = num,
115
+ HTTP_METHOD_MAP(XX)
116
+ #undef XX
111
117
  };
112
118
 
113
119
 
114
120
  enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
115
121
 
116
122
 
123
+ /* Flag values for http_parser.flags field */
124
+ enum flags
125
+ { F_CHUNKED = 1 << 0
126
+ , F_CONNECTION_KEEP_ALIVE = 1 << 1
127
+ , F_CONNECTION_CLOSE = 1 << 2
128
+ , F_TRAILING = 1 << 3
129
+ , F_UPGRADE = 1 << 4
130
+ , F_SKIPBODY = 1 << 5
131
+ };
132
+
133
+
134
+ /* Map for errno-related constants
135
+ *
136
+ * The provided argument should be a macro that takes 2 arguments.
137
+ */
138
+ #define HTTP_ERRNO_MAP(XX) \
139
+ /* No error */ \
140
+ XX(OK, "success") \
141
+ \
142
+ /* Callback-related errors */ \
143
+ XX(CB_message_begin, "the on_message_begin callback failed") \
144
+ XX(CB_status_complete, "the on_status_complete callback failed") \
145
+ XX(CB_url, "the on_url callback failed") \
146
+ XX(CB_header_field, "the on_header_field callback failed") \
147
+ XX(CB_header_value, "the on_header_value callback failed") \
148
+ XX(CB_headers_complete, "the on_headers_complete callback failed") \
149
+ XX(CB_body, "the on_body callback failed") \
150
+ XX(CB_message_complete, "the on_message_complete callback failed") \
151
+ \
152
+ /* Parsing-related errors */ \
153
+ XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
154
+ XX(HEADER_OVERFLOW, \
155
+ "too many header bytes seen; overflow detected") \
156
+ XX(CLOSED_CONNECTION, \
157
+ "data received after completed connection: close message") \
158
+ XX(INVALID_VERSION, "invalid HTTP version") \
159
+ XX(INVALID_STATUS, "invalid HTTP status code") \
160
+ XX(INVALID_METHOD, "invalid HTTP method") \
161
+ XX(INVALID_URL, "invalid URL") \
162
+ XX(INVALID_HOST, "invalid host") \
163
+ XX(INVALID_PORT, "invalid port") \
164
+ XX(INVALID_PATH, "invalid path") \
165
+ XX(INVALID_QUERY_STRING, "invalid query string") \
166
+ XX(INVALID_FRAGMENT, "invalid fragment") \
167
+ XX(LF_EXPECTED, "LF character expected") \
168
+ XX(INVALID_HEADER_TOKEN, "invalid character in header") \
169
+ XX(INVALID_CONTENT_LENGTH, \
170
+ "invalid character in content-length header") \
171
+ XX(INVALID_CHUNK_SIZE, \
172
+ "invalid character in chunk size header") \
173
+ XX(INVALID_CONSTANT, "invalid constant string") \
174
+ XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
175
+ XX(STRICT, "strict mode assertion failed") \
176
+ XX(PAUSED, "parser is paused") \
177
+ XX(UNKNOWN, "an unknown error occurred")
178
+
179
+
180
+ /* Define HPE_* values for each errno value above */
181
+ #define HTTP_ERRNO_GEN(n, s) HPE_##n,
182
+ enum http_errno {
183
+ HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
184
+ };
185
+ #undef HTTP_ERRNO_GEN
186
+
187
+
188
+ /* Get an http_errno value from an http_parser */
189
+ #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
190
+
191
+
117
192
  struct http_parser {
118
193
  /** PRIVATE **/
119
- unsigned char type : 2;
120
- unsigned char flags : 6;
121
- unsigned char state;
122
- unsigned char header_state;
123
- unsigned char index;
194
+ unsigned char type : 2; /* enum http_parser_type */
195
+ unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
196
+ unsigned char state; /* enum state from http_parser.c */
197
+ unsigned char header_state; /* enum header_state from http_parser.c */
198
+ unsigned char index; /* index into current matcher */
124
199
 
125
- uint32_t nread;
126
- int64_t content_length;
200
+ uint32_t nread; /* # bytes read in various scenarios */
201
+ uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
127
202
 
128
203
  /** READ-ONLY **/
129
204
  unsigned short http_major;
130
205
  unsigned short http_minor;
131
206
  unsigned short status_code; /* responses only */
132
- unsigned char method; /* requests only */
207
+ unsigned char method; /* requests only */
208
+ unsigned char http_errno : 7;
133
209
 
134
210
  /* 1 = Upgrade header was present and the parser has exited because of that.
135
211
  * 0 = No upgrade header present.
136
212
  * Should be checked when http_parser_execute() returns in addition to
137
213
  * error checking.
138
214
  */
139
- char upgrade;
215
+ unsigned char upgrade : 1;
140
216
 
141
217
  /** PUBLIC **/
142
218
  void *data; /* A pointer to get hook to the "connection" or "socket" object */
@@ -145,10 +221,8 @@ struct http_parser {
145
221
 
146
222
  struct http_parser_settings {
147
223
  http_cb on_message_begin;
148
- http_data_cb on_path;
149
- http_data_cb on_query_string;
150
224
  http_data_cb on_url;
151
- http_data_cb on_fragment;
225
+ http_cb on_status_complete;
152
226
  http_data_cb on_header_field;
153
227
  http_data_cb on_header_value;
154
228
  http_cb on_headers_complete;
@@ -157,6 +231,36 @@ struct http_parser_settings {
157
231
  };
158
232
 
159
233
 
234
+ enum http_parser_url_fields
235
+ { UF_SCHEMA = 0
236
+ , UF_HOST = 1
237
+ , UF_PORT = 2
238
+ , UF_PATH = 3
239
+ , UF_QUERY = 4
240
+ , UF_FRAGMENT = 5
241
+ , UF_USERINFO = 6
242
+ , UF_MAX = 7
243
+ };
244
+
245
+
246
+ /* Result structure for http_parser_parse_url().
247
+ *
248
+ * Callers should index into field_data[] with UF_* values iff field_set
249
+ * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
250
+ * because we probably have padding left over), we convert any port to
251
+ * a uint16_t.
252
+ */
253
+ struct http_parser_url {
254
+ uint16_t field_set; /* Bitmask of (1 << UF_*) values */
255
+ uint16_t port; /* Converted UF_PORT string */
256
+
257
+ struct {
258
+ uint16_t off; /* Offset into buffer in which field starts */
259
+ uint16_t len; /* Length of run in buffer */
260
+ } field_data[UF_MAX];
261
+ };
262
+
263
+
160
264
  void http_parser_init(http_parser *parser, enum http_parser_type type);
161
265
 
162
266
 
@@ -167,15 +271,32 @@ size_t http_parser_execute(http_parser *parser,
167
271
 
168
272
 
169
273
  /* If http_should_keep_alive() in the on_headers_complete or
170
- * on_message_complete callback returns true, then this will be should be
274
+ * on_message_complete callback returns 0, then this should be
171
275
  * the last message on the connection.
172
276
  * If you are the server, respond with the "Connection: close" header.
173
277
  * If you are the client, close the connection.
174
278
  */
175
- int http_should_keep_alive(http_parser *parser);
279
+ int http_should_keep_alive(const http_parser *parser);
176
280
 
177
281
  /* Returns a string version of the HTTP method. */
178
- const char *http_method_str(enum http_method);
282
+ const char *http_method_str(enum http_method m);
283
+
284
+ /* Return a string name of the given error */
285
+ const char *http_errno_name(enum http_errno err);
286
+
287
+ /* Return a string description of the given error */
288
+ const char *http_errno_description(enum http_errno err);
289
+
290
+ /* Parse a URL; return nonzero on failure */
291
+ int http_parser_parse_url(const char *buf, size_t buflen,
292
+ int is_connect,
293
+ struct http_parser_url *u);
294
+
295
+ /* Pause or un-pause the parser; a nonzero value pauses */
296
+ void http_parser_pause(http_parser *parser, int paused);
297
+
298
+ /* Checks if this is the final chunk of the body. */
299
+ int http_body_is_final(const http_parser *parser);
179
300
 
180
301
  #ifdef __cplusplus
181
302
  }
@@ -0,0 +1,22 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$">
6
+ <sourceFolder url="file://$MODULE_DIR$/impl" isTestSource="false" />
7
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ <orderEntry type="module-library">
12
+ <library>
13
+ <CLASSES>
14
+ <root url="jar://$MODULE_DIR$/../ext/primitives.jar!/" />
15
+ </CLASSES>
16
+ <JAVADOC />
17
+ <SOURCES />
18
+ </library>
19
+ </orderEntry>
20
+ </component>
21
+ </module>
22
+
@@ -0,0 +1,41 @@
1
+ package http_parser;
2
+
3
+ public class FieldData {
4
+ public int off;
5
+ public int len;
6
+
7
+ public FieldData(){}
8
+
9
+ public FieldData(int off, int len){
10
+ this.off = off;
11
+ this.len = len;
12
+ }
13
+
14
+ @Override
15
+ public boolean equals(Object o) {
16
+ if (this == o) return true;
17
+ if (o == null || getClass() != o.getClass()) return false;
18
+
19
+ FieldData fieldData = (FieldData) o;
20
+
21
+ if (len != fieldData.len) return false;
22
+ if (off != fieldData.off) return false;
23
+
24
+ return true;
25
+ }
26
+
27
+ @Override
28
+ public int hashCode() {
29
+ int result = off;
30
+ result = 31 * result + len;
31
+ return result;
32
+ }
33
+
34
+ @Override
35
+ public String toString() {
36
+ return "FieldData{" +
37
+ "off=" + off +
38
+ ", len=" + len +
39
+ '}';
40
+ }
41
+ }