AlexanderMac / http-z

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

Reconstructed request headers not congruent #38

Closed FrancYescO closed 4 years ago

FrancYescO commented 4 years ago

Take this example code:

                let plainMessage = [
                'GET /features?p1=v1&p1=gg HTTP/1.1',
                'Accept: */*',
                'Accept-Encoding: gzip,deflate, BBBBB,          deflate',
                'Host: example.com',
                'Accept-Language: en-US;q=0.6,en;q=0.4                 ,AAAA',
                'Host: example.com:8000',
                'Host: example.com:1234',
                '',
                ''
              ].join('\n');

              let messageModel = httpZ.parse(plainMessage);
              console.log(JSON.stringify(messageModel, null, 2));

              plainMessage = httpZ.build(messageModel);
              console.log(plainMessage);

               /* output:
              GET /features?p1=v1&p1=gg HTTP/1.1
              Host: example.com
              Accept: *//*
              Accept-Encoding: gzip, deflate, BBBBB, deflate
              Accept-Language: en-US;q=0.6, en;q=0.4, AAAA
              Host: example.com:8000
              Host: example.com:1234
              *//

Too much parsing is done on the headers, this will cause losing white spaces between headers values when we build it back and also, due to the fact that the (first-found) Host header is stripped out from the headers object array when the request is built back we lose the order (will be forcely moved to the second row)

AlexanderMac commented 4 years ago

Too much parsing is done on the headers, this will cause losing white spaces between headers values when we build it back...

According to HTTP RFC spec leading and trailing spaces inside the header value must be omitted.

... and also, due to the fact that the (first-found) Host header is stripped out from the headers object array when the request is built back we lose the order (will be forcely moved to the second row)

Your message is incorrect. HttpZ uses the fist host header to build model.host field, and removes it from the headers list.

FrancYescO commented 4 years ago

Yes but why host get removed from the headers as it still remain one of it ?

And you are right about the RFC, but as a parser library we should have a way to manage these strange behaviours

AlexanderMac commented 4 years ago

Yes but why host get removed from the headers as it still remain one of it ?

Because it's added in httpZ model.

FrancYescO commented 4 years ago

Do you have a way to put it in the reconstructed header in the same order?

AlexanderMac commented 4 years ago

I don't see any benefit from duplicating host in two places.

FrancYescO commented 4 years ago

The benefit was to have the headers (host) in the right order.