midori_http_parser 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.gitmodules +6 -0
  4. data/.travis.yml +33 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE-MIT +20 -0
  7. data/README.md +90 -0
  8. data/Rakefile +6 -0
  9. data/bench/standalone.rb +23 -0
  10. data/bench/thin.rb +58 -0
  11. data/ext/ruby_http_parser/.gitignore +1 -0
  12. data/ext/ruby_http_parser/RubyHttpParserService.java +18 -0
  13. data/ext/ruby_http_parser/ext_help.h +18 -0
  14. data/ext/ruby_http_parser/extconf.rb +24 -0
  15. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +495 -0
  16. data/ext/ruby_http_parser/ruby_http_parser.c +516 -0
  17. data/ext/ruby_http_parser/vendor/.gitkeep +0 -0
  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 +48 -0
  20. data/ext/ruby_http_parser/vendor/http-parser-java/README.md +183 -0
  21. data/ext/ruby_http_parser/vendor/http-parser-java/TODO +28 -0
  22. data/ext/ruby_http_parser/vendor/http-parser-java/build.xml +74 -0
  23. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +2175 -0
  24. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
  25. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +304 -0
  26. data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
  27. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPCallback.java +8 -0
  29. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPDataCallback.java +34 -0
  30. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPErrorCallback.java +12 -0
  31. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPException.java +9 -0
  32. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +113 -0
  33. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParser.java +36 -0
  34. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
  35. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +256 -0
  36. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserType.java +13 -0
  37. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/Util.java +111 -0
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPCallback.java +5 -0
  39. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPDataCallback.java +25 -0
  40. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPErrorCallback.java +7 -0
  41. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +2171 -0
  42. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +83 -0
  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 +212 -0
  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 +117 -0
  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 +3425 -0
  57. data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +845 -0
  58. data/ext/ruby_http_parser/vendor/http-parser-java/tests.utf8 +17 -0
  59. data/ext/ruby_http_parser/vendor/http-parser-java/tools/byte_constants.rb +6 -0
  60. data/ext/ruby_http_parser/vendor/http-parser-java/tools/const_char.rb +13 -0
  61. data/ext/ruby_http_parser/vendor/http-parser-java/tools/lowcase.rb +15 -0
  62. data/ext/ruby_http_parser/vendor/http-parser-java/tools/parse_tests.rb +33 -0
  63. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +68 -0
  64. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +23 -0
  65. data/ext/ruby_http_parser/vendor/http-parser/README.md +246 -0
  66. data/ext/ruby_http_parser/vendor/http-parser/bench.c +111 -0
  67. data/ext/ruby_http_parser/vendor/http-parser/contrib/parsertrace.c +160 -0
  68. data/ext/ruby_http_parser/vendor/http-parser/contrib/url_parser.c +47 -0
  69. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +2470 -0
  70. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +111 -0
  71. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +432 -0
  72. data/ext/ruby_http_parser/vendor/http-parser/test.c +4226 -0
  73. data/ext/ruby_http_parser/vendor/http-parser/test_fast +0 -0
  74. data/ext/ruby_http_parser/vendor/http-parser/test_g +0 -0
  75. data/lib/http/parser.rb +1 -0
  76. data/lib/http_parser.rb +21 -0
  77. data/midori_http_parser.gemspec +24 -0
  78. data/spec/parser_spec.rb +376 -0
  79. data/spec/spec_helper.rb +1 -0
  80. data/spec/support/requests.json +631 -0
  81. data/spec/support/responses.json +375 -0
  82. data/tasks/compile.rake +42 -0
  83. data/tasks/fixtures.rake +71 -0
  84. data/tasks/spec.rake +5 -0
  85. data/tasks/submodules.rake +7 -0
  86. metadata +206 -0
@@ -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
+ }
@@ -0,0 +1,9 @@
1
+ package http_parser;
2
+
3
+ @SuppressWarnings("serial")
4
+ public class HTTPException extends RuntimeException {
5
+
6
+ public HTTPException(String mes) {
7
+ super(mes);
8
+ }
9
+ }
@@ -0,0 +1,113 @@
1
+ package http_parser;
2
+
3
+ import java.nio.charset.Charset;
4
+
5
+ public enum HTTPMethod {
6
+ HTTP_DELETE("DELETE")// = 0
7
+ , HTTP_GET("GET")
8
+ , HTTP_HEAD("HEAD")
9
+ , HTTP_POST("POST")
10
+ , HTTP_PUT("PUT")
11
+ , HTTP_PATCH("PATCH")
12
+ , HTTP_LINK("LINK")
13
+ , HTTP_UNLINK("UNLINK")
14
+ /* pathological */
15
+ , HTTP_CONNECT("CONNECT")
16
+ , HTTP_OPTIONS("OPTIONS")
17
+ , HTTP_TRACE("TRACE")
18
+ /* webdav */
19
+ , HTTP_COPY("COPY")
20
+ , HTTP_LOCK("LOCK")
21
+ , HTTP_MKCOL("MKCOL")
22
+ , HTTP_MOVE("MOVE")
23
+ , HTTP_PROPFIND("PROPFIND")
24
+ , HTTP_PROPPATCH("PROPPATCH")
25
+ , HTTP_UNLOCK("UNLOCK")
26
+ , HTTP_REPORT("REPORT")
27
+ , HTTP_MKACTIVITY("MKACTIVITY")
28
+ , HTTP_CHECKOUT("CHECKOUT")
29
+ , HTTP_MERGE("MERGE")
30
+ , HTTP_MSEARCH("M-SEARCH")
31
+ , HTTP_NOTIFY("NOTIFY")
32
+ , HTTP_SUBSCRIBE("SUBSCRIBE")
33
+ , HTTP_UNSUBSCRIBE("UNSUBSCRIBE")
34
+ , HTTP_PURGE("PURGE")
35
+ ;
36
+
37
+ private static Charset ASCII;
38
+ static {
39
+ ASCII = Charset.forName("US-ASCII");;
40
+ }
41
+ public byte[] bytes;
42
+
43
+ HTTPMethod(String name) {
44
+ // good grief, Charlie Brown, the following is necessary because
45
+ // java is retarded:
46
+ // illegal reference to static field from initializer
47
+ // this.bytes = name.getBytes(ASCII);
48
+ // yet it's not illegal to reference static fields from
49
+ // methods called from initializer.
50
+ init(name);
51
+ }
52
+ public static HTTPMethod parse(String s) {
53
+ if ("HTTP_DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
54
+ else if ("DELETE".equalsIgnoreCase(s)) {return HTTP_DELETE;}
55
+ else if ("HTTP_GET".equalsIgnoreCase(s)) {return HTTP_GET;}
56
+ else if ("GET".equalsIgnoreCase(s)) {return HTTP_GET;}
57
+ else if ("HTTP_HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
58
+ else if ("HEAD".equalsIgnoreCase(s)) {return HTTP_HEAD;}
59
+ else if ("HTTP_POST".equalsIgnoreCase(s)) {return HTTP_POST;}
60
+ else if ("POST".equalsIgnoreCase(s)) {return HTTP_POST;}
61
+ else if ("HTTP_PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
62
+ else if ("PUT".equalsIgnoreCase(s)) {return HTTP_PUT;}
63
+ else if ("HTTP_PATCH".equalsIgnoreCase(s)) {return HTTP_PATCH;}
64
+ else if ("PATCH".equalsIgnoreCase(s)) {return HTTP_PATCH;}
65
+ else if ("HTTP_CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
66
+ else if ("CONNECT".equalsIgnoreCase(s)) {return HTTP_CONNECT;}
67
+ else if ("HTTP_OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
68
+ else if ("OPTIONS".equalsIgnoreCase(s)) {return HTTP_OPTIONS;}
69
+ else if ("HTTP_TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
70
+ else if ("TRACE".equalsIgnoreCase(s)) {return HTTP_TRACE;}
71
+ else if ("HTTP_COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
72
+ else if ("COPY".equalsIgnoreCase(s)) {return HTTP_COPY;}
73
+ else if ("HTTP_LINK".equalsIgnoreCase(s)) {return HTTP_LINK;}
74
+ else if ("LINK".equalsIgnoreCase(s)) {return HTTP_LINK;}
75
+ else if ("HTTP_LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
76
+ else if ("LOCK".equalsIgnoreCase(s)) {return HTTP_LOCK;}
77
+ else if ("HTTP_MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
78
+ else if ("MKCOL".equalsIgnoreCase(s)) {return HTTP_MKCOL;}
79
+ else if ("HTTP_MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
80
+ else if ("MOVE".equalsIgnoreCase(s)) {return HTTP_MOVE;}
81
+ else if ("HTTP_PROPFIND".equalsIgnoreCase(s)){return HTTP_PROPFIND;}
82
+ else if ("PROPFIND".equalsIgnoreCase(s)) {return HTTP_PROPFIND;}
83
+ else if ("HTTP_PROPPATCH".equalsIgnoreCase(s)){return HTTP_PROPPATCH;}
84
+ else if ("PROPPATCH".equalsIgnoreCase(s)) {return HTTP_PROPPATCH;}
85
+ else if ("HTTP_UNLINK".equalsIgnoreCase(s)) {return HTTP_UNLINK;}
86
+ else if ("UNLINK".equalsIgnoreCase(s)) {return HTTP_UNLINK;}
87
+ else if ("HTTP_UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
88
+ else if ("UNLOCK".equalsIgnoreCase(s)) {return HTTP_UNLOCK;}
89
+ else if ("HTTP_REPORT".equalsIgnoreCase(s)) {return HTTP_REPORT;}
90
+ else if ("REPORT".equalsIgnoreCase(s)){return HTTP_REPORT;}
91
+ else if ("HTTP_MKACTIVITY".equalsIgnoreCase(s)) {return HTTP_MKACTIVITY;}
92
+ else if ("MKACTIVITY".equalsIgnoreCase(s)){return HTTP_MKACTIVITY;}
93
+ else if ("HTTP_CHECKOUT".equalsIgnoreCase(s)) {return HTTP_CHECKOUT;}
94
+ else if ("CHECKOUT".equalsIgnoreCase(s)){return HTTP_CHECKOUT;}
95
+ else if ("HTTP_MERGE".equalsIgnoreCase(s)) {return HTTP_MERGE;}
96
+ else if ("MERGE".equalsIgnoreCase(s)){return HTTP_MERGE;}
97
+ else if ("HTTP_MSEARCH".equalsIgnoreCase(s)) {return HTTP_MSEARCH;}
98
+ else if ("M-SEARCH".equalsIgnoreCase(s)) {return HTTP_MSEARCH;}
99
+ else if ("HTTP_NOTIFY".equalsIgnoreCase(s)) {return HTTP_NOTIFY;}
100
+ else if ("NOTIFY".equalsIgnoreCase(s)) {return HTTP_NOTIFY;}
101
+ else if ("HTTP_SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
102
+ else if ("SUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_SUBSCRIBE;}
103
+ else if ("HTTP_UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
104
+ else if ("UNSUBSCRIBE".equalsIgnoreCase(s)) {return HTTP_UNSUBSCRIBE;}
105
+ else if ("PATCH".equalsIgnoreCase(s)) {return HTTP_PATCH;}
106
+ else if ("PURGE".equalsIgnoreCase(s)) {return HTTP_PURGE;}
107
+ else {return null;}
108
+ }
109
+ void init (String name) {
110
+ ASCII = null == ASCII ? Charset.forName("US-ASCII") : ASCII;
111
+ this.bytes = name.getBytes(ASCII);
112
+ }
113
+ }
@@ -0,0 +1,36 @@
1
+ package http_parser;
2
+
3
+ import java.nio.ByteBuffer;
4
+
5
+ public class HTTPParser extends http_parser.lolevel.HTTPParser {
6
+
7
+ public HTTPParser() { super(); }
8
+ public HTTPParser(ParserType type) { super(type); }
9
+
10
+ public int getMajor() {
11
+ return super.http_major;
12
+ }
13
+
14
+ public int getMinor() {
15
+ return super.http_minor;
16
+ }
17
+
18
+ public int getStatusCode() {
19
+ return super.status_code;
20
+ }
21
+
22
+ public HTTPMethod getHTTPMethod() {
23
+ return super.method;
24
+ }
25
+
26
+ public boolean getUpgrade() {
27
+ return super.upgrade;
28
+ }
29
+
30
+ public boolean shouldKeepAlive() {
31
+ return super.http_should_keep_alive();
32
+ }
33
+ public void execute(ParserSettings settings, ByteBuffer data) {
34
+ this.execute(settings.getLoLevelSettings(), data);
35
+ }
36
+ }
@@ -0,0 +1,76 @@
1
+ package http_parser;
2
+
3
+ import http_parser.lolevel.*;
4
+ import http_parser.lolevel.HTTPParser;
5
+
6
+ import java.io.UnsupportedEncodingException;
7
+ import java.nio.ByteBuffer;
8
+ import java.util.Arrays;
9
+
10
+ /**
11
+ */
12
+ public class HTTPParserUrl {
13
+
14
+ public int field_set;
15
+ public int port;
16
+
17
+ public FieldData[] field_data = new FieldData[]{
18
+ new FieldData(0,0),
19
+ new FieldData(0,0),
20
+ new FieldData(0,0),
21
+ new FieldData(0,0),
22
+ new FieldData(0,0),
23
+ new FieldData(0,0)
24
+ }; //UF_MAX
25
+
26
+ public HTTPParserUrl(){}
27
+
28
+ public HTTPParserUrl(int field_set, int port, FieldData[] field_data){
29
+ this.field_set = field_set;
30
+ this.port = port;
31
+ this.field_data = field_data;
32
+ }
33
+
34
+ public String getFieldValue(HTTPParser.UrlFields field, ByteBuffer data) throws UnsupportedEncodingException {
35
+ FieldData fd = this.field_data[field.getIndex()];
36
+ if(fd.off == 0 & fd.len == 0) return "";
37
+ byte[] dst = new byte[fd.len];
38
+ int current_pos = data.position();
39
+ data.position(fd.off);
40
+ data.get(dst,0,fd.len);
41
+ data.position(current_pos);
42
+ String v = new String(dst, "UTF8");
43
+ return v;
44
+ }
45
+
46
+ @Override
47
+ public boolean equals(Object o) {
48
+ if (this == o) return true;
49
+ if (o == null || getClass() != o.getClass()) return false;
50
+
51
+ HTTPParserUrl that = (HTTPParserUrl) o;
52
+
53
+ if (field_set != that.field_set) return false;
54
+ if (port != that.port) return false;
55
+ if (!Arrays.equals(field_data, that.field_data)) return false;
56
+
57
+ return true;
58
+ }
59
+
60
+ @Override
61
+ public int hashCode() {
62
+ int result = field_set;
63
+ result = 31 * result + port;
64
+ result = 31 * result + Arrays.hashCode(field_data);
65
+ return result;
66
+ }
67
+
68
+ @Override
69
+ public String toString() {
70
+ return "HTTPParserUrl{" +
71
+ "field_set=" + field_set +
72
+ ", port=" + port +
73
+ ", field_data=" + (field_data == null ? null : Arrays.asList(field_data)) +
74
+ '}';
75
+ }
76
+ }
@@ -0,0 +1,256 @@
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 HTTPCallback on_status_complete;
15
+ public HTTPDataCallback on_header_field;
16
+ public HTTPDataCallback on_header_value;
17
+
18
+ public HTTPCallback on_headers_complete;
19
+ public HTTPDataCallback on_body;
20
+ public HTTPCallback on_message_complete;
21
+
22
+ public HTTPErrorCallback on_error;
23
+
24
+ private HTTPCallback _on_message_begin;
25
+ private HTTPDataCallback _on_path;
26
+ private HTTPDataCallback _on_query_string;
27
+ private HTTPDataCallback _on_url;
28
+ private HTTPDataCallback _on_fragment;
29
+ private HTTPCallback _on_status_complete;
30
+ private HTTPDataCallback _on_header_field;
31
+ private HTTPDataCallback _on_header_value;
32
+ private HTTPCallback _on_headers_complete;
33
+ private HTTPDataCallback _on_body;
34
+ private HTTPCallback _on_message_complete;
35
+ private HTTPErrorCallback _on_error;
36
+
37
+ private http_parser.lolevel.ParserSettings settings;
38
+
39
+ protected ByteList field = new ByteList();
40
+ protected ByteList value = new ByteList();
41
+ protected ByteList body = new ByteList();
42
+
43
+ public ParserSettings() {
44
+ this.settings = new http_parser.lolevel.ParserSettings();
45
+ createMirrorCallbacks();
46
+ attachCallbacks();
47
+ }
48
+
49
+ protected http_parser.lolevel.ParserSettings getLoLevelSettings() {
50
+ return this.settings;
51
+ }
52
+
53
+ private void createMirrorCallbacks() {
54
+ this._on_message_begin = new HTTPCallback() {
55
+ public int cb(HTTPParser p) {
56
+ if (null != ParserSettings.this.on_message_begin) {
57
+ return ParserSettings.this.on_message_begin.cb(p);
58
+ }
59
+ return 0;
60
+ }
61
+ };
62
+ this._on_path = new HTTPDataCallback() {
63
+ @Override
64
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
65
+ if (null != ParserSettings.this.on_path) {
66
+ return ParserSettings.this.on_path.cb(p, by, pos, len);
67
+ }
68
+ return 0;
69
+ }
70
+ };
71
+ this._on_query_string = new HTTPDataCallback() {
72
+ @Override
73
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
74
+ if (null != ParserSettings.this.on_query_string) {
75
+ return ParserSettings.this.on_query_string.cb(p, by, pos, len);
76
+ }
77
+ return 0;
78
+ }
79
+ };
80
+ this._on_url = new HTTPDataCallback() {
81
+ @Override
82
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
83
+ if (null != ParserSettings.this.on_url) {
84
+ return ParserSettings.this.on_url.cb(p, by, pos, len);
85
+ }
86
+ return 0;
87
+ }
88
+ };
89
+ this._on_fragment = new HTTPDataCallback() {
90
+ @Override
91
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
92
+ if (null != ParserSettings.this.on_fragment) {
93
+ return ParserSettings.this.on_fragment.cb(p, by, pos, len);
94
+ }
95
+ return 0;
96
+ }
97
+ };
98
+ this._on_status_complete = new HTTPCallback() {
99
+ @Override
100
+ public int cb(HTTPParser p) {
101
+ if (null != ParserSettings.this.on_status_complete) {
102
+ return ParserSettings.this.on_status_complete.cb(p);
103
+ }
104
+ return 0;
105
+ }
106
+ };
107
+ this._on_error = new HTTPErrorCallback() {
108
+ @Override
109
+ public void cb(HTTPParser parser, String error) {
110
+ if (null != ParserSettings.this.on_error) {
111
+ ParserSettings.this.on_error.cb(parser, error);
112
+ } else {
113
+ throw new HTTPException(error);
114
+ }
115
+
116
+ }
117
+ };
118
+
119
+
120
+
121
+ // (on_header_field and on_header_value shortened to on_h_*)
122
+ // ------------------------ ------------ --------------------------------------------
123
+ // | State (prev. callback) | Callback | Description/action |
124
+ // ------------------------ ------------ --------------------------------------------
125
+ // | nothing (first call) | on_h_field | Allocate new buffer and copy callback data |
126
+ // | | | into it |
127
+ // ------------------------ ------------ --------------------------------------------
128
+ // | value | on_h_field | New header started. |
129
+ // | | | Copy current name,value buffers to headers |
130
+ // | | | list and allocate new buffer for new name |
131
+ // ------------------------ ------------ --------------------------------------------
132
+ // | field | on_h_field | Previous name continues. Reallocate name |
133
+ // | | | buffer and append callback data to it |
134
+ // ------------------------ ------------ --------------------------------------------
135
+ // | field | on_h_value | Value for current header started. Allocate |
136
+ // | | | new buffer and copy callback data to it |
137
+ // ------------------------ ------------ --------------------------------------------
138
+ // | value | on_h_value | Value continues. Reallocate value buffer |
139
+ // | | | and append callback data to it |
140
+ // ------------------------ ------------ --------------------------------------------
141
+ this._on_header_field = new HTTPDataCallback() {
142
+ @Override
143
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
144
+ // previous value complete, call on_value with full value, reset value.
145
+ if (0 != ParserSettings.this.value.size()) {
146
+ // check we're even interested...
147
+ if (null != ParserSettings.this.on_header_value) {
148
+ byte [] valueArr = ParserSettings.this.value.toArray();
149
+ int ret = ParserSettings.this.on_header_value.cb(p, valueArr, 0, valueArr.length);
150
+ if (0 != ret) {
151
+ return ret;
152
+ }
153
+ ParserSettings.this.value.clear();
154
+ }
155
+ }
156
+
157
+ if (null == ParserSettings.this.on_header_field) {
158
+ return 0;
159
+ }
160
+
161
+ ParserSettings.this.field.addAll(by);
162
+ return 0;
163
+ }
164
+ };
165
+ this._on_header_value = new HTTPDataCallback() {
166
+ @Override
167
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
168
+
169
+ // previous field complete, call on_field with full field value, reset field.
170
+ if (0 != ParserSettings.this.field.size()) {
171
+ // check we're even interested...
172
+ if (null != ParserSettings.this.on_header_field) {
173
+ byte [] fieldArr = ParserSettings.this.field.toArray();
174
+ int ret = ParserSettings.this.on_header_field.cb(p, fieldArr, 0, fieldArr.length);
175
+ if (0 != ret) {
176
+ return ret;
177
+ }
178
+ ParserSettings.this.field.clear();
179
+ }
180
+ }
181
+
182
+ if (null == ParserSettings.this.on_header_value) {
183
+ return 0;
184
+ }
185
+ ParserSettings.this.value.addAll(by);
186
+ return 0;
187
+ }
188
+ };
189
+ this._on_headers_complete = new HTTPCallback() {
190
+ @Override
191
+ public int cb(HTTPParser parser) {
192
+ // is there an uncompleted value ... ?
193
+ if (0 != ParserSettings.this.value.size()) {
194
+ // check we're even interested...
195
+ if (null != ParserSettings.this.on_header_value) {
196
+ byte [] valueArr = ParserSettings.this.value.toArray();
197
+ int ret = ParserSettings.this.on_header_value.cb(parser, valueArr, 0, valueArr.length);
198
+ if (0 != ret) {
199
+ return ret;
200
+ }
201
+ ParserSettings.this.value.clear();
202
+ }
203
+ }
204
+ if (null != ParserSettings.this.on_headers_complete) {
205
+ return ParserSettings.this.on_headers_complete.cb(parser);
206
+ }
207
+ return 0;
208
+ }
209
+
210
+ };
211
+ this._on_body = new HTTPDataCallback() {
212
+ @Override
213
+ public int cb(HTTPParser p, byte[] by, int pos, int len) {
214
+ if (null != ParserSettings.this.on_body) {
215
+ ParserSettings.this.body.addAll(by, pos, len);
216
+ }
217
+ return 0;
218
+ }
219
+ };
220
+
221
+ this._on_message_complete = new HTTPCallback() {
222
+ @Override
223
+ public int cb(HTTPParser parser) {
224
+ if (null != ParserSettings.this.on_body) {
225
+ byte [] body = ParserSettings.this.body.toArray();
226
+ int ret = ParserSettings.this.on_body.cb(parser, body, 0, body.length);
227
+ if (0!=ret) {
228
+ return ret;
229
+ }
230
+ ParserSettings.this.body.clear();
231
+ }
232
+ if (null != ParserSettings.this.on_message_complete) {
233
+ return ParserSettings.this.on_message_complete.cb(parser);
234
+ }
235
+ return 0;
236
+ }
237
+ };
238
+
239
+ }
240
+
241
+ private void attachCallbacks() {
242
+ // these are certainly set, because we mirror them ...
243
+ this.settings.on_message_begin = this._on_message_begin;
244
+ this.settings.on_path = this._on_path;
245
+ this.settings.on_query_string = this._on_query_string;
246
+ this.settings.on_url = this._on_url;
247
+ this.settings.on_fragment = this._on_fragment;
248
+ this.settings.on_status_complete = this._on_status_complete;
249
+ this.settings.on_header_field = this._on_header_field;
250
+ this.settings.on_header_value = this._on_header_value;
251
+ this.settings.on_headers_complete = this._on_headers_complete;
252
+ this.settings.on_body = this._on_body;
253
+ this.settings.on_message_complete = this._on_message_complete;
254
+ this.settings.on_error = this._on_error;
255
+ }
256
+ }