Skip to content

handle gzip #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ function bufferToString(buffer) {
* @return {Object} result {headers: {Status: 200, 'content-type': 'text/html'}, body: 'xxxx'}
*/
function parse(buffer) {
var source = bufferToString(buffer);
var source = Buffer.concat(buffer);
// http spec: use `\r\n` as line break, except body
var result = {};
var lines = source.split('\r\n');
var line;

// headers
var headers = {};
while (lines.length) {
line = lines.shift();
if (line) {
line = line.split(':');
headers[line[0]] = line[1];
}
else {
break;
var start = 0;
for (var i=0; i < source.length; i++) {
// check for `\r\n
if (source[i] === 13 && source[i+1] === 10) {
var line = source.slice(start, i).toString();
var c = line.split(': ');
if (c[0] !== '' && c.length === 2) {
headers[c[0]] = c[1];
}
start = i+2;
}
}

result.headers = headers;

// body
// if body has `\r\n`, join it back to the body string
result.body = lines.join('\r\n');
result.body = source.slice(start, source.length);

return result;
}
Expand Down Expand Up @@ -98,10 +99,11 @@ exports = module.exports = function (options) {
var end = options.end || function (e) {
var statusCode = e.statusCode;
var headers = e.headers;
var body = e.body || '';
var body = e.body || Buffer.from('', 'utf8');

res.writeHead(statusCode, headers);
res.end(body);
res.write(body, 'binary');
res.end();
};

var info = url.parse(req.url);
Expand Down