http_parser.rb 0.5.3 → 0.6.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/.gitmodules +3 -3
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +9 -2
  4. data/README.md +50 -45
  5. data/bench/standalone.rb +23 -0
  6. data/bench/thin.rb +1 -0
  7. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +66 -58
  8. data/ext/ruby_http_parser/ruby_http_parser.c +10 -41
  9. data/ext/ruby_http_parser/vendor/http-parser-java/AUTHORS +32 -0
  10. data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +5 -1
  11. data/ext/ruby_http_parser/vendor/http-parser-java/README.md +133 -1
  12. data/ext/ruby_http_parser/vendor/http-parser-java/TODO +6 -0
  13. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +1029 -615
  14. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
  15. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +177 -43
  16. data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
  17. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
  18. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPHeadersCompleteCallback.java +13 -0
  19. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +4 -1
  20. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
  21. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +2 -2
  22. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/Util.java +6 -6
  23. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPHeadersCompleteCallback.java +12 -0
  24. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +715 -637
  25. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +1 -1
  26. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +71 -21
  27. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/ParseUrl.java +51 -0
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +1 -1
  29. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +1 -0
  30. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +2 -1
  31. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +1 -0
  32. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +6 -17
  33. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +1 -0
  34. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +1 -0
  35. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +1 -0
  36. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Url.java +127 -0
  37. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +80 -9
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +2 -1
  39. data/ext/ruby_http_parser/vendor/http-parser-java/test.c +1141 -210
  40. data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +230 -71
  41. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +32 -0
  42. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +5 -1
  43. data/ext/ruby_http_parser/vendor/http-parser/README.md +9 -2
  44. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +1029 -615
  45. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +79 -0
  46. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +145 -16
  47. data/ext/ruby_http_parser/vendor/http-parser/test.c +1065 -141
  48. data/http_parser.rb.gemspec +3 -1
  49. data/spec/parser_spec.rb +41 -17
  50. data/spec/support/requests.json +236 -24
  51. data/spec/support/responses.json +182 -36
  52. data/tasks/compile.rake +2 -2
  53. data/tasks/fixtures.rake +7 -1
  54. metadata +57 -19
  55. data/ext/ruby_http_parser/vendor/http-parser-java/compile +0 -1
  56. data/ext/ruby_http_parser/vendor/http-parser-java/test_permutations +0 -1
  57. data/ext/ruby_http_parser/vendor/http-parser-java/test_unit +0 -1
  58. data/ext/ruby_http_parser/vendor/http-parser-java/test_utf8 +0 -1
