http_parser.rb 0.5.1 → 0.6.0.beta.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 (70) hide show
  1. data/.gitmodules +4 -4
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +11 -4
  4. data/LICENSE-MIT +20 -0
  5. data/README.md +51 -46
  6. data/bench/standalone.rb +23 -0
  7. data/bench/thin.rb +1 -0
  8. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +121 -56
  9. data/ext/ruby_http_parser/ruby_http_parser.c +89 -48
  10. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +32 -0
  11. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +5 -1
  12. data/ext/ruby_http_parser/vendor/http-parser/Makefile +34 -17
  13. data/ext/ruby_http_parser/vendor/http-parser/README.md +9 -2
  14. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +1045 -589
  15. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +79 -0
  16. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +148 -17
  17. data/ext/ruby_http_parser/vendor/http-parser/test.c +1138 -128
  18. data/ext/ruby_http_parser/vendor/http-parser-java/AUTHORS +32 -0
  19. data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +30 -1
  20. data/ext/ruby_http_parser/vendor/http-parser-java/Makefile +34 -17
  21. data/ext/ruby_http_parser/vendor/http-parser-java/README.md +30 -18
  22. data/ext/ruby_http_parser/vendor/http-parser-java/TODO +9 -0
  23. data/ext/ruby_http_parser/vendor/http-parser-java/build.xml +74 -0
  24. data/ext/ruby_http_parser/vendor/http-parser-java/ext/primitives.jar +0 -0
  25. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +1075 -607
  26. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
  27. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +190 -40
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
  29. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
  30. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPCallback.java +8 -0
  31. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPDataCallback.java +34 -0
  32. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPErrorCallback.java +12 -0
  33. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPException.java +4 -2
  34. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPHeadersCompleteCallback.java +13 -0
  35. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +67 -52
  36. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParser.java +5 -0
  37. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +323 -0
  39. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/{lolevel/Util.java → Util.java} +34 -35
  40. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPHeadersCompleteCallback.java +12 -0
  41. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +839 -587
  42. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +2 -1
  43. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +374 -0
  44. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/ParseUrl.java +51 -0
  45. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +69 -0
  46. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +52 -0
  47. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +16 -0
  48. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +48 -0
  49. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +171 -446
  50. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +62 -0
  51. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +3 -1
  52. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +27 -0
  53. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Url.java +127 -0
  54. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +236 -0
  55. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +59 -0
  56. data/ext/ruby_http_parser/vendor/http-parser-java/test.c +1318 -184
  57. data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +355 -49
  58. data/ext/ruby_http_parser/vendor/http-parser-java/tests.utf8 +17 -0
  59. data/http_parser.rb.gemspec +10 -2
  60. data/lib/http_parser.rb +17 -0
  61. data/spec/parser_spec.rb +157 -23
  62. data/spec/support/requests.json +236 -24
  63. data/spec/support/responses.json +182 -36
  64. data/tasks/compile.rake +5 -3
  65. data/tasks/fixtures.rake +8 -2
  66. metadata +209 -127
  67. data/ext/ruby_http_parser/vendor/http-parser-java/CONTRIBUTIONS +0 -4
  68. data/ext/ruby_http_parser/vendor/http-parser-java/compile +0 -1
  69. data/ext/ruby_http_parser/vendor/http-parser-java/test_permutations +0 -1
  70. data/ext/ruby_http_parser/vendor/http-parser-java/test_unit +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
+
@@ -1,4 +1,4 @@
1
- /* Copyright 2009,2010 Ryan Dahl <ry@tinyclouds.org>
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
2
  *
3
3
  * Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  * of this software and associated documentation files (the "Software"), to
@@ -24,9 +24,11 @@
24
24
  extern "C" {
25
25
  #endif
26
26
 
27
+ #define HTTP_PARSER_VERSION_MAJOR 1
28
+ #define HTTP_PARSER_VERSION_MINOR 0
27
29
 
28
30
  #include <sys/types.h>
29
- #if defined(_WIN32) && !defined(__MINGW32__)
31
+ #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
30
32
  typedef __int8 int8_t;
31
33
  typedef unsigned __int8 uint8_t;
32
34
  typedef __int16 int16_t;
@@ -47,8 +49,13 @@ typedef int ssize_t;
47
49
  */
48
50
  #ifndef HTTP_PARSER_STRICT
49
51
  # define HTTP_PARSER_STRICT 1
50
- #else
51
- # 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
52
59
  #endif
53
60
 
54
61
 
@@ -58,6 +65,7 @@ typedef int ssize_t;
58
65
 
59
66
  typedef struct http_parser http_parser;
60
67
  typedef struct http_parser_settings http_parser_settings;
68
+ typedef struct http_parser_result http_parser_result;
61
69
 
62
70
 
63
71
  /* Callbacks should return non-zero to indicate an error. The parser will
@@ -106,35 +114,118 @@ enum http_method
106
114
  , HTTP_NOTIFY
107
115
  , HTTP_SUBSCRIBE
108
116
  , HTTP_UNSUBSCRIBE
117
+ /* RFC-5789 */
118
+ , HTTP_PATCH
119
+ , HTTP_PURGE
109
120
  };
110
121
 
111
122
 
112
123
  enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
113
124
 
114
125
 
126
+ /* Flag values for http_parser.flags field */
127
+ enum flags
128
+ { F_CHUNKED = 1 << 0
129
+ , F_CONNECTION_KEEP_ALIVE = 1 << 1
130
+ , F_CONNECTION_CLOSE = 1 << 2
131
+ , F_TRAILING = 1 << 3
132
+ , F_UPGRADE = 1 << 4
133
+ , F_SKIPBODY = 1 << 5
134
+ };
135
+
136
+
137
+ /* Map for errno-related constants
138
+ *
139
+ * The provided argument should be a macro that takes 2 arguments.
140
+ */
141
+ #define HTTP_ERRNO_MAP(XX) \
142
+ /* No error */ \
143
+ XX(OK, "success") \
144
+ \
145
+ /* Callback-related errors */ \
146
+ XX(CB_message_begin, "the on_message_begin callback failed") \
147
+ XX(CB_url, "the on_url callback failed") \
148
+ XX(CB_header_field, "the on_header_field callback failed") \
149
+ XX(CB_header_value, "the on_header_value callback failed") \
150
+ XX(CB_headers_complete, "the on_headers_complete callback failed") \
151
+ XX(CB_body, "the on_body callback failed") \
152
+ XX(CB_message_complete, "the on_message_complete callback failed") \
153
+ \
154
+ /* Parsing-related errors */ \
155
+ XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
156
+ XX(HEADER_OVERFLOW, \
157
+ "too many header bytes seen; overflow detected") \
158
+ XX(CLOSED_CONNECTION, \
159
+ "data received after completed connection: close message") \
160
+ XX(INVALID_VERSION, "invalid HTTP version") \
161
+ XX(INVALID_STATUS, "invalid HTTP status code") \
162
+ XX(INVALID_METHOD, "invalid HTTP method") \
163
+ XX(INVALID_URL, "invalid URL") \
164
+ XX(INVALID_HOST, "invalid host") \
165
+ XX(INVALID_PORT, "invalid port") \
166
+ XX(INVALID_PATH, "invalid path") \
167
+ XX(INVALID_QUERY_STRING, "invalid query string") \
168
+ XX(INVALID_FRAGMENT, "invalid fragment") \
169
+ XX(LF_EXPECTED, "LF character expected") \
170
+ XX(INVALID_HEADER_TOKEN, "invalid character in header") \
171
+ XX(INVALID_CONTENT_LENGTH, \
172
+ "invalid character in content-length header") \
173
+ XX(INVALID_CHUNK_SIZE, \
174
+ "invalid character in chunk size header") \
175
+ XX(INVALID_CONSTANT, "invalid constant string") \
176
+ XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
177
+ XX(STRICT, "strict mode assertion failed") \
178
+ XX(PAUSED, "parser is paused") \
179
+ XX(UNKNOWN, "an unknown error occurred")
180
+
181
+
182
+ /* Define HPE_* values for each errno value above */
183
+ #define HTTP_ERRNO_GEN(n, s) HPE_##n,
184
+ enum http_errno {
185
+ HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
186
+ };
187
+ #undef HTTP_ERRNO_GEN
188
+
189
+
190
+ /* Get an http_errno value from an http_parser */
191
+ #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
192
+
193
+ /* Get the line number that generated the current error */
194
+ #if HTTP_PARSER_DEBUG
195
+ #define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
196
+ #else
197
+ #define HTTP_PARSER_ERRNO_LINE(p) 0
198
+ #endif
199
+
200
+
115
201
  struct http_parser {
116
202
  /** PRIVATE **/
117
- unsigned char type : 2;
118
- unsigned char flags : 6;
119
- unsigned char state;
120
- unsigned char header_state;
121
- unsigned char index;
203
+ unsigned char type : 2; /* enum http_parser_type */
204
+ unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
205
+ unsigned char state; /* enum state from http_parser.c */
206
+ unsigned char header_state; /* enum header_state from http_parser.c */
207
+ unsigned char index; /* index into current matcher */
122
208
 
123
- uint32_t nread;
124
- int64_t content_length;
209
+ uint32_t nread; /* # bytes read in various scenarios */
210
+ uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
125
211
 
126
212
  /** READ-ONLY **/
127
213
  unsigned short http_major;
128
214
  unsigned short http_minor;
129
215
  unsigned short status_code; /* responses only */
130
- unsigned char method; /* requests only */
216
+ unsigned char method; /* requests only */
217
+ unsigned char http_errno : 7;
131
218
 
132
219
  /* 1 = Upgrade header was present and the parser has exited because of that.
133
220
  * 0 = No upgrade header present.
134
221
  * Should be checked when http_parser_execute() returns in addition to
135
222
  * error checking.
136
223
  */
137
- char upgrade;
224
+ unsigned char upgrade : 1;
225
+
226
+ #if HTTP_PARSER_DEBUG
227
+ uint32_t error_lineno;
228
+ #endif
138
229
 
139
230
  /** PUBLIC **/
140
231
  void *data; /* A pointer to get hook to the "connection" or "socket" object */
@@ -143,10 +234,7 @@ struct http_parser {
143
234
 
144
235
  struct http_parser_settings {
145
236
  http_cb on_message_begin;
146
- http_data_cb on_path;
147
- http_data_cb on_query_string;
148
237
  http_data_cb on_url;
149
- http_data_cb on_fragment;
150
238
  http_data_cb on_header_field;
151
239
  http_data_cb on_header_value;
152
240
  http_cb on_headers_complete;
@@ -155,6 +243,35 @@ struct http_parser_settings {
155
243
  };
156
244
 
157
245
 
246
+ enum http_parser_url_fields
247
+ { UF_SCHEMA = 0
248
+ , UF_HOST = 1
249
+ , UF_PORT = 2
250
+ , UF_PATH = 3
251
+ , UF_QUERY = 4
252
+ , UF_FRAGMENT = 5
253
+ , UF_MAX = 6
254
+ };
255
+
256
+
257
+ /* Result structure for http_parser_parse_url().
258
+ *
259
+ * Callers should index into field_data[] with UF_* values iff field_set
260
+ * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
261
+ * because we probably have padding left over), we convert any port to
262
+ * a uint16_t.
263
+ */
264
+ struct http_parser_url {
265
+ uint16_t field_set; /* Bitmask of (1 << UF_*) values */
266
+ uint16_t port; /* Converted UF_PORT string */
267
+
268
+ struct {
269
+ uint16_t off; /* Offset into buffer in which field starts */
270
+ uint16_t len; /* Length of run in buffer */
271
+ } field_data[UF_MAX];
272
+ };
273
+
274
+
158
275
  void http_parser_init(http_parser *parser, enum http_parser_type type);
159
276
 
160
277
 
@@ -173,7 +290,21 @@ size_t http_parser_execute(http_parser *parser,
173
290
  int http_should_keep_alive(http_parser *parser);
174
291
 
175
292
  /* Returns a string version of the HTTP method. */
176
- const char *http_method_str(enum http_method);
293
+ const char *http_method_str(enum http_method m);
294
+
295
+ /* Return a string name of the given error */
296
+ const char *http_errno_name(enum http_errno err);
297
+
298
+ /* Return a string description of the given error */
299
+ const char *http_errno_description(enum http_errno err);
300
+
301
+ /* Parse a URL; return nonzero on failure */
302
+ int http_parser_parse_url(const char *buf, size_t buflen,
303
+ int is_connect,
304
+ struct http_parser_url *u);
305
+
306
+ /* Pause or un-pause the parser; a nonzero value pauses */
307
+ void http_parser_pause(http_parser *parser, int paused);
177
308
 
178
309
  #ifdef __cplusplus
179
310
  }