Skip to content
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
4 changes: 2 additions & 2 deletions bench/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ if (isURL) {
}

if (isHTTP) {
requests.forEach((request, name) => {
for(const [request, name] of requests) {
console.log('http loose: "%s" (C)', name);

spawnSync('./test/tmp/http-loose-request-c', [
Expand All @@ -79,5 +79,5 @@ if (isHTTP) {
'bench',
request
], { stdio: 'inherit' });
});
}
}
14 changes: 8 additions & 6 deletions src/llhttp/c-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ export class CHeaders {

res += `enum ${name} {\n`;
const keys = Object.keys(map);
keys.forEach((key, i) => {
const isLast = i === keys.length - 1;
const keysLength = keys.length;
for (let i = 0; i < keysLength; i++) {
const key = keys[i];
const isLast = i === keysLength - 1;

let value: number | string = map[key];

Expand All @@ -78,7 +80,7 @@ export class CHeaders {
if (!isLast) {
res += ',\n';
}
});
}
res += '\n};\n';
res += `typedef enum ${name} ${name}_t;\n`;

Expand All @@ -89,9 +91,9 @@ export class CHeaders {
let res = '';

res += `#define ${name}_MAP(XX) \\\n`;
Object.keys(map).forEach((key) => {
res += ` XX(${map[key]!}, ${key.replace(/-/g, '')}, ${key}) \\\n`;
});
for (const [key, value] of Object.entries(map)) {
res += ` XX(${value!}, ${key.replace(/-/g, '')}, ${key}) \\\n`;
}
res += '\n';

return res;
Expand Down
9 changes: 7 additions & 2 deletions src/llhttp/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export enum ERROR {
PAUSED_H2_UPGRADE = 23,

USER = 24,

CB_URL_COMPLETE = 26,
CB_STATUS_COMPLETE = 27,
CB_HEADER_FIELD_COMPLETE = 28,
CB_HEADER_VALUE_COMPLETE = 29,
}

export enum TYPE {
Expand Down Expand Up @@ -189,11 +194,11 @@ export const METHODS_RTSP = [
export const METHOD_MAP = enumToMap(METHODS);
export const H_METHOD_MAP: IEnumMap = {};

Object.keys(METHOD_MAP).forEach((key) => {
for (const key of Object.keys(METHOD_MAP)) {
if (/^H/.test(key)) {
H_METHOD_MAP[key] = METHOD_MAP[key];
}
});
}

export enum FINISH {
SAFE = 0,
Expand Down
62 changes: 43 additions & 19 deletions src/llhttp/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export class HTTP {
};
/* tslint:enable:object-literal-sort-keys */

NODES.forEach((name) => this.nodes.set(name, p.node(name) as Match));
for (const name of NODES) {
this.nodes.set(name, p.node(name) as Match);
}
}

public build(): IHTTPResult {
Expand Down Expand Up @@ -338,14 +340,16 @@ export class HTTP {
notEqual: url.entry.normal,
}));

const onUrlCompleteHTTP = p.invoke(this.callback.onUrlComplete);
onUrlCompleteHTTP.otherwise(n('req_http_start'));
const onUrlCompleteHTTP = this.invokePausable(
'on_url_complete', ERROR.CB_URL_COMPLETE, n('req_http_start'),
);

url.exit.toHTTP
.otherwise(onUrlCompleteHTTP);

const onUrlCompleteHTTP09 = p.invoke(this.callback.onUrlComplete);
onUrlCompleteHTTP09.otherwise(n('header_field_start'));
const onUrlCompleteHTTP09 = this.invokePausable(
'on_url_complete', ERROR.CB_URL_COMPLETE, n('header_field_start'),
);

url.exit.toHTTP09
.otherwise(
Expand Down Expand Up @@ -425,8 +429,9 @@ export class HTTP {
.select(SPECIAL_HEADERS, this.store('header_state', 'header_field_colon'))
.otherwise(this.resetHeaderState('header_field_general'));

const onHeaderFieldComplete = p.invoke(this.callback.onHeaderFieldComplete);
onHeaderFieldComplete.otherwise(n('header_value_discard_ws'));
const onHeaderFieldComplete = this.invokePausable(
'on_header_field_complete', ERROR.CB_HEADER_FIELD_COMPLETE, n('header_value_discard_ws'),
);

const onInvalidHeaderFieldChar =
p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header field char');
Expand Down Expand Up @@ -488,8 +493,9 @@ export class HTTP {
n('header_value_discard_lws'));
}

const onHeaderValueComplete = p.invoke(this.callback.onHeaderValueComplete);
onHeaderValueComplete.otherwise(n('header_field_start'));
const onHeaderValueComplete = this.invokePausable(
'on_header_value_complete', ERROR.CB_HEADER_VALUE_COMPLETE, n('header_field_start'),
);

const emptyContentLengthError = p.error(
ERROR.INVALID_CONTENT_LENGTH, 'Empty Content-Length');
Expand Down Expand Up @@ -1000,16 +1006,34 @@ export class HTTP {
private invokePausable(name: string, errorCode: ERROR, next: string | Node)
: Node {
let cb;
if (name === 'on_message_begin') {
cb = this.callback.onMessageBegin;
} else if (name === 'on_message_complete') {
cb = this.callback.onMessageComplete;
} else if (name === 'on_chunk_header') {
cb = this.callback.onChunkHeader;
} else if (name === 'on_chunk_complete') {
cb = this.callback.onChunkComplete;
} else {
throw new Error('Unknown callback: ' + name);

switch (name) {
case 'on_message_begin':
cb = this.callback.onMessageBegin;
break;
case 'on_url_complete':
cb = this.callback.onUrlComplete;
break;
case 'on_status_complete':
cb = this.callback.onStatusComplete;
break;
case 'on_header_field_complete':
cb = this.callback.onHeaderFieldComplete;
break;
case 'on_header_value_complete':
cb = this.callback.onHeaderValueComplete;
break;
case 'on_message_complete':
cb = this.callback.onMessageComplete;
break;
case 'on_chunk_header':
cb = this.callback.onChunkHeader;
break;
case 'on_chunk_complete':
cb = this.callback.onChunkComplete;
break;
default:
throw new Error('Unknown callback: ' + name);
}

const p = this.llparse;
Expand Down
18 changes: 6 additions & 12 deletions src/llhttp/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ export class URL {
.match('//', this.spanStart('host', server))
.otherwise(p.error(ERROR.INVALID_URL, 'Unexpected char in url schema'));

[
server,
serverWithAt,
].forEach((node) => {
for (const node of [server, serverWithAt]) {
node
.peek('/', this.spanEnd('host', this.spanStart('path').skipTo(path)))
.match('?', this.spanEnd('host', this.spanStart('query', query)))
Expand All @@ -112,7 +109,7 @@ export class URL {
if (node !== serverWithAt) {
node.match('@', serverWithAt);
}
});
}

serverWithAt
.match('@', p.error(ERROR.INVALID_URL, 'Double @ in url'));
Expand Down Expand Up @@ -142,10 +139,10 @@ export class URL {
.otherwise(
p.error(ERROR.INVALID_URL, 'Invalid char in url fragment start'));

[ start, schema, schemaDelim ].forEach((node) => {
for (const node of [ start, schema, schemaDelim ]) {
/* No whitespace allowed here */
node.match([ ' ', '\r', '\n' ], this.errorInvalid);
});
}

// Adaptors
const toHTTP = this.node('to_http');
Expand All @@ -161,10 +158,7 @@ export class URL {
.match('\r\n', toHTTP09)
.otherwise(p.error(ERROR.INVALID_URL, 'Expected CRLF'));

[
server, serverWithAt, queryOrFragment, queryStart, query,
fragment,
].forEach((node) => {
for (const node of [server, serverWithAt, queryOrFragment, queryStart, query, fragment]) {
let spanName: SpanName | undefined;

if (node === server || node === serverWithAt) {
Expand All @@ -187,7 +181,7 @@ export class URL {

node.peek('\r', endTo(skipCRLF));
node.peek('\n', endTo(skipToHTTP09));
});
}

return {
entry,
Expand Down
10 changes: 5 additions & 5 deletions src/llhttp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ export function enumToMap(
): IEnumMap {
const res: IEnumMap = {};

Object.keys(obj).forEach((key) => {
for (const key of Object.keys(obj)) {
const value = obj[key];
if (typeof value !== 'number') {
return;
continue;
}
if (filter && !filter.includes(value)) {
return;
continue;
}
if (exceptions && exceptions.includes(value)) {
return;
continue;
}
res[key] = value;
});
}

return res;
}
10 changes: 4 additions & 6 deletions src/native/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,17 @@ struct llhttp_settings_s {

/* Possible return values 0, -1, `HPE_PAUSED` */
llhttp_cb on_message_complete;
llhttp_cb on_url_complete;
llhttp_cb on_status_complete;
llhttp_cb on_header_field_complete;
llhttp_cb on_header_value_complete;

/* When on_chunk_header is called, the current chunk length is stored
* in parser->content_length.
* Possible return values 0, -1, `HPE_PAUSED`
*/
llhttp_cb on_chunk_header;
llhttp_cb on_chunk_complete;

/* Information-only callbacks, return value is ignored */
llhttp_cb on_url_complete;
llhttp_cb on_status_complete;
llhttp_cb on_header_field_complete;
llhttp_cb on_header_value_complete;
};

/* Initialize the parser with specific type and user settings.
Expand Down
Loading