@@ -7,6 +7,7 @@ import { HttpHandler, HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
7
7
import { setConnectionTimeout } from "./set-connection-timeout" ;
8
8
import { setSocketTimeout } from "./set-socket-timeout" ;
9
9
import { writeRequestBody } from "./write-request-body" ;
10
+ import { getTransformedHeaders } from "./get-transformed-headers" ;
10
11
11
12
export class NodeHttpHandler implements HttpHandler {
12
13
private readonly httpAgent : http . Agent ;
@@ -25,33 +26,9 @@ export class NodeHttpHandler implements HttpHandler {
25
26
26
27
handle (
27
28
request : HttpRequest ,
28
- options : HttpOptions
29
+ { abortSignal } : HttpOptions
29
30
) : Promise < { response : HttpResponse } > {
30
- // determine which http(s) client to use
31
- const isSSL = request . protocol === "https:" ;
32
- const httpClient = isSSL ? https : http ;
33
-
34
- let path = request . path ;
35
- if ( request . query ) {
36
- const queryString = buildQueryString ( request . query ) ;
37
- if ( queryString ) {
38
- path += `?${ queryString } ` ;
39
- }
40
- }
41
-
42
- const nodeHttpsOptions : https . RequestOptions = {
43
- headers : request . headers ,
44
- host : request . hostname ,
45
- method : request . method ,
46
- path : path ,
47
- port : request . port ,
48
- agent : isSSL ? this . httpsAgent : this . httpAgent
49
- } ;
50
-
51
31
return new Promise ( ( resolve , reject ) => {
52
- const abortSignal = options && options . abortSignal ;
53
- const { connectionTimeout, socketTimeout } = this . httpOptions ;
54
-
55
32
// if the request was already aborted, prevent doing extra work
56
33
if ( abortSignal && abortSignal . aborted ) {
57
34
const abortError = new Error ( "Request aborted" ) ;
@@ -60,21 +37,23 @@ export class NodeHttpHandler implements HttpHandler {
60
37
return ;
61
38
}
62
39
63
- // create the http request
64
- const req = ( httpClient as typeof http ) . request ( nodeHttpsOptions , res => {
65
- const httpHeaders = res . headers ;
66
- const transformedHeaders : HeaderBag = { } ;
67
-
68
- for ( let name of Object . keys ( httpHeaders ) ) {
69
- let headerValues = < string > httpHeaders [ name ] ;
70
- transformedHeaders [ name ] = Array . isArray ( headerValues )
71
- ? headerValues . join ( "," )
72
- : headerValues ;
73
- }
40
+ // determine which http(s) client to use
41
+ const isSSL = request . protocol === "https:" ;
42
+ const queryString = buildQueryString ( request . query || { } ) ;
43
+ const nodeHttpsOptions : https . RequestOptions = {
44
+ headers : request . headers ,
45
+ host : request . hostname ,
46
+ method : request . method ,
47
+ path : queryString ? ` ${ request . path } ? ${ queryString } ` : request . path ,
48
+ port : request . port ,
49
+ agent : isSSL ? this . httpsAgent : this . httpAgent
50
+ } ;
74
51
52
+ // create the http request
53
+ const req = ( isSSL ? https : http ) . request ( nodeHttpsOptions , res => {
75
54
const httpResponse = new HttpResponse ( {
76
55
statusCode : res . statusCode || - 1 ,
77
- headers : transformedHeaders ,
56
+ headers : getTransformedHeaders ( res . headers ) ,
78
57
body : res
79
58
} ) ;
80
59
resolve ( { response : httpResponse } ) ;
@@ -83,8 +62,8 @@ export class NodeHttpHandler implements HttpHandler {
83
62
req . on ( "error" , reject ) ;
84
63
85
64
// wire-up any timeout logic
86
- setConnectionTimeout ( req , reject , connectionTimeout ) ;
87
- setSocketTimeout ( req , reject , socketTimeout ) ;
65
+ setConnectionTimeout ( req , reject , this . httpOptions . connectionTimeout ) ;
66
+ setSocketTimeout ( req , reject , this . httpOptions . socketTimeout ) ;
88
67
89
68
// wire-up abort logic
90
69
if ( abortSignal ) {
0 commit comments