Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Created October 8, 2017 19:04
Show Gist options
  • Save burdiuz/d5a6b3cbd1ef96e47f2cfced94a832a8 to your computer and use it in GitHub Desktop.
Save burdiuz/d5a6b3cbd1ef96e47f2cfced94a832a8 to your computer and use it in GitHub Desktop.
Parse HTTP request headers
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