nodejs / http-parser

http request/response parser for c
MIT License
6.32k stars 1.53k forks source link

Examples #529

Closed Onepamopa closed 3 years ago

Onepamopa commented 3 years ago

Hello,

Can someone tell me how exactly can I check if a certain header exists, and get its value ? I can't quite figure out how this parser works...

indutny commented 3 years ago

Hello @Onepamopa !

First of all, (as a shameless plug) I'd recommend moving to https://github.com/nodejs/llhttp instead of http_parser. As stated in readme this project is not actively maintained.

You can pickup the latest release, and init the parser with the settings object that has following callbacks:

The idea is to track what header fields you get and record their respective values. on_headers_complete marks the end of the http request and the start of the request's data. You can check the recorded header fields at this point to see if certain header is present.

Hope this helps, Fedor.

Onepamopa commented 3 years ago

That's what I don't get - the parser uses user-defined callbacks, which should contain what, exactly? If I want to extract a value of a header - I should do that where, and ... how ?

indutny commented 3 years ago

That's what I don't get - the parser uses user-defined callbacks, which should contain what, exactly?

The code for handling appropriate events.

If I want to extract a value of a header - I should do that where, and ... how ?

In the on_header_field and on_header_value callbacks. You can just do strncmp on the field, and then take the value from the next on_header_value call.

Onepamopa commented 3 years ago

Are there any examples of that ?

indutny commented 3 years ago

Have you seen a short setup in llhttp's README?

Onepamopa commented 3 years ago

@indutny I have seen the short setup, I haven't seen example callback functions extracting url, http method, headers, etc.