qhttpx 1.8.5 → 1.8.11
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.
- package/CHANGELOG.md +52 -0
- package/README.md +72 -52
- package/binding.gyp +18 -0
- package/dist/examples/api-server.js +29 -8
- package/dist/examples/basic.d.ts +1 -0
- package/dist/examples/basic.js +10 -0
- package/dist/examples/compression.d.ts +1 -0
- package/dist/examples/compression.js +17 -0
- package/dist/examples/cors.d.ts +1 -0
- package/dist/examples/cors.js +19 -0
- package/dist/examples/errors.d.ts +1 -0
- package/dist/examples/errors.js +25 -0
- package/dist/examples/file-upload.d.ts +1 -0
- package/dist/examples/file-upload.js +24 -0
- package/dist/examples/fusion.d.ts +1 -0
- package/dist/examples/fusion.js +21 -0
- package/dist/examples/rate-limiting.d.ts +1 -0
- package/dist/examples/rate-limiting.js +17 -0
- package/dist/examples/validation.d.ts +1 -0
- package/dist/examples/validation.js +23 -0
- package/dist/examples/websockets.d.ts +1 -0
- package/dist/examples/websockets.js +20 -0
- package/dist/package.json +11 -1
- package/dist/src/benchmarks/simple-json.js +6 -4
- package/dist/src/cli/index.js +33 -11
- package/dist/src/core/errors.d.ts +34 -0
- package/dist/src/core/errors.js +70 -0
- package/dist/src/core/native-adapter.d.ts +11 -0
- package/dist/src/core/native-adapter.js +211 -0
- package/dist/src/core/server.d.ts +52 -4
- package/dist/src/core/server.js +389 -261
- package/dist/src/core/types.d.ts +37 -0
- package/dist/src/index.d.ts +6 -1
- package/dist/src/index.js +19 -3
- package/dist/src/middleware/compression.d.ts +1 -5
- package/dist/src/middleware/cors.d.ts +1 -10
- package/dist/src/middleware/presets.d.ts +4 -1
- package/dist/src/middleware/presets.js +22 -3
- package/dist/src/middleware/rate-limit.d.ts +1 -19
- package/dist/src/middleware/rate-limit.js +6 -0
- package/dist/src/middleware/security.d.ts +1 -2
- package/dist/src/native/index.d.ts +29 -0
- package/dist/src/native/index.js +64 -0
- package/dist/src/router/radix-tree.d.ts +2 -0
- package/dist/src/router/radix-tree.js +54 -4
- package/dist/src/router/router.d.ts +1 -0
- package/dist/src/router/router.js +42 -2
- package/dist/tests/native-adapter.test.d.ts +1 -0
- package/dist/tests/native-adapter.test.js +71 -0
- package/dist/tests/resources.test.js +3 -0
- package/dist/tests/security.test.js +2 -2
- package/docs/AEGIS.md +34 -9
- package/docs/BENCHMARKS.md +8 -7
- package/docs/ERRORS.md +112 -0
- package/docs/FUSION.md +68 -0
- package/docs/MIDDLEWARE.md +65 -0
- package/docs/ROUTING.md +70 -0
- package/docs/STATIC.md +61 -0
- package/docs/WEBSOCKETS.md +76 -0
- package/package.json +11 -1
- package/src/native/README.md +31 -0
- package/src/native/addon.cc +8 -0
- package/src/native/index.ts +78 -0
- package/src/native/picohttpparser.c +608 -0
- package/src/native/picohttpparser.h +71 -0
- package/src/native/server.cc +262 -0
- package/src/native/server.h +30 -0
- package/.eslintrc.json +0 -22
- package/.github/workflows/ci.yml +0 -32
- package/.github/workflows/npm-publish.yml +0 -37
- package/.github/workflows/release.yml +0 -21
- package/.prettierrc +0 -7
- package/assets/logo.svg +0 -25
- package/eslint.config.cjs +0 -26
- package/examples/api-server.ts +0 -62
- package/src/benchmarks/quantam-users.ts +0 -70
- package/src/benchmarks/simple-json.ts +0 -71
- package/src/benchmarks/ultra-mode.ts +0 -127
- package/src/cli/index.ts +0 -214
- package/src/client/index.ts +0 -93
- package/src/core/batch.ts +0 -110
- package/src/core/body-parser.ts +0 -151
- package/src/core/buffer-pool.ts +0 -96
- package/src/core/config.ts +0 -60
- package/src/core/fusion.ts +0 -210
- package/src/core/logger.ts +0 -70
- package/src/core/metrics.ts +0 -166
- package/src/core/resources.ts +0 -38
- package/src/core/scheduler.ts +0 -126
- package/src/core/scope.ts +0 -87
- package/src/core/serializer.ts +0 -41
- package/src/core/server.ts +0 -1234
- package/src/core/stream.ts +0 -111
- package/src/core/tasks.ts +0 -138
- package/src/core/types.ts +0 -192
- package/src/core/websocket.ts +0 -112
- package/src/core/worker-queue.ts +0 -90
- package/src/database/adapters/memory.ts +0 -99
- package/src/database/adapters/mongo.ts +0 -116
- package/src/database/adapters/postgres.ts +0 -86
- package/src/database/adapters/sqlite.ts +0 -44
- package/src/database/coalescer.ts +0 -153
- package/src/database/manager.ts +0 -97
- package/src/database/types.ts +0 -24
- package/src/index.ts +0 -58
- package/src/middleware/compression.ts +0 -147
- package/src/middleware/cors.ts +0 -98
- package/src/middleware/presets.ts +0 -50
- package/src/middleware/rate-limit.ts +0 -106
- package/src/middleware/security.ts +0 -109
- package/src/middleware/static.ts +0 -216
- package/src/openapi/generator.ts +0 -167
- package/src/router/radix-router.ts +0 -119
- package/src/router/radix-tree.ts +0 -106
- package/src/router/router.ts +0 -190
- package/src/testing/index.ts +0 -104
- package/src/utils/cookies.ts +0 -67
- package/src/utils/logger.ts +0 -59
- package/src/utils/signals.ts +0 -45
- package/src/utils/sse.ts +0 -41
- package/src/validation/index.ts +0 -3
- package/src/validation/simple.ts +0 -93
- package/src/validation/types.ts +0 -38
- package/src/validation/zod.ts +0 -14
- package/src/views/index.ts +0 -1
- package/src/views/types.ts +0 -4
- package/tests/adapters.test.ts +0 -120
- package/tests/batch.test.ts +0 -139
- package/tests/body-parser.test.ts +0 -83
- package/tests/compression-sse.test.ts +0 -98
- package/tests/cookies.test.ts +0 -74
- package/tests/cors.test.ts +0 -79
- package/tests/database.test.ts +0 -90
- package/tests/dx.test.ts +0 -130
- package/tests/ecosystem.test.ts +0 -156
- package/tests/features.test.ts +0 -51
- package/tests/fusion.test.ts +0 -121
- package/tests/http-basic.test.ts +0 -161
- package/tests/logger.test.ts +0 -48
- package/tests/middleware.test.ts +0 -137
- package/tests/observability.test.ts +0 -91
- package/tests/openapi.test.ts +0 -74
- package/tests/plugin.test.ts +0 -85
- package/tests/plugins.test.ts +0 -93
- package/tests/rate-limit.test.ts +0 -97
- package/tests/resources.test.ts +0 -64
- package/tests/scheduler.test.ts +0 -71
- package/tests/schema-routes.test.ts +0 -89
- package/tests/security.test.ts +0 -128
- package/tests/server-db.test.ts +0 -72
- package/tests/smoke.test.ts +0 -9
- package/tests/sqlite-fusion.test.ts +0 -106
- package/tests/static.test.ts +0 -111
- package/tests/stream.test.ts +0 -58
- package/tests/task-metrics.test.ts +0 -78
- package/tests/tasks.test.ts +0 -90
- package/tests/testing.test.ts +0 -53
- package/tests/validation.test.ts +0 -126
- package/tests/websocket.test.ts +0 -132
- package/tsconfig.json +0 -17
- package/vitest.config.ts +0 -9
|
@@ -0,0 +1,608 @@
|
|
|
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. Choose whichever you prefer.
|
|
7
|
+
*
|
|
8
|
+
* MIT License
|
|
9
|
+
*
|
|
10
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
* in the Software without restriction, including without limitation the rights
|
|
13
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
* furnished to do so, subject to the following conditions:
|
|
16
|
+
*
|
|
17
|
+
* The above copyright notice and this permission notice shall be included in
|
|
18
|
+
* all copies or substantial portions of the Software.
|
|
19
|
+
*
|
|
20
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
* THE SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
#include <assert.h>
|
|
29
|
+
#include <stddef.h>
|
|
30
|
+
#include <string.h>
|
|
31
|
+
#ifdef __SSE4_2__
|
|
32
|
+
#ifdef _MSC_VER
|
|
33
|
+
#include <nmmintrin.h>
|
|
34
|
+
#else
|
|
35
|
+
#include <x86intrin.h>
|
|
36
|
+
#endif
|
|
37
|
+
#endif
|
|
38
|
+
#include "picohttpparser.h"
|
|
39
|
+
|
|
40
|
+
/* $Id: 76fd50a6690f48a32aa353b20d9d443e6d109847 $ */
|
|
41
|
+
|
|
42
|
+
#if __GNUC__ >= 3
|
|
43
|
+
#define likely(x) __builtin_expect(!!(x), 1)
|
|
44
|
+
#define unlikely(x) __builtin_expect(!!(x), 0)
|
|
45
|
+
#else
|
|
46
|
+
#define likely(x) (x)
|
|
47
|
+
#define unlikely(x) (x)
|
|
48
|
+
#endif
|
|
49
|
+
|
|
50
|
+
#ifdef _MSC_VER
|
|
51
|
+
#define ALIGNED(n) _declspec(align(n))
|
|
52
|
+
#else
|
|
53
|
+
#define ALIGNED(n) __attribute__((aligned(n)))
|
|
54
|
+
#endif
|
|
55
|
+
|
|
56
|
+
#define IS_PRINTABLE_ASCII(c) ((unsigned char)(c)-040u < 0137u)
|
|
57
|
+
|
|
58
|
+
#define CHECK_EOF() \
|
|
59
|
+
if (buf == buf_end) { \
|
|
60
|
+
*ret = -2; \
|
|
61
|
+
return NULL; \
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
#define EXPECT_CHAR_NO_CHECK(ch) \
|
|
65
|
+
if (*buf++ != ch) { \
|
|
66
|
+
*ret = -1; \
|
|
67
|
+
return NULL; \
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#define EXPECT_CHAR(ch) \
|
|
71
|
+
CHECK_EOF(); \
|
|
72
|
+
EXPECT_CHAR_NO_CHECK(ch);
|
|
73
|
+
|
|
74
|
+
#define ADVANCE_TOKEN(tok, toklen) \
|
|
75
|
+
do { \
|
|
76
|
+
const char *tok_start = buf; \
|
|
77
|
+
static const char ALIGNED(16) ranges2[] = "\000\040\177\177"; \
|
|
78
|
+
int found2; \
|
|
79
|
+
buf = findchar_fast(buf, buf_end, ranges2, sizeof(ranges2) - 1, &found2); \
|
|
80
|
+
if (!found2) { \
|
|
81
|
+
CHECK_EOF(); \
|
|
82
|
+
} \
|
|
83
|
+
while (1) { \
|
|
84
|
+
if (*buf == ' ') { \
|
|
85
|
+
break; \
|
|
86
|
+
} else if (unlikely(!IS_PRINTABLE_ASCII(*buf))) { \
|
|
87
|
+
if ((unsigned char)*buf < '\040' || *buf == '\177') { \
|
|
88
|
+
*ret = -1; \
|
|
89
|
+
return NULL; \
|
|
90
|
+
} \
|
|
91
|
+
} \
|
|
92
|
+
++buf; \
|
|
93
|
+
CHECK_EOF(); \
|
|
94
|
+
} \
|
|
95
|
+
tok = tok_start; \
|
|
96
|
+
toklen = buf - tok_start; \
|
|
97
|
+
} while (0)
|
|
98
|
+
|
|
99
|
+
static const char *token_char_map = "\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"
|
|
100
|
+
"\0\1\0\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"
|
|
101
|
+
"\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"
|
|
102
|
+
"\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\1\1\1\1\0"
|
|
103
|
+
"\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"
|
|
104
|
+
"\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"
|
|
105
|
+
"\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"
|
|
106
|
+
"\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";
|
|
107
|
+
|
|
108
|
+
static const char *findchar_fast(const char *buf, const char *buf_end, const char *ranges, size_t ranges_size, int *found)
|
|
109
|
+
{
|
|
110
|
+
*found = 0;
|
|
111
|
+
#if __SSE4_2__
|
|
112
|
+
if (likely(buf_end - buf >= 16)) {
|
|
113
|
+
__m128i ranges16 = _mm_loadu_si128((const __m128i *)ranges);
|
|
114
|
+
|
|
115
|
+
size_t left = (buf_end - buf) & ~15;
|
|
116
|
+
do {
|
|
117
|
+
__m128i b16 = _mm_loadu_si128((const __m128i *)buf);
|
|
118
|
+
int r = _mm_cmpestri(ranges16, ranges_size, b16, 16, _SIDD_LEAST_SIGNIFICANT | _SIDD_CMP_RANGES | _SIDD_UBYTE_OPS);
|
|
119
|
+
if (unlikely(r != 16)) {
|
|
120
|
+
buf += r;
|
|
121
|
+
*found = 1;
|
|
122
|
+
return buf;
|
|
123
|
+
}
|
|
124
|
+
buf += 16;
|
|
125
|
+
left -= 16;
|
|
126
|
+
} while (likely(left != 0));
|
|
127
|
+
}
|
|
128
|
+
#endif
|
|
129
|
+
return buf;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
static const char *get_token_to_eol(const char *buf, const char *buf_end, const char **token, size_t *token_len, int *ret)
|
|
133
|
+
{
|
|
134
|
+
const char *token_start = buf;
|
|
135
|
+
|
|
136
|
+
#ifdef __SSE4_2__
|
|
137
|
+
static const char ALIGNED(16) ranges1[] = "\0\010"
|
|
138
|
+
/* allow HT */
|
|
139
|
+
"\012\037"
|
|
140
|
+
/* allow SP and up to but not including DEL */
|
|
141
|
+
"\177\177"
|
|
142
|
+
/* allow chars w. MSB set */
|
|
143
|
+
"\0\0\0\0";
|
|
144
|
+
int found;
|
|
145
|
+
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
|
|
146
|
+
if (found)
|
|
147
|
+
goto FOUND_CTL;
|
|
148
|
+
#else
|
|
149
|
+
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
|
|
150
|
+
while (likely(buf_end - buf >= 8)) {
|
|
151
|
+
#define DOIT() \
|
|
152
|
+
do { \
|
|
153
|
+
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) \
|
|
154
|
+
goto NonPrintable; \
|
|
155
|
+
++buf; \
|
|
156
|
+
} while (0)
|
|
157
|
+
DOIT();
|
|
158
|
+
DOIT();
|
|
159
|
+
DOIT();
|
|
160
|
+
DOIT();
|
|
161
|
+
DOIT();
|
|
162
|
+
DOIT();
|
|
163
|
+
DOIT();
|
|
164
|
+
DOIT();
|
|
165
|
+
#undef DOIT
|
|
166
|
+
continue;
|
|
167
|
+
NonPrintable:
|
|
168
|
+
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
|
169
|
+
goto FOUND_CTL;
|
|
170
|
+
}
|
|
171
|
+
++buf;
|
|
172
|
+
}
|
|
173
|
+
#endif
|
|
174
|
+
for (;; ++buf) {
|
|
175
|
+
CHECK_EOF();
|
|
176
|
+
if (unlikely(!IS_PRINTABLE_ASCII(*buf))) {
|
|
177
|
+
if ((likely((unsigned char)*buf < '\040') && likely(*buf != '\011')) || unlikely(*buf == '\177')) {
|
|
178
|
+
goto FOUND_CTL;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
FOUND_CTL:
|
|
183
|
+
if (likely(*buf == '\015')) {
|
|
184
|
+
++buf;
|
|
185
|
+
EXPECT_CHAR('\012');
|
|
186
|
+
*token_len = buf - 2 - token_start;
|
|
187
|
+
} else if (*buf == '\012') {
|
|
188
|
+
*token_len = buf - token_start;
|
|
189
|
+
++buf;
|
|
190
|
+
} else {
|
|
191
|
+
*ret = -1;
|
|
192
|
+
return NULL;
|
|
193
|
+
}
|
|
194
|
+
*token = token_start;
|
|
195
|
+
|
|
196
|
+
return buf;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static const char *is_complete(const char *buf, const char *buf_end, size_t last_len, int *ret)
|
|
200
|
+
{
|
|
201
|
+
int ret_cnt = 0;
|
|
202
|
+
buf = last_len < 3 ? buf : buf + last_len - 3;
|
|
203
|
+
|
|
204
|
+
while (1) {
|
|
205
|
+
CHECK_EOF();
|
|
206
|
+
if (*buf == '\015') {
|
|
207
|
+
++buf;
|
|
208
|
+
CHECK_EOF();
|
|
209
|
+
EXPECT_CHAR('\012');
|
|
210
|
+
++ret_cnt;
|
|
211
|
+
} else if (*buf == '\012') {
|
|
212
|
+
++buf;
|
|
213
|
+
++ret_cnt;
|
|
214
|
+
} else {
|
|
215
|
+
++buf;
|
|
216
|
+
ret_cnt = 0;
|
|
217
|
+
}
|
|
218
|
+
if (ret_cnt == 2) {
|
|
219
|
+
return buf;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
*ret = -2;
|
|
224
|
+
return NULL;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
#define PARSE_INT(valp_, mul_) \
|
|
228
|
+
if (*buf < '0' || '9' < *buf) { \
|
|
229
|
+
buf++; \
|
|
230
|
+
*ret = -1; \
|
|
231
|
+
return NULL; \
|
|
232
|
+
} \
|
|
233
|
+
*(valp_) = (mul_) * (*buf++ - '0');
|
|
234
|
+
|
|
235
|
+
#define PARSE_INT_3(valp_) \
|
|
236
|
+
do { \
|
|
237
|
+
int res_ = 0; \
|
|
238
|
+
PARSE_INT(&res_, 100); \
|
|
239
|
+
*valp_ = res_; \
|
|
240
|
+
PARSE_INT(&res_, 10); \
|
|
241
|
+
*valp_ += res_; \
|
|
242
|
+
PARSE_INT(&res_, 1); \
|
|
243
|
+
*valp_ += res_; \
|
|
244
|
+
} while (0)
|
|
245
|
+
|
|
246
|
+
/* returned pointer is always within [buf, buf_end), or null */
|
|
247
|
+
static const char *parse_token(const char *buf, const char *buf_end, const char **token, size_t *token_len, char next_char,
|
|
248
|
+
int *ret)
|
|
249
|
+
{
|
|
250
|
+
const char *token_start = buf;
|
|
251
|
+
|
|
252
|
+
#ifdef __SSE4_2__
|
|
253
|
+
static const char ALIGNED(16) ranges1[] = "\0\040"
|
|
254
|
+
/* allow HT */
|
|
255
|
+
"\177\177"
|
|
256
|
+
/* allow chars w. MSB set */
|
|
257
|
+
"\0\0\0\0";
|
|
258
|
+
int found;
|
|
259
|
+
buf = findchar_fast(buf, buf_end, ranges1, sizeof(ranges1) - 1, &found);
|
|
260
|
+
if (found)
|
|
261
|
+
goto FOUND_CTL;
|
|
262
|
+
#else
|
|
263
|
+
/* find non-printable char within the next 8 bytes, this is the hottest code; manually inlined */
|
|
264
|
+
while (likely(buf_end - buf >= 8)) {
|
|
265
|
+
#define DOIT() \
|
|
266
|
+
do { \
|
|
267
|
+
if (unlikely(!token_char_map[(unsigned char)*buf])) \
|
|
268
|
+
goto NonToken; \
|
|
269
|
+
++buf; \
|
|
270
|
+
} while (0)
|
|
271
|
+
DOIT();
|
|
272
|
+
DOIT();
|
|
273
|
+
DOIT();
|
|
274
|
+
DOIT();
|
|
275
|
+
DOIT();
|
|
276
|
+
DOIT();
|
|
277
|
+
DOIT();
|
|
278
|
+
DOIT();
|
|
279
|
+
#undef DOIT
|
|
280
|
+
continue;
|
|
281
|
+
NonToken:
|
|
282
|
+
goto FOUND_CTL;
|
|
283
|
+
}
|
|
284
|
+
#endif
|
|
285
|
+
for (;; ++buf) {
|
|
286
|
+
CHECK_EOF();
|
|
287
|
+
if (unlikely(!token_char_map[(unsigned char)*buf])) {
|
|
288
|
+
goto FOUND_CTL;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
FOUND_CTL:
|
|
292
|
+
if (*buf == next_char) {
|
|
293
|
+
*token_len = buf - token_start;
|
|
294
|
+
++buf;
|
|
295
|
+
*token = token_start;
|
|
296
|
+
return buf;
|
|
297
|
+
}
|
|
298
|
+
*ret = -1;
|
|
299
|
+
return NULL;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
static const char *parse_http_version(const char *buf, const char *buf_end, int *minor_version, int *ret)
|
|
303
|
+
{
|
|
304
|
+
/* we want at least [HTTP/1.<two chars>] to try to parse */
|
|
305
|
+
if (buf_end - buf < 9) {
|
|
306
|
+
*ret = -2;
|
|
307
|
+
return NULL;
|
|
308
|
+
}
|
|
309
|
+
EXPECT_CHAR_NO_CHECK('H');
|
|
310
|
+
EXPECT_CHAR_NO_CHECK('T');
|
|
311
|
+
EXPECT_CHAR_NO_CHECK('T');
|
|
312
|
+
EXPECT_CHAR_NO_CHECK('P');
|
|
313
|
+
EXPECT_CHAR_NO_CHECK('/');
|
|
314
|
+
EXPECT_CHAR_NO_CHECK('1');
|
|
315
|
+
EXPECT_CHAR_NO_CHECK('.');
|
|
316
|
+
PARSE_INT(minor_version, 1);
|
|
317
|
+
return buf;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
static const char *parse_headers(const char *buf, const char *buf_end, struct phr_header *headers, size_t *num_headers,
|
|
321
|
+
size_t max_headers, int *ret)
|
|
322
|
+
{
|
|
323
|
+
for (;; ++*num_headers) {
|
|
324
|
+
CHECK_EOF();
|
|
325
|
+
if (*buf == '\015') {
|
|
326
|
+
++buf;
|
|
327
|
+
EXPECT_CHAR('\012');
|
|
328
|
+
break;
|
|
329
|
+
} else if (*buf == '\012') {
|
|
330
|
+
++buf;
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
if (*num_headers == max_headers) {
|
|
334
|
+
*ret = -1;
|
|
335
|
+
return NULL;
|
|
336
|
+
}
|
|
337
|
+
if (!(*num_headers != 0 && (*buf == ' ' || *buf == '\t'))) {
|
|
338
|
+
/* parsing name, but do not discard SP before colon, see
|
|
339
|
+
* http://www.mozilla.org/security/announce/2006/mfsa2006-33.html */
|
|
340
|
+
if ((buf = parse_token(buf, buf_end, &headers[*num_headers].name, &headers[*num_headers].name_len, ':', ret)) ==
|
|
341
|
+
NULL) {
|
|
342
|
+
return NULL;
|
|
343
|
+
}
|
|
344
|
+
if (headers[*num_headers].name_len == 0) {
|
|
345
|
+
*ret = -1;
|
|
346
|
+
return NULL;
|
|
347
|
+
}
|
|
348
|
+
++buf;
|
|
349
|
+
for (;; ++buf) {
|
|
350
|
+
CHECK_EOF();
|
|
351
|
+
if (!(*buf == ' ' || *buf == '\t')) {
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
} else {
|
|
356
|
+
headers[*num_headers].name = NULL;
|
|
357
|
+
headers[*num_headers].name_len = 0;
|
|
358
|
+
}
|
|
359
|
+
if ((buf = get_token_to_eol(buf, buf_end, &headers[*num_headers].value, &headers[*num_headers].value_len, ret)) ==
|
|
360
|
+
NULL) {
|
|
361
|
+
return NULL;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return buf;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
int phr_parse_request(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path,
|
|
368
|
+
size_t *path_len, int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len)
|
|
369
|
+
{
|
|
370
|
+
const char *buf = buf_start, *buf_end = buf_start + len;
|
|
371
|
+
size_t max_headers = *num_headers;
|
|
372
|
+
int r;
|
|
373
|
+
|
|
374
|
+
*method = NULL;
|
|
375
|
+
*method_len = 0;
|
|
376
|
+
*path = NULL;
|
|
377
|
+
*path_len = 0;
|
|
378
|
+
*minor_version = -1;
|
|
379
|
+
*num_headers = 0;
|
|
380
|
+
|
|
381
|
+
/* if last_len != 0, check if the request is complete (a fast countermeasure
|
|
382
|
+
againt slowloris */
|
|
383
|
+
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
|
384
|
+
return r;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if ((buf = parse_token(buf, buf_end, method, method_len, ' ', &r)) == NULL) {
|
|
388
|
+
return r;
|
|
389
|
+
}
|
|
390
|
+
if (*method_len == 0) {
|
|
391
|
+
return -1;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if ((buf = parse_token(buf, buf_end, path, path_len, ' ', &r)) == NULL) {
|
|
395
|
+
return r;
|
|
396
|
+
}
|
|
397
|
+
if (*path_len == 0) {
|
|
398
|
+
return -1;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if ((buf = parse_http_version(buf, buf_end, minor_version, &r)) == NULL) {
|
|
402
|
+
return r;
|
|
403
|
+
}
|
|
404
|
+
if (*buf == '\015') {
|
|
405
|
+
++buf;
|
|
406
|
+
if (buf == buf_end) return -2;
|
|
407
|
+
if (*buf != '\012') return -1;
|
|
408
|
+
} else if (*buf == '\012') {
|
|
409
|
+
++buf;
|
|
410
|
+
} else {
|
|
411
|
+
return -1;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
|
|
415
|
+
return r;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return buf - buf_start;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
int phr_parse_response(const char *buf_start, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
|
422
|
+
struct phr_header *headers, size_t *num_headers, size_t last_len)
|
|
423
|
+
{
|
|
424
|
+
const char *buf = buf_start, *buf_end = buf_start + len;
|
|
425
|
+
size_t max_headers = *num_headers;
|
|
426
|
+
int r;
|
|
427
|
+
|
|
428
|
+
*minor_version = -1;
|
|
429
|
+
*status = 0;
|
|
430
|
+
*msg = NULL;
|
|
431
|
+
*msg_len = 0;
|
|
432
|
+
*num_headers = 0;
|
|
433
|
+
|
|
434
|
+
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
|
435
|
+
againt slowloris */
|
|
436
|
+
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
|
437
|
+
return r;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if ((buf = parse_http_version(buf, buf_end, minor_version, &r)) == NULL) {
|
|
441
|
+
return r;
|
|
442
|
+
}
|
|
443
|
+
if (*buf == ' ') {
|
|
444
|
+
++buf;
|
|
445
|
+
} else {
|
|
446
|
+
return -1;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (buf_end - buf < 3) {
|
|
450
|
+
return -2;
|
|
451
|
+
}
|
|
452
|
+
if (!('0' <= buf[0] && buf[0] <= '9') || !('0' <= buf[1] && buf[1] <= '9') || !('0' <= buf[2] && buf[2] <= '9')) {
|
|
453
|
+
return -1;
|
|
454
|
+
}
|
|
455
|
+
*status = (buf[0] - '0') * 100 + (buf[1] - '0') * 10 + (buf[2] - '0');
|
|
456
|
+
buf += 3;
|
|
457
|
+
|
|
458
|
+
if (*buf == ' ') {
|
|
459
|
+
++buf;
|
|
460
|
+
} else {
|
|
461
|
+
return -1;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if ((buf = get_token_to_eol(buf, buf_end, msg, msg_len, &r)) == NULL) {
|
|
465
|
+
return r;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
|
|
469
|
+
return r;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return buf - buf_start;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
int phr_parse_headers(const char *buf_start, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len)
|
|
476
|
+
{
|
|
477
|
+
const char *buf = buf_start, *buf_end = buf_start + len;
|
|
478
|
+
size_t max_headers = *num_headers;
|
|
479
|
+
int r;
|
|
480
|
+
|
|
481
|
+
*num_headers = 0;
|
|
482
|
+
|
|
483
|
+
/* if last_len != 0, check if the response is complete (a fast countermeasure
|
|
484
|
+
againt slowloris */
|
|
485
|
+
if (last_len != 0 && is_complete(buf, buf_end, last_len, &r) == NULL) {
|
|
486
|
+
return r;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
if ((buf = parse_headers(buf, buf_end, headers, num_headers, max_headers, &r)) == NULL) {
|
|
490
|
+
return r;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return buf - buf_start;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
#undef PARSE_INT
|
|
497
|
+
#undef PARSE_INT_3
|
|
498
|
+
#undef EXPECT_CHAR
|
|
499
|
+
#undef EXPECT_CHAR_NO_CHECK
|
|
500
|
+
#undef CHECK_EOF
|
|
501
|
+
|
|
502
|
+
ssize_t phr_decode_chunked(char *buf, size_t *buf_sz)
|
|
503
|
+
{
|
|
504
|
+
char *dst = buf, *src = buf, *end = buf + *buf_sz;
|
|
505
|
+
size_t chunk_len;
|
|
506
|
+
|
|
507
|
+
while (1) {
|
|
508
|
+
if (end - src < 2)
|
|
509
|
+
return -2;
|
|
510
|
+
// Parse hex chunk size
|
|
511
|
+
chunk_len = 0;
|
|
512
|
+
int i = 0;
|
|
513
|
+
while (1) {
|
|
514
|
+
if (src >= end) return -2;
|
|
515
|
+
char c = *src++;
|
|
516
|
+
if (c >= '0' && c <= '9') {
|
|
517
|
+
chunk_len = chunk_len * 16 + (c - '0');
|
|
518
|
+
} else if (c >= 'a' && c <= 'f') {
|
|
519
|
+
chunk_len = chunk_len * 16 + (c - 'a' + 10);
|
|
520
|
+
} else if (c >= 'A' && c <= 'F') {
|
|
521
|
+
chunk_len = chunk_len * 16 + (c - 'A' + 10);
|
|
522
|
+
} else if (c == '\r') {
|
|
523
|
+
if (src >= end || *src != '\n') return -2; // Expect \n
|
|
524
|
+
src++;
|
|
525
|
+
break;
|
|
526
|
+
} else if (c == ';') {
|
|
527
|
+
// Extension, skip until CRLF
|
|
528
|
+
while (src < end) {
|
|
529
|
+
if (*src++ == '\r') {
|
|
530
|
+
if (src >= end || *src != '\n') return -2;
|
|
531
|
+
src++;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
if (src >= end) return -2; // Incomplete
|
|
536
|
+
break;
|
|
537
|
+
} else {
|
|
538
|
+
return -1; // Invalid char
|
|
539
|
+
}
|
|
540
|
+
if (++i > 8) return -1; // Too long hex
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (chunk_len == 0) {
|
|
544
|
+
// Last chunk, trailer headers may follow (not implemented here, we just stop)
|
|
545
|
+
// But we must consume the final CRLF if present?
|
|
546
|
+
// The spec says 0\r\n\r\n
|
|
547
|
+
if (end - src >= 2 && src[0] == '\r' && src[1] == '\n') {
|
|
548
|
+
src += 2;
|
|
549
|
+
}
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if ((size_t)(end - src) < chunk_len + 2) return -2; // Need chunk + CRLF
|
|
554
|
+
|
|
555
|
+
// Move data
|
|
556
|
+
memmove(dst, src, chunk_len);
|
|
557
|
+
dst += chunk_len;
|
|
558
|
+
src += chunk_len;
|
|
559
|
+
|
|
560
|
+
// Consume CRLF
|
|
561
|
+
if (src[0] != '\r' || src[1] != '\n') return -1;
|
|
562
|
+
src += 2;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
*buf_sz = dst - buf;
|
|
566
|
+
return src - buf;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
int phr_parse_chunked(const char *buf, size_t len, size_t *chunk_size)
|
|
570
|
+
{
|
|
571
|
+
// Minimal implementation just to read the size line
|
|
572
|
+
const char *src = buf;
|
|
573
|
+
const char *end = buf + len;
|
|
574
|
+
size_t sz = 0;
|
|
575
|
+
int i = 0;
|
|
576
|
+
|
|
577
|
+
while (1) {
|
|
578
|
+
if (src >= end) return -2;
|
|
579
|
+
char c = *src++;
|
|
580
|
+
if (c >= '0' && c <= '9') {
|
|
581
|
+
sz = sz * 16 + (c - '0');
|
|
582
|
+
} else if (c >= 'a' && c <= 'f') {
|
|
583
|
+
sz = sz * 16 + (c - 'a' + 10);
|
|
584
|
+
} else if (c >= 'A' && c <= 'F') {
|
|
585
|
+
sz = sz * 16 + (c - 'A' + 10);
|
|
586
|
+
} else if (c == '\r') {
|
|
587
|
+
if (src >= end) return -2;
|
|
588
|
+
if (*src != '\n') return -1;
|
|
589
|
+
src++;
|
|
590
|
+
*chunk_size = sz;
|
|
591
|
+
return src - buf;
|
|
592
|
+
} else if (c == ';') {
|
|
593
|
+
while (src < end) {
|
|
594
|
+
if (*src++ == '\r') {
|
|
595
|
+
if (src >= end) return -2;
|
|
596
|
+
if (*src != '\n') return -1;
|
|
597
|
+
src++;
|
|
598
|
+
*chunk_size = sz;
|
|
599
|
+
return src - buf;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
return -2;
|
|
603
|
+
} else {
|
|
604
|
+
return -1;
|
|
605
|
+
}
|
|
606
|
+
if (++i > 8) return -1;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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. Choose whichever you prefer.
|
|
7
|
+
*
|
|
8
|
+
* MIT License
|
|
9
|
+
*
|
|
10
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
* in the Software without restriction, including without limitation the rights
|
|
13
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
* furnished to do so, subject to the following conditions:
|
|
16
|
+
*
|
|
17
|
+
* The above copyright notice and this permission notice shall be included in
|
|
18
|
+
* all copies or substantial portions of the Software.
|
|
19
|
+
*
|
|
20
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
+
* THE SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
#ifndef picohttpparser_h
|
|
29
|
+
#define picohttpparser_h
|
|
30
|
+
|
|
31
|
+
#include <sys/types.h>
|
|
32
|
+
|
|
33
|
+
#ifdef __cplusplus
|
|
34
|
+
extern "C" {
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
/* contains name and value of a header (name == NULL if is a continuing line
|
|
38
|
+
* of a multiline header */
|
|
39
|
+
struct phr_header {
|
|
40
|
+
const char *name;
|
|
41
|
+
size_t name_len;
|
|
42
|
+
const char *value;
|
|
43
|
+
size_t value_len;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/* returns number of bytes consumed if successful, -2 if request is partial,
|
|
47
|
+
* -1 if failed */
|
|
48
|
+
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
|
|
49
|
+
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
|
50
|
+
|
|
51
|
+
/* returns number of bytes consumed if successful, -2 if response is partial,
|
|
52
|
+
* -1 if failed */
|
|
53
|
+
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
|
|
54
|
+
struct phr_header *headers, size_t *num_headers, size_t last_len);
|
|
55
|
+
|
|
56
|
+
/* returns number of bytes consumed if successful, -2 if response is partial,
|
|
57
|
+
* -1 if failed */
|
|
58
|
+
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
|
|
59
|
+
|
|
60
|
+
/* decodes a chunked-encoded par of the message */
|
|
61
|
+
ssize_t phr_decode_chunked(char *buf, size_t *buf_sz);
|
|
62
|
+
|
|
63
|
+
/* returns number of bytes consumed if successful, -2 if response is partial,
|
|
64
|
+
* -1 if failed */
|
|
65
|
+
int phr_parse_chunked(const char *buf, size_t len, size_t *chunk_size);
|
|
66
|
+
|
|
67
|
+
#ifdef __cplusplus
|
|
68
|
+
}
|
|
69
|
+
#endif
|
|
70
|
+
|
|
71
|
+
#endif
|