@@ -19,12 +19,11 @@ enum ParseState {
1919}
2020
2121export class HttpRequestParser implements RequestParser {
22- private readonly _restClientSettings : RestClientSettings = RestClientSettings . Instance ;
23- private static readonly defaultMethod = 'GET' ;
24- private static readonly queryStringLinePrefix = / ^ \s * [ & \? ] / ;
25- private static readonly inputFileSyntax = / ^ < \s + ( .+ ?) \s * $ / ;
22+ private readonly defaultMethod = 'GET' ;
23+ private readonly queryStringLinePrefix = / ^ \s * [ & \? ] / ;
24+ private readonly inputFileSyntax = / ^ < \s + ( .+ ?) \s * $ / ;
2625
27- public constructor ( private requestRawText : string ) {
26+ public constructor ( private readonly requestRawText : string , private readonly settings : RestClientSettings ) {
2827 }
2928
3029 public async parseHttpRequest ( name ?: string ) : Promise < HttpRequest > {
@@ -44,7 +43,7 @@ export class HttpRequestParser implements RequestParser {
4443 case ParseState . URL :
4544 requestLines . push ( currentLine . trim ( ) ) ;
4645 if ( nextLine === undefined
47- || HttpRequestParser . queryStringLinePrefix . test ( nextLine ) ) {
46+ || this . queryStringLinePrefix . test ( nextLine ) ) {
4847 // request with request line only
4948 } else if ( nextLine . trim ( ) ) {
5049 state = ParseState . Header ;
@@ -71,10 +70,10 @@ export class HttpRequestParser implements RequestParser {
7170 }
7271
7372 // parse request line
74- const requestLine = HttpRequestParser . parseRequestLine ( requestLines . join ( EOL ) ) ;
73+ const requestLine = this . parseRequestLine ( requestLines . join ( EOL ) ) ;
7574
7675 // parse headers lines
77- const headers = parseRequestHeaders ( headersLines ) ;
76+ const headers = parseRequestHeaders ( headersLines , this . settings . defaultHeaders , requestLine . url ) ;
7877
7978 // let underlying node.js library recalculate the content length
8079 removeHeader ( headers , 'content-length' ) ;
@@ -93,18 +92,18 @@ export class HttpRequestParser implements RequestParser {
9392 }
9493
9594 // parse body lines
96- const contentTypeHeader = getContentType ( headers ) || getContentType ( this . _restClientSettings . defaultHeaders ) ;
97- let body = await HttpRequestParser . parseRequestBody ( bodyLines , contentTypeHeader ) ;
95+ const contentTypeHeader = getContentType ( headers ) ;
96+ let body = await this . parseBody ( bodyLines , contentTypeHeader ) ;
9897 if ( isGraphQlRequest ) {
99- const variables = await HttpRequestParser . parseRequestBody ( variableLines , contentTypeHeader ) ;
98+ const variables = await this . parseBody ( variableLines , contentTypeHeader ) ;
10099
101100 const graphQlPayload = {
102101 query : body ,
103102 variables : variables ? JSON . parse ( variables . toString ( ) ) : { }
104103 } ;
105104 body = JSON . stringify ( graphQlPayload ) ;
106- } else if ( this . _restClientSettings . formParamEncodingStrategy !== FormParamEncodingStrategy . Never && typeof body === 'string' && MimeUtility . isFormUrlEncoded ( contentTypeHeader ) ) {
107- if ( this . _restClientSettings . formParamEncodingStrategy === FormParamEncodingStrategy . Always ) {
105+ } else if ( this . settings . formParamEncodingStrategy !== FormParamEncodingStrategy . Never && typeof body === 'string' && MimeUtility . isFormUrlEncoded ( contentTypeHeader ) ) {
106+ if ( this . settings . formParamEncodingStrategy === FormParamEncodingStrategy . Always ) {
108107 const stringPairs = body . split ( '&' ) ;
109108 const encodedStringPairs : string [ ] = [ ] ;
110109 for ( const stringPair of stringPairs ) {
@@ -119,7 +118,7 @@ export class HttpRequestParser implements RequestParser {
119118 }
120119
121120 // if Host header provided and url is relative path, change to absolute url
122- const host = getHeader ( headers , 'Host' ) || getHeader ( this . _restClientSettings . defaultHeaders , 'host' ) ;
121+ const host = getHeader ( headers , 'Host' ) ;
123122 if ( host && requestLine . url [ 0 ] === '/' ) {
124123 const [ , port ] = host . toString ( ) . split ( ':' ) ;
125124 const scheme = port === '443' || port === '8443' ? 'https' : 'http' ;
@@ -129,7 +128,7 @@ export class HttpRequestParser implements RequestParser {
129128 return new HttpRequest ( requestLine . method , requestLine . url , headers , body , bodyLines . join ( EOL ) , name ) ;
130129 }
131130
132- private static parseRequestLine ( line : string ) : { method : string , url : string } {
131+ private parseRequestLine ( line : string ) : { method : string , url : string } {
133132 // Request-Line = Method SP Request-URI SP HTTP-Version CRLF
134133 const words = line . split ( ' ' ) . filter ( Boolean ) ;
135134
@@ -151,7 +150,7 @@ export class HttpRequestParser implements RequestParser {
151150 return { method, url } ;
152151 }
153152
154- private static async parseRequestBody ( lines : string [ ] , contentTypeHeader : string | undefined ) : Promise < string | Stream | undefined > {
153+ private async parseBody ( lines : string [ ] , contentTypeHeader : string | undefined ) : Promise < string | Stream | undefined > {
155154 if ( lines . length === 0 ) {
156155 return undefined ;
157156 }
@@ -198,7 +197,7 @@ export class HttpRequestParser implements RequestParser {
198197 }
199198 }
200199
201- private static getLineEnding ( contentTypeHeader : string | undefined ) {
200+ private getLineEnding ( contentTypeHeader : string | undefined ) {
202201 return MimeUtility . isMultiPartFormData ( contentTypeHeader ) ? '\r\n' : EOL ;
203202 }
204203}
0 commit comments