http_parser.rb 0.5.1 → 0.6.0.beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. data/.gitmodules +4 -4
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +11 -4
  4. data/LICENSE-MIT +20 -0
  5. data/README.md +51 -46
  6. data/bench/standalone.rb +23 -0
  7. data/bench/thin.rb +1 -0
  8. data/ext/ruby_http_parser/org/ruby_http_parser/RubyHttpParser.java +121 -56
  9. data/ext/ruby_http_parser/ruby_http_parser.c +89 -48
  10. data/ext/ruby_http_parser/vendor/http-parser/AUTHORS +32 -0
  11. data/ext/ruby_http_parser/vendor/http-parser/LICENSE-MIT +5 -1
  12. data/ext/ruby_http_parser/vendor/http-parser/Makefile +34 -17
  13. data/ext/ruby_http_parser/vendor/http-parser/README.md +9 -2
  14. data/ext/ruby_http_parser/vendor/http-parser/http_parser.c +1045 -589
  15. data/ext/ruby_http_parser/vendor/http-parser/http_parser.gyp +79 -0
  16. data/ext/ruby_http_parser/vendor/http-parser/http_parser.h +148 -17
  17. data/ext/ruby_http_parser/vendor/http-parser/test.c +1138 -128
  18. data/ext/ruby_http_parser/vendor/http-parser-java/AUTHORS +32 -0
  19. data/ext/ruby_http_parser/vendor/http-parser-java/LICENSE-MIT +30 -1
  20. data/ext/ruby_http_parser/vendor/http-parser-java/Makefile +34 -17
  21. data/ext/ruby_http_parser/vendor/http-parser-java/README.md +30 -18
  22. data/ext/ruby_http_parser/vendor/http-parser-java/TODO +9 -0
  23. data/ext/ruby_http_parser/vendor/http-parser-java/build.xml +74 -0
  24. data/ext/ruby_http_parser/vendor/http-parser-java/ext/primitives.jar +0 -0
  25. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.c +1075 -607
  26. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.gyp +79 -0
  27. data/ext/ruby_http_parser/vendor/http-parser-java/http_parser.h +190 -40
  28. data/ext/ruby_http_parser/vendor/http-parser-java/src/Http-parser.java.iml +22 -0
  29. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/FieldData.java +41 -0
  30. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPCallback.java +8 -0
  31. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPDataCallback.java +34 -0
  32. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPErrorCallback.java +12 -0
  33. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPException.java +4 -2
  34. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPHeadersCompleteCallback.java +13 -0
  35. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPMethod.java +67 -52
  36. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParser.java +5 -0
  37. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/HTTPParserUrl.java +76 -0
  38. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/ParserSettings.java +323 -0
  39. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/{lolevel/Util.java → Util.java} +34 -35
  40. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPHeadersCompleteCallback.java +12 -0
  41. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/HTTPParser.java +839 -587
  42. data/ext/ruby_http_parser/vendor/http-parser-java/src/impl/http_parser/lolevel/ParserSettings.java +2 -1
  43. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Message.java +374 -0
  44. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/ParseUrl.java +51 -0
  45. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Requests.java +69 -0
  46. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Responses.java +52 -0
  47. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Test.java +16 -0
  48. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestHeaderOverflowError.java +48 -0
  49. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestLoaderNG.java +171 -446
  50. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/TestNoOverflowLongBody.java +62 -0
  51. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/UnitTest.java +3 -1
  52. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Upgrade.java +27 -0
  53. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Url.java +127 -0
  54. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/Util.java +236 -0
  55. data/ext/ruby_http_parser/vendor/http-parser-java/src/test/http_parser/lolevel/WrongContentLength.java +59 -0
  56. data/ext/ruby_http_parser/vendor/http-parser-java/test.c +1318 -184
  57. data/ext/ruby_http_parser/vendor/http-parser-java/tests.dumped +355 -49
  58. data/ext/ruby_http_parser/vendor/http-parser-java/tests.utf8 +17 -0
  59. data/http_parser.rb.gemspec +10 -2
  60. data/lib/http_parser.rb +17 -0
  61. data/spec/parser_spec.rb +157 -23
  62. data/spec/support/requests.json +236 -24
  63. data/spec/support/responses.json +182 -36
  64. data/tasks/compile.rake +5 -3
  65. data/tasks/fixtures.rake +8 -2
  66. metadata +209 -127
  67. data/ext/ruby_http_parser/vendor/http-parser-java/CONTRIBUTIONS +0 -4
  68. data/ext/ruby_http_parser/vendor/http-parser-java/compile +0 -1
  69. data/ext/ruby_http_parser/vendor/http-parser-java/test_permutations +0 -1
  70. data/ext/ruby_http_parser/vendor/http-parser-java/test_unit +0 -1
@@ -0,0 +1,32 @@
1
+ # Authors ordered by first contribution.
2
+ Ryan Dahl <ry@tinyclouds.org>
3
+ Jeremy Hinegardner <jeremy@hinegardner.org>
4
+ Sergey Shepelev <temotor@gmail.com>
5
+ Joe Damato <ice799@gmail.com>
6
+ tomika <tomika_nospam@freemail.hu>
7
+ Phoenix Sol <phoenix@burninglabs.com>
8
+ Cliff Frey <cliff@meraki.com>
9
+ Ewen Cheslack-Postava <ewencp@cs.stanford.edu>
10
+ Santiago Gala <sgala@apache.org>
11
+ Tim Becker <tim.becker@syngenio.de>
12
+ Jeff Terrace <jterrace@gmail.com>
13
+ Ben Noordhuis <info@bnoordhuis.nl>
14
+ Nathan Rajlich <nathan@tootallnate.net>
15
+ Mark Nottingham <mnot@mnot.net>
16
+ Aman Gupta <aman@tmm1.net>
17
+ Tim Becker <tim.becker@kuriositaet.de>
18
+ Sean Cunningham <sean.cunningham@mandiant.com>
19
+ Peter Griess <pg@std.in>
20
+ Salman Haq <salman.haq@asti-usa.com>
21
+ Cliff Frey <clifffrey@gmail.com>
22
+ Jon Kolb <jon@b0g.us>
23
+ Fouad Mardini <f.mardini@gmail.com>
24
+ Paul Querna <pquerna@apache.org>
25
+ Felix Geisendörfer <felix@debuggable.com>
26
+ koichik <koichik@improvement.jp>
27
+ Andre Caron <andre.l.caron@gmail.com>
28
+ Ivo Raisr <ivosh@ivosh.net>
29
+ James McLaughlin <jamie@lacewing-project.org>
30
+ David Gwynne <loki@animata.net>
31
+ LE ROUX Thomas <thomas@procheo.fr>
32
+ Randy Rizun <rrizun@ortivawireless.com>
@@ -1,4 +1,33 @@
1
- Copyright 2009,2010 Ryan Dahl <ry@tinyclouds.org>
1
+ Copyright 2010 Tim Becker <tim.becker@kuriositaet.de>
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
+ --- END OF LICENSE
22
+
23
+ This code mainly based on code with the following license:
24
+
25
+
26
+ http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
27
+ Igor Sysoev.
28
+
29
+ Additional changes are licensed under the same terms as NGINX and
30
+ copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
31
 
3
32
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
33
  of this software and associated documentation files (the "Software"), to
@@ -1,41 +1,58 @@
1
- OPT_DEBUG=-O0 -g -Wall -Wextra -Werror -I.
2
- OPT_FAST=-O3 -DHTTP_PARSER_STRICT=0 -I.
3
-
4
1
  CC?=gcc
2
+ AR?=ar
3
+
4
+ CPPFLAGS += -I.
5
+ CPPFLAGS_DEBUG = $(CPPFLAGS) -DHTTP_PARSER_STRICT=1 -DHTTP_PARSER_DEBUG=1
6
+ CPPFLAGS_DEBUG += $(CPPFLAGS_DEBUG_EXTRA)
7
+ CPPFLAGS_FAST = $(CPPFLAGS) -DHTTP_PARSER_STRICT=0 -DHTTP_PARSER_DEBUG=0
8
+ CPPFLAGS_FAST += $(CPPFLAGS_FAST_EXTRA)
5
9
 
10
+ CFLAGS += -Wall -Wextra -Werror
11
+ CFLAGS_DEBUG = $(CFLAGS) -O0 -g $(CFLAGS_DEBUG_EXTRA)
12
+ CFLAGS_FAST = $(CFLAGS) -O3 $(CFLAGS_FAST_EXTRA)
13
+ CFLAGS_LIB = $(CFLAGS_FAST) -fPIC
6
14
 
7
- test: test_g
15
+ test: test_g test_fast
8
16
  ./test_g
17
+ ./test_fast
9
18
 
10
19
  test_g: http_parser_g.o test_g.o
11
- $(CC) $(OPT_DEBUG) http_parser_g.o test_g.o -o $@
20
+ $(CC) $(CFLAGS_DEBUG) $(LDFLAGS) http_parser_g.o test_g.o -o $@
12
21
 
13
22
  test_g.o: test.c http_parser.h Makefile
14
- $(CC) $(OPT_DEBUG) -c test.c -o $@
15
-
16
- test.o: test.c http_parser.h Makefile
17
- $(CC) $(OPT_FAST) -c test.c -o $@
23
+ $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) -c test.c -o $@
18
24
 
19
25
  http_parser_g.o: http_parser.c http_parser.h Makefile
20
- $(CC) $(OPT_DEBUG) -c http_parser.c -o $@
26
+ $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) -c http_parser.c -o $@
21
27
 
22
- test-valgrind: test_g
23
- valgrind ./test_g
28
+ test_fast: http_parser.o test.o http_parser.h
29
+ $(CC) $(CFLAGS_FAST) $(LDFLAGS) http_parser.o test.o -o $@
24
30
 
25
- http_parser.o: http_parser.c http_parser.h Makefile
26
- $(CC) $(OPT_FAST) -c http_parser.c
31
+ test.o: test.c http_parser.h Makefile
32
+ $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c test.c -o $@
27
33
 
28
- test_fast: http_parser.o test.c http_parser.h
29
- $(CC) $(OPT_FAST) http_parser.o test.c -o $@
34
+ http_parser.o: http_parser.c http_parser.h Makefile
35
+ $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c
30
36
 
31
37
  test-run-timed: test_fast
32
38
  while(true) do time ./test_fast > /dev/null; done
33
39
 
40
+ test-valgrind: test_g
41
+ valgrind ./test_g
42
+
43
+ libhttp_parser.o: http_parser.c http_parser.h Makefile
44
+ $(CC) $(CPPFLAGS_FAST) $(CFLAGS_LIB) -c http_parser.c -o libhttp_parser.o
45
+
46
+ library: libhttp_parser.o
47
+ $(CC) -shared -o libhttp_parser.so libhttp_parser.o
48
+
49
+ package: http_parser.o
50
+ $(AR) rcs libhttp_parser.a http_parser.o
34
51
 
35
52
  tags: http_parser.c http_parser.h test.c
36
53
  ctags $^
37
54
 
38
55
  clean:
39
- rm -f *.o test test_fast test_g http_parser.tar tags
56
+ rm -f *.o *.a test test_fast test_g http_parser.tar tags libhttp_parser.so libhttp_parser.o
40
57
 
41
58
  .PHONY: clean package test-run test-run-timed test-valgrind
@@ -1,20 +1,20 @@
1
1
  HTTP Parser
2
2
  ===========
3
3
 
4
- This is a parser for HTTP messages written in C. It parses both requests and
5
- responses. The parser is designed to be used in performance HTTP
6
- applications. It does not make any syscalls nor allocations, it does not
7
- buffer data, it can be interrupted at anytime. Depending on your
8
- architecture, it only requires about 40 bytes of data per message
9
- stream (in a web server that is per connection).
4
+ This is a parser for HTTP written in Java, based quite heavily on
5
+ the Ryan Dahl's C Version: `http-parser` available here:
6
+
7
+ http://github.com/ry/http-parser
8
+
9
+ It parses both requests and responses. The parser is designed to be used
10
+ in performance HTTP applications.
10
11
 
11
12
  Features:
12
13
 
13
- * No dependencies
14
+ * No dependencies (probably won't be able to keep it up)
14
15
  * Handles persistent streams (keep-alive).
15
16
  * Decodes chunked encoding.
16
17
  * Upgrade support
17
- * Defends against buffer overflow attacks.
18
18
 
19
19
  The parser extracts the following information from HTTP messages:
20
20
 
@@ -24,13 +24,32 @@ The parser extracts the following information from HTTP messages:
24
24
  * Response status code
25
25
  * Transfer-Encoding
26
26
  * HTTP version
27
- * Request path, query string, fragment
27
+ * Request URL
28
28
  * Message body
29
29
 
30
+ Building
31
+ --------
32
+
33
+ use `ant compile|test|jar`
30
34
 
31
35
  Usage
32
36
  -----
33
37
 
38
+ TODO: in the present form, usage of the Java version of the parser
39
+ shouldn't be too difficult to figure out for someone familiar with the
40
+ C version.
41
+
42
+ More documentation will follow shortly, in case you're looking for an
43
+ easy to use http library, this lib is probably not what you are
44
+ looking for anyway ...
45
+
46
+ All text after this paragraph (and most of the text above it) are from
47
+ the original C version of the README and are currently only here for
48
+ reference. In case you encounter any difficulties, find bugs, need
49
+ help or have suggestions, feel free to contact me at
50
+ (tim.becker@kuriositaet.de).
51
+
52
+
34
53
  One `http_parser` object is used per TCP connection. Initialize the struct
35
54
  using `http_parser_init()` and set the callbacks. That might look something
36
55
  like this for a request parser:
@@ -39,10 +58,10 @@ like this for a request parser:
39
58
  settings.on_path = my_path_callback;
40
59
  settings.on_header_field = my_header_field_callback;
41
60
  /* ... */
42
- settings.data = my_socket;
43
61
 
44
62
  http_parser *parser = malloc(sizeof(http_parser));
45
63
  http_parser_init(parser, HTTP_REQUEST);
64
+ parser->data = my_socket;
46
65
 
47
66
  When data is received on the socket execute the parser and check for errors.
48
67
 
@@ -126,7 +145,7 @@ There are two types of callbacks:
126
145
  * notification `typedef int (*http_cb) (http_parser*);`
127
146
  Callbacks: on_message_begin, on_headers_complete, on_message_complete.
128
147
  * data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);`
129
- Callbacks: (requests only) on_path, on_query_string, on_uri, on_fragment,
148
+ Callbacks: (requests only) on_uri,
130
149
  (common) on_header_field, on_header_value, on_body;
131
150
 
132
151
  Callbacks must return 0 on success. Returning a non-zero value indicates
@@ -162,10 +181,3 @@ and apply following logic:
162
181
  | value | on_h_value | Value continues. Reallocate value buffer |
163
182
  | | | and append callback data to it |
164
183
  ------------------------ ------------ --------------------------------------------
165
-
166
-
167
- See examples of reading in headers:
168
-
169
- * [partial example](http://gist.github.com/155877) in C
170
- * [from http-parser tests](http://github.com/ry/http-parser/blob/37a0ff8928fb0d83cec0d0d8909c5a4abcd221af/test.c#L403) in C
171
- * [from Node library](http://github.com/ry/node/blob/842eaf446d2fdcb33b296c67c911c32a0dabc747/src/http.js#L284) in Javascript
@@ -1,3 +1,12 @@
1
+ decide how to handle errs per default:
2
+ - ry: "set state to dead", return `read`
3
+ - current: call on_error w/ details, if no on_error handler set,
4
+ throw Exception, else call on_error and behave like orig...
5
+
6
+ some tests from test.c left to port
7
+ (scan ...)
8
+ documentation
9
+
1
10
  hi level callback interface
2
11
  eventloop
3
12
  state() as a function (?)
@@ -0,0 +1,74 @@
1
+ <?xml version="1.0"?>
2
+
3
+ <project name="http-parser" default="compile" basedir=".">
4
+
5
+ <echo message="pulling in property files"/>
6
+ <property file="build.properties"/>
7
+ <property name="classes.dir" value="classes"/>
8
+ <property name="assemble.dir" value="assemble"/>
9
+ <property name="lib.dir" value="lib"/>
10
+
11
+
12
+
13
+ <target name="prepare">
14
+ <mkdir dir="${classes.dir}" />
15
+ <mkdir dir="${assemble.dir}" />
16
+ <mkdir dir="${lib.dir}" />
17
+ </target>
18
+
19
+ <target name="clean">
20
+ <delete dir="${classes.dir}"/>
21
+ <delete dir="lib"/>
22
+ <delete dir="doc"/>
23
+ <delete dir="${assemble.dir}"/>
24
+ </target>
25
+
26
+ <target name="compile" depends="prepare">
27
+ <javac srcdir="src" destdir="${classes.dir}" debug='true'>
28
+ <classpath>
29
+ <pathelement path="${classpath}"/>
30
+ <pathelement location="ext/http-parser.jar"/>
31
+ <pathelement location="ext/primitives.jar"/>
32
+ </classpath>
33
+ </javac>
34
+ </target>
35
+
36
+ <target name="jar" depends="compile">
37
+ <copy todir="${assemble.dir}">
38
+ <fileset dir="classes"/>
39
+ </copy>
40
+ <jar basedir="${assemble.dir}" destfile="lib/${ant.project.name}.jar"/>
41
+ </target>
42
+
43
+ <target name="run" depends="jar">
44
+ <echo message="don't know how to run"/>
45
+ </target>
46
+
47
+ <target name="doc" depends="prepare">
48
+ <javadoc sourcepath="src/impl" destdir="doc">
49
+ </javadoc>
50
+ </target>
51
+
52
+ <target name="test_permutations" depends="compile">
53
+ <java classname="http_parser.lolevel.TestLoaderNG">
54
+ <arg value="tests.dumped"/>
55
+ <classpath>
56
+ <pathelement location="classes/"/>
57
+ <pathelement location="ext/primitives.jar/"/>
58
+ </classpath>
59
+ </java>
60
+ </target>
61
+
62
+ <target name="test_unit" depends="compile">
63
+ <java classname="http_parser.lolevel.Test">
64
+ <arg value="tests.dumped"/>
65
+ <classpath>
66
+ <pathelement location="classes/"/>
67
+ <pathelement location="ext/primitives.jar/"/>
68
+ </classpath>
69
+ </java>
70
+ </target>
71
+
72
+ <target name="test" depends="test_permutations, test_unit"/>
73
+
74
+ </project>