Skip to content

Instantly share code, notes, and snippets.

@WimJongeneel
Created January 12, 2020 13:15
Show Gist options
  • Save WimJongeneel/44671cce78ba2078565e191a09b5bc7f to your computer and use it in GitHub Desktop.
Save WimJongeneel/44671cce78ba2078565e191a09b5bc7f to your computer and use it in GitHub Desktop.
const parseRequest = (s: string): Request => {
const [firstLine, rest] = divideStringOn(s, '\r\n')
const [method, url, protocol] = firstLine.split(' ', 3)
const [headers, body] = divideStringOn(rest, '\r\n\r\n')
const parsedHeaders = headers.split('\r\n').reduce((map, header) => {
const [key, value] = divideStringOn(header, ': ')
return map.set(key, value)
}, new Map())
return { protocol, method, url, headers: parsedHeaders, body }
}
const divideStringOn = (s: string, search: string) => {
const index = s.indexOf(search)
const first = s.slice(0, index)
const rest = s.slice(index + search.length)
return [first, rest]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment