forked from humcycles/node-parsley
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Halliday
committed
Oct 23, 2011
1 parent
98d9417
commit 41cbea4
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
parsley | ||
======= | ||
|
||
HTTP parser written in node with hooks into the raw header and raw body. | ||
|
||
You should only need this module if you're writing an HTTP proxy or something | ||
similarly nefarious. | ||
|
||
example | ||
======= | ||
|
||
raw.js | ||
------ | ||
|
||
````javascript | ||
var parsley = require('parsley'); | ||
var net = require('net'); | ||
|
||
net.createServer(function (stream) { | ||
parsley(stream, function (req) { | ||
var head = []; | ||
req.on('rawHead', function (buf) { | ||
head.push(buf); | ||
}); | ||
|
||
var body = []; | ||
req.on('rawBody', function (buf) { | ||
body.push(buf); | ||
}); | ||
|
||
req.on('end', function () { | ||
console.dir(head.map(String)); | ||
console.dir(body.map(String)); | ||
}); | ||
}); | ||
}).listen(7000); | ||
```` | ||
|
||
```` | ||
substack : node-parsley $ echo beep | curl -sNT. localhost:7000 | ||
^C | ||
substack : node-parsley $ | ||
```` | ||
|
||
```` | ||
substack : node-parsley $ node example/raw.js | ||
[ 'PUT ', | ||
'/ ', | ||
'HTTP/', | ||
'1.1\r\n', | ||
'User-Agent:', | ||
' curl/7.21.3 (x86_64-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18\r\n', | ||
'Host:', | ||
' localhost:7000\r\n', | ||
'Accept:', | ||
' */*\r\n', | ||
'Transfer-Encoding:', | ||
' chunked\r\n', | ||
'Expect:', | ||
' 100-continue\r\n', | ||
'\r\n' ] | ||
[ '5\r\n', 'beep\n', '\r\n', '0\r\n', '\r\n' ] | ||
```` | ||
|
||
methods | ||
======= | ||
|
||
var parsley = require('parsley') | ||
|
||
parsley(stream, cb) | ||
------------------- | ||
|
||
Parse `stream`, calling `cb` with a new `http.IncomingMessage` | ||
object as soon as the first piece of data comes in. | ||
|
||
If the connection is keep-alive, multiple requests may come through `cb` on the | ||
same stream. | ||
|
||
events | ||
====== | ||
|
||
The `http.IncomingMessage` that `cb` gets called with has these events: | ||
|
||
"data", buf | ||
----------- | ||
|
||
Emitted when data comes in not including the extra pieces like transfer-encoding | ||
framing. | ||
|
||
"end" | ||
----- | ||
|
||
Emitted when the request ends. | ||
|
||
"rawHead", buf | ||
-------------- | ||
|
||
Emitted whenever any data comes in before the body. | ||
|
||
"rawBody", buf | ||
-------------- | ||
|
||
Emitted whenever any data comes in after the header but before the request ends | ||
for the case of keep-alive requests. | ||
|
||
"error", err | ||
------------ | ||
|
||
Emitted when the stream fails to parse. | ||
|
||
motivation | ||
========== | ||
|
||
Turns out, it's super hard to get node's http parser to spit out the raw request | ||
buffers only for a particular request. But with parsley, you can listen on | ||
`'rawHead'` and `'rawBody'` events to get the raw buffer stream as it gets | ||
parsed. | ||
|
||
install | ||
======= | ||
|
||
With [npm](http://npmjs.org) do: | ||
|
||
npm install parsley |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var parsley = require('../'); | ||
var net = require('net'); | ||
|
||
net.createServer(function (stream) { | ||
parsley(stream, function (req) { | ||
var head = []; | ||
req.on('rawHead', function (buf) { | ||
head.push(buf); | ||
}); | ||
|
||
var body = []; | ||
req.on('rawBody', function (buf) { | ||
body.push(buf); | ||
}); | ||
|
||
req.on('end', function () { | ||
console.dir(head.map(String)); | ||
console.dir(body.map(String)); | ||
}); | ||
}); | ||
}).listen(7000); |