Skip to content

Commit

Permalink
fix(executor): bypass check for invalid field-vchar (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
derevnjuk authored Sep 15, 2020
1 parent 90befa0 commit 1c60ba3
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/RequestExecutor/DefaultRequestExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import request from 'request-promise';
import { Response } from 'request';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { parse } from 'url';
import { OutgoingMessage } from 'http';

export class DefaultRequestExecutor implements RequestExecutor {
private readonly agent?: SocksProxyAgent;
Expand All @@ -27,18 +28,23 @@ export class DefaultRequestExecutor implements RequestExecutor {

public async execute(script: Script): Promise<ScriptResult> {
try {
const rawHeaders: Record<string, string | string[]> = {
...script.headers,
...this.options.headers
};
const response: Response = await request({
gzip: true,
url: Helpers.encodeURL(script.url),
strictSSL: false,
agent: this.agent,
body: script.body,
method: script.method?.toUpperCase(),
headers: { ...script.headers, ...this.options.headers },
timeout: this.options.timeout,
resolveWithFullResponse: true,
followRedirect: false
});
}).on('request', (req: OutgoingMessage) =>
this.setHeaders(req, rawHeaders)
);

return new ScriptResult({
status: response.statusCode,
Expand Down Expand Up @@ -72,4 +78,32 @@ export class DefaultRequestExecutor implements RequestExecutor {
});
}
}

private setHeaders(
req: OutgoingMessage,
rawHeaders: Record<string, string | string[]>
): void {
const symbols: symbol[] = Object.getOwnPropertySymbols(req);
const kOutHeaders: symbol = symbols.find(
(item) => item.toString() === 'Symbol(kOutHeaders)'
);

if (!req.headersSent && kOutHeaders && rawHeaders) {
const headers = (req[kOutHeaders] =
req[kOutHeaders] ?? Object.create(null));

this.mergeHeaders(rawHeaders, headers);
}
}

private mergeHeaders(
src: Record<string, string | string[]>,
dest: Record<string, [string, string | string[]]>
) {
Object.entries(src).forEach(([key, value]: [string, string | string[]]) => {
if (key) {
dest[key.toLowerCase()] = [key, value ?? ''];
}
});
}
}

0 comments on commit 1c60ba3

Please sign in to comment.