@@ -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
+
@@ -28,7 +28,7 @@ extern "C" {
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
32
  typedef __int8 int8_t;
33
33
  typedef unsigned __int8 uint8_t;
34
34
  typedef __int16 int16_t;
@@ -49,8 +49,13 @@ typedef int ssize_t;
49
49
  */
50
50
  #ifndef HTTP_PARSER_STRICT
51
51
  # define HTTP_PARSER_STRICT 1
52
- #else
53
- # define HTTP_PARSER_STRICT 0
52
+ #endif
53
+
54
+ /* Compile with -DHTTP_PARSER_DEBUG=1 to add extra debugging information to
55
+ * the error reporting facility.
56
+ */
57
+ #ifndef HTTP_PARSER_DEBUG
58
+ # define HTTP_PARSER_DEBUG 0
54
59
  #endif
55
60
 
56
61
 
@@ -80,63 +85,152 @@ typedef int (*http_cb) (http_parser*);
80
85
 
81
86
 
82
87
  /* Request Methods */
88
+ #define HTTP_METHOD_MAP(XX) \
89
+ XX(0, DELETE) \
90
+ XX(1, GET) \
91
+ XX(2, HEAD) \
92
+ XX(3, POST) \
93
+ XX(4, PUT) \
94
+ /* pathological */ \
95
+ XX(5, CONNECT) \
96
+ XX(6, OPTIONS) \
97
+ XX(7, TRACE) \
98
+ /* webdav */ \
99
+ XX(8, COPY) \
100
+ XX(9, LOCK) \
101
+ XX(10, MKCOL) \
102
+ XX(11, MOVE) \
103
+ XX(12, PROPFIND) \
104
+ XX(13, PROPPATCH) \
105
+ XX(14, UNLOCK) \
106
+ /* subversion */ \
107
+ XX(15, REPORT) \
108
+ XX(16, MKACTIVITY) \
109
+ XX(17, CHECKOUT) \
110
+ XX(18, MERGE) \
111
+ /* upnp */ \
112
+ XX(19, MSEARCH) \
113
+ XX(20, NOTIFY) \
114
+ XX(21, SUBSCRIBE) \
115
+ XX(22, UNSUBSCRIBE) \
116
+ /* RFC-5789 */ \
117
+ XX(23, PATCH) \
118
+ XX(24, PURGE) \
119
+
83
120
  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
121
+ {
122
+ #define XX(num, name) HTTP_##name = num,
123
+ HTTP_METHOD_MAP(XX)
124
+ #undef X
111
125
  };
112
126
 
113
127
 
114
128
  enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
115
129
 
116
130
 
131
+ /* Flag values for http_parser.flags field */
132
+ enum flags
133
+ { F_CHUNKED = 1 << 0
134
+ , F_CONNECTION_KEEP_ALIVE = 1 << 1
135
+ , F_CONNECTION_CLOSE = 1 << 2
136
+ , F_TRAILING = 1 << 3
137
+ , F_UPGRADE = 1 << 4
138
+ , F_SKIPBODY = 1 << 5
139
+ };
140
+
141
+
142
+ /* Map for errno-related constants
143
+ *
144
+ * The provided argument should be a macro that takes 2 arguments.
145
+ */
146
+ #define HTTP_ERRNO_MAP(XX) \
147
+ /* No error */ \
148
+ XX(OK, "success") \
149
+ \
150
+ /* Callback-related errors */ \
151
+ XX(CB_message_begin, "the on_message_begin callback failed") \
152
+ XX(CB_url, "the on_url callback failed") \
153
+ XX(CB_header_field, "the on_header_field callback failed") \
154
+ XX(CB_header_value, "the on_header_value callback failed") \
155
+ XX(CB_headers_complete, "the on_headers_complete callback failed") \
156
+ XX(CB_body, "the on_body callback failed") \
157
+ XX(CB_message_complete, "the on_message_complete callback failed") \
158
+ \
159
+ /* Parsing-related errors */ \
160
+ XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
161
+ XX(HEADER_OVERFLOW, \
162
+ "too many header bytes seen; overflow detected") \
163
+ XX(CLOSED_CONNECTION, \
164
+ "data received after completed connection: close message") \
165
+ XX(INVALID_VERSION, "invalid HTTP version") \
166
+ XX(INVALID_STATUS, "invalid HTTP status code") \
167
+ XX(INVALID_METHOD, "invalid HTTP method") \
168
+ XX(INVALID_URL, "invalid URL") \
169
+ XX(INVALID_HOST, "invalid host") \
170
+ XX(INVALID_PORT, "invalid port") \
171
+ XX(INVALID_PATH, "invalid path") \
172
+ XX(INVALID_QUERY_STRING, "invalid query string") \
173
+ XX(INVALID_FRAGMENT, "invalid fragment") \
174
+ XX(LF_EXPECTED, "LF character expected") \
175
+ XX(INVALID_HEADER_TOKEN, "invalid character in header") \
176
+ XX(INVALID_CONTENT_LENGTH, \
177
+ "invalid character in content-length header") \
178
+ XX(INVALID_CHUNK_SIZE, \
179
+ "invalid character in chunk size header") \
180
+ XX(INVALID_CONSTANT, "invalid constant string") \
181
+ XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
182
+ XX(STRICT, "strict mode assertion failed") \
183
+ XX(PAUSED, "parser is paused") \
184
+ XX(UNKNOWN, "an unknown error occurred")
185
+
186
+
187
+ /* Define HPE_* values for each errno value above */
188
+ #define HTTP_ERRNO_GEN(n, s) HPE_##n,
189
+ enum http_errno {
190
+ HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
191
+ };
192
+ #undef HTTP_ERRNO_GEN
193
+
194
+
195
+ /* Get an http_errno value from an http_parser */
196
+ #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
197
+
198
+ /* Get the line number that generated the current error */
199
+ #if HTTP_PARSER_DEBUG
200
+ #define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
201
+ #else
202
+ #define HTTP_PARSER_ERRNO_LINE(p) 0
203
+ #endif
204
+
205
+
117
206
  struct http_parser {
118
207
  /** PRIVATE **/
119
- unsigned char type : 2;
120
- unsigned char flags : 6;
121
- unsigned char state;
122
- unsigned char header_state;
123
- unsigned char index;
208
+ unsigned char type : 2; /* enum http_parser_type */
209
+ unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
210
+ unsigned char state; /* enum state from http_parser.c */
211
+ unsigned char header_state; /* enum header_state from http_parser.c */
212
+ unsigned char index; /* index into current matcher */
124
213
 
125
- uint32_t nread;
126
- int64_t content_length;
214
+ uint32_t nread; /* # bytes read in various scenarios */
215
+ uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
127
216
 
128
217
  /** READ-ONLY **/
129
218
  unsigned short http_major;
130
219
  unsigned short http_minor;
131
220
  unsigned short status_code; /* responses only */
132
- unsigned char method; /* requests only */
221
+ unsigned char method; /* requests only */
222
+ unsigned char http_errno : 7;
133
223
 
134
224
  /* 1 = Upgrade header was present and the parser has exited because of that.
135
225
  * 0 = No upgrade header present.
136
226
  * Should be checked when http_parser_execute() returns in addition to
137
227
  * error checking.
138
228
  */
139
- char upgrade;
229
+ unsigned char upgrade : 1;
230
+
231
+ #if HTTP_PARSER_DEBUG
232
+ uint32_t error_lineno;
233
+ #endif
140
234
 
141
235
  /** PUBLIC **/
142
236
  void *data; /* A pointer to get hook to the "connection" or "socket" object */
@@ -145,10 +239,7 @@ struct http_parser {
145
239
 
146
240
  struct http_parser_settings {
147
241
  http_cb on_message_begin;
148
- http_data_cb on_path;
149
- http_data_cb on_query_string;
150
242
  http_data_cb on_url;
151
- http_data_cb on_fragment;
152
243
  http_data_cb on_header_field;
153
244
  http_data_cb on_header_value;
154
245
  http_cb on_headers_complete;
@@ -157,6 +248,35 @@ struct http_parser_settings {
157
248
  };
158
249
 
159
250
 
251
+ enum http_parser_url_fields
252
+ { UF_SCHEMA = 0
253
+ , UF_HOST = 1
254
+ , UF_PORT = 2
255
+ , UF_PATH = 3
256
+ , UF_QUERY = 4
257
+ , UF_FRAGMENT = 5
258
+ , UF_MAX = 6
259
+ };
260
+
261
+
262
+ /* Result structure for http_parser_parse_url().
263
+ *
264
+ * Callers should index into field_data[] with UF_* values iff field_set
265
+ * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
266
+ * because we probably have padding left over), we convert any port to
267
+ * a uint16_t.
268
+ */
269
+ struct http_parser_url {
270
+ uint16_t field_set; /* Bitmask of (1 << UF_*) values */
271
+ uint16_t port; /* Converted UF_PORT string */
272
+
273
+ struct {
274
+ uint16_t off; /* Offset into buffer in which field starts */
275
+ uint16_t len; /* Length of run in buffer */
276
+ } field_data[UF_MAX];
277
+ };
278
+
279
+
160
280
  void http_parser_init(http_parser *parser, enum http_parser_type type);
161
281
 
162
282
 
@@ -175,7 +295,21 @@ size_t http_parser_execute(http_parser *parser,
175
295
  int http_should_keep_alive(http_parser *parser);
176
296
 
177
297
  /* Returns a string version of the HTTP method. */
178
- const char *http_method_str(enum http_method);
298
+ const char *http_method_str(enum http_method m);
299
+
300
+ /* Return a string name of the given error */
301
+ const char *http_errno_name(enum http_errno err);
302
+
303
+ /* Return a string description of the given error */
304
+ const char *http_errno_description(enum http_errno err);
305
+
306
+ /* Parse a URL; return nonzero on failure */
307
+ int http_parser_parse_url(const char *buf, size_t buflen,
308
+ int is_connect,
309
+ struct http_parser_url *u);
310
+
311
+ /* Pause or un-pause the parser; a nonzero value pauses */
312
+ void http_parser_pause(http_parser *parser, int paused);
179
313
 
180
314
  #ifdef __cplusplus
181
315
  }
@@ -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
+ }
@@ -0,0 +1,13 @@
1
+ package http_parser;
2
+
3
+ /**
4
+ * Special interface for headers_complete callback.
5
+ * This is somewhat different than other callbacks because if the user returns 1, we
6
+ * will interpret that as saying that this message has no body. This
7
+ * is needed for the annoying case of receiving a response to a HEAD
8
+ * request.
9
+ */
10
+ public abstract class HTTPHeadersCompleteCallback extends HTTPCallback
11
+ implements http_parser.lolevel.HTTPHeadersCompleteCallback{
12
+
13
+ }
@@ -28,7 +28,8 @@ public enum HTTPMethod {
28
28
  , HTTP_NOTIFY("NOTIFY")
29
29
  , HTTP_SUBSCRIBE("SUBSCRIBE")
30
30
  , HTTP_UNSUBSCRIBE("UNSUBSCRIBE")
31
-
31
+ , HTTP_PATCH("PATCH")
32
+ , HTTP_PURGE("PURGE")
32
33
  ;
33
34
 
34
35
  private static Charset ASCII;
@@ -93,6 +94,8 @@ public enum HTTPMethod {
93
94
  else if ("SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
94
95
  else if ("HTTP_UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
95
96
  else if ("UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
97
+ else if ("PATCH".equalsIgnoreCase(s)) {return HTTP_PATCH;}
98
+ else if ("PURGE".equalsIgnoreCase(s)) {return HTTP_PURGE;}
96
99
  else {return null;}
97
100
  }
98
101
  void init (String name) {