AlexanderMac / http-z

Parse/build HTTP message to/from model
MIT License
17 stars 6 forks source link

Request parsing error when using relative URL (no hostname) #17

Closed savageautomate closed 5 years ago

savageautomate commented 5 years ago

I am attempting to use this library to parse a received HTTP request on a server socket. In this use case the hostname is not included in the raw request string and only a relative URL path is included.

Example: GET /features?p1=v1 HTTP/1.1

The following example code will generate a parsing error:

const httpZ = require('http-z');
let requestMsg = [
    'GET /features?p1=v1 HTTP/1.1',
    'Host: example.com',
    'Accept: */*',
    'Accept-Encoding: gzip,deflate',
    'Accept-Language: en-US;q=0.6, en;q=0.4',
    '',
    ''
].join('\n');

let requestObj = httpZ.parseRequest(requestMsg);
console.error(JSON.stringify(requestObj, null, 2));

Error:

[ERROR]  Error: Method SP Request-URI SP HTTP-Version CRLF. Data: GET /features?p1=v1 HTTP/1.1
AlexanderMac commented 5 years ago

Url in the request start line must be an absolute URL, because I use it to determine protocol (http or https).

savageautomate commented 5 years ago

Ok, so the library can't be used as part of a HTTP server implementation then, only a HTTP client.

AlexanderMac commented 5 years ago

The library works only with an absolute URL, because RFC doesn't describe how https request should look like. If you want to use it on the server side, configure your request module (or what you use) to generate http-message with absolute URL.