Skip to content

Commit 005a3da

Browse files
committed
release: 3.0.0
1 parent 8b3939e commit 005a3da

4 files changed

Lines changed: 577 additions & 440 deletions

File tree

include/llhttp.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef INCLUDE_LLHTTP_H_
22
#define INCLUDE_LLHTTP_H_
33

4-
#define LLHTTP_VERSION_MAJOR 2
5-
#define LLHTTP_VERSION_MINOR 2
4+
#define LLHTTP_VERSION_MAJOR 3
5+
#define LLHTTP_VERSION_MINOR 0
66
#define LLHTTP_VERSION_PATCH 0
77

88
#ifndef LLHTTP_STRICT_MODE
@@ -33,10 +33,11 @@ struct llhttp__internal_s {
3333
uint8_t http_major;
3434
uint8_t http_minor;
3535
uint8_t header_state;
36-
uint16_t flags;
36+
uint8_t lenient_flags;
3737
uint8_t upgrade;
38-
uint16_t status_code;
3938
uint8_t finish;
39+
uint16_t flags;
40+
uint16_t status_code;
4041
void* settings;
4142
};
4243

@@ -91,11 +92,16 @@ enum llhttp_flags {
9192
F_CONTENT_LENGTH = 0x20,
9293
F_SKIPBODY = 0x40,
9394
F_TRAILING = 0x80,
94-
F_LENIENT = 0x100,
9595
F_TRANSFER_ENCODING = 0x200
9696
};
9797
typedef enum llhttp_flags llhttp_flags_t;
9898

99+
enum llhttp_lenient_flags {
100+
LENIENT_HEADERS = 0x1,
101+
LENIENT_CHUNKED_LENGTH = 0x2
102+
};
103+
typedef enum llhttp_lenient_flags llhttp_lenient_flags_t;
104+
99105
enum llhttp_type {
100106
HTTP_BOTH = 0,
101107
HTTP_REQUEST = 1,
@@ -286,6 +292,11 @@ struct llhttp_settings_s {
286292
*/
287293
llhttp_cb on_chunk_header;
288294
llhttp_cb on_chunk_complete;
295+
296+
llhttp_cb on_url_complete;
297+
llhttp_cb on_status_complete;
298+
llhttp_cb on_header_field_complete;
299+
llhttp_cb on_header_value_complete;
289300
};
290301

291302
/* Initialize the parser with specific type and user settings.
@@ -400,7 +411,20 @@ const char* llhttp_method_name(llhttp_method_t method);
400411
*
401412
* **(USE AT YOUR OWN RISK)**
402413
*/
403-
void llhttp_set_lenient(llhttp_t* parser, int enabled);
414+
void llhttp_set_lenient_headers(llhttp_t* parser, int enabled);
415+
416+
417+
/* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
418+
* `Content-Length` headers (disabled by default).
419+
*
420+
* Normally `llhttp` would error when `Transfer-Encoding` is present in
421+
* conjunction with `Content-Length`. This error is important to prevent HTTP
422+
* request smuggling, but may be less desirable for small number of cases
423+
* involving legacy servers.
424+
*
425+
* **(USE AT YOUR OWN RISK)**
426+
*/
427+
void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
404428

405429
#ifdef __cplusplus
406430
} /* extern "C" */

src/api.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,19 @@ const char* llhttp_method_name(llhttp_method_t method) {
127127
}
128128

129129

130-
void llhttp_set_lenient(llhttp_t* parser, int enabled) {
130+
void llhttp_set_lenient_headers(llhttp_t* parser, int enabled) {
131131
if (enabled) {
132-
parser->flags |= F_LENIENT;
132+
parser->lenient_flags |= LENIENT_HEADERS;
133133
} else {
134-
parser->flags &= ~F_LENIENT;
134+
parser->lenient_flags &= ~LENIENT_HEADERS;
135+
}
136+
}
137+
138+
void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled) {
139+
if (enabled) {
140+
parser->lenient_flags |= LENIENT_CHUNKED_LENGTH;
141+
} else {
142+
parser->lenient_flags &= ~LENIENT_CHUNKED_LENGTH;
135143
}
136144
}
137145

@@ -153,27 +161,55 @@ int llhttp__on_url(llhttp_t* s, const char* p, const char* endp) {
153161
}
154162

155163

164+
int llhttp__on_url_complete(llhttp_t* s, const char* p, const char* endp) {
165+
int err;
166+
CALLBACK_MAYBE(s, on_url_complete, s);
167+
return err;
168+
}
169+
170+
156171
int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) {
157172
int err;
158173
CALLBACK_MAYBE(s, on_status, s, p, endp - p);
159174
return err;
160175
}
161176

162177

178+
int llhttp__on_status_complete(llhttp_t* s, const char* p, const char* endp) {
179+
int err;
180+
CALLBACK_MAYBE(s, on_status_complete, s);
181+
return err;
182+
}
183+
184+
163185
int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) {
164186
int err;
165187
CALLBACK_MAYBE(s, on_header_field, s, p, endp - p);
166188
return err;
167189
}
168190

169191

192+
int llhttp__on_header_field_complete(llhttp_t* s, const char* p, const char* endp) {
193+
int err;
194+
CALLBACK_MAYBE(s, on_header_field_complete, s);
195+
return err;
196+
}
197+
198+
170199
int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) {
171200
int err;
172201
CALLBACK_MAYBE(s, on_header_value, s, p, endp - p);
173202
return err;
174203
}
175204

176205

206+
int llhttp__on_header_value_complete(llhttp_t* s, const char* p, const char* endp) {
207+
int err;
208+
CALLBACK_MAYBE(s, on_header_value_complete, s);
209+
return err;
210+
}
211+
212+
177213
int llhttp__on_headers_complete(llhttp_t* s, const char* p, const char* endp) {
178214
int err;
179215
CALLBACK_MAYBE(s, on_headers_complete, s);

src/http.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ int llhttp__after_headers_complete(llhttp_t* parser, const char* p,
5151
/* chunked encoding - ignore Content-Length header, prepare for a chunk */
5252
return 2;
5353
} else if (parser->flags & F_TRANSFER_ENCODING) {
54-
if (parser->type == HTTP_REQUEST && (parser->flags & F_LENIENT) == 0) {
54+
if (parser->type == HTTP_REQUEST &&
55+
(parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0) {
5556
/* RFC 7230 3.3.3 */
5657

5758
/* If a Transfer-Encoding header field
@@ -97,9 +98,7 @@ int llhttp__after_message_complete(llhttp_t* parser, const char* p,
9798

9899
should_keep_alive = llhttp_should_keep_alive(parser);
99100
parser->finish = HTTP_FINISH_SAFE;
100-
101-
/* Keep `F_LENIENT` flag between messages, but reset every other flag */
102-
parser->flags &= F_LENIENT;
101+
parser->flags = 0;
103102

104103
/* NOTE: this is ignored in loose parsing mode */
105104
return should_keep_alive;

0 commit comments

Comments
 (0)