Example #1
0
Connection.prototype._sendBody = function (channel, body, properties) {
  // Handles 3 cases
  // - body is utf8 string
  // - body is instance of Buffer
  // - body is an object and its JSON representation is sent
  // Does not handle the case for streaming bodies.
  if (typeof(body) == 'string') {
    var length = Buffer.byteLength(body);
    //debug('send message length ' + length);

    sendHeader(this, channel, length, properties);

    //debug('header sent');

    var b = new Buffer(7+length+1);
    b.used = 0;
    b[b.used++] = 3; // constants.frameBody
    serializeInt(b, 2, channel);
    serializeInt(b, 4, length);

    b.utf8Write(body, b.used);
    b.used += length;

    b[b.used++] = 206; // constants.frameEnd;
    return this.write(b);

    //debug('body sent: ' + JSON.stringify(b));

  } else if (body instanceof Buffer) {
    sendHeader(this, channel, body.length, properties);

    var b = new Buffer(7);
    b.used = 0;
    b[b.used++] = 3; // constants.frameBody
    serializeInt(b, 2, channel);
    serializeInt(b, 4, body.length);
    this.write(b);

    this.write(body);

    return this.write(String.fromCharCode(206)); // frameEnd

  } else {
    // Optimize for JSON.
    // Use asciiWrite() which is much faster than utf8Write().
    var jsonBody = JSON.stringify(body);
    var length = jsonBody.length;

    debug('sending json: ' + jsonBody);

    properties = mixin({contentType: 'text/json' }, properties);

    sendHeader(this, channel, length, properties);

    var b = new Buffer(7+length+1);
    b.used = 0;

    b[b.used++] = 3; // constants.frameBody
    serializeInt(b, 2, channel);
    serializeInt(b, 4, length);

    b.asciiWrite(jsonBody, b.used);
    b.used += length;

    b[b.used++] = 206; // constants.frameEnd;
    return this.write(b);
  }
};
Example #2
0
  print('.');
  assert.equal(i % 256, c[i]);
}


var asciiString = "hello world";
var offset = 100;
for (var j = 0; j < 500; j++) {

  for (var i = 0; i < asciiString.length; i++) {
    b[i] = asciiString.charCodeAt(i);
  }
  var asciiSlice = b.toString('ascii', 0, asciiString.length);
  assert.equal(asciiString, asciiSlice);

  var written = b.asciiWrite(asciiString, offset);
  assert.equal(asciiString.length, written);
  var asciiSlice = b.toString('ascii', offset, offset+asciiString.length);
  assert.equal(asciiString, asciiSlice);

  var sliceA = b.slice(offset, offset+asciiString.length);
  var sliceB = b.slice(offset, offset+asciiString.length);
  for (var i = 0; i < asciiString.length; i++) {
    assert.equal(sliceA[i], sliceB[i]);
  }

  // TODO utf8 slice tests
}


for (var j = 0; j < 100; j++) {
Example #3
0
// The purpose of this test is not to check HTTP compliance but to test the
// binding. Tests for pathological http messages should be submitted
// upstream to http://github.com/ry/http-parser for inclusion into
// deps/http-parser/test.c

var HTTPParser = process.binding('http_parser').HTTPParser;

var parser = new HTTPParser("request");

var Buffer = require('buffer').Buffer;
var buffer = new Buffer(1024);

var request = "GET /hello HTTP/1.1\r\n\r\n";

buffer.asciiWrite(request, 0, request.length);

var callbacks = 0;

parser.onMessageBegin = function () {
  console.log("message begin");
  callbacks++;
};

parser.onHeadersComplete = function (info) {
  console.log("headers complete: " + JSON.stringify(info));
  assert.equal('GET', info.method);
  assert.equal(1, info.versionMajor);
  assert.equal(1, info.versionMinor);
  callbacks++;
};