-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TEX-537] Refactor HTTP request and response representations (#554)
This PR does a bunch of initial refactoring and cleaning up of the representations of HTTP request and response objects throughout the codebase, ahead of making some larger changes to support gRPC-web request comparisons. The changes in this PR are mostly code structure in nature rather than functional. There is one functionality change in the decision on how to compare two HTTP request bodies, which I've called out in comments.
- Loading branch information
1 parent
ac7f701
commit dc0743a
Showing
10 changed files
with
892 additions
and
712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import brotli from "brotli"; | ||
import zlib from "zlib"; | ||
import { ParsedMediaType as ParsedContentType } from "content-type"; | ||
|
||
/** | ||
* Headers of a request or response. | ||
*/ | ||
export interface HttpHeaders { | ||
[headerName: string]: string | string[] | undefined; | ||
} | ||
|
||
/** | ||
* The common fields of a HTTP request. | ||
*/ | ||
export interface HttpRequest { | ||
host?: string; | ||
method: string; | ||
path: string; | ||
headers: HttpHeaders; | ||
body: Buffer; | ||
} | ||
|
||
export interface HttpRequestWithHost extends HttpRequest { | ||
host: string; | ||
} | ||
|
||
/** | ||
* The common fields of a HTTP response. | ||
*/ | ||
export interface HttpResponse { | ||
status: { | ||
code: number; | ||
}; | ||
headers: HttpHeaders; | ||
body: Buffer; | ||
} | ||
|
||
export function getHeaderAsString( | ||
headers: HttpHeaders, | ||
headerName: string, | ||
): string { | ||
const rawValue = headers[headerName]; | ||
if (rawValue === undefined) { | ||
return ""; | ||
} else if (typeof rawValue === "string") { | ||
return rawValue; | ||
} else { | ||
return rawValue[0]; | ||
} | ||
} | ||
|
||
export function getHttpRequestContentType(request: HttpRequest): string { | ||
return ( | ||
getHeaderAsString(request.headers, "content-type") || | ||
"application/octet-stream" | ||
); | ||
} | ||
|
||
export function getHttpRequestBodyDecoded(request: HttpRequest): Buffer { | ||
// Process the content-encoding before looking at the content-type. | ||
const contentEncoding = getHeaderAsString( | ||
request.headers, | ||
"content-encoding", | ||
); | ||
switch (contentEncoding) { | ||
case "": | ||
return request.body; | ||
case "br": | ||
return Buffer.from(brotli.decompress(request.body)); | ||
case "gzip": | ||
return zlib.gunzipSync(request.body); | ||
default: | ||
throw Error(`Unhandled content-encoding value "${contentEncoding}"`); | ||
} | ||
} | ||
|
||
export function decodeHttpRequestBodyToString( | ||
request: HttpRequest, | ||
contentType: ParsedContentType, | ||
): string { | ||
const encoding = contentType.parameters.charset as BufferEncoding | undefined; | ||
return getHttpRequestBodyDecoded(request).toString(encoding || "utf-8"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.