Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed bugs in writer

  • Loading branch information...
commit 72135e4ad0f08c84f19f6cbf523d6ca042403961 1 parent 8a345d8
@billywhizz authored
Showing with 120 additions and 115 deletions.
  1. +120 −115 lib/fastcgi.js
View
235 lib/fastcgi.js
@@ -135,132 +135,136 @@ function Parser() {
_parser.execute = function(buffer, start, end) {
if(!start) start = 0;
if(!end) end = buffer.length;
- for (var i = start; i < end; i++) {
- switch(_parser.state) {
- case HEADER:
- if(_loc == FCGI_HEADER_LEN - 1) {
- var header = _record.header;
- _header[_loc] = buffer[i];
- var j = 0;
- header.version = _header[j++];
- header.type = _header[j++];
- header.recordId = (_header[j++] << 8) + _header[j++];
- header.contentLength = (_header[j++] << 8) + _header[j++];
- header.paddingLength = _header[j++];
- _record.body = {};
- if(_record.header.contentLength > 0) {
- _parser.onHeader(header);
- _parser.state = BODY;
+ try {
+ for (var i = start; i < end; i++) {
+ switch(_parser.state) {
+ case HEADER:
+ if(_loc == FCGI_HEADER_LEN - 1) {
+ var header = _record.header;
+ _header[_loc] = buffer[i];
+ var j = 0;
+ header.version = _header[j++];
+ header.type = _header[j++];
+ header.recordId = (_header[j++] << 8) + _header[j++];
+ header.contentLength = (_header[j++] << 8) + _header[j++];
+ header.paddingLength = _header[j++];
+ _record.body = {};
+ if(_record.header.contentLength > 0) {
+ _parser.onHeader(header);
+ _parser.state = BODY;
+ }
+ else {
+ _parser.onRecord(_record);
+ }
+ _loc=0;
}
else {
- _parser.onRecord(_record);
+ _header[_loc++] = buffer[i];
}
- _loc=0;
- }
- else {
- _header[_loc++] = buffer[i];
- }
- break;
- case BODY:
- if(_loc == _record.header.contentLength - 1) {
- _body[_loc] = buffer[i];
- switch(_record.header.type) {
- case FCGI_BEGIN:
- var j = 0;
- _record.body = {
- "role": (_body[j++] << 8) + _body[j++],
- "flags": _body[j++]
- }
- break;
- case FCGI_ABORT:
- break;
- case FCGI_END:
- var j = 0;
- _record.body = {
- "status": (_body[j++] << 24) + (_body[j++] << 16) + (_body[j++] << 8) + _body[j++],
- "protocolStatus": _body[j++]
- }
- break;
- case FCGI_PARAMS:
- case FCGI_GET_VALUES:
- case FCGI_GET_VALUES_RESULT:
- var j = 0, name = "", value = "", vlen = 0, nlen = 0;
- _record.body.params = {};
- var rlen = _record.header.contentLength;
- j = rlen;
- while(j < rlen) {
- nlen = _body[j];
- if(nlen >> 7 == 1) {
- nlen = ((_body[j++] << 24) + (_body[j++] << 16) + (_body[j++] << 8) + _body[j++]) & 0x7fffffff;
- }
- else {
- j++;
+ break;
+ case BODY:
+ if(_loc == _record.header.contentLength - 1) {
+ _body[_loc] = buffer[i];
+ switch(_record.header.type) {
+ case FCGI_BEGIN:
+ var j = 0;
+ _record.body = {
+ "role": (_body[j++] << 8) + _body[j++],
+ "flags": _body[j++]
}
- vlen = _body[j];
- if(vlen >> 7 == 1) {
- vlen = ((_body[j++] << 24) + (_body[j++] << 16) + (_body[j++] << 8) + _body[j++]) & 0x7fffffff;
+ break;
+ case FCGI_ABORT:
+ break;
+ case FCGI_END:
+ var j = 0;
+ _record.body = {
+ "status": (_body[j++] << 24) + (_body[j++] << 16) + (_body[j++] << 8) + _body[j++],
+ "protocolStatus": _body[j++]
}
- else {
- j++;
+ break;
+ case FCGI_PARAMS:
+ case FCGI_GET_VALUES:
+ case FCGI_GET_VALUES_RESULT:
+ var j = 0, name = "", value = "", vlen = 0, nlen = 0;
+ _record.body.params = {};
+ var rlen = _record.header.contentLength;
+ while(j < rlen) {
+ nlen = _body[j];
+ if(nlen >> 7 == 1) {
+ nlen = ((_body[j++] << 24) + (_body[j++] << 16) + (_body[j++] << 8) + _body[j++]) & 0x7fffffff;
+ }
+ else {
+ j++;
+ }
+ vlen = _body[j];
+ if(vlen >> 7 == 1) {
+ vlen = ((_body[j++] << 24) + (_body[j++] << 16) + (_body[j++] << 8) + _body[j++]) & 0x7fffffff;
+ }
+ else {
+ j++;
+ }
+ if((j + nlen + vlen) <= _body.length) {
+ var nv = _body.asciiSlice(j, j + nlen + vlen);
+ j += (nlen + vlen);
+ name = nv.substring(0, nlen);
+ value = nv.substring(nlen);
+ _parser.onParam(name, value);
+ _record.body.params[name] = value;
+ }
+ else {
+ _parser.onError(new Error(JSON.stringify(BUFFER_OVERRUN)));
+ j = rlen;
+ }
}
- if((j + nlen + vlen) <= _body.length) {
- var nv = _body.asciiSlice(j, j + nlen + vlen);
- j += (nlen + vlen);
- name = nv.substring(0, nlen);
- value = nv.substring(nlen);
- _parser.onParam(name, value);
- _record.body.params[name] = value;
+ break;
+ case FCGI_STDIN:
+ case FCGI_STDOUT:
+ case FCGI_STDERR:
+ case FCGI_DATA:
+ switch(_parser.encoding) {
+ case "utf8":
+ _record.body = _body.utf8Slice(0, _record.header.contentLength);
+ break;
+ case "ascii":
+ _record.body = _body.asciiSlice(0, _record.header.contentLength);
+ break;
+ default:
+ _parser.onBody(_body, 0, _record.header.contentLength);
+ break;
}
- else {
- _parser.onError(new Error(JSON.stringify(BUFFER_OVERRUN)));
- j = rlen;
+ break;
+ case FCGI_UNKNOWN_TYPE:
+ default:
+ _record.body = {
+ "type": _body[0]
}
- }
- break;
- case FCGI_STDIN:
- case FCGI_STDOUT:
- case FCGI_STDERR:
- case FCGI_DATA:
- switch(_parser.encoding) {
- case "utf8":
- _record.body = _body.utf8Slice(0, _record.header.contentLength);
- break;
- case "ascii":
- _record.body = _body.asciiSlice(0, _record.header.contentLength);
- break;
- default:
- _parser.onBody(_body, 0, _record.header.contentLength);
- break;
- }
- break;
- case FCGI_UNKNOWN_TYPE:
- default:
- _record.body = {
- "type": _body[0]
- }
- break;
- }
- _parser.onRecord(_record);
- _loc = 0;
- if(_record.header.paddingLength > 0) {
- _parser.state = PADDING;
+ break;
+ }
+ _parser.onRecord(_record);
+ _loc = 0;
+ if(_record.header.paddingLength > 0) {
+ _parser.state = PADDING;
+ }
+ else {
+ _parser.state = HEADER;
+ }
}
else {
+ _body[_loc++] = buffer[i];
+ }
+ break;
+ case PADDING:
+ if(_loc++ == _record.header.paddingLength - 1) {
_parser.state = HEADER;
+ _loc = 0;
}
- }
- else {
- _body[_loc++] = buffer[i];
- }
- break;
- case PADDING:
- if(_loc++ == _record.header.paddingLength - 1) {
- _parser.state = HEADER;
- _loc = 0;
- }
- break;
+ break;
+ }
}
}
+ catch(ex) {
+ console.log(ex);
+ }
}
}
@@ -305,7 +309,7 @@ function Writer() {
while(plen--) {
var param = params[index++];
var name = param[0];
- var value = param[1];
+ var value = param[1].toString();
var nlen = name.length;
var vlen = value.length;
if(nlen > 127) {
@@ -328,6 +332,7 @@ function Writer() {
else {
_buffer[i++] = vlen & 0xff;
}
+ //_buffer.write(name + value, i, "ascii");
_buffer.asciiWrite(name + value, i, nlen + vlen);
i += (nlen + vlen);
}
@@ -384,14 +389,14 @@ exports.constants = constants;
exports.getParamLength = function(params) {
var size = 0;
params.forEach(function(param) {
- size += (param[0].length + param[1].length);
+ size += (param[0].length + param[1].toString().length);
if(param[0].length > 127) {
size += 4;
}
else {
size++;
}
- if(param[1].length > 127) {
+ if(param[1].toString().length > 127) {
size += 4;
}
else {
Please sign in to comment.
Something went wrong with that request. Please try again.