http-parser 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +20 -0
- data/README.md +70 -0
- data/Rakefile +0 -0
- data/ext/Rakefile +9 -0
- data/ext/http-parser/http_parser.c +2175 -0
- data/ext/http-parser/http_parser.h +305 -0
- data/http-parser.gemspec +32 -0
- data/lib/http-parser.rb +9 -0
- data/lib/http-parser/errors.rb +74 -0
- data/lib/http-parser/ext.rb +7 -0
- data/lib/http-parser/parser.rb +198 -0
- data/lib/http-parser/types.rb +312 -0
- data/lib/http-parser/version.rb +3 -0
- data/spec/error_spec.rb +45 -0
- data/spec/instance_spec.rb +98 -0
- data/spec/parser_spec.rb +274 -0
- metadata +123 -0
@@ -0,0 +1,305 @@
|
|
1
|
+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
2
|
+
*
|
3
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
* of this software and associated documentation files (the "Software"), to
|
5
|
+
* deal in the Software without restriction, including without limitation the
|
6
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
* furnished to do so, subject to the following conditions:
|
9
|
+
*
|
10
|
+
* The above copyright notice and this permission notice shall be included in
|
11
|
+
* all copies or substantial portions of the Software.
|
12
|
+
*
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
19
|
+
* IN THE SOFTWARE.
|
20
|
+
*/
|
21
|
+
#ifndef http_parser_h
|
22
|
+
#define http_parser_h
|
23
|
+
#ifdef __cplusplus
|
24
|
+
extern "C" {
|
25
|
+
#endif
|
26
|
+
|
27
|
+
/* Also update SONAME in the Makefile whenever you change these. */
|
28
|
+
#define HTTP_PARSER_VERSION_MAJOR 2
|
29
|
+
#define HTTP_PARSER_VERSION_MINOR 1
|
30
|
+
|
31
|
+
#include <sys/types.h>
|
32
|
+
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
|
33
|
+
#include <BaseTsd.h>
|
34
|
+
#include <stddef.h>
|
35
|
+
typedef __int8 int8_t;
|
36
|
+
typedef unsigned __int8 uint8_t;
|
37
|
+
typedef __int16 int16_t;
|
38
|
+
typedef unsigned __int16 uint16_t;
|
39
|
+
typedef __int32 int32_t;
|
40
|
+
typedef unsigned __int32 uint32_t;
|
41
|
+
typedef __int64 int64_t;
|
42
|
+
typedef unsigned __int64 uint64_t;
|
43
|
+
#else
|
44
|
+
#include <stdint.h>
|
45
|
+
#endif
|
46
|
+
|
47
|
+
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
|
48
|
+
* faster
|
49
|
+
*/
|
50
|
+
#ifndef HTTP_PARSER_STRICT
|
51
|
+
# define HTTP_PARSER_STRICT 1
|
52
|
+
#endif
|
53
|
+
|
54
|
+
/* Maximium header size allowed */
|
55
|
+
#define HTTP_MAX_HEADER_SIZE (80*1024)
|
56
|
+
|
57
|
+
|
58
|
+
typedef struct http_parser http_parser;
|
59
|
+
typedef struct http_parser_settings http_parser_settings;
|
60
|
+
|
61
|
+
|
62
|
+
/* Callbacks should return non-zero to indicate an error. The parser will
|
63
|
+
* then halt execution.
|
64
|
+
*
|
65
|
+
* The one exception is on_headers_complete. In a HTTP_RESPONSE parser
|
66
|
+
* returning '1' from on_headers_complete will tell the parser that it
|
67
|
+
* should not expect a body. This is used when receiving a response to a
|
68
|
+
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
|
69
|
+
* chunked' headers that indicate the presence of a body.
|
70
|
+
*
|
71
|
+
* http_data_cb does not return data chunks. It will be call arbitrarily
|
72
|
+
* many times for each string. E.G. you might get 10 callbacks for "on_url"
|
73
|
+
* each providing just a few characters more data.
|
74
|
+
*/
|
75
|
+
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
|
76
|
+
typedef int (*http_cb) (http_parser*);
|
77
|
+
|
78
|
+
|
79
|
+
/* Request Methods */
|
80
|
+
#define HTTP_METHOD_MAP(XX) \
|
81
|
+
XX(0, DELETE, DELETE) \
|
82
|
+
XX(1, GET, GET) \
|
83
|
+
XX(2, HEAD, HEAD) \
|
84
|
+
XX(3, POST, POST) \
|
85
|
+
XX(4, PUT, PUT) \
|
86
|
+
/* pathological */ \
|
87
|
+
XX(5, CONNECT, CONNECT) \
|
88
|
+
XX(6, OPTIONS, OPTIONS) \
|
89
|
+
XX(7, TRACE, TRACE) \
|
90
|
+
/* webdav */ \
|
91
|
+
XX(8, COPY, COPY) \
|
92
|
+
XX(9, LOCK, LOCK) \
|
93
|
+
XX(10, MKCOL, MKCOL) \
|
94
|
+
XX(11, MOVE, MOVE) \
|
95
|
+
XX(12, PROPFIND, PROPFIND) \
|
96
|
+
XX(13, PROPPATCH, PROPPATCH) \
|
97
|
+
XX(14, SEARCH, SEARCH) \
|
98
|
+
XX(15, UNLOCK, UNLOCK) \
|
99
|
+
/* subversion */ \
|
100
|
+
XX(16, REPORT, REPORT) \
|
101
|
+
XX(17, MKACTIVITY, MKACTIVITY) \
|
102
|
+
XX(18, CHECKOUT, CHECKOUT) \
|
103
|
+
XX(19, MERGE, MERGE) \
|
104
|
+
/* upnp */ \
|
105
|
+
XX(20, MSEARCH, M-SEARCH) \
|
106
|
+
XX(21, NOTIFY, NOTIFY) \
|
107
|
+
XX(22, SUBSCRIBE, SUBSCRIBE) \
|
108
|
+
XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
|
109
|
+
/* RFC-5789 */ \
|
110
|
+
XX(24, PATCH, PATCH) \
|
111
|
+
XX(25, PURGE, PURGE) \
|
112
|
+
|
113
|
+
enum http_method
|
114
|
+
{
|
115
|
+
#define XX(num, name, string) HTTP_##name = num,
|
116
|
+
HTTP_METHOD_MAP(XX)
|
117
|
+
#undef XX
|
118
|
+
};
|
119
|
+
|
120
|
+
|
121
|
+
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
|
122
|
+
|
123
|
+
|
124
|
+
/* Flag values for http_parser.flags field */
|
125
|
+
enum flags
|
126
|
+
{ F_CHUNKED = 1 << 0
|
127
|
+
, F_CONNECTION_KEEP_ALIVE = 1 << 1
|
128
|
+
, F_CONNECTION_CLOSE = 1 << 2
|
129
|
+
, F_TRAILING = 1 << 3
|
130
|
+
, F_UPGRADE = 1 << 4
|
131
|
+
, F_SKIPBODY = 1 << 5
|
132
|
+
};
|
133
|
+
|
134
|
+
|
135
|
+
/* Map for errno-related constants
|
136
|
+
*
|
137
|
+
* The provided argument should be a macro that takes 2 arguments.
|
138
|
+
*/
|
139
|
+
#define HTTP_ERRNO_MAP(XX) \
|
140
|
+
/* No error */ \
|
141
|
+
XX(OK, "success") \
|
142
|
+
\
|
143
|
+
/* Callback-related errors */ \
|
144
|
+
XX(CB_message_begin, "the on_message_begin callback failed") \
|
145
|
+
XX(CB_status_complete, "the on_status_complete callback failed") \
|
146
|
+
XX(CB_url, "the on_url callback failed") \
|
147
|
+
XX(CB_header_field, "the on_header_field callback failed") \
|
148
|
+
XX(CB_header_value, "the on_header_value callback failed") \
|
149
|
+
XX(CB_headers_complete, "the on_headers_complete callback failed") \
|
150
|
+
XX(CB_body, "the on_body callback failed") \
|
151
|
+
XX(CB_message_complete, "the on_message_complete callback failed") \
|
152
|
+
\
|
153
|
+
/* Parsing-related errors */ \
|
154
|
+
XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
|
155
|
+
XX(HEADER_OVERFLOW, \
|
156
|
+
"too many header bytes seen; overflow detected") \
|
157
|
+
XX(CLOSED_CONNECTION, \
|
158
|
+
"data received after completed connection: close message") \
|
159
|
+
XX(INVALID_VERSION, "invalid HTTP version") \
|
160
|
+
XX(INVALID_STATUS, "invalid HTTP status code") \
|
161
|
+
XX(INVALID_METHOD, "invalid HTTP method") \
|
162
|
+
XX(INVALID_URL, "invalid URL") \
|
163
|
+
XX(INVALID_HOST, "invalid host") \
|
164
|
+
XX(INVALID_PORT, "invalid port") \
|
165
|
+
XX(INVALID_PATH, "invalid path") \
|
166
|
+
XX(INVALID_QUERY_STRING, "invalid query string") \
|
167
|
+
XX(INVALID_FRAGMENT, "invalid fragment") \
|
168
|
+
XX(LF_EXPECTED, "LF character expected") \
|
169
|
+
XX(INVALID_HEADER_TOKEN, "invalid character in header") \
|
170
|
+
XX(INVALID_CONTENT_LENGTH, \
|
171
|
+
"invalid character in content-length header") \
|
172
|
+
XX(INVALID_CHUNK_SIZE, \
|
173
|
+
"invalid character in chunk size header") \
|
174
|
+
XX(INVALID_CONSTANT, "invalid constant string") \
|
175
|
+
XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
|
176
|
+
XX(STRICT, "strict mode assertion failed") \
|
177
|
+
XX(PAUSED, "parser is paused") \
|
178
|
+
XX(UNKNOWN, "an unknown error occurred")
|
179
|
+
|
180
|
+
|
181
|
+
/* Define HPE_* values for each errno value above */
|
182
|
+
#define HTTP_ERRNO_GEN(n, s) HPE_##n,
|
183
|
+
enum http_errno {
|
184
|
+
HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
|
185
|
+
};
|
186
|
+
#undef HTTP_ERRNO_GEN
|
187
|
+
|
188
|
+
|
189
|
+
/* Get an http_errno value from an http_parser */
|
190
|
+
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
|
191
|
+
|
192
|
+
|
193
|
+
struct http_parser {
|
194
|
+
/** PRIVATE **/
|
195
|
+
unsigned char type : 2; /* enum http_parser_type */
|
196
|
+
unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
|
197
|
+
unsigned char state; /* enum state from http_parser.c */
|
198
|
+
unsigned char header_state; /* enum header_state from http_parser.c */
|
199
|
+
unsigned char index; /* index into current matcher */
|
200
|
+
|
201
|
+
uint32_t nread; /* # bytes read in various scenarios */
|
202
|
+
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
|
203
|
+
|
204
|
+
/** READ-ONLY **/
|
205
|
+
unsigned short http_major;
|
206
|
+
unsigned short http_minor;
|
207
|
+
unsigned short status_code; /* responses only */
|
208
|
+
unsigned char method; /* requests only */
|
209
|
+
unsigned char http_errno : 7;
|
210
|
+
|
211
|
+
/* 1 = Upgrade header was present and the parser has exited because of that.
|
212
|
+
* 0 = No upgrade header present.
|
213
|
+
* Should be checked when http_parser_execute() returns in addition to
|
214
|
+
* error checking.
|
215
|
+
*/
|
216
|
+
unsigned char upgrade : 1;
|
217
|
+
|
218
|
+
/** PUBLIC **/
|
219
|
+
void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
220
|
+
};
|
221
|
+
|
222
|
+
|
223
|
+
struct http_parser_settings {
|
224
|
+
http_cb on_message_begin;
|
225
|
+
http_data_cb on_url;
|
226
|
+
http_cb on_status_complete;
|
227
|
+
http_data_cb on_header_field;
|
228
|
+
http_data_cb on_header_value;
|
229
|
+
http_cb on_headers_complete;
|
230
|
+
http_data_cb on_body;
|
231
|
+
http_cb on_message_complete;
|
232
|
+
};
|
233
|
+
|
234
|
+
|
235
|
+
enum http_parser_url_fields
|
236
|
+
{ UF_SCHEMA = 0
|
237
|
+
, UF_HOST = 1
|
238
|
+
, UF_PORT = 2
|
239
|
+
, UF_PATH = 3
|
240
|
+
, UF_QUERY = 4
|
241
|
+
, UF_FRAGMENT = 5
|
242
|
+
, UF_USERINFO = 6
|
243
|
+
, UF_MAX = 7
|
244
|
+
};
|
245
|
+
|
246
|
+
|
247
|
+
/* Result structure for http_parser_parse_url().
|
248
|
+
*
|
249
|
+
* Callers should index into field_data[] with UF_* values iff field_set
|
250
|
+
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
|
251
|
+
* because we probably have padding left over), we convert any port to
|
252
|
+
* a uint16_t.
|
253
|
+
*/
|
254
|
+
struct http_parser_url {
|
255
|
+
uint16_t field_set; /* Bitmask of (1 << UF_*) values */
|
256
|
+
uint16_t port; /* Converted UF_PORT string */
|
257
|
+
|
258
|
+
struct {
|
259
|
+
uint16_t off; /* Offset into buffer in which field starts */
|
260
|
+
uint16_t len; /* Length of run in buffer */
|
261
|
+
} field_data[UF_MAX];
|
262
|
+
};
|
263
|
+
|
264
|
+
|
265
|
+
void http_parser_init(http_parser *parser, enum http_parser_type type);
|
266
|
+
|
267
|
+
|
268
|
+
size_t http_parser_execute(http_parser *parser,
|
269
|
+
const http_parser_settings *settings,
|
270
|
+
const char *data,
|
271
|
+
size_t len);
|
272
|
+
|
273
|
+
|
274
|
+
/* If http_should_keep_alive() in the on_headers_complete or
|
275
|
+
* on_message_complete callback returns 0, then this should be
|
276
|
+
* the last message on the connection.
|
277
|
+
* If you are the server, respond with the "Connection: close" header.
|
278
|
+
* If you are the client, close the connection.
|
279
|
+
*/
|
280
|
+
int http_should_keep_alive(const http_parser *parser);
|
281
|
+
|
282
|
+
/* Returns a string version of the HTTP method. */
|
283
|
+
const char *http_method_str(enum http_method m);
|
284
|
+
|
285
|
+
/* Return a string name of the given error */
|
286
|
+
const char *http_errno_name(enum http_errno err);
|
287
|
+
|
288
|
+
/* Return a string description of the given error */
|
289
|
+
const char *http_errno_description(enum http_errno err);
|
290
|
+
|
291
|
+
/* Parse a URL; return nonzero on failure */
|
292
|
+
int http_parser_parse_url(const char *buf, size_t buflen,
|
293
|
+
int is_connect,
|
294
|
+
struct http_parser_url *u);
|
295
|
+
|
296
|
+
/* Pause or un-pause the parser; a nonzero value pauses */
|
297
|
+
void http_parser_pause(http_parser *parser, int paused);
|
298
|
+
|
299
|
+
/* Checks if this is the final chunk of the body. */
|
300
|
+
int http_body_is_final(const http_parser *parser);
|
301
|
+
|
302
|
+
#ifdef __cplusplus
|
303
|
+
}
|
304
|
+
#endif
|
305
|
+
#endif
|
data/http-parser.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "http-parser/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "http-parser"
|
7
|
+
s.version = HttpParser::VERSION
|
8
|
+
s.authors = ["Stephen von Takach"]
|
9
|
+
s.email = ["steve@cotag.me"]
|
10
|
+
s.homepage = "https://github.com/cotag/http-parser"
|
11
|
+
s.summary = "Ruby bindings to joyent/http-parser"
|
12
|
+
s.description = <<-EOF
|
13
|
+
A super fast http parser for ruby.
|
14
|
+
Cross platform and multiple ruby implementation support thanks to ffi.
|
15
|
+
EOF
|
16
|
+
|
17
|
+
|
18
|
+
s.add_dependency 'ffi-compiler', '>= 0.0.2'
|
19
|
+
s.add_dependency 'rake'
|
20
|
+
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
s.add_development_dependency 'yard'
|
23
|
+
|
24
|
+
|
25
|
+
s.files = Dir["{lib}/**/*"] + %w(Rakefile http-parser.gemspec README.md LICENSE)
|
26
|
+
s.files += ["ext/http-parser/http_parser.c", "ext/http-parser/http_parser.h"]
|
27
|
+
s.test_files = Dir["spec/**/*"]
|
28
|
+
s.extra_rdoc_files = ["README.md"]
|
29
|
+
|
30
|
+
s.extensions << "ext/Rakefile"
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
end
|
data/lib/http-parser.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require "ffi" # Bindings to C libraries
|
2
|
+
|
3
|
+
require "http-parser/ext" # Loads http-parser ext
|
4
|
+
require "http-parser/errors" # http-parser error definitions
|
5
|
+
require "http-parser/types" # http-parser data structures
|
6
|
+
require "http-parser/parser" # the core http-parser abstraction
|
7
|
+
|
8
|
+
module HttpParser
|
9
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
|
2
|
+
module HttpParser
|
3
|
+
class Error < StandardError
|
4
|
+
class OK < Error; end
|
5
|
+
|
6
|
+
# Any callback-related errors
|
7
|
+
class CALLBACK < Error; end
|
8
|
+
|
9
|
+
# Parsing-related errors
|
10
|
+
class INVALID_EOF_STATE < Error; end
|
11
|
+
class HEADER_OVERFLOW < Error; end
|
12
|
+
class CLOSED_CONNECTION < Error; end
|
13
|
+
|
14
|
+
class INVALID_VERSION < Error; end
|
15
|
+
class INVALID_STATUS < Error; end
|
16
|
+
class INVALID_METHOD < Error; end
|
17
|
+
class INVALID_URL < Error; end
|
18
|
+
class INVALID_HOST < Error; end
|
19
|
+
class INVALID_PORT < Error; end
|
20
|
+
class INVALID_PATH < Error; end
|
21
|
+
class INVALID_QUERY_STRING < Error; end
|
22
|
+
class INVALID_FRAGMENT < Error; end
|
23
|
+
class LF_EXPECTED < Error; end
|
24
|
+
class INVALID_HEADER_TOKEN < Error; end
|
25
|
+
class INVALID_CONTENT_LENGTH < Error; end
|
26
|
+
class INVALID_CHUNK_SIZE < Error; end
|
27
|
+
class INVALID_CONSTANT < Error; end
|
28
|
+
class INVALID_INTERNAL_STATE < Error; end
|
29
|
+
class STRICT < Error; end
|
30
|
+
class PAUSED < Error; end
|
31
|
+
|
32
|
+
class UNKNOWN < Error; end
|
33
|
+
end
|
34
|
+
|
35
|
+
ERRORS = {
|
36
|
+
:OK => Error::OK,
|
37
|
+
|
38
|
+
:CB_message_begin => Error::CALLBACK,
|
39
|
+
:CB_status_complete => Error::CALLBACK,
|
40
|
+
:CB_url => Error::CALLBACK,
|
41
|
+
:CB_header_field => Error::CALLBACK,
|
42
|
+
:CB_header_value => Error::CALLBACK,
|
43
|
+
:CB_headers_complete => Error::CALLBACK,
|
44
|
+
:CB_body => Error::CALLBACK,
|
45
|
+
:CB_message_complete => Error::CALLBACK,
|
46
|
+
|
47
|
+
:INVALID_EOF_STATE => Error::INVALID_EOF_STATE,
|
48
|
+
:HEADER_OVERFLOW => Error::HEADER_OVERFLOW,
|
49
|
+
:CLOSED_CONNECTION => Error::CLOSED_CONNECTION,
|
50
|
+
:INVALID_VERSION => Error::INVALID_VERSION,
|
51
|
+
:INVALID_STATUS => Error::INVALID_STATUS,
|
52
|
+
:INVALID_METHOD => Error::INVALID_METHOD,
|
53
|
+
:INVALID_URL => Error::INVALID_URL,
|
54
|
+
:INVALID_HOST => Error::INVALID_HOST,
|
55
|
+
:INVALID_PORT => Error::INVALID_PORT,
|
56
|
+
:INVALID_PATH => Error::INVALID_PATH,
|
57
|
+
:INVALID_QUERY_STRING => Error::INVALID_QUERY_STRING,
|
58
|
+
:INVALID_FRAGMENT => Error::INVALID_FRAGMENT,
|
59
|
+
:LF_EXPECTED => Error::LF_EXPECTED,
|
60
|
+
:INVALID_HEADER_TOKEN => Error::INVALID_HEADER_TOKEN,
|
61
|
+
:INVALID_CONTENT_LENGTH => Error::INVALID_CONTENT_LENGTH,
|
62
|
+
:INVALID_CHUNK_SIZE => Error::INVALID_CHUNK_SIZE,
|
63
|
+
:INVALID_CONSTANT => Error::INVALID_CONSTANT,
|
64
|
+
:INVALID_INTERNAL_STATE => Error::INVALID_INTERNAL_STATE,
|
65
|
+
:STRICT => Error::STRICT,
|
66
|
+
:PAUSED => Error::PAUSED,
|
67
|
+
|
68
|
+
:UNKNOWN => Error::UNKNOWN
|
69
|
+
}
|
70
|
+
|
71
|
+
attach_function :err_desc, :http_errno_description, [:int], :string, :blocking => true
|
72
|
+
attach_function :err_name, :http_errno_name, [:int], :string, :blocking => true
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,198 @@
|
|
1
|
+
|
2
|
+
module HttpParser
|
3
|
+
class Parser
|
4
|
+
#
|
5
|
+
# Returns a new request/response instance variable
|
6
|
+
#
|
7
|
+
def self.new_instance &block
|
8
|
+
::HttpParser::Instance.new &block
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
#
|
13
|
+
# Initializes the Parser instance.
|
14
|
+
#
|
15
|
+
def initialize
|
16
|
+
@settings = ::HttpParser::Settings.new
|
17
|
+
yield self if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Registers an `on_message_begin` callback.
|
22
|
+
#
|
23
|
+
# @yield [instance]
|
24
|
+
# The given block will be called when the HTTP message begins.
|
25
|
+
#
|
26
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
27
|
+
# The state so far of the request / response being processed.
|
28
|
+
#
|
29
|
+
def on_message_begin(&block)
|
30
|
+
@settings[:on_message_begin] = Callback.new(&block)
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Registers an `on_url` callback.
|
35
|
+
#
|
36
|
+
# @yield [instance, url]
|
37
|
+
# The given block will be called when the Request URI is recognized
|
38
|
+
# within the Request-Line.
|
39
|
+
#
|
40
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
41
|
+
# The state so far of the request / response being processed.
|
42
|
+
#
|
43
|
+
# @yieldparam [String] url
|
44
|
+
# The recognized Request URI.
|
45
|
+
#
|
46
|
+
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2
|
47
|
+
#
|
48
|
+
def on_url(&block)
|
49
|
+
@settings[:on_url] = DataCallback.new(&block)
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Registers an `on_status_complete` callback.
|
54
|
+
#
|
55
|
+
# @yield [instance]
|
56
|
+
# The given block will be called when the status is recognized.
|
57
|
+
#
|
58
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
59
|
+
# The state so far of the request / response being processed.
|
60
|
+
#
|
61
|
+
def on_status_complete(&block)
|
62
|
+
@settings[:on_status_complete] = Callback.new(&block)
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# Registers an `on_header_field` callback.
|
67
|
+
#
|
68
|
+
# @yield [instance, field]
|
69
|
+
# The given block will be called when a Header name is recognized
|
70
|
+
# in the Headers.
|
71
|
+
#
|
72
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
73
|
+
# The state so far of the request / response being processed.
|
74
|
+
#
|
75
|
+
# @yieldparam [String] field
|
76
|
+
# A recognized Header name.
|
77
|
+
#
|
78
|
+
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.5
|
79
|
+
#
|
80
|
+
def on_header_field(&block)
|
81
|
+
@settings[:on_header_field] = DataCallback.new(&block)
|
82
|
+
end
|
83
|
+
|
84
|
+
#
|
85
|
+
# Registers an `on_header_value` callback.
|
86
|
+
#
|
87
|
+
# @yield [instance, value]
|
88
|
+
# The given block will be called when a Header value is recognized
|
89
|
+
# in the Headers.
|
90
|
+
#
|
91
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
92
|
+
# The state so far of the request / response being processed.
|
93
|
+
#
|
94
|
+
# @yieldparam [String] value
|
95
|
+
# A recognized Header value.
|
96
|
+
#
|
97
|
+
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.5
|
98
|
+
#
|
99
|
+
def on_header_value(&block)
|
100
|
+
@settings[:on_header_value] = DataCallback.new(&block)
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
# Registers an `on_headers_complete` callback.
|
105
|
+
#
|
106
|
+
# @yield [instance]
|
107
|
+
# The given block will be called when the Headers stop.
|
108
|
+
#
|
109
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
110
|
+
# The state so far of the request / response being processed.
|
111
|
+
#
|
112
|
+
def on_headers_complete(&block)
|
113
|
+
@settings[:on_headers_complete] = Callback.new(&block)
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
# Registers an `on_body` callback.
|
118
|
+
#
|
119
|
+
# @yield [instance, body]
|
120
|
+
# The given block will be called when the body is recognized in the
|
121
|
+
# message body.
|
122
|
+
#
|
123
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
124
|
+
# The state so far of the request / response being processed.
|
125
|
+
#
|
126
|
+
# @yieldparam [String] body
|
127
|
+
# The full body or a chunk of the body from a chunked
|
128
|
+
# Transfer-Encoded stream.
|
129
|
+
#
|
130
|
+
# @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.5
|
131
|
+
#
|
132
|
+
def on_body(&block)
|
133
|
+
@settings[:on_body] = DataCallback.new(&block)
|
134
|
+
end
|
135
|
+
|
136
|
+
#
|
137
|
+
# Registers an `on_message_begin` callback.
|
138
|
+
#
|
139
|
+
# @yield [instance]
|
140
|
+
# The given block will be called when the message completes.
|
141
|
+
#
|
142
|
+
# @yieldparam [Paceman::HttpParser::Instance] instance
|
143
|
+
# The state so far of the request / response being processed.
|
144
|
+
#
|
145
|
+
def on_message_complete(&block)
|
146
|
+
@settings[:on_message_complete] = Callback.new(&block)
|
147
|
+
end
|
148
|
+
|
149
|
+
#
|
150
|
+
# Parses data.
|
151
|
+
#
|
152
|
+
# @param [Paceman::HttpParser::Instance] inst
|
153
|
+
# The state so far of the request / response being processed.
|
154
|
+
#
|
155
|
+
# @param [String] data
|
156
|
+
# The data to parse against the instance specified.
|
157
|
+
#
|
158
|
+
# @return [Boolean]
|
159
|
+
# Returns true if the data was parsed successfully.
|
160
|
+
#
|
161
|
+
def parse(inst, data)
|
162
|
+
::HttpParser.http_parser_execute(inst, @settings, data, data.length)
|
163
|
+
return !inst.error?
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
protected
|
168
|
+
|
169
|
+
|
170
|
+
class Callback < Proc
|
171
|
+
#
|
172
|
+
# Creates a new Parser callback.
|
173
|
+
#
|
174
|
+
def self.new(&block)
|
175
|
+
super do |parser|
|
176
|
+
begin
|
177
|
+
catch(:return) { yield(parser); 0 }
|
178
|
+
rescue
|
179
|
+
-1
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
class DataCallback < Proc
|
186
|
+
def self.new(&block)
|
187
|
+
super do |parser, buffer, length|
|
188
|
+
begin
|
189
|
+
data = buffer.get_bytes(0, length)
|
190
|
+
catch(:return) { yield(parser, data); 0 }
|
191
|
+
rescue
|
192
|
+
-1
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|