Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

all updated to use http-raw@1.1.0

  • Loading branch information...
commit 91c6f7ac81f03cbde1992178a4ccc2c29c55ce8c 1 parent 08c34cb
@substack authored
Showing with 22 additions and 20 deletions.
  1. +11 −9 lib/insert_headers.js
  2. +11 −11 test/insert.js
View
20 lib/insert_headers.js
@@ -1,12 +1,11 @@
module.exports = function (bufs, headers) {
- if (bufs === undefined) return 0;
var bytesAdded = 0;
var insert = [];
for (var key in headers) {
if (typeof headers[key] === 'string' || headers[key]) {
var b = new Buffer(key + ': ' + headers[key] + '\r\n');
- insert.push([ b, 0, b.length ]);
+ insert.push(b);
bytesAdded += b.length;
}
}
@@ -16,14 +15,17 @@ module.exports = function (bufs, headers) {
var ix = [], jx = [];
for (var i = 0; i < bufs.length; i++) {
- var b = bufs[i];
- var buf = b[0], start = b[1], end = b[2];
-
- for (var j = start; j < end; j++) {
+ var buf = bufs[i];
+ for (var j = 0; j < buf.length; j++) {
+ var cur = typeof buf === 'string'
+ ? buf.charCodeAt(j)
+ : buf[j]
+ ;
+
w[0] = w[1], ix[0] = ix[1], jx[0] = jx[1];
w[1] = w[2], ix[1] = ix[2], jx[1] = jx[2];
w[2] = w[3], ix[2] = ix[3], jx[2] = jx[3];
- w[3] = buf[j], ix[3] = i, jx[3] = j;
+ w[3] = cur, ix[3] = i, jx[3] = j;
if (w[2] === 10 && w[3] === 10
|| (w[0] === 13 && w[1] === 10 && w[2] === 13 && w[3] === 10)) {
@@ -36,8 +38,8 @@ module.exports = function (bufs, headers) {
var jj = jx[2];
}
- if (jj > 0) insert.unshift([ bufs[ii][0], bufs[ii][1], jj ]);
- insert.push([ bufs[ii][0], jj, bufs[ii][2] ]);
+ if (jj > 0) insert.unshift(bufs[ii].slice(0, jj));
+ insert.push(bufs[ii].slice(jj, bufs[ii].length));
insert.unshift(ii, 1);
bufs.splice.apply(bufs, insert);
View
22 test/insert.js
@@ -1,6 +1,6 @@
var test = require('tap').test;
var insertHeaders = require('../lib/insert_headers');
-var splitUp = require('./lib/split_up.js');
+var chunky = require('chunky');
test('insert headers', function (t) {
t.plan(50 * 3);
@@ -12,19 +12,19 @@ test('insert headers', function (t) {
].join('\r\n');
for (var i = 0; i < 50; i++) {
- var bufs = splitUp(msg);
- t.equal(bufs.map(function (b) {
- return String(b[0].slice(b[1],b[2]));
- }).join(''), msg);
-
- var bufs_ = bufs.slice();
+ var bufs = chunky(msg);
+ t.equal(bufs.map(String).join(''), msg);
var n = insertHeaders(bufs, { foo : 'bar', baz : 'quux' });
t.equal(n, 'foo: bar\r\nbaz: quux\r\n'.length);
- t.ok(
- bufs.length === bufs_.length + 2
- || bufs.length === bufs_.length + 3
- );
+ t.equal(bufs.map(String).join(''), [
+ 'POST / HTTP/1.1',
+ 'Host: beep',
+ 'foo: bar',
+ 'baz: quux',
+ '',
+ 'sound=boop'
+ ].join('\r\n'));
}
t.end();
});
Please sign in to comment.
Something went wrong with that request. Please try again.