Skip to content

Commit fc5bbaa

Browse files
committed
feat(client): Added filter hook "pre_args" to initialize request arguments, before processing it
1 parent 8606263 commit fc5bbaa

File tree

6 files changed

+166
-1
lines changed

6 files changed

+166
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- fix(client): Preventing from memory leak when `h2` is selected by ALPN.
88
- fix(client): Allow string-type content-length.
99
- fix(client): Renamed `EVersion.AUTO` to `EVersion.ALPN`.
10+
- feat(client): Added filter hook `pre_args` to initialize request arguments, before processing it.
1011
- build(deps): removed all runtime dependencies.
1112

1213
## v1.0.5

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
'api',
1313
'hook',
1414
'docs',
15+
'test',
1516
'lint',
1617
'branch',
1718
'deps',

initialize-test-data.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ rm -rf test
88

99
./generate-ca.sh
1010
./generate-cert.sh a.local.org
11+
./generate-cert.sh d.local.org
1112
./generate-cert.sh b.local.org c.local.org
1213
./generate-cert.sh x.local.org local.org '*.local.org'
1314

1415
echo "Initialization completed."
15-
echo "Please add a.local.org, b.local.org, c.local.org, x.local.org, local.org,"
16+
echo "Please add a.local.org, b.local.org, c.local.org, d.local.org, x.local.org, local.org,"
1617
echo "dddd.local.org, g.local.org to /etc/hosts or C:/Windows/System32/drivers/etc/hosts"

src/examples/003-https-2-quick-start.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const server = $NativeHttps.createSecureServer({
2929
ca: $FS.readFileSync('./test/ca/cert.pem'),
3030
cert: $FS.readFileSync('./test/certs/b.local.org/cert.pem'),
3131
key: $FS.readFileSync('./test/certs/b.local.org/key.pem'),
32+
allowHTTP1: true,
3233
}, function(req, resp) {
3334

3435
if (req.method === 'GET') {
@@ -143,6 +144,7 @@ server.listen(SERVER_PORT, SERVER_ADDR, SERVER_BACKLOG, (): void => {
143144
},
144145
method: 'POST',
145146
localAddress: '127.0.0.22',
147+
version: $Http.EVersion.ALPN,
146148
ca: $FS.readFileSync('./test/ca/cert.pem'),
147149
data: 'Auto-detected HTTP/2',
148150
connectionOptions: {
@@ -170,6 +172,7 @@ server.listen(SERVER_PORT, SERVER_ADDR, SERVER_BACKLOG, (): void => {
170172
},
171173
method: 'POST',
172174
localAddress: '127.0.0.22',
175+
version: $Http.EVersion.ALPN,
173176
ca: $FS.readFileSync('./test/ca/cert.pem'),
174177
data: 'Auto-detected HTTP/2',
175178
connectionOptions: {
@@ -196,6 +199,7 @@ server.listen(SERVER_PORT, SERVER_ADDR, SERVER_BACKLOG, (): void => {
196199
pathname: '/',
197200
},
198201
method: 'GET',
202+
version: $Http.EVersion.ALPN,
199203
localAddress: '127.0.0.22',
200204
ca: $FS.readFileSync('./test/ca/cert.pem'),
201205
connectionOptions: {
@@ -214,6 +218,7 @@ server.listen(SERVER_PORT, SERVER_ADDR, SERVER_BACKLOG, (): void => {
214218
pathname: '/',
215219
},
216220
method: 'GET',
221+
version: $Http.EVersion.ALPN,
217222
localAddress: '127.0.0.22',
218223
ca: $FS.readFileSync('./test/ca/cert.pem'),
219224
connectionOptions: {
@@ -232,6 +237,159 @@ server.listen(SERVER_PORT, SERVER_ADDR, SERVER_BACKLOG, (): void => {
232237
console.error(e);
233238
}
234239

240+
req = await hcli.request({
241+
url: {
242+
protocol: 'https',
243+
hostname: SERVER_HOST,
244+
port: SERVER_PORT,
245+
pathname: '/',
246+
},
247+
method: 'GET',
248+
// version: $Http.EVersion.ALPN, // should use HTTP/1.1 by default here
249+
localAddress: '127.0.0.22',
250+
ca: $FS.readFileSync('./test/ca/cert.pem'),
251+
connectionOptions: {
252+
remoteHost: SERVER_ADDR
253+
}
254+
});
255+
256+
try {
257+
258+
console.log(`HTTP/${req.protocol === $Http.EProtocol.HTTP_1 ? '1.1' : '2'} ${req.statusCode}`);
259+
console.log((await req.getBuffer()).toString());
260+
}
261+
catch (e) {
262+
263+
console.error(e);
264+
}
265+
266+
hcli.filters.register({ // now force to use ALPN
267+
name: 'pre_request',
268+
key: 'force_alpn',
269+
callback: (req) => {
270+
req.version = $Http.EVersion.ALPN;
271+
return req;
272+
}
273+
});
274+
275+
req = await hcli.request({
276+
url: {
277+
protocol: 'https',
278+
hostname: SERVER_HOST,
279+
port: SERVER_PORT,
280+
pathname: '/',
281+
},
282+
method: 'GET',
283+
localAddress: '127.0.0.22',
284+
ca: $FS.readFileSync('./test/ca/cert.pem'),
285+
connectionOptions: {
286+
remoteHost: SERVER_ADDR
287+
}
288+
});
289+
290+
try {
291+
292+
console.log(`HTTP/${req.protocol === $Http.EProtocol.HTTP_1 ? '1.1' : '2'} ${req.statusCode}`);
293+
console.log((await req.getBuffer()).toString());
294+
295+
}
296+
catch (e) {
297+
298+
console.error(e);
299+
}
300+
301+
req = await hcli.request({
302+
url: {
303+
protocol: 'https',
304+
hostname: SERVER_HOST,
305+
port: SERVER_PORT,
306+
pathname: '/',
307+
},
308+
method: 'GET',
309+
localAddress: '127.0.0.22',
310+
version: $Http.EVersion.HTTP_1_1, // will be ignored
311+
ca: $FS.readFileSync('./test/ca/cert.pem'),
312+
connectionOptions: {
313+
remoteHost: SERVER_ADDR
314+
}
315+
});
316+
317+
try {
318+
319+
console.log(`HTTP/${req.protocol === $Http.EProtocol.HTTP_1 ? '1.1' : '2'} ${req.statusCode}`);
320+
console.log((await req.getBuffer()).toString());
321+
322+
}
323+
catch (e) {
324+
325+
console.error(e);
326+
}
327+
328+
hcli.filters.unregister('pre_request', 'force_alpn');
329+
330+
hcli.filters.register({ // now make ALPN as default
331+
name: 'pre_args',
332+
key: 'default_version',
333+
callback: (req) => {
334+
req.version ??= $Http.EVersion.ALPN;
335+
return req;
336+
}
337+
});
338+
339+
req = await hcli.request({
340+
url: {
341+
protocol: 'https',
342+
hostname: SERVER_HOST,
343+
port: SERVER_PORT,
344+
pathname: '/',
345+
},
346+
method: 'GET',
347+
localAddress: '127.0.0.22',
348+
ca: $FS.readFileSync('./test/ca/cert.pem'),
349+
// version: $Http.EVersion.ALPN, // will use ALPN by default
350+
connectionOptions: {
351+
remoteHost: SERVER_ADDR
352+
}
353+
});
354+
355+
try {
356+
357+
console.log(`HTTP/${req.protocol === $Http.EProtocol.HTTP_1 ? '1.1' : '2'} ${req.statusCode}`);
358+
console.log((await req.getBuffer()).toString());
359+
360+
}
361+
catch (e) {
362+
363+
console.error(e);
364+
}
365+
366+
req = await hcli.request({
367+
url: {
368+
protocol: 'https',
369+
hostname: SERVER_HOST,
370+
port: SERVER_PORT,
371+
pathname: '/',
372+
},
373+
method: 'GET',
374+
localAddress: '127.0.0.22',
375+
version: $Http.EVersion.HTTP_1_1, // will use 1.1
376+
ca: $FS.readFileSync('./test/ca/cert.pem'),
377+
connectionOptions: {
378+
remoteHost: SERVER_ADDR
379+
}
380+
});
381+
382+
try {
383+
384+
console.log(`HTTP/${req.protocol === $Http.EProtocol.HTTP_1 ? '1.1' : '2'} ${req.statusCode}`);
385+
console.log((await req.getBuffer()).toString());
386+
387+
}
388+
catch (e) {
389+
390+
console.error(e);
391+
}
392+
235393
hcli.close();
236394

237395
server.close();

src/lib/Client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class HttpClient implements C.IClient {
6969
optsIn.data = '';
7070
}
7171

72+
optsIn = await this.filters.filter('pre_args', optsIn);
73+
7274
if (typeof optsIn.url === 'string') {
7375

7476
const theURL = $url.parse(optsIn.url, true);

src/lib/Common/Client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import type * as Filters from '../Filters';
2121
export interface IFilters {
2222

2323
['pre_request']: (opts: Req.IRequestOptions) => Req.IRequestOptions;
24+
25+
['pre_args']: (opts: Req.IRequestOptionsInput) => Req.IRequestOptionsInput;
2426
}
2527

2628
export interface IKeyValueCache {

0 commit comments

Comments
 (0)