Skip to content

Commit a60921b

Browse files
committed
feat: Added on_chunk_parameters callback.
1 parent 94a8e7e commit a60921b

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

src/llhttp/http.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const NODES: ReadonlyArray<string> = [
8484
'chunk_size_almost_done',
8585
'chunk_size_almost_done_lf',
8686
'chunk_parameters',
87+
'chunk_parameters_ows',
8788
'chunk_data',
8889
'chunk_data_almost_done',
8990
'chunk_data_almost_done_skip',
@@ -101,6 +102,7 @@ const NODES: ReadonlyArray<string> = [
101102

102103
interface ISpanMap {
103104
readonly body: source.Span;
105+
readonly chunkParameters: source.Span;
104106
readonly headerField: source.Span;
105107
readonly headerValue: source.Span;
106108
readonly status: source.Span;
@@ -157,6 +159,7 @@ export class HTTP {
157159

158160
this.span = {
159161
body: p.span(p.code.span('llhttp__on_body')),
162+
chunkParameters: p.span(p.code.span('llhttp__on_chunk_parameters')),
160163
headerField: p.span(p.code.span('llhttp__on_header_field')),
161164
headerValue: p.span(p.code.span('llhttp__on_header_value')),
162165
status: p.span(p.code.span('llhttp__on_status')),
@@ -798,12 +801,16 @@ export class HTTP {
798801

799802
n('chunk_size_otherwise')
800803
.match('\r', n('chunk_size_almost_done'))
801-
.match([ ';', ' ' ], n('chunk_parameters'))
804+
.match([ ';' ], n('chunk_parameters_ows'))
802805
.otherwise(p.error(ERROR.INVALID_CHUNK_SIZE,
803806
'Invalid character in chunk size'));
804807

808+
n('chunk_parameters_ows')
809+
.match(' ', n('chunk_parameters_ows'))
810+
.otherwise(span.chunkParameters.start(n('chunk_parameters')));
811+
805812
n('chunk_parameters')
806-
.match('\r', n('chunk_size_almost_done'))
813+
.peek('\r', span.chunkParameters.end().skipTo(n('chunk_size_almost_done')))
807814
.match(HEADER_CHARS, n('chunk_parameters'))
808815
.otherwise(p.error(ERROR.STRICT,
809816
'Invalid character in chunk parameters'));

src/native/api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,13 @@ int llhttp__on_chunk_header(llhttp_t* s, const char* p, const char* endp) {
355355
}
356356

357357

358+
int llhttp__on_chunk_parameters(llhttp_t* s, const char* p, const char* endp) {
359+
int err;
360+
SPAN_CALLBACK_MAYBE(s, on_chunk_parameters, p, endp - p);
361+
return err;
362+
}
363+
364+
358365
int llhttp__on_chunk_complete(llhttp_t* s, const char* p, const char* endp) {
359366
int err;
360367
CALLBACK_MAYBE(s, on_chunk_complete);

src/native/api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ struct llhttp_settings_s {
3838
*/
3939
llhttp_cb on_headers_complete;
4040

41+
/* Possible return values 0, -1, HPE_USER */
42+
llhttp_data_cb on_chunk_parameters;
43+
4144
/* Possible return values 0, -1, HPE_USER */
4245
llhttp_data_cb on_body;
4346

test/fixtures/extra.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ int llhttp__on_body(llparse_t* s, const char* p, const char* endp) {
203203
return llparse__print_span("body", p, endp);
204204
}
205205

206+
int llhttp__on_chunk_parameters(llparse_t* s, const char* p, const char* endp) {
207+
if (llparse__in_bench)
208+
return 0;
209+
return llparse__print_span("chunk parameters", p, endp);
210+
}
211+
206212

207213
int llhttp__on_chunk_header(llparse_t* s, const char* p, const char* endp) {
208214
if (llparse__in_bench)

test/request/transfer-encoding.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,11 @@ off=66 header_field complete
223223
off=67 len=7 span[header_value]="chunked"
224224
off=76 header_value complete
225225
off=78 headers complete method=3 v=1/1 flags=208 content_length=0
226+
off=81 len=40 span[chunk parameters]="ilovew3;somuchlove=aretheseparametersfor"
226227
off=123 chunk header len=5
227228
off=123 len=5 span[body]="hello"
228229
off=130 chunk complete
230+
off=133 len=14 span[chunk parameters]="blahblah; blah"
229231
off=149 chunk header len=6
230232
off=149 len=6 span[body]=" world"
231233
off=157 chunk complete
@@ -598,7 +600,7 @@ off=37 header_field complete
598600
off=38 len=7 span[header_value]="chunked"
599601
off=47 header_value complete
600602
off=49 headers complete method=4 v=1/1 flags=208 content_length=0
601-
off=51 error code=2 reason="Invalid character in chunk parameters"
603+
off=50 error code=12 reason="Invalid character in chunk size"
602604
```
603605

604606
## Invalid OBS fold after chunked value

test/response/transfer-encoding.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,7 @@ off=61 header_field complete
3333
off=62 len=7 span[header_value]="chunked"
3434
off=71 header_value complete
3535
off=73 headers complete status=200 v=1/1 flags=208 content_length=0
36-
off=79 chunk header len=37
37-
off=79 len=35 span[body]="This is the data in the first chunk"
38-
off=114 len=1 span[body]=cr
39-
off=115 len=1 span[body]=lf
40-
off=118 chunk complete
41-
off=122 chunk header len=28
42-
off=122 len=26 span[body]="and this is the second one"
43-
off=148 len=1 span[body]=cr
44-
off=149 len=1 span[body]=lf
45-
off=152 chunk complete
46-
off=157 chunk header len=0
47-
off=159 chunk complete
48-
off=159 message complete
36+
off=75 error code=12 reason="Invalid character in chunk size"
4937
```
5038

5139
### `chunked` before other transfer-encoding

0 commit comments

Comments
 (0)