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
data/.gitmodules CHANGED
@@ -1,6 +1,6 @@
1
- [submodule "ext/ruby_http_parser/vendor/http-parser"]
1
+ [submodule "http-parser"]
2
2
  path = ext/ruby_http_parser/vendor/http-parser
3
- url = git://github.com/ry/http-parser.git
4
- [submodule "ext/ruby_http_parser/vendor/http-parser-java"]
3
+ url = git://github.com/joyent/http-parser.git
4
+ [submodule "http-parser-java"]
5
5
  path = ext/ruby_http_parser/vendor/http-parser-java
6
- url = git://github.com/a2800276/http-parser.java.git
6
+ url = git://github.com/http-parser/http-parser.java
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
data/Gemfile.lock CHANGED
@@ -1,16 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- http_parser.rb (0.5.1)
4
+ http_parser.rb (0.5.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
+ benchmark_suite (0.8.0)
9
10
  diff-lcs (1.1.2)
11
+ ffi (1.0.11)
12
+ ffi (1.0.11-java)
10
13
  json (1.5.1)
11
14
  json (1.5.1-java)
12
- rake (0.8.7)
13
- rake-compiler (0.7.5)
15
+ rake (0.9.2)
16
+ rake-compiler (0.7.9)
14
17
  rake
15
18
  rspec (2.4.0)
16
19
  rspec-core (~> 2.4.0)
@@ -20,13 +23,17 @@ GEM
20
23
  rspec-expectations (2.4.0)
21
24
  diff-lcs (~> 1.1.2)
22
25
  rspec-mocks (2.4.0)
26
+ yajl-ruby (1.1.0)
23
27
 
24
28
  PLATFORMS
25
29
  java
26
30
  ruby
27
31
 
28
32
  DEPENDENCIES
33
+ benchmark_suite
34
+ ffi
29
35
  http_parser.rb!
30
36
  json (>= 1.4.6)
31
- rake-compiler (>= 0.7.5)
37
+ rake-compiler (>= 0.7.9)
32
38
  rspec (>= 2.0.1)
39
+ yajl-ruby (>= 0.8.1)
data/LICENSE-MIT ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2009,2010 Marc-André Cournoyer <macournoyer@gmail.com>
2
+ Copyright 2010,2011 Aman Gupta <aman@tmm1.net>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to
6
+ deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
+ sell copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
+ IN THE SOFTWARE.
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  A simple callback-based HTTP request/response parser for writing http
4
4
  servers, clients and proxies.
5
5
 
6
- This gem is built on top of [ry/http-parser](http://github.com/ry/http-parser) and its java port [a2800276/http-parser.java](http://github.com/a2800276/http-parser.java).
6
+ This gem is built on top of [joyent/http-parser](http://github.com/joyent/http-parser) and its java port [a2800276/http-parser.java](http://github.com/a2800276/http-parser.java).
7
7
 
8
8
  ## Supported Platforms
9
9
 
@@ -16,70 +16,75 @@ This gem aims to work on all major Ruby platforms, including:
16
16
 
17
17
  ## Usage
18
18
 
19
- require "http/parser"
19
+ ```ruby
20
+ require "http/parser"
20
21
 
21
- parser = Http::Parser.new
22
+ parser = Http::Parser.new
22
23
 
23
- parser.on_headers_complete = proc do
24
- p parser.http_version
24
+ parser.on_headers_complete = proc do
25
+ p parser.http_version
25
26
 
26
- p parser.http_method # for requests
27
- p parser.request_url
27
+ p parser.http_method # for requests
28
+ p parser.request_url
28
29
 
29
- p parser.status_code # for responses
30
+ p parser.status_code # for responses
30
31
 
31
- p parser.headers
32
- end
32
+ p parser.headers
33
+ end
33
34
 
34
- parser.on_body = proc do |chunk|
35
- # One chunk of the body
36
- p chunk
37
- end
35
+ parser.on_body = proc do |chunk|
36
+ # One chunk of the body
37
+ p chunk
38
+ end
38
39
 
39
- parser.on_message_complete = proc do |env|
40
- # Headers and body is all parsed
41
- puts "Done!"
42
- end
40
+ parser.on_message_complete = proc do |env|
41
+ # Headers and body is all parsed
42
+ puts "Done!"
43
+ end
44
+ ```
43
45
 
44
- # Feed raw data from the socket to the parser
45
- parser << raw_data
46
+ # Feed raw data from the socket to the parser
47
+ `parser << raw_data`
46
48
 
47
49
  ## Advanced Usage
48
50
 
49
51
  ### Accept callbacks on an object
50
52
 
51
- module MyHttpConnection
52
- def connection_completed
53
- @parser = Http::Parser.new(self)
54
- end
53
+ ```ruby
54
+ module MyHttpConnection
55
+ def connection_completed
56
+ @parser = Http::Parser.new(self)
57
+ end
55
58
 
56
- def receive_data(data)
57
- @parser << data
58
- end
59
+ def receive_data(data)
60
+ @parser << data
61
+ end
59
62
 
60
- def on_message_begin
61
- @headers = nil
62
- @body = ''
63
- end
63
+ def on_message_begin
64
+ @headers = nil
65
+ @body = ''
66
+ end
64
67
 
65
- def on_headers_complete
66
- @headers = @parser.headers
67
- end
68
+ def on_headers_complete(headers)
69
+ @headers = headers
70
+ end
68
71
 
69
- def on_body(chunk)
70
- @body << chunk
71
- end
72
+ def on_body(chunk)
73
+ @body << chunk
74
+ end
72
75
 
73
- def on_message_complete
74
- p [@headers, @body]
75
- end
76
- end
76
+ def on_message_complete
77
+ p [@headers, @body]
78
+ end
79
+ end
80
+ ```
77
81
 
78
82
  ### Stop parsing after headers
79
83
 
80
- parser = Http::Parser.new
81
- parser.on_headers_complete = proc{ :stop }
82
-
83
- offset = parser << request_data
84
- body = request_data[offset..-1]
84
+ ```ruby
85
+ parser = Http::Parser.new
86
+ parser.on_headers_complete = proc{ :stop }
85
87
 
88
+ offset = parser << request_data
89
+ body = request_data[offset..-1]
90
+ ```
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.dirname(__FILE__) + "/../lib"
3
+ require "rubygems"
4
+ require "http/parser"
5
+ require "benchmark/ips"
6
+
7
+ request = <<-REQUEST
8
+ GET / HTTP/1.1
9
+ Host: www.example.com
10
+ Connection: keep-alive
11
+ User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.78 S
12
+ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
13
+ Accept-Encoding: gzip,deflate,sdch
14
+ Accept-Language: en-US,en;q=0.8
15
+ Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
16
+
17
+ REQUEST
18
+ request.gsub!(/\n/m, "\r\n")
19
+
20
+ Benchmark.ips do |ips|
21
+ ips.report("instance") { Http::Parser.new }
22
+ ips.report("parsing") { Http::Parser.new << request }
23
+ end
data/bench/thin.rb CHANGED
@@ -3,6 +3,7 @@ require "rubygems"
3
3
  require "thin_parser"
4
4
  require "http_parser"
5
5
  require "benchmark"
6
+ require "stringio"
6
7
 
7
8
  data = "POST /postit HTTP/1.1\r\n" +
8
9
  "Host: localhost:3000\r\n" +
@@ -1,12 +1,14 @@
1
1
  package org.ruby_http_parser;
2
2
 
3
3
  import org.jruby.Ruby;
4
+ import org.jruby.RubyArray;
4
5
  import org.jruby.RubyClass;
5
6
  import org.jruby.RubyHash;
6
7
  import org.jruby.RubyModule;
7
8
  import org.jruby.RubyNumeric;
8
9
  import org.jruby.RubyObject;
9
10
  import org.jruby.RubyString;
11
+ import org.jruby.RubySymbol;
10
12
 
11
13
  import org.jruby.runtime.ObjectAllocator;
12
14
  import org.jruby.runtime.ThreadContext;
@@ -15,6 +17,10 @@ import org.jruby.runtime.builtin.IRubyObject;
15
17
  import org.jruby.anno.JRubyMethod;
16
18
  import org.jruby.exceptions.RaiseException;
17
19
 
20
+ import org.jruby.util.ByteList;
21
+
22
+ import org.jcodings.specific.UTF8Encoding;
23
+
18
24
  import java.nio.ByteBuffer;
19
25
  import http_parser.*;
20
26
  import http_parser.lolevel.ParserSettings;
@@ -23,6 +29,11 @@ import http_parser.lolevel.HTTPDataCallback;
23
29
 
24
30
  public class RubyHttpParser extends RubyObject {
25
31
 
32
+ @JRubyMethod(name = "strict?", module = true)
33
+ public static IRubyObject strict(IRubyObject recv) {
34
+ return recv.getRuntime().newBoolean(true);
35
+ }
36
+
26
37
  public static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
27
38
  public IRubyObject allocate(Ruby runtime, RubyClass klass) {
28
39
  return new RubyHttpParser(runtime, klass);
@@ -59,10 +70,15 @@ public class RubyHttpParser extends RubyObject {
59
70
  private IRubyObject queryString;
60
71
  private IRubyObject fragment;
61
72
 
73
+ private IRubyObject header_value_type;
74
+ private IRubyObject upgradeData;
75
+
62
76
  private IRubyObject callback_object;
63
77
 
64
- private String _current_header;
65
- private String _last_header;
78
+ private boolean completed;
79
+
80
+ private byte[] _current_header;
81
+ private byte[] _last_header;
66
82
 
67
83
  public RubyHttpParser(final Ruby runtime, RubyClass clazz) {
68
84
  super(runtime,clazz);
@@ -77,6 +93,10 @@ public class RubyHttpParser extends RubyObject {
77
93
 
78
94
  this.callback_object = null;
79
95
 
96
+ this.completed = false;
97
+
98
+ this.header_value_type = runtime.getModule("HTTP").getClass("Parser").getInstanceVariable("@default_header_value_type");
99
+
80
100
  initSettings();
81
101
  init();
82
102
  }
@@ -87,28 +107,7 @@ public class RubyHttpParser extends RubyObject {
87
107
  this.settings.on_url = new HTTPDataCallback() {
88
108
  public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
89
109
  byte[] data = fetchBytes(buf, pos, len);
90
- ((RubyString)requestUrl).concat(runtime.newString(new String(data)));
91
- return 0;
92
- }
93
- };
94
- this.settings.on_path = new HTTPDataCallback() {
95
- public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
96
- byte[] data = fetchBytes(buf, pos, len);
97
- ((RubyString)requestPath).concat(runtime.newString(new String(data)));
98
- return 0;
99
- }
100
- };
101
- this.settings.on_query_string = new HTTPDataCallback() {
102
- public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
103
- byte[] data = fetchBytes(buf, pos, len);
104
- ((RubyString)queryString).concat(runtime.newString(new String(data)));
105
- return 0;
106
- }
107
- };
108
- this.settings.on_fragment = new HTTPDataCallback() {
109
- public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
110
- byte[] data = fetchBytes(buf, pos, len);
111
- ((RubyString)fragment).concat(runtime.newString(new String(data)));
110
+ ((RubyString)requestUrl).cat(data);
112
111
  return 0;
113
112
  }
114
113
  };
@@ -118,37 +117,65 @@ public class RubyHttpParser extends RubyObject {
118
117
  byte[] data = fetchBytes(buf, pos, len);
119
118
 
120
119
  if (_current_header == null)
121
- _current_header = new String(data);
122
- else
123
- _current_header = _current_header.concat(new String(data));
120
+ _current_header = data;
121
+ else {
122
+ byte[] tmp = new byte[_current_header.length + data.length];
123
+ System.arraycopy(_current_header, 0, tmp, 0, _current_header.length);
124
+ System.arraycopy(data, 0, tmp, _current_header.length, data.length);
125
+ _current_header = tmp;
126
+ }
124
127
 
125
128
  return 0;
126
129
  }
127
130
  };
131
+ final RubySymbol arraysSym = runtime.newSymbol("arrays");
132
+ final RubySymbol mixedSym = runtime.newSymbol("mixed");
133
+ final RubySymbol stopSym = runtime.newSymbol("stop");
134
+ final RubySymbol resetSym = runtime.newSymbol("reset");
128
135
  this.settings.on_header_value = new HTTPDataCallback() {
129
136
  public int cb (http_parser.lolevel.HTTPParser p, ByteBuffer buf, int pos, int len) {
130
137
  byte[] data = fetchBytes(buf, pos, len);
131
138
  ThreadContext context = headers.getRuntime().getCurrentContext();
132
139
  IRubyObject key, val;
140
+ int new_field = 0;
133
141
 
134
142
  if (_current_header != null) {
143
+ new_field = 1;
135
144
  _last_header = _current_header;
136
145
  _current_header = null;
146
+ }
137
147
 
138
- key = (IRubyObject)runtime.newString(_last_header);
148
+ key = RubyString.newString(runtime, new ByteList(_last_header, UTF8Encoding.INSTANCE, false));
149
+ val = headers.op_aref(context, key);
150
+
151
+ if (new_field == 1) {
152
+ if (val.isNil()) {
153
+ if (header_value_type == arraysSym) {
154
+ headers.op_aset(context, key, RubyArray.newArrayLight(runtime, RubyString.newStringLight(runtime, 10)));
155
+ } else {
156
+ headers.op_aset(context, key, RubyString.newStringLight(runtime, 10));
157
+ }
158
+ } else {
159
+ if (header_value_type == mixedSym) {
160
+ if (val instanceof RubyString) {
161
+ headers.op_aset(context, key, RubyArray.newArrayLight(runtime, val, RubyString.newStringLight(runtime, 10)));
162
+ } else {
163
+ ((RubyArray)val).add(RubyString.newStringLight(runtime, 10));
164
+ }
165
+ } else if (header_value_type == arraysSym) {
166
+ ((RubyArray)val).add(RubyString.newStringLight(runtime, 10));
167
+ } else {
168
+ ((RubyString)val).cat(',').cat(' ');
169
+ }
170
+ }
139
171
  val = headers.op_aref(context, key);
172
+ }
140
173
 
141
- if (!val.isNil())
142
- ((RubyString)val).cat(", ".getBytes());
143
- } else {
144
- key = (IRubyObject)runtime.newString(_last_header);
145
- val = headers.op_aref(context, key);
174
+ if (val instanceof RubyArray) {
175
+ val = ((RubyArray)val).entry(-1);
146
176
  }
147
177
 
148
- if (val.isNil())
149
- headers.op_aset(context, key, runtime.newString(new String(data)));
150
- else
151
- ((RubyString)val).cat(data);
178
+ ((RubyString)val).cat(data);
152
179
 
153
180
  return 0;
154
181
  }
@@ -158,15 +185,17 @@ public class RubyHttpParser extends RubyObject {
158
185
  public int cb (http_parser.lolevel.HTTPParser p) {
159
186
  headers = new RubyHash(runtime);
160
187
 
161
- requestUrl = runtime.newString("");
162
- requestPath = runtime.newString("");
163
- queryString = runtime.newString("");
164
- fragment = runtime.newString("");
188
+ requestUrl = RubyString.newEmptyString(runtime);
189
+ requestPath = RubyString.newEmptyString(runtime);
190
+ queryString = RubyString.newEmptyString(runtime);
191
+ fragment = RubyString.newEmptyString(runtime);
192
+
193
+ upgradeData = RubyString.newEmptyString(runtime);
165
194
 
166
195
  IRubyObject ret = runtime.getNil();
167
196
 
168
197
  if (callback_object != null) {
169
- if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_message_begin")).toJava(Boolean.class) == Boolean.TRUE) {
198
+ if (((RubyObject)callback_object).respondsTo("on_message_begin")) {
170
199
  ThreadContext context = callback_object.getRuntime().getCurrentContext();
171
200
  ret = callback_object.callMethod(context, "on_message_begin");
172
201
  }
@@ -175,7 +204,7 @@ public class RubyHttpParser extends RubyObject {
175
204
  ret = on_message_begin.callMethod(context, "call");
176
205
  }
177
206
 
178
- if (ret == runtime.newSymbol("stop")) {
207
+ if (ret == stopSym) {
179
208
  throw new StopException();
180
209
  } else {
181
210
  return 0;
@@ -186,8 +215,10 @@ public class RubyHttpParser extends RubyObject {
186
215
  public int cb (http_parser.lolevel.HTTPParser p) {
187
216
  IRubyObject ret = runtime.getNil();
188
217
 
218
+ completed = true;
219
+
189
220
  if (callback_object != null) {
190
- if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_message_complete")).toJava(Boolean.class) == Boolean.TRUE) {
221
+ if (((RubyObject)callback_object).respondsTo("on_message_complete")) {
191
222
  ThreadContext context = callback_object.getRuntime().getCurrentContext();
192
223
  ret = callback_object.callMethod(context, "on_message_complete");
193
224
  }
@@ -196,7 +227,7 @@ public class RubyHttpParser extends RubyObject {
196
227
  ret = on_message_complete.callMethod(context, "call");
197
228
  }
198
229
 
199
- if (ret == runtime.newSymbol("stop")) {
230
+ if (ret == stopSym) {
200
231
  throw new StopException();
201
232
  } else {
202
233
  return 0;
@@ -208,7 +239,7 @@ public class RubyHttpParser extends RubyObject {
208
239
  IRubyObject ret = runtime.getNil();
209
240
 
210
241
  if (callback_object != null) {
211
- if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_headers_complete")).toJava(Boolean.class) == Boolean.TRUE) {
242
+ if (((RubyObject)callback_object).respondsTo("on_headers_complete")) {
212
243
  ThreadContext context = callback_object.getRuntime().getCurrentContext();
213
244
  ret = callback_object.callMethod(context, "on_headers_complete", headers);
214
245
  }
@@ -217,8 +248,10 @@ public class RubyHttpParser extends RubyObject {
217
248
  ret = on_headers_complete.callMethod(context, "call", headers);
218
249
  }
219
250
 
220
- if (ret == runtime.newSymbol("stop")) {
251
+ if (ret == stopSym) {
221
252
  throw new StopException();
253
+ } else if (ret == resetSym) {
254
+ return 1;
222
255
  } else {
223
256
  return 0;
224
257
  }
@@ -230,16 +263,16 @@ public class RubyHttpParser extends RubyObject {
230
263
  byte[] data = fetchBytes(buf, pos, len);
231
264
 
232
265
  if (callback_object != null) {
233
- if (((RubyObject)callback_object).respond_to_p(runtime.newSymbol("on_body")).toJava(Boolean.class) == Boolean.TRUE) {
266
+ if (((RubyObject)callback_object).respondsTo("on_body")) {
234
267
  ThreadContext context = callback_object.getRuntime().getCurrentContext();
235
- ret = callback_object.callMethod(context, "on_body", callback_object.getRuntime().newString(new String(data)));
268
+ ret = callback_object.callMethod(context, "on_body", RubyString.newString(runtime, new ByteList(data, UTF8Encoding.INSTANCE, false)));
236
269
  }
237
270
  } else if (on_body != null) {
238
271
  ThreadContext context = on_body.getRuntime().getCurrentContext();
239
- ret = on_body.callMethod(context, "call", on_body.getRuntime().newString(new String(data)));
272
+ ret = on_body.callMethod(context, "call", RubyString.newString(runtime, new ByteList(data, UTF8Encoding.INSTANCE, false)));
240
273
  }
241
274
 
242
- if (ret == runtime.newSymbol("stop")) {
275
+ if (ret == stopSym) {
243
276
  throw new StopException();
244
277
  } else {
245
278
  return 0;
@@ -250,12 +283,15 @@ public class RubyHttpParser extends RubyObject {
250
283
 
251
284
  private void init() {
252
285
  this.parser = new HTTPParser();
286
+ this.parser.HTTP_PARSER_STRICT = true;
253
287
  this.headers = null;
254
288
 
255
289
  this.requestUrl = runtime.getNil();
256
290
  this.requestPath = runtime.getNil();
257
291
  this.queryString = runtime.getNil();
258
292
  this.fragment = runtime.getNil();
293
+
294
+ this.upgradeData = runtime.getNil();
259
295
  }
260
296
 
261
297
  @JRubyMethod(name = "initialize")
@@ -269,6 +305,12 @@ public class RubyHttpParser extends RubyObject {
269
305
  return initialize();
270
306
  }
271
307
 
308
+ @JRubyMethod(name = "initialize")
309
+ public IRubyObject initialize(IRubyObject arg, IRubyObject arg2) {
310
+ header_value_type = arg2;
311
+ return initialize(arg);
312
+ }
313
+
272
314
  @JRubyMethod(name = "on_message_begin=")
273
315
  public IRubyObject set_on_message_begin(IRubyObject cb) {
274
316
  on_message_begin = cb;
@@ -296,7 +338,8 @@ public class RubyHttpParser extends RubyObject {
296
338
  @JRubyMethod(name = "<<")
297
339
  public IRubyObject execute(IRubyObject data) {
298
340
  RubyString str = (RubyString)data;
299
- ByteBuffer buf = ByteBuffer.wrap(str.getBytes());
341
+ ByteList byteList = str.getByteList();
342
+ ByteBuffer buf = ByteBuffer.wrap(byteList.getUnsafeBytes(), byteList.getBegin(), byteList.getRealSize());
300
343
  boolean stopped = false;
301
344
 
302
345
  try {
@@ -308,8 +351,10 @@ public class RubyHttpParser extends RubyObject {
308
351
  }
309
352
 
310
353
  if (parser.getUpgrade()) {
311
- // upgrade request
312
- } else if (buf.hasRemaining()) {
354
+ byte[] upData = fetchBytes(buf, buf.position(), buf.limit() - buf.position());
355
+ ((RubyString)upgradeData).cat(upData);
356
+
357
+ } else if (buf.hasRemaining() && !completed) {
313
358
  if (!stopped)
314
359
  throw new RaiseException(runtime, eParserError, "Could not parse data entirely", true);
315
360
  }
@@ -319,12 +364,12 @@ public class RubyHttpParser extends RubyObject {
319
364
 
320
365
  @JRubyMethod(name = "keep_alive?")
321
366
  public IRubyObject shouldKeepAlive() {
322
- return parser.shouldKeepAlive() ? runtime.getTrue() : runtime.getFalse();
367
+ return runtime.newBoolean(parser.shouldKeepAlive());
323
368
  }
324
369
 
325
370
  @JRubyMethod(name = "upgrade?")
326
371
  public IRubyObject shouldUpgrade() {
327
- return parser.getUpgrade() ? runtime.getTrue() : runtime.getFalse();
372
+ return runtime.newBoolean(parser.getUpgrade());
328
373
  }
329
374
 
330
375
  @JRubyMethod(name = "http_major")
@@ -394,6 +439,26 @@ public class RubyHttpParser extends RubyObject {
394
439
  return fragment == null ? runtime.getNil() : fragment;
395
440
  }
396
441
 
442
+ @JRubyMethod(name = "header_value_type")
443
+ public IRubyObject getHeaderValueType() {
444
+ return header_value_type == null ? runtime.getNil() : header_value_type;
445
+ }
446
+
447
+ @JRubyMethod(name = "header_value_type=")
448
+ public IRubyObject set_header_value_type(IRubyObject val) {
449
+ String valString = val.toString();
450
+ if (valString != "mixed" && valString != "arrays" && valString != "strings") {
451
+ throw runtime.newArgumentError("Invalid header value type");
452
+ }
453
+ header_value_type = val;
454
+ return val;
455
+ }
456
+
457
+ @JRubyMethod(name = "upgrade_data")
458
+ public IRubyObject upgradeData() {
459
+ return upgradeData == null ? runtime.getNil() : upgradeData;
460
+ }
461
+
397
462
  @JRubyMethod(name = "reset!")
398
463
  public IRubyObject reset() {
399
464
  init();