Created
October 8, 2017 19:04
-
-
Save burdiuz/d5a6b3cbd1ef96e47f2cfced94a832a8 to your computer and use it in GitHub Desktop.
Parse HTTP request headers
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
export const parseHTTPRequest = (data) => { | |
const headers = {}; | |
const rgx = /^([^\:\r\n]+)\:\s*([^\r\n]+)$/gm; | |
const [requestHeader, method, path, httpVersion] = data.match(/^(\w+)\s+([^\r\n]+)\s+HTTP\/([\d\.]+)/); | |
const request = { requestHeader, method, path, httpVersion, headers }; | |
let header; | |
while ((header = rgx.exec(data))) { | |
const [, name, value] = header; | |
headers[name] = value; | |
} | |
return request; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment