File tree Expand file tree Collapse file tree 12 files changed +136
-11
lines changed Expand file tree Collapse file tree 12 files changed +136
-11
lines changed Original file line number Diff line number Diff line change 11.vscode /
2+ reports /
Original file line number Diff line number Diff line change 3333 },
3434 "lint" : {
3535 "plugins" : [
36- " jsr:@loat-dev/lint-plugins@0.3.6 /colon_spacing" ,
37- " jsr:@loat-dev/lint-plugins@0.3.6 /imports" ,
38- " jsr:@loat-dev/lint-plugins@0.3.6 /arrays"
36+ " jsr:@loat-dev/lint-plugins@0.3.11 /colon_spacing" ,
37+ " jsr:@loat-dev/lint-plugins@0.3.11 /imports" ,
38+ " jsr:@loat-dev/lint-plugins@0.3.11 /arrays"
3939 ],
4040 "rules" : {
4141 "tags" : [
Original file line number Diff line number Diff line change 11export { InvalidStartLineError } from './invalid_start_line_error.ts'
2+ export { InvalidResponseError } from './invalid_response_error.ts'
Original file line number Diff line number Diff line change 1+ export class InvalidResponseError extends Error {
2+ constructor ( ) {
3+ super ( 'Invalid response. Did not find header end.' ) ;
4+ }
5+ }
Original file line number Diff line number Diff line change 1+ export { parseChunkedBody } from './parse_chunked_body.ts'
Original file line number Diff line number Diff line change 1+ export function parseChunkedBody ( body : string ) : string {
2+ return body
3+ }
Original file line number Diff line number Diff line change 1+ import { assertEquals } from '@std/assert'
2+ import { parseHeaders } from './parse_headers.ts' ;
3+
4+ Deno . test ( 'Tests "parseHeaders"' , async ( test ) => {
5+ await test . step ( {
6+ name : 'Parse valid HTTP headers.' ,
7+ fn ( ) : void {
8+ const responseContent = 'key-1: value1\r\nkey-2: value 2 with space'
9+ assertEquals (
10+ Object . fromEntries ( parseHeaders ( responseContent ) ) ,
11+ {
12+ 'key-1' : 'value1' ,
13+ 'key-2' : 'value 2 with space'
14+ }
15+ )
16+ }
17+ } )
18+
19+ await test . step ( {
20+ name : 'Parse (valid) HTTP headers. With leading/trailing newline.' ,
21+ fn ( ) : void {
22+ const responseContent = '\r\nkey-1: value1\r\nkey-2: value 2 with space\r\n'
23+ assertEquals (
24+ Object . fromEntries ( parseHeaders ( responseContent ) ) ,
25+ {
26+ 'key-1' : 'value1' ,
27+ 'key-2' : 'value 2 with space'
28+ }
29+ )
30+ }
31+ } )
32+
33+ await test . step ( {
34+ name : 'Parse (valid) HTTP headers. With space in key name.' ,
35+ fn ( ) : void {
36+ const responseContent = 'key 1: value 1 with space'
37+ assertEquals (
38+ Object . fromEntries ( parseHeaders ( responseContent ) ) ,
39+ { 'key 1' : 'value 1 with space' }
40+ )
41+ }
42+ } )
43+ } )
Original file line number Diff line number Diff line change 11/**
2- * This function parses the HTTP response, and returns the headers
2+ * This function parses the HTTP response, and returns the headers.
3+ *
4+ * @param response Response string
5+ * @returns The headers as `<key>: <value>` map.
36 */
4- export function parseHeaders ( response : string ) : Record < string , string > {
5- return { }
7+ export function parseHeaders ( response : string ) : Map < string , string > {
8+ const headers = new Map < string , string > ( ) ;
9+ response . trim ( ) . split ( '\r\n' ) . forEach ( ( line ) => {
10+ const [ key , value ] = line . split ( ':' ) ;
11+ headers . set ( key , value . trim ( ) ) ;
12+ } )
13+
14+ return headers ;
615}
Original file line number Diff line number Diff line change 1+ import { parseStartLine } from './start_line/index.ts' ;
12import { parseHeaders } from './headers/index.ts' ;
3+ import { parseChunkedBody } from './body/index.ts' ;
24import { splitResponse } from './split_response.ts' ;
3- import { parseStartLine } from './start_line/index.ts' ;
45
56export function parse ( response : string ) : Response {
67 const splitResult = splitResponse ( response )
78
89 const startLine = parseStartLine ( splitResult . startLine ) ;
910 const headers = parseHeaders ( splitResult . headers ) ;
10- const body = splitResult . body ;
11+ let body = splitResult . body ;
12+
13+ if ( headers . get ( 'Transfer-Encoding' ) === 'chunked' ) {
14+ body = parseChunkedBody ( body ) ;
15+ }
1116
1217 return new Response (
1318 body ,
You can’t perform that action at this time.
0 commit comments