Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added on_chunk_parameters callback. #264

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/llhttp/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const NODES: ReadonlyArray<string> = [
'chunk_size_almost_done',
'chunk_size_almost_done_lf',
'chunk_parameters',
'chunk_parameters_ows',
'chunk_data',
'chunk_data_almost_done',
'chunk_data_almost_done_skip',
Expand All @@ -101,6 +102,7 @@ const NODES: ReadonlyArray<string> = [

interface ISpanMap {
readonly body: source.Span;
readonly chunkParameters: source.Span;
readonly headerField: source.Span;
readonly headerValue: source.Span;
readonly status: source.Span;
Expand Down Expand Up @@ -157,6 +159,7 @@ export class HTTP {

this.span = {
body: p.span(p.code.span('llhttp__on_body')),
chunkParameters: p.span(p.code.span('llhttp__on_chunk_parameters')),
headerField: p.span(p.code.span('llhttp__on_header_field')),
headerValue: p.span(p.code.span('llhttp__on_header_value')),
status: p.span(p.code.span('llhttp__on_status')),
Expand Down Expand Up @@ -798,12 +801,16 @@ export class HTTP {

n('chunk_size_otherwise')
.match('\r', n('chunk_size_almost_done'))
.match([ ';', ' ' ], n('chunk_parameters'))
.match([ ';' ], n('chunk_parameters_ows'))
.otherwise(p.error(ERROR.INVALID_CHUNK_SIZE,
'Invalid character in chunk size'));

n('chunk_parameters_ows')
.match(' ', n('chunk_parameters_ows'))
.otherwise(span.chunkParameters.start(n('chunk_parameters')));

n('chunk_parameters')
.match('\r', n('chunk_size_almost_done'))
.peek('\r', span.chunkParameters.end().skipTo(n('chunk_size_almost_done')))
.match(HEADER_CHARS, n('chunk_parameters'))
.otherwise(p.error(ERROR.STRICT,
'Invalid character in chunk parameters'));
Expand Down
7 changes: 7 additions & 0 deletions src/native/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ int llhttp__on_chunk_header(llhttp_t* s, const char* p, const char* endp) {
}


int llhttp__on_chunk_parameters(llhttp_t* s, const char* p, const char* endp) {
int err;
SPAN_CALLBACK_MAYBE(s, on_chunk_parameters, p, endp - p);
return err;
}


int llhttp__on_chunk_complete(llhttp_t* s, const char* p, const char* endp) {
int err;
CALLBACK_MAYBE(s, on_chunk_complete);
Expand Down
3 changes: 3 additions & 0 deletions src/native/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ struct llhttp_settings_s {
*/
llhttp_cb on_headers_complete;

/* Possible return values 0, -1, HPE_USER */
llhttp_data_cb on_chunk_parameters;

/* Possible return values 0, -1, HPE_USER */
llhttp_data_cb on_body;

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/extra.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ int llhttp__on_body(llparse_t* s, const char* p, const char* endp) {
return llparse__print_span("body", p, endp);
}

int llhttp__on_chunk_parameters(llparse_t* s, const char* p, const char* endp) {
if (llparse__in_bench)
return 0;
return llparse__print_span("chunk parameters", p, endp);
}


int llhttp__on_chunk_header(llparse_t* s, const char* p, const char* endp) {
if (llparse__in_bench)
Expand Down
4 changes: 3 additions & 1 deletion test/request/transfer-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,11 @@ off=66 header_field complete
off=67 len=7 span[header_value]="chunked"
off=76 header_value complete
off=78 headers complete method=3 v=1/1 flags=208 content_length=0
off=81 len=40 span[chunk parameters]="ilovew3;somuchlove=aretheseparametersfor"
off=123 chunk header len=5
off=123 len=5 span[body]="hello"
off=130 chunk complete
off=133 len=14 span[chunk parameters]="blahblah; blah"
off=149 chunk header len=6
off=149 len=6 span[body]=" world"
off=157 chunk complete
Expand Down Expand Up @@ -598,7 +600,7 @@ off=37 header_field complete
off=38 len=7 span[header_value]="chunked"
off=47 header_value complete
off=49 headers complete method=4 v=1/1 flags=208 content_length=0
off=51 error code=2 reason="Invalid character in chunk parameters"
off=50 error code=12 reason="Invalid character in chunk size"
```

## Invalid OBS fold after chunked value
Expand Down
14 changes: 1 addition & 13 deletions test/response/transfer-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ off=61 header_field complete
off=62 len=7 span[header_value]="chunked"
off=71 header_value complete
off=73 headers complete status=200 v=1/1 flags=208 content_length=0
off=79 chunk header len=37
off=79 len=35 span[body]="This is the data in the first chunk"
off=114 len=1 span[body]=cr
off=115 len=1 span[body]=lf
off=118 chunk complete
off=122 chunk header len=28
off=122 len=26 span[body]="and this is the second one"
off=148 len=1 span[body]=cr
off=149 len=1 span[body]=lf
off=152 chunk complete
off=157 chunk header len=0
off=159 chunk complete
off=159 message complete
off=75 error code=12 reason="Invalid character in chunk size"
```

### `chunked` before other transfer-encoding
Expand Down
Loading