rhebok 0.0.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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.gitmodules +3 -0
- data/.travis.yml +8 -0
- data/Changes +4 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +378 -0
- data/README.md +208 -0
- data/Rakefile +21 -0
- data/ext/rhebok/extconf.rb +2 -0
- data/ext/rhebok/picohttpparser/.gitattributes +1 -0
- data/ext/rhebok/picohttpparser/.gitmodules +3 -0
- data/ext/rhebok/picohttpparser/.travis.yml +6 -0
- data/ext/rhebok/picohttpparser/Makefile +40 -0
- data/ext/rhebok/picohttpparser/README.md +24 -0
- data/ext/rhebok/picohttpparser/bench.c +53 -0
- data/ext/rhebok/picohttpparser/picohttpparser.c +434 -0
- data/ext/rhebok/picohttpparser/picohttpparser.h +63 -0
- data/ext/rhebok/picohttpparser/test.c +253 -0
- data/ext/rhebok/rhebok.c +795 -0
- data/lib/rack/handler/rhebok.rb +210 -0
- data/lib/rhebok.rb +7 -0
- data/lib/rhebok/version.rb +3 -0
- data/rhebok.gemspec +51 -0
- data/test/spec_02_basic.rb +7 -0
- data/test/spec_02_server.rb +87 -0
- data/test/testrequest.rb +79 -0
- metadata +143 -0
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/extensiontask"
|
3
|
+
|
4
|
+
Rake::ExtensionTask.new "rhebok" do |ext|
|
5
|
+
ext.lib_dir = "lib/rhebok"
|
6
|
+
end
|
7
|
+
|
8
|
+
task :bacon do
|
9
|
+
opts = ENV['TEST'] || '-a'
|
10
|
+
specopts = ENV['TESTOPTS'] || '-q'
|
11
|
+
sh "bacon -I./lib:./test -w #{opts} #{specopts}"
|
12
|
+
end
|
13
|
+
|
14
|
+
task :test do
|
15
|
+
Rake::Task["clobber"].invoke
|
16
|
+
Rake::Task["compile"].invoke
|
17
|
+
Rake::Task["bacon"].invoke
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
task :default => :test
|
@@ -0,0 +1 @@
|
|
1
|
+
picohttpparser.* ident
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#
|
2
|
+
# Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
3
|
+
# Shigeo Mitsunari
|
4
|
+
#
|
5
|
+
# The software is licensed under either the MIT License (below) or the Perl
|
6
|
+
# license.
|
7
|
+
#
|
8
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
# of this software and associated documentation files (the "Software"), to
|
10
|
+
# deal in the Software without restriction, including without limitation the
|
11
|
+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
12
|
+
# sell copies of the Software, and to permit persons to whom the Software is
|
13
|
+
# furnished to do so, subject to the following conditions:
|
14
|
+
#
|
15
|
+
# The above copyright notice and this permission notice shall be included in
|
16
|
+
# all copies or substantial portions of the Software.
|
17
|
+
#
|
18
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
23
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
24
|
+
# IN THE SOFTWARE.
|
25
|
+
|
26
|
+
CC?=gcc
|
27
|
+
PROVE?=prove
|
28
|
+
|
29
|
+
all:
|
30
|
+
|
31
|
+
test: test-bin
|
32
|
+
$(PROVE) -v ./test-bin
|
33
|
+
|
34
|
+
test-bin: picohttpparser.c picotest/picotest.c test.c
|
35
|
+
$(CC) -Wall $(CFLAGS) $(LDFLAGS) -o $@ $^
|
36
|
+
|
37
|
+
clean:
|
38
|
+
rm -f test-bin
|
39
|
+
|
40
|
+
.PHONY: test
|
@@ -0,0 +1,24 @@
|
|
1
|
+
PicoHTTPParser
|
2
|
+
=============
|
3
|
+
|
4
|
+
Copyright (c) 2009-2014 [Kazuho Oku](https://github.com/kazuho), [Tokuhiro Matsuno](https://github.com/tokuhirom), [Daisuke Murase](https://github.com/typester), [Shigeo Mitsunari](https://github.com/herumi)
|
5
|
+
|
6
|
+
PicoHTTPParser is a tiny, primitive, fast HTTP request/response parser.
|
7
|
+
|
8
|
+
Unlike most parsers, it is stateless and does not allocate memory by itself.
|
9
|
+
All it does is accept pointer to buffer and the output structure, and setups the pointers in the latter to point at the necessary portions of the buffer.
|
10
|
+
|
11
|
+
The code is widely deployed within Perl applications through popular modules that use it, including [Plack](https://metacpan.org/pod/Plack), [Starman](https://metacpan.org/pod/Starman), [Starlet](https://metacpan.org/pod/Starlet), [Furl](https://metacpan.org/pod/Furl). It is also the HTTP/1 parser of [H2O](https://github.com/h2o/h2o).
|
12
|
+
|
13
|
+
Check out [test.c] to find out how to use the parser.
|
14
|
+
|
15
|
+
The software is dual-licensed under the Perl License or the MIT License.
|
16
|
+
|
17
|
+
Benchmark
|
18
|
+
---------
|
19
|
+
|
20
|
+

|
21
|
+
|
22
|
+
The benchmark code is from [fukamachi/fast-http@6b91103](https://github.com/fukamachi/fast-http/tree/6b9110347c7a3407310c08979aefd65078518478).
|
23
|
+
|
24
|
+
The internals of picohttpparser has been described to some extent in [my blog entry]( http://blog.kazuhooku.com/2014/11/the-internals-h2o-or-how-to-write-fast.html).
|
@@ -0,0 +1,53 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
3
|
+
* Shigeo Mitsunari
|
4
|
+
*
|
5
|
+
* The software is licensed under either the MIT License (below) or the Perl
|
6
|
+
* license.
|
7
|
+
*
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
* of this software and associated documentation files (the "Software"), to
|
10
|
+
* deal in the Software without restriction, including without limitation the
|
11
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
12
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
13
|
+
* furnished to do so, subject to the following conditions:
|
14
|
+
*
|
15
|
+
* The above copyright notice and this permission notice shall be included in
|
16
|
+
* all copies or substantial portions of the Software.
|
17
|
+
*
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
23
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
24
|
+
* IN THE SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include <assert.h>
|
28
|
+
#include <stdio.h>
|
29
|
+
#include "picohttpparser.h"
|
30
|
+
|
31
|
+
#define REQ "GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\nHost: www.kittyhell.com\r\nUser-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: ja,en-us;q=0.7,en;q=0.3\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 115\r\nConnection: keep-alive\r\nCookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; __utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral\r\n\r\n"
|
32
|
+
|
33
|
+
int main(void)
|
34
|
+
{
|
35
|
+
const char* method;
|
36
|
+
size_t method_len;
|
37
|
+
const char* path;
|
38
|
+
size_t path_len;
|
39
|
+
int minor_version;
|
40
|
+
struct phr_header headers[32];
|
41
|
+
size_t num_headers;
|
42
|
+
int i, ret;
|
43
|
+
|
44
|
+
for (i = 0; i < 10000000; i++) {
|
45
|
+
num_headers = sizeof(headers) / sizeof(headers[0]);
|
46
|
+
ret = phr_parse_request(REQ, sizeof(REQ) - 1, &method, &method_len, &path,
|
47
|
+
&path_len, &minor_version, headers, &num_headers,
|
48
|
+
0);
|
49
|
+
assert(ret == sizeof(REQ) - 1);
|
50
|
+
}
|
51
|
+
|
52
|
+
return 0;
|
53
|
+
}
|
@@ -0,0 +1,434 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2009-2014 Kazuho Oku, Tokuhiro Matsuno, Daisuke Murase,
|
3
|
+
* Shigeo Mitsunari
|
4
|
+
*
|
5
|
+
* The software is licensed under either the MIT License (below) or the Perl
|
6
|
+
* license.
|
7
|
+
*
|
8
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
9
|
+
* of this software and associated documentation files (the "Software"), to
|
10
|
+
* deal in the Software without restriction, including without limitation the
|
11
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
12
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
13
|
+
* furnished to do so, subject to the following conditions:
|
14
|
+
*
|
15
|
+
* The above copyright notice and this permission notice shall be included in
|
16
|
+
* all copies or substantial portions of the Software.
|
17
|
+
*
|
18
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
23
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
24
|
+
* IN THE SOFTWARE.
|
25
|
+
*/
|
26
|
+
|
27
|
+
#include <stddef.h>
|
28
|
+
#ifdef __SSE4_2__
|
29
|
+
# include <x86intrin.h>
|
30
|
+
#endif
|
31
|
+
#include "picohttpparser.h"
|
32
|
+
|
33
|
+
/* $Id$ */
|
34
|
+
|
35
|
+
#if __GNUC__ >= 3
|
36
|
+
# define likely(x) __builtin_expect(!!(x), 1)
|
37
|
+
# define unlikely(x) __builtin_expect(!!(x), 0)
|
38
|
+
#else
|
39
|
+
# define likely(x) (x)
|
40
|
+
# define unlikely(x) (x)
|
41
|
+
#endif
|
42
|
+
|
43
|
+
#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c) - 040u < 0137u)
|
44
|
+
|
45
|
+
#define CHECK_EOF() \
|
46
|
+
if (buf == buf_end) { \
|
47
|
+
*ret = -2; \
|
48
|
+
return NULL; \
|
49
|
+
}
|
50
|
+
|
51
|
+
#define EXPECT_CHAR(ch) \
|
52
|
+
CHECK_EOF(); \
|
53
|
+
if (*buf++ != ch) { \
|
54
|
+
*ret = -1; \
|
55
|
+
return NULL; \
|
56
|
+
}
|
57
|
+
|
58
|
+
#define ADVANCE_TOKEN(tok, toklen) do { \
|
59
|
+
const char* tok_start = buf; \
|
60
|
+
static const char ranges2[] __attribute__((aligned(16))) = "\000\040\177\177"; \
|
61
|
+
int found2; \
|
62
|
+
buf = findchar_fast(buf, buf_end, ranges2, sizeof(ranges2) - 1, &found2); \
|
63
|
+
if (! found2) { \
|
64
|
+
CHECK_EOF(); \
|
65
|
+
} \
|
66
|
+
while (1) { \
|
67
|
+
if (*buf == ' ') { \
|
68
|
+
break; \
|
69
|
+
} else if (unlikely(! IS_PRINTABLE_ASCII(*buf))) { \
|
70
|
+
if ((unsigned char)*buf < '\040' || *buf == '\177') { \
|
71
|
+
*ret = -1; \
|
72
|
+
return NULL; \
|
73
|
+
} \
|
74
|
+
} \
|
75
|
+
++buf; \
|
76
|
+
CHECK_EOF(); \
|
77
|
+
} \
|
78
|
+
tok = tok_start; \
|
79
|
+
toklen = buf - tok_start; \
|
80
|
+
} while (0)
|
81
|
+
|
82
|
+
static const char* token_char_map =
|
83
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
84
|
+
"\0\1\1\1\1\1\1\1\0\0\1\1\0\1\1\0\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0"
|
85
|
+
"\0\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\1\1"
|
86
|
+
"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\1\0\1\0"
|
87
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
88
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
89
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
90
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
|
91
|
+
|
92
|
+
static const char* findchar_fast(const char* buf, const char* buf_end, const char *ranges, size_t ranges_size, int* found)
|
93
|
+
{
|
94
|
+
*found = 0;
|
95
|
+
#if __SSE4_2__
|
96
|
+
if (likely(buf_end - buf >= 16)) {
|
97
|
+
__m128i ranges16 = _mm_loadu_si128((const __m128i*)ranges);
|
98
|
+
|
99
|
+
size_t left = (buf_end - buf) & ~15;
|
100
|
+
do {
|
101
|
+
__m128i b16 = _mm_loadu_si128((void*)buf);
|
102
|
+
int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
|
103
|
+
if (unlikely(r != 16)) {
|
104
|
+
buf += r;
|
105
|
+
*found = 1;
|
106
|
+
break;
|
107
|
+
}
|
108
|
+
buf += 16;
|
109
|
+
left -= 16;
|
110
|
+
} while (likely(left != 0));
|
111
|
+
}
|
112
|
+
#endif
|
113
|
+
return buf;
|
114
|
+
}
|
115
|
+
|
116
|
+
static const char* get_token_to_eol(const char* buf, const char* buf_end,
|
117
|
+
const char** token, size_t* token_len,
|
118
|
+
int* ret)
|
119
|
+
{
|
120
|
+
const char* token_start = buf;
|
121
|
+
|
122
|
+
#ifdef __SSE4_2__
|
123
|
+
static const char ranges1[] =
|
124
|
+
"\0\010"
|
125
|
+
/* allow HT */
|
126
|
+
"\012\037"
|
127
|
+
/* allow SP and up to but not including DEL */
|
128
|
+
"\177\177"
|
129
|
+
/* allow chars w. MSB set */
|
130
|
+
;
|
131
|
+
int found;
|
132
|
+
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
|
133
|
+
if (found)
|
134
|
+
goto FOUND_CTL;
|
135
|
+
#else
|
136
|
+
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
|
137
|
+
while (likely(buf_end - buf >= 8)) {
|
138
|
+
#define DOIT() if (unlikely(! IS_PRINTABLE_ASCII(*buf))) goto NonPrintable; ++buf
|
139
|
+
DOIT(); DOIT(); DOIT(); DOIT();
|
140
|
+
DOIT(); DOIT(); DOIT(); DOIT();
|
141
|
+
#undef DOIT
|
142
|
+
continue;
|
143
|
+
NonPrintable:
|
144
|
+
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
145
|
+
goto FOUND_CTL;
|
146
|
+
}
|
147
|
+
++buf;
|
148
|
+
}
|
149
|
+
#endif
|
150
|
+
for (; ; ++buf) {
|
151
|
+
CHECK_EOF();
|
152
|
+
if (unlikely(! IS_PRINTABLE_ASCII(*buf))) {
|
153
|
+
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
154
|
+
goto FOUND_CTL;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
FOUND_CTL:
|
159
|
+
if (likely(*buf == '\015')) {
|
160
|
+
++buf;
|
161
|
+
EXPECT_CHAR('\012');
|
162
|
+
*token_len = buf - 2 - token_start;
|
163
|
+
} else if (*buf == '\012') {
|
164
|
+
*token_len = buf - token_start;
|
165
|
+
++buf;
|
166
|
+
} else {
|
167
|
+
*ret = -1;
|
168
|
+
return NULL;
|
169
|
+
}
|
170
|
+
*token = token_start;
|
171
|
+
|
172
|
+
return buf;
|
173
|
+
}
|
174
|
+
|
175
|
+
static const char* is_complete(const char* buf, const char* buf_end,
|
176
|
+
size_t last_len, int* ret)
|
177
|
+
{
|
178
|
+
int ret_cnt = 0;
|
179
|
+
buf = last_len < 3 ? buf : buf + last_len - 3;
|
180
|
+
|
181
|
+
while (1) {
|
182
|
+
CHECK_EOF();
|
183
|
+
if (*buf == '\015') {
|
184
|
+
++buf;
|
185
|
+
CHECK_EOF();
|
186
|
+
EXPECT_CHAR('\012');
|
187
|
+
++ret_cnt;
|
188
|
+
} else if (*buf == '\012') {
|
189
|
+
++buf;
|
190
|
+
++ret_cnt;
|
191
|
+
} else {
|
192
|
+
++buf;
|
193
|
+
ret_cnt = 0;
|
194
|
+
}
|
195
|
+
if (ret_cnt == 2) {
|
196
|
+
return buf;
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
*ret = -2;
|
201
|
+
return NULL;
|
202
|
+
}
|
203
|
+
|
204
|
+
/* *_buf is always within [buf, buf_end) upon success */
|
205
|
+
static const char* parse_int(const char* buf, const char* buf_end, int* value,
|
206
|
+
int* ret)
|
207
|
+
{
|
208
|
+
int v;
|
209
|
+
CHECK_EOF();
|
210
|
+
if (! ('0' <= *buf && *buf <= '9')) {
|
211
|
+
*ret = -1;
|
212
|
+
return NULL;
|
213
|
+
}
|
214
|
+
v = 0;
|
215
|
+
for (; ; ++buf) {
|
216
|
+
CHECK_EOF();
|
217
|
+
if ('0' <= *buf && *buf <= '9') {
|
218
|
+
v = v * 10 + *buf - '0';
|
219
|
+
} else {
|
220
|
+
break;
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
*value = v;
|
225
|
+
return buf;
|
226
|
+
}
|
227
|
+
|
228
|
+
/* returned pointer is always within [buf, buf_end), or null */
|
229
|
+
static const char* parse_http_version(const char* buf, const char* buf_end,
|
230
|
+
int* minor_version, int* ret)
|
231
|
+
{
|
232
|
+
EXPECT_CHAR('H'); EXPECT_CHAR('T'); EXPECT_CHAR('T'); EXPECT_CHAR('P');
|
233
|
+
EXPECT_CHAR('/'); EXPECT_CHAR('1'); EXPECT_CHAR('.');
|
234
|
+
return parse_int(buf, buf_end, minor_version, ret);
|
235
|
+
}
|
236
|
+
|
237
|
+
static const char* parse_headers(const char* buf, const char* buf_end,
|
238
|
+
struct phr_header* headers,
|
239
|
+
size_t* num_headers, size_t max_headers,
|
240
|
+
int* ret)
|
241
|
+
{
|
242
|
+
for (; ; ++*num_headers) {
|
243
|
+
CHECK_EOF();
|
244
|
+
if (*buf == '\015') {
|
245
|
+
++buf;
|
246
|
+
EXPECT_CHAR('\012');
|
247
|
+
break;
|
248
|
+
} else if (*buf == '\012') {
|
249
|
+
++buf;
|
250
|
+
break;
|
251
|
+
}
|
252
|
+
if (*num_headers == max_headers) {
|
253
|
+
*ret = -1;
|
254
|
+
return NULL;
|
255
|
+
}
|
256
|
+
if (! (*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
|
257
|
+
if (! token_char_map[(unsigned char)*buf]) {
|
258
|
+
*ret = -1;
|
259
|
+
return NULL;
|
260
|
+
}
|
261
|
+
/* parsing name, but do not discard SP before colon, see
|
262
|
+
* http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
|
263
|
+
headers[*num_headers].name = buf;
|
264
|
+
static const char ranges1[] __attribute__((aligned(16))) = "::\x00\037";
|
265
|
+
int found;
|
266
|
+
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
|
267
|
+
if (! found) {
|
268
|
+
CHECK_EOF();
|
269
|
+
}
|
270
|
+
while (1) {
|
271
|
+
if (*buf == ':') {
|
272
|
+
break;
|
273
|
+
} else if (*buf < ' ') {
|
274
|
+
*ret = -1;
|
275
|
+
return NULL;
|
276
|
+
}
|
277
|
+
++buf;
|
278
|
+
CHECK_EOF();
|
279
|
+
}
|
280
|
+
headers[*num_headers].name_len = buf - headers[*num_headers].name;
|
281
|
+
++buf;
|
282
|
+
for (; ; ++buf) {
|
283
|
+
CHECK_EOF();
|
284
|
+
if (! (*buf == ' ' || *buf == '\t')) {
|
285
|
+
break;
|
286
|
+
}
|
287
|
+
}
|
288
|
+
} else {
|
289
|
+
headers[*num_headers].name = NULL;
|
290
|
+
headers[*num_headers].name_len = 0;
|
291
|
+
}
|
292
|
+
if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value,
|
293
|
+
&headers[*num_headers].value_len, ret))
|
294
|
+
== NULL) {
|
295
|
+
return NULL;
|
296
|
+
}
|
297
|
+
}
|
298
|
+
return buf;
|
299
|
+
}
|
300
|
+
|
301
|
+
const char* parse_request(const char* buf, const char* buf_end,
|
302
|
+
const char** method, size_t* method_len,
|
303
|
+
const char** path, size_t* path_len,
|
304
|
+
int* minor_version, struct phr_header* headers,
|
305
|
+
size_t* num_headers, size_t max_headers, int* ret)
|
306
|
+
{
|
307
|
+
/* skip first empty line (some clients add CRLF after POST content) */
|
308
|
+
CHECK_EOF();
|
309
|
+
if (*buf == '\015') {
|
310
|
+
++buf;
|
311
|
+
EXPECT_CHAR('\012');
|
312
|
+
} else if (*buf == '\012') {
|
313
|
+
++buf;
|
314
|
+
}
|
315
|
+
|
316
|
+
/* parse request line */
|
317
|
+
ADVANCE_TOKEN(*method, *method_len);
|
318
|
+
++buf;
|
319
|
+
ADVANCE_TOKEN(*path, *path_len);
|
320
|
+
++buf;
|
321
|
+
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
|
322
|
+
return NULL;
|
323
|
+
}
|
324
|
+
if (*buf == '\015') {
|
325
|
+
++buf;
|
326
|
+
EXPECT_CHAR('\012');
|
327
|
+
} else if (*buf == '\012') {
|
328
|
+
++buf;
|
329
|
+
} else {
|
330
|
+
*ret = -1;
|
331
|
+
return NULL;
|
332
|
+
}
|
333
|
+
|
334
|
+
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
|
335
|
+
}
|
336
|
+
|
337
|
+
int phr_parse_request(const char* buf_start, size_t len, const char** method,
|
338
|
+
size_t* method_len, const char** path, size_t* path_len,
|
339
|
+
int* minor_version, struct phr_header* headers,
|
340
|
+
size_t* num_headers, size_t last_len)
|
341
|
+
{
|
342
|
+
const char * buf = buf_start, * buf_end = buf_start + len;
|
343
|
+
size_t max_headers = *num_headers;
|
344
|
+
int r;
|
345
|
+
|
346
|
+
*method = NULL;
|
347
|
+
*method_len = 0;
|
348
|
+
*path = NULL;
|
349
|
+
*path_len = 0;
|
350
|
+
*minor_version = -1;
|
351
|
+
*num_headers = 0;
|
352
|
+
|
353
|
+
/* if last_len != 0, check if the request is complete (a fast countermeasure
|
354
|
+
againt slowloris */
|
355
|
+
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
356
|
+
return r;
|
357
|
+
}
|
358
|
+
|
359
|
+
if ((buf = parse_request(buf, buf_end, method, method_len, path, path_len,
|
360
|
+
minor_version, headers, num_headers, max_headers,
|
361
|
+
&r))
|
362
|
+
== NULL) {
|
363
|
+
return r;
|
364
|
+
}
|
365
|
+
|
366
|
+
return (int)(buf - buf_start);
|
367
|
+
}
|
368
|
+
|
369
|
+
static const char* parse_response(const char* buf, const char* buf_end,
|
370
|
+
int* minor_version, int* status,
|
371
|
+
const char** msg, size_t* msg_len,
|
372
|
+
struct phr_header* headers,
|
373
|
+
size_t* num_headers, size_t max_headers,
|
374
|
+
int* ret)
|
375
|
+
{
|
376
|
+
/* parse "HTTP/1.x" */
|
377
|
+
if ((buf = parse_http_version(buf, buf_end, minor_version, ret)) == NULL) {
|
378
|
+
return NULL;
|
379
|
+
}
|
380
|
+
/* skip space */
|
381
|
+
if (*buf++ != ' ') {
|
382
|
+
*ret = -1;
|
383
|
+
return NULL;
|
384
|
+
}
|
385
|
+
/* parse status code */
|
386
|
+
if ((buf = parse_int(buf, buf_end, status, ret)) == NULL) {
|
387
|
+
return NULL;
|
388
|
+
}
|
389
|
+
/* skip space */
|
390
|
+
if (*buf++ != ' ') {
|
391
|
+
*ret = -1;
|
392
|
+
return NULL;
|
393
|
+
}
|
394
|
+
/* get message */
|
395
|
+
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, ret)) == NULL) {
|
396
|
+
return NULL;
|
397
|
+
}
|
398
|
+
|
399
|
+
return parse_headers(buf, buf_end, headers, num_headers, max_headers, ret);
|
400
|
+
}
|
401
|
+
|
402
|
+
int phr_parse_response(const char* buf_start, size_t len, int* minor_version,
|
403
|
+
int* status, const char** msg, size_t* msg_len,
|
404
|
+
struct phr_header* headers, size_t* num_headers,
|
405
|
+
size_t last_len)
|
406
|
+
{
|
407
|
+
const char * buf = buf_start, * buf_end = buf + len;
|
408
|
+
size_t max_headers = *num_headers;
|
409
|
+
int r;
|
410
|
+
|
411
|
+
*minor_version = -1;
|
412
|
+
*status = 0;
|
413
|
+
*msg = NULL;
|
414
|
+
*msg_len = 0;
|
415
|
+
*num_headers = 0;
|
416
|
+
|
417
|
+
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
418
|
+
against slowloris */
|
419
|
+
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
420
|
+
return r;
|
421
|
+
}
|
422
|
+
|
423
|
+
if ((buf = parse_response(buf, buf_end, minor_version, status, msg, msg_len,
|
424
|
+
headers, num_headers, max_headers, &r))
|
425
|
+
== NULL) {
|
426
|
+
return r;
|
427
|
+
}
|
428
|
+
|
429
|
+
return (int)(buf - buf_start);
|
430
|
+
}
|
431
|
+
|
432
|
+
#undef CHECK_EOF
|
433
|
+
#undef EXPECT_CHAR
|
434
|
+
#undef ADVANCE_TOKEN
|