Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

HTTP Parser - fatal - parse request string #1574

Closed
sjanecki opened this issue Aug 23, 2011 · 5 comments
Closed

HTTP Parser - fatal - parse request string #1574

sjanecki opened this issue Aug 23, 2011 · 5 comments
Labels

Comments

@sjanecki
Copy link

I have request message in a string.
I want to convert it to request Object (to easly retreive headers and so on)
Snippet:

     var http = require('http');

     var str =   "GET /test HTTP/1.1\r\n" +
                "User-Agent: curl/7.18.0 (i486-pc-linux-gnu)\r\n" +
                "Host: 0.0.0.0=5000\r\n" +
                "Accept: */*\r\n" +
                "\r\n";

    var buffer = new Buffer(str);

    var parser = http.parsers.alloc();
    parser.reinitialize('request');
    parser.onIncoming = function(res){
         console.log(res.headers);
    };
    parser.execute(buffer, 0, buffer.length);

Node is failing on assertion:

assertion "!current_buffer" failed: file "../src/node_http_parser.cc", line 263, function: static v8::Handlev8::Value node:
:Parser::Execute(const v8::Arguments&)
Aborted (core dumped)

How I can do this? I want to parse http request string.

@koichik
Copy link

koichik commented Aug 23, 2011

It works with v0.4.11 and v0.5.4 (Ubuntu).

$ node -v
v0.4.11
$ node 1574.js
{ 'user-agent': 'curl/7.18.0 (i486-pc-linux-gnu)',
  host: '0.0.0.0=5000',
  accept: '*/*' }

What version of node are you running?

@sjanecki
Copy link
Author

i;m running the same version 0.4.11 but in context of server.create...
i've checked version i've attached in previous post and it worked.

I will post better example in minutes

@sjanecki
Copy link
Author

in context of server.create it doesnt work:

check this, please:

https://gist.github.com/1165112

@koichik
Copy link

koichik commented Aug 23, 2011

Because http-parser is still processing a current request.
You can use new http-parser within process.nextTick():

var server = http.createServer(function(request, response){
  process.nextTick(function() {
    var str =   "GET /test HTTP/1.1\r\n" +
            "User-Agent: curl/7.18.0 (i486-pc-linux-gnu)\r\n" +
            "Host: 0.0.0.0=5000\r\n" +
            "Accept: */*\r\n" +
            "\r\n";

    var buffer = new Buffer(str);

    var parser = http.parsers.alloc();
    parser.reinitialize('request');
    parser.onIncoming = function(res){
        console.log(res.headers);
    };
    parser.execute(buffer, 0, buffer.length);
    parser.finish(); // don't forget
  });
});

@sjanecki
Copy link
Author

thank you this is what i wanted!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants