http_parser.rb 0.5.1-x86-mswin32-60 → 0.5.2-x86-mswin32-60
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.
- data/Gemfile.lock +16 -16
- data/LICENSE-MIT +20 -0
- data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +68 -11
- data/ext/ruby_http_parser/ruby_http_parser.c +74 -6
- data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +26 -1
- data/ext/ruby_http_parser/vendor/http-parser-java/README.md +23 -143
- data/ext/ruby_http_parser/vendor/http-parser-java/TODO +3 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/build.xml +74 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +115 -61
- data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +19 -3
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPCallback.java +8 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPDataCallback.java +34 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPErrorCallback.java +12 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPException.java +4 -2
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +64 -52
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParser.java +5 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +323 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/{lolevel/Util.java → Util.java} +27 -28
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +259 -85
- data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +1 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +324 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +69 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +51 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +15 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +47 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +183 -447
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +61 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +2 -1
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +26 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +165 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +58 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/test.c +232 -29
- data/ext/ruby_http_parser/vendor/http-parser-java/test_permutations +1 -1
- data/ext/ruby_http_parser/vendor/http-parser-java/test_unit +1 -1
- data/ext/ruby_http_parser/vendor/http-parser-java/test_utf8 +1 -0
- data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +154 -7
- data/ext/ruby_http_parser/vendor/http-parser-java/tests.utf8 +17 -0
- data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +1 -1
- data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +52 -10
- data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +3 -1
- data/ext/ruby_http_parser/vendor/http-parser/test.c +89 -3
- data/http_parser.rb.gemspec +8 -2
- data/lib/http_parser.rb +17 -0
- data/spec/parser_spec.rb +97 -6
- data/tasks/compile.rake +3 -1
- metadata +83 -20
- data/ext/ruby_http_parser/vendor/http-parser-java/CONTRIBUTIONS +0 -4
@@ -1,4 +1,4 @@
|
|
1
|
-
/* Copyright
|
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
|
-
#
|
31
|
+
#if defined(_WIN32) && !defined(__MINGW32__)
|
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
|
@@ -92,6 +103,11 @@ enum http_method
|
|
92
103
|
, HTTP_MKACTIVITY
|
93
104
|
, HTTP_CHECKOUT
|
94
105
|
, HTTP_MERGE
|
106
|
+
/* upnp */
|
107
|
+
, HTTP_MSEARCH
|
108
|
+
, HTTP_NOTIFY
|
109
|
+
, HTTP_SUBSCRIBE
|
110
|
+
, HTTP_UNSUBSCRIBE
|
95
111
|
};
|
96
112
|
|
97
113
|
|
@@ -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
|
+
}
|
@@ -23,54 +23,60 @@ 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
|
+
|
32
|
+
;
|
27
33
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
private static Charset ASCII;
|
35
|
+
static {
|
36
|
+
ASCII = Charset.forName("US-ASCII");;
|
37
|
+
}
|
38
|
+
public byte[] bytes;
|
33
39
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
40
|
+
HTTPMethod(String name) {
|
41
|
+
// good grief, Charlie Brown, the following is necessary because
|
42
|
+
// java is retarded:
|
43
|
+
// illegal reference to static field from initializer
|
38
44
|
// this.bytes = name.getBytes(ASCII);
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
45
|
+
// yet it's not illegal to reference static fields from
|
46
|
+
// methods called from initializer.
|
47
|
+
init(name);
|
48
|
+
}
|
49
|
+
public static HTTPMethod parse(String s) {
|
50
|
+
if ("HTTP_DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
|
51
|
+
else if ("DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
|
52
|
+
else if ("HTTP_GET".equalsIgnoreCase(s)) {return HTTP_GET;}
|
53
|
+
else if ("GET".equalsIgnoreCase(s)) {return HTTP_GET;}
|
54
|
+
else if ("HTTP_HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
|
55
|
+
else if ("HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
|
56
|
+
else if ("HTTP_POST".equalsIgnoreCase(s)) {return HTTP_POST;}
|
57
|
+
else if ("POST".equalsIgnoreCase(s)) {return HTTP_POST;}
|
58
|
+
else if ("HTTP_PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
|
59
|
+
else if ("PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
|
60
|
+
else if ("HTTP_CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
|
61
|
+
else if ("CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
|
62
|
+
else if ("HTTP_OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
|
63
|
+
else if ("OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
|
64
|
+
else if ("HTTP_TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
|
65
|
+
else if ("TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
|
66
|
+
else if ("HTTP_COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
|
67
|
+
else if ("COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
|
68
|
+
else if ("HTTP_LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
|
69
|
+
else if ("LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
|
70
|
+
else if ("HTTP_MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
|
71
|
+
else if ("MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
|
72
|
+
else if ("HTTP_MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
|
73
|
+
else if ("MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
|
74
|
+
else if ("HTTP_PROPFIND".equalsIgnoreCase(s)){return HTTP_PROPFIND;}
|
75
|
+
else if ("PROPFIND".equalsIgnoreCase(s)) {return HTTP_PROPFIND;}
|
76
|
+
else if ("HTTP_PROPPATCH".equalsIgnoreCase(s)){return HTTP_PROPPATCH;}
|
77
|
+
else if ("PROPPATCH".equalsIgnoreCase(s)) {return HTTP_PROPPATCH;}
|
78
|
+
else if ("HTTP_UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
|
79
|
+
else if ("UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
|
74
80
|
else if ("HTTP_REPORT".equalsIgnoreCase(s)) {return HTTP_REPORT;}
|
75
81
|
else if ("REPORT".equalsIgnoreCase(s)){return HTTP_REPORT;}
|
76
82
|
else if ("HTTP_MKACTIVITY".equalsIgnoreCase(s)) {return HTTP_MKACTIVITY;}
|
@@ -79,12 +85,18 @@ public enum HTTPMethod {
|
|
79
85
|
else if ("CHECKOUT".equalsIgnoreCase(s)){return HTTP_CHECKOUT;}
|
80
86
|
else if ("HTTP_MERGE".equalsIgnoreCase(s)) {return HTTP_MERGE;}
|
81
87
|
else if ("MERGE".equalsIgnoreCase(s)){return HTTP_MERGE;}
|
82
|
-
|
83
|
-
|
88
|
+
else if ("HTTP_MSEARCH".equalsIgnoreCase(s)) {return HTTP_MSEARCH;}
|
89
|
+
else if ("M-SEARCH".equalsIgnoreCase(s)) {return HTTP_MSEARCH;}
|
90
|
+
else if ("HTTP_NOTIFY".equalsIgnoreCase(s)) {return HTTP_NOTIFY;}
|
91
|
+
else if ("NOTIFY".equalsIgnoreCase(s)) {return HTTP_NOTIFY;}
|
92
|
+
else if ("HTTP_SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
|
93
|
+
else if ("SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
|
94
|
+
else if ("HTTP_UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
|
95
|
+
else if ("UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
|
96
|
+
else {return null;}
|
84
97
|
}
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
98
|
+
void init (String name) {
|
99
|
+
ASCII = null == ASCII ? Charset.forName("US-ASCII") : ASCII;
|
100
|
+
this.bytes = name.getBytes(ASCII);
|
101
|
+
}
|
90
102
|
}
|
@@ -1,5 +1,7 @@
|
|
1
1
|
package http_parser;
|
2
2
|
|
3
|
+
import java.nio.ByteBuffer;
|
4
|
+
|
3
5
|
public class HTTPParser extends http_parser.lolevel.HTTPParser {
|
4
6
|
|
5
7
|
public HTTPParser() { super(); }
|
@@ -28,4 +30,7 @@ public class HTTPParser extends http_parser.lolevel.HTTPParser {
|
|
28
30
|
public boolean shouldKeepAlive() {
|
29
31
|
return super.http_should_keep_alive();
|
30
32
|
}
|
33
|
+
public void execute(ParserSettings settings, ByteBuffer data) {
|
34
|
+
this.execute(settings.getLoLevelSettings(), data);
|
35
|
+
}
|
31
36
|
}
|
@@ -0,0 +1,323 @@
|
|
1
|
+
package http_parser;
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
import primitive.collection.ByteList;
|
6
|
+
|
7
|
+
public class ParserSettings extends http_parser.lolevel.ParserSettings {
|
8
|
+
|
9
|
+
public HTTPCallback on_message_begin;
|
10
|
+
public HTTPDataCallback on_path;
|
11
|
+
public HTTPDataCallback on_query_string;
|
12
|
+
public HTTPDataCallback on_url;
|
13
|
+
public HTTPDataCallback on_fragment;
|
14
|
+
public HTTPDataCallback on_header_field;
|
15
|
+
public HTTPDataCallback on_header_value;
|
16
|
+
|
17
|
+
public HTTPCallback on_headers_complete;
|
18
|
+
public HTTPDataCallback on_body;
|
19
|
+
public HTTPCallback on_message_complete;
|
20
|
+
|
21
|
+
public HTTPErrorCallback on_error;
|
22
|
+
|
23
|
+
private HTTPCallback _on_message_begin;
|
24
|
+
private HTTPDataCallback _on_path;
|
25
|
+
private HTTPDataCallback _on_query_string;
|
26
|
+
private HTTPDataCallback _on_url;
|
27
|
+
private HTTPDataCallback _on_fragment;
|
28
|
+
private HTTPDataCallback _on_header_field;
|
29
|
+
private HTTPDataCallback _on_header_value;
|
30
|
+
private HTTPCallback _on_headers_complete;
|
31
|
+
private HTTPDataCallback _on_body;
|
32
|
+
private HTTPCallback _on_message_complete;
|
33
|
+
private HTTPErrorCallback _on_error;
|
34
|
+
|
35
|
+
private http_parser.lolevel.ParserSettings settings;
|
36
|
+
|
37
|
+
protected ByteList field = new ByteList();
|
38
|
+
protected ByteList value = new ByteList();
|
39
|
+
protected ByteList body = new ByteList();
|
40
|
+
|
41
|
+
public ParserSettings() {
|
42
|
+
this.settings = new http_parser.lolevel.ParserSettings();
|
43
|
+
createMirrorCallbacks();
|
44
|
+
attachCallbacks();
|
45
|
+
}
|
46
|
+
|
47
|
+
protected http_parser.lolevel.ParserSettings getLoLevelSettings() {
|
48
|
+
return this.settings;
|
49
|
+
}
|
50
|
+
|
51
|
+
private void createMirrorCallbacks() {
|
52
|
+
this._on_message_begin = new HTTPCallback() {
|
53
|
+
public int cb(HTTPParser p) {
|
54
|
+
if (null != ParserSettings.this.on_message_begin) {
|
55
|
+
return ParserSettings.this.on_message_begin.cb(p);
|
56
|
+
}
|
57
|
+
return 0;
|
58
|
+
}
|
59
|
+
};
|
60
|
+
this._on_path = new HTTPDataCallback() {
|
61
|
+
@Override
|
62
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
63
|
+
if (null != ParserSettings.this.on_path) {
|
64
|
+
return ParserSettings.this.on_path.cb(p, by, pos, len);
|
65
|
+
}
|
66
|
+
return 0;
|
67
|
+
}
|
68
|
+
};
|
69
|
+
this._on_query_string = new HTTPDataCallback() {
|
70
|
+
@Override
|
71
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
72
|
+
if (null != ParserSettings.this.on_query_string) {
|
73
|
+
return ParserSettings.this.on_query_string.cb(p, by, pos, len);
|
74
|
+
}
|
75
|
+
return 0;
|
76
|
+
}
|
77
|
+
};
|
78
|
+
this._on_url = new HTTPDataCallback() {
|
79
|
+
@Override
|
80
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
81
|
+
if (null != ParserSettings.this.on_url) {
|
82
|
+
return ParserSettings.this.on_url.cb(p, by, pos, len);
|
83
|
+
}
|
84
|
+
return 0;
|
85
|
+
}
|
86
|
+
};
|
87
|
+
this._on_fragment = new HTTPDataCallback() {
|
88
|
+
@Override
|
89
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
90
|
+
if (null != ParserSettings.this.on_fragment) {
|
91
|
+
return ParserSettings.this.on_fragment.cb(p, by, pos, len);
|
92
|
+
}
|
93
|
+
return 0;
|
94
|
+
}
|
95
|
+
};
|
96
|
+
this._on_error = new HTTPErrorCallback() {
|
97
|
+
@Override
|
98
|
+
public void cb(HTTPParser parser, String error) {
|
99
|
+
if (null != ParserSettings.this.on_error) {
|
100
|
+
ParserSettings.this.on_error.cb(parser, error);
|
101
|
+
} else {
|
102
|
+
throw new HTTPException(error);
|
103
|
+
}
|
104
|
+
|
105
|
+
}
|
106
|
+
};
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
// (on_header_field and on_header_value shortened to on_h_*)
|
111
|
+
// ------------------------ ------------ --------------------------------------------
|
112
|
+
// | State (prev. callback) | Callback | Description/action |
|
113
|
+
// ------------------------ ------------ --------------------------------------------
|
114
|
+
// | nothing (first call) | on_h_field | Allocate new buffer and copy callback data |
|
115
|
+
// | | | into it |
|
116
|
+
// ------------------------ ------------ --------------------------------------------
|
117
|
+
// | value | on_h_field | New header started. |
|
118
|
+
// | | | Copy current name,value buffers to headers |
|
119
|
+
// | | | list and allocate new buffer for new name |
|
120
|
+
// ------------------------ ------------ --------------------------------------------
|
121
|
+
// | field | on_h_field | Previous name continues. Reallocate name |
|
122
|
+
// | | | buffer and append callback data to it |
|
123
|
+
// ------------------------ ------------ --------------------------------------------
|
124
|
+
// | field | on_h_value | Value for current header started. Allocate |
|
125
|
+
// | | | new buffer and copy callback data to it |
|
126
|
+
// ------------------------ ------------ --------------------------------------------
|
127
|
+
// | value | on_h_value | Value continues. Reallocate value buffer |
|
128
|
+
// | | | and append callback data to it |
|
129
|
+
// ------------------------ ------------ --------------------------------------------
|
130
|
+
this._on_header_field = new HTTPDataCallback() {
|
131
|
+
@Override
|
132
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
133
|
+
// previous value complete, call on_value with full value, reset value.
|
134
|
+
if (0 != ParserSettings.this.value.size()) {
|
135
|
+
// check we're even interested...
|
136
|
+
if (null != ParserSettings.this.on_header_value) {
|
137
|
+
byte [] valueArr = ParserSettings.this.value.toArray();
|
138
|
+
int ret = ParserSettings.this.on_header_value.cb(p, valueArr, 0, valueArr.length);
|
139
|
+
if (0 != ret) {
|
140
|
+
return ret;
|
141
|
+
}
|
142
|
+
ParserSettings.this.value.clear();
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
if (null == ParserSettings.this.on_header_field) {
|
147
|
+
return 0;
|
148
|
+
}
|
149
|
+
|
150
|
+
ParserSettings.this.field.addAll(by);
|
151
|
+
return 0;
|
152
|
+
}
|
153
|
+
};
|
154
|
+
this._on_header_value = new HTTPDataCallback() {
|
155
|
+
@Override
|
156
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
157
|
+
|
158
|
+
// previous field complete, call on_field with full field value, reset field.
|
159
|
+
if (0 != ParserSettings.this.field.size()) {
|
160
|
+
// check we're even interested...
|
161
|
+
if (null != ParserSettings.this.on_header_field) {
|
162
|
+
byte [] fieldArr = ParserSettings.this.field.toArray();
|
163
|
+
int ret = ParserSettings.this.on_header_field.cb(p, fieldArr, 0, fieldArr.length);
|
164
|
+
if (0 != ret) {
|
165
|
+
return ret;
|
166
|
+
}
|
167
|
+
ParserSettings.this.field.clear();
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
if (null == ParserSettings.this.on_header_value) {
|
172
|
+
return 0;
|
173
|
+
}
|
174
|
+
ParserSettings.this.value.addAll(by);
|
175
|
+
return 0;
|
176
|
+
}
|
177
|
+
};
|
178
|
+
this._on_headers_complete = new HTTPCallback() {
|
179
|
+
@Override
|
180
|
+
public int cb(HTTPParser parser) {
|
181
|
+
// is there an uncompleted value ... ?
|
182
|
+
if (0 != ParserSettings.this.value.size()) {
|
183
|
+
// check we're even interested...
|
184
|
+
if (null != ParserSettings.this.on_header_value) {
|
185
|
+
byte [] valueArr = ParserSettings.this.value.toArray();
|
186
|
+
int ret = ParserSettings.this.on_header_value.cb(parser, valueArr, 0, valueArr.length);
|
187
|
+
if (0 != ret) {
|
188
|
+
return ret;
|
189
|
+
}
|
190
|
+
ParserSettings.this.value.clear();
|
191
|
+
}
|
192
|
+
}
|
193
|
+
if (null != ParserSettings.this.on_headers_complete) {
|
194
|
+
return ParserSettings.this.on_headers_complete.cb(parser);
|
195
|
+
}
|
196
|
+
return 0;
|
197
|
+
}
|
198
|
+
|
199
|
+
};
|
200
|
+
this._on_body = new HTTPDataCallback() {
|
201
|
+
@Override
|
202
|
+
public int cb(HTTPParser p, byte[] by, int pos, int len) {
|
203
|
+
if (null != ParserSettings.this.on_body) {
|
204
|
+
ParserSettings.this.body.addAll(by, pos, len);
|
205
|
+
}
|
206
|
+
return 0;
|
207
|
+
}
|
208
|
+
};
|
209
|
+
|
210
|
+
this._on_message_complete = new HTTPCallback() {
|
211
|
+
@Override
|
212
|
+
public int cb(HTTPParser parser) {
|
213
|
+
if (null != ParserSettings.this.on_body) {
|
214
|
+
byte [] body = ParserSettings.this.body.toArray();
|
215
|
+
int ret = ParserSettings.this.on_body.cb(parser, body, 0, body.length);
|
216
|
+
if (0!=ret) {
|
217
|
+
return ret;
|
218
|
+
}
|
219
|
+
ParserSettings.this.body.clear();
|
220
|
+
}
|
221
|
+
if (null != ParserSettings.this.on_message_complete) {
|
222
|
+
return ParserSettings.this.on_message_complete.cb(parser);
|
223
|
+
}
|
224
|
+
return 0;
|
225
|
+
}
|
226
|
+
};
|
227
|
+
|
228
|
+
}
|
229
|
+
|
230
|
+
private void attachCallbacks() {
|
231
|
+
// these are certainly set, because we mirror them ...
|
232
|
+
this.settings.on_message_begin = this._on_message_begin;
|
233
|
+
this.settings.on_path = this._on_path;
|
234
|
+
this.settings.on_query_string = this._on_query_string;
|
235
|
+
this.settings.on_url = this._on_url;
|
236
|
+
this.settings.on_fragment = this._on_fragment;
|
237
|
+
this.settings.on_header_field = this._on_header_field;
|
238
|
+
this.settings.on_header_value = this._on_header_value;
|
239
|
+
this.settings.on_headers_complete = this._on_headers_complete;
|
240
|
+
this.settings.on_body = this._on_body;
|
241
|
+
this.settings.on_message_complete = this._on_message_complete;
|
242
|
+
this.settings.on_error = this._on_error;
|
243
|
+
}
|
244
|
+
}
|
245
|
+
//import http_parser.HTTPException;
|
246
|
+
//public class ParserSettings extends http_parser.lolevel.ParserSettings{
|
247
|
+
//
|
248
|
+
//
|
249
|
+
//
|
250
|
+
//
|
251
|
+
// public HTTPCallback on_message_begin;
|
252
|
+
// public HTTPDataCallback on_path;
|
253
|
+
// public HTTPDataCallback on_query_string;
|
254
|
+
// public HTTPDataCallback on_url;
|
255
|
+
// public HTTPDataCallback on_fragment;
|
256
|
+
// public HTTPDataCallback on_header_field;
|
257
|
+
// public HTTPDataCallback on_header_value;
|
258
|
+
// public HTTPCallback on_headers_complete;
|
259
|
+
// public HTTPDataCallback on_body;
|
260
|
+
// public HTTPCallback on_message_complete;
|
261
|
+
// public HTTPErrorCallback on_error;
|
262
|
+
//
|
263
|
+
// void call_on_message_begin (HTTPParser p) {
|
264
|
+
// call_on(on_message_begin, p);
|
265
|
+
// }
|
266
|
+
//
|
267
|
+
// void call_on_message_complete (HTTPParser p) {
|
268
|
+
// call_on(on_message_complete, p);
|
269
|
+
// }
|
270
|
+
//
|
271
|
+
// // this one is a little bit different:
|
272
|
+
// // the current `position` of the buffer is the location of the
|
273
|
+
// // error, `ini_pos` indicates where the position of
|
274
|
+
// // the buffer when it was passed to the `execute` method of the parser, i.e.
|
275
|
+
// // using this information and `limit` we'll know all the valid data
|
276
|
+
// // in the buffer around the error we can use to print pretty error
|
277
|
+
// // messages.
|
278
|
+
// void call_on_error (HTTPParser p, String mes, ByteBuffer buf, int ini_pos) {
|
279
|
+
// if (null != on_error) {
|
280
|
+
// on_error.cb(p, mes, buf, ini_pos);
|
281
|
+
// }
|
282
|
+
// // if on_error gets called it MUST throw an exception, else the parser
|
283
|
+
// // will attempt to continue parsing, which it can't because it's
|
284
|
+
// // in an invalid state.
|
285
|
+
// throw new HTTPException(mes);
|
286
|
+
// }
|
287
|
+
//
|
288
|
+
// void call_on_header_field (HTTPParser p, ByteBuffer buf, int pos, int len) {
|
289
|
+
// call_on(on_header_field, p, buf, pos, len);
|
290
|
+
// }
|
291
|
+
// void call_on_query_string (HTTPParser p, ByteBuffer buf, int pos, int len) {
|
292
|
+
// call_on(on_query_string, p, buf, pos, len);
|
293
|
+
// }
|
294
|
+
// void call_on_fragment (HTTPParser p, ByteBuffer buf, int pos, int len) {
|
295
|
+
// call_on(on_fragment, p, buf, pos, len);
|
296
|
+
// }
|
297
|
+
// void call_on_path (HTTPParser p, ByteBuffer buf, int pos, int len) {
|
298
|
+
// call_on(on_path, p, buf, pos, len);
|
299
|
+
// }
|
300
|
+
// void call_on_header_value (HTTPParser p, ByteBuffer buf, int pos, int len) {
|
301
|
+
// call_on(on_header_value, p, buf, pos, len);
|
302
|
+
// }
|
303
|
+
// void call_on_url (HTTPParser p, ByteBuffer buf, int pos, int len) {
|
304
|
+
// call_on(on_url, p, buf, pos, len);
|
305
|
+
// }
|
306
|
+
// void call_on_body(HTTPParser p, ByteBuffer buf, int pos, int len) {
|
307
|
+
// call_on(on_body, p, buf, pos, len);
|
308
|
+
// }
|
309
|
+
// void call_on_headers_complete(HTTPParser p) {
|
310
|
+
// call_on(on_headers_complete, p);
|
311
|
+
// }
|
312
|
+
// void call_on (HTTPCallback cb, HTTPParser p) {
|
313
|
+
// // cf. CALLBACK2 macro
|
314
|
+
// if (null != cb) {
|
315
|
+
// cb.cb(p);
|
316
|
+
// }
|
317
|
+
// }
|
318
|
+
// void call_on (HTTPDataCallback cb, HTTPParser p, ByteBuffer buf, int pos, int len) {
|
319
|
+
// if (null != cb && -1 != pos) {
|
320
|
+
// cb.cb(p,buf,pos,len);
|
321
|
+
// }
|
322
|
+
// }
|
323
|
+
//}
|