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,13 +24,24 @@
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
- #include <stdint.h>
31
+ #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
32
+ typedef __int8 int8_t;
33
+ typedef unsigned __int8 uint8_t;
34
+ typedef __int16 int16_t;
35
+ typedef unsigned __int16 uint16_t;
36
+ typedef __int32 int32_t;
37
+ typedef unsigned __int32 uint32_t;
38
+ typedef __int64 int64_t;
39
+ typedef unsigned __int64 uint64_t;
30
40
 
31
- #ifdef _WIN32
32
41
  typedef unsigned int size_t;
33
42
  typedef int ssize_t;
43
+ #else
44
+ #include <stdint.h>
34
45
  #endif
35
46
 
36
47
  /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
@@ -38,8 +49,13 @@ typedef int ssize_t;
38
49
  */
39
50
  #ifndef HTTP_PARSER_STRICT
40
51
  # define HTTP_PARSER_STRICT 1
41
- #else
42
- # 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
43
59
  #endif
44
60
 
45
61
 
@@ -69,58 +85,152 @@ typedef int (*http_cb) (http_parser*);
69
85
 
70
86
 
71
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
+
72
120
  enum http_method
73
- { HTTP_DELETE = 0
74
- , HTTP_GET
75
- , HTTP_HEAD
76
- , HTTP_POST
77
- , HTTP_PUT
78
- /* pathological */
79
- , HTTP_CONNECT
80
- , HTTP_OPTIONS
81
- , HTTP_TRACE
82
- /* webdav */
83
- , HTTP_COPY
84
- , HTTP_LOCK
85
- , HTTP_MKCOL
86
- , HTTP_MOVE
87
- , HTTP_PROPFIND
88
- , HTTP_PROPPATCH
89
- , HTTP_UNLOCK
90
- /* subversion */
91
- , HTTP_REPORT
92
- , HTTP_MKACTIVITY
93
- , HTTP_CHECKOUT
94
- , HTTP_MERGE
121
+ {
122
+ #define XX(num, name) HTTP_##name = num,
123
+ HTTP_METHOD_MAP(XX)
124
+ #undef X
95
125
  };
96
126
 
97
127
 
98
128
  enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
99
129
 
100
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
+
101
206
  struct http_parser {
102
207
  /** PRIVATE **/
103
- unsigned char type : 2;
104
- unsigned char flags : 6;
105
- unsigned char state;
106
- unsigned char header_state;
107
- 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 */
108
213
 
109
- uint32_t nread;
110
- 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) */
111
216
 
112
217
  /** READ-ONLY **/
113
218
  unsigned short http_major;
114
219
  unsigned short http_minor;
115
220
  unsigned short status_code; /* responses only */
116
- unsigned char method; /* requests only */
221
+ unsigned char method; /* requests only */
222
+ unsigned char http_errno : 7;
117
223
 
118
224
  /* 1 = Upgrade header was present and the parser has exited because of that.
119
225
  * 0 = No upgrade header present.
120
226
  * Should be checked when http_parser_execute() returns in addition to
121
227
  * error checking.
122
228
  */
123
- char upgrade;
229
+ unsigned char upgrade : 1;
230
+
231
+ #if HTTP_PARSER_DEBUG
232
+ uint32_t error_lineno;
233
+ #endif
124
234
 
125
235
  /** PUBLIC **/
126
236
  void *data; /* A pointer to get hook to the "connection" or "socket" object */
@@ -129,10 +239,7 @@ struct http_parser {
129
239
 
130
240
  struct http_parser_settings {
131
241
  http_cb on_message_begin;
132
- http_data_cb on_path;
133
- http_data_cb on_query_string;
134
242
  http_data_cb on_url;
135
- http_data_cb on_fragment;
136
243
  http_data_cb on_header_field;
137
244
  http_data_cb on_header_value;
138
245
  http_cb on_headers_complete;
@@ -141,6 +248,35 @@ struct http_parser_settings {
141
248
  };
142
249
 
143
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
+
144
280
  void http_parser_init(http_parser *parser, enum http_parser_type type);
145
281
 
146
282
 
@@ -159,7 +295,21 @@ size_t http_parser_execute(http_parser *parser,
159
295
  int http_should_keep_alive(http_parser *parser);
160
296
 
161
297
  /* Returns a string version of the HTTP method. */
162
- 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);
163
313
 
164
314
  #ifdef __cplusplus
165
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,8 @@
1
+ package http_parser;
2
+
3
+ public abstract class HTTPCallback implements http_parser.lolevel.HTTPCallback{
4
+ public int cb (http_parser.lolevel.HTTPParser parser) {
5
+ return this.cb((HTTPParser)parser);
6
+ }
7
+ public abstract int cb (HTTPParser parser);
8
+ }
@@ -0,0 +1,34 @@
1
+ package http_parser;
2
+
3
+ import java.nio.ByteBuffer;
4
+
5
+ public abstract class HTTPDataCallback implements http_parser.lolevel.HTTPDataCallback{
6
+ /*
7
+ Very raw and extremly foolhardy! DANGER!
8
+ The whole Buffer concept is difficult enough to grasp as it is,
9
+ we pass in a buffer with an arbitrary position.
10
+
11
+ The interesting data is located at position pos and is len
12
+ bytes long.
13
+
14
+ The contract of this callback is that the buffer is
15
+ returned in the state that it was passed in, so implementing
16
+ this require good citizenship, you'll need to remember the current
17
+ position, change the position to get at the data you're interested
18
+ in and then set the position back to how you found it...
19
+
20
+ Therefore: there is an abstract implementation that implements
21
+ cb as described above, and provides a new callback
22
+ with signature @see cb(byte[], int, int)
23
+ */
24
+ public int cb(http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
25
+ byte [] by = new byte[len];
26
+ int saved = buf.position();
27
+ buf.position(pos);
28
+ buf.get(by);
29
+ buf.position(saved);
30
+ return cb((HTTPParser)p, by, 0, len);
31
+ }
32
+
33
+ public abstract int cb(HTTPParser p, byte[] by, int pos, int len);
34
+ }
@@ -0,0 +1,12 @@
1
+ package http_parser;
2
+
3
+
4
+ import java.nio.ByteBuffer;
5
+
6
+ public abstract class HTTPErrorCallback implements http_parser.lolevel.HTTPErrorCallback{
7
+ public void cb (http_parser.lolevel.HTTPParser parser, String mes, ByteBuffer buf, int initial_position) {
8
+ this.cb((HTTPParser)parser, Util.error(mes, buf, initial_position));
9
+ }
10
+
11
+ public abstract void cb(HTTPParser parser, String error);
12
+ }
@@ -1,7 +1,9 @@
1
1
  package http_parser;
2
2
 
3
+ @SuppressWarnings("serial")
3
4
  public class HTTPException extends RuntimeException {
4
- public HTTPException(String mes) {
5
+
6
+ public HTTPException(String mes) {
5
7
  super(mes);
6
- }
8
+ }
7
9
  }
@@ -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
+ }
@@ -23,54 +23,61 @@ public enum HTTPMethod {
23
23
  , HTTP_REPORT("REPORT")
24
24
  , HTTP_MKACTIVITY("MKACTIVITY")
25
25
  , HTTP_CHECKOUT("CHECKOUT")
26
- , HTTP_MERGE("MERGE");
26
+ , HTTP_MERGE("MERGE")
27
+ , HTTP_MSEARCH("M-SEARCH")
28
+ , HTTP_NOTIFY("NOTIFY")
29
+ , HTTP_SUBSCRIBE("SUBSCRIBE")
30
+ , HTTP_UNSUBSCRIBE("UNSUBSCRIBE")
31
+ , HTTP_PATCH("PATCH")
32
+ , HTTP_PURGE("PURGE")
33
+ ;
27
34
 
28
- private static Charset ASCII;
29
- static {
30
- ASCII = Charset.forName("US-ASCII");;
31
- }
32
- public byte[] bytes;
35
+ private static Charset ASCII;
36
+ static {
37
+ ASCII = Charset.forName("US-ASCII");;
38
+ }
39
+ public byte[] bytes;
33
40
 
34
- HTTPMethod(String name) {
35
- // good grief, Charlie Brown, the following is necessary because
36
- // java is retarded:
37
- // illegal reference to static field from initializer
41
+ HTTPMethod(String name) {
42
+ // good grief, Charlie Brown, the following is necessary because
43
+ // java is retarded:
44
+ // illegal reference to static field from initializer
38
45
  // this.bytes = name.getBytes(ASCII);
39
- // yet it's not illegal to reference static fields from
40
- // methods called from initializer.
41
- init(name);
42
- }
43
- public static HTTPMethod parse(String s) {
44
- if ("HTTP_DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
45
- else if ("DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
46
- else if ("HTTP_GET".equalsIgnoreCase(s)) {return HTTP_GET;}
47
- else if ("GET".equalsIgnoreCase(s)) {return HTTP_GET;}
48
- else if ("HTTP_HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
49
- else if ("HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
50
- else if ("HTTP_POST".equalsIgnoreCase(s)) {return HTTP_POST;}
51
- else if ("POST".equalsIgnoreCase(s)) {return HTTP_POST;}
52
- else if ("HTTP_PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
53
- else if ("PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
54
- else if ("HTTP_CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
55
- else if ("CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
56
- else if ("HTTP_OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
57
- else if ("OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
58
- else if ("HTTP_TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
59
- else if ("TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
60
- else if ("HTTP_COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
61
- else if ("COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
62
- else if ("HTTP_LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
63
- else if ("LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
64
- else if ("HTTP_MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
65
- else if ("MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
66
- else if ("HTTP_MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
67
- else if ("MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
68
- else if ("HTTP_PROPFIND".equalsIgnoreCase(s)){return HTTP_PROPFIND;}
69
- else if ("PROPFIND".equalsIgnoreCase(s)) {return HTTP_PROPFIND;}
70
- else if ("HTTP_PROPPATCH".equalsIgnoreCase(s)){return HTTP_PROPPATCH;}
71
- else if ("PROPPATCH".equalsIgnoreCase(s)) {return HTTP_PROPPATCH;}
72
- else if ("HTTP_UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
73
- else if ("UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
46
+ // yet it's not illegal to reference static fields from
47
+ // methods called from initializer.
48
+ init(name);
49
+ }
50
+ public static HTTPMethod parse(String s) {
51
+ if ("HTTP_DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
52
+ else if ("DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
53
+ else if ("HTTP_GET".equalsIgnoreCase(s)) {return HTTP_GET;}
54
+ else if ("GET".equalsIgnoreCase(s)) {return HTTP_GET;}
55
+ else if ("HTTP_HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
56
+ else if ("HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
57
+ else if ("HTTP_POST".equalsIgnoreCase(s)) {return HTTP_POST;}
58
+ else if ("POST".equalsIgnoreCase(s)) {return HTTP_POST;}
59
+ else if ("HTTP_PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
60
+ else if ("PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
61
+ else if ("HTTP_CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
62
+ else if ("CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
63
+ else if ("HTTP_OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
64
+ else if ("OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
65
+ else if ("HTTP_TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
66
+ else if ("TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
67
+ else if ("HTTP_COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
68
+ else if ("COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
69
+ else if ("HTTP_LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
70
+ else if ("LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
71
+ else if ("HTTP_MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
72
+ else if ("MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
73
+ else if ("HTTP_MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
74
+ else if ("MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
75
+ else if ("HTTP_PROPFIND".equalsIgnoreCase(s)){return HTTP_PROPFIND;}
76
+ else if ("PROPFIND".equalsIgnoreCase(s)) {return HTTP_PROPFIND;}
77
+ else if ("HTTP_PROPPATCH".equalsIgnoreCase(s)){return HTTP_PROPPATCH;}
78
+ else if ("PROPPATCH".equalsIgnoreCase(s)) {return HTTP_PROPPATCH;}
79
+ else if ("HTTP_UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
80
+ else if ("UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
74
81
  else if ("HTTP_REPORT".equalsIgnoreCase(s)) {return HTTP_REPORT;}
75
82
  else if ("REPORT".equalsIgnoreCase(s)){return HTTP_REPORT;}
76
83
  else if ("HTTP_MKACTIVITY".equalsIgnoreCase(s)) {return HTTP_MKACTIVITY;}
@@ -79,12 +86,20 @@ public enum HTTPMethod {
79
86
  else if ("CHECKOUT".equalsIgnoreCase(s)){return HTTP_CHECKOUT;}
80
87
  else if ("HTTP_MERGE".equalsIgnoreCase(s)) {return HTTP_MERGE;}
81
88
  else if ("MERGE".equalsIgnoreCase(s)){return HTTP_MERGE;}
82
-
83
- else {return null;}
89
+ else if ("HTTP_MSEARCH".equalsIgnoreCase(s)) {return HTTP_MSEARCH;}
90
+ else if ("M-SEARCH".equalsIgnoreCase(s)) {return HTTP_MSEARCH;}
91
+ else if ("HTTP_NOTIFY".equalsIgnoreCase(s)) {return HTTP_NOTIFY;}
92
+ else if ("NOTIFY".equalsIgnoreCase(s)) {return HTTP_NOTIFY;}
93
+ else if ("HTTP_SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
94
+ else if ("SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
95
+ else if ("HTTP_UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
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;}
99
+ else {return null;}
84
100
  }
85
- void init (String name) {
86
- ASCII = null == ASCII ? Charset.forName("US-ASCII") : ASCII;
87
- this.bytes = name.getBytes(ASCII);
88
- }
89
-
101
+ void init (String name) {
102
+ ASCII = null == ASCII ? Charset.forName("US-ASCII") : ASCII;
103
+ this.bytes = name.getBytes(ASCII);
104
+ }
90
105
  }