Skip to content
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

Fix client hangs #173

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 18 additions & 5 deletions client/chrome/content/http/HttpParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,24 @@ HttpParser.prototype.parseResponseCode = function(response) {
HttpParser.prototype.readFully = function(socket) {
var response = "";
var buf = null;

while ((buf = socket.readString()) != null) {
response += buf;
dump("Read: " + buf + "\n");
var length = null;

while ((buf = socket.readString(length)) != null) {
response += buff;

// Try to limit reads according to content-length header
if (length != null) length -= buff.length;
else {
var headers_end = response.indexOf('\r\n\r\n');
if (headers_end != -1) {
var match = /(^|\r\n)content-length:\s+(\d+)\r\n/i
.exec(response.substring(0, headers_end+2));
if (match) {
length = (headers_end + 4 + parseInt(match[2])) - response.length;
}
}
}
}

return response;
};
};
27 changes: 14 additions & 13 deletions client/chrome/content/sockets/ConvergenceClientSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ ConvergenceClientSocket.prototype.writeBytes = function(buffer, length) {
return NSPR.lib.PR_Write(this.fd, buffer, length);
};

ConvergenceClientSocket.prototype.readString = function() {
var buffer = new NSPR.lib.buffer(4096);
ConvergenceClientSocket.prototype.readString = function(length) {
if (length === null) length = 4095;
else if (length <= 0) return null;
var buffer = new NSPR.lib.buffer(length + 1);
var read;

while (((read = NSPR.lib.PR_Read(this.fd, buffer, 4095)) == -1) &&
while (((read = NSPR.lib.PR_Read(this.fd, buffer, length)) == -1) &&
(NSPR.lib.PR_GetError() == NSPR.lib.PR_WOULD_BLOCK_ERROR))
{
dump("polling on read...\n");
Expand All @@ -151,21 +153,20 @@ ConvergenceClientSocket.prototype.readString = function() {
};

ConvergenceClientSocket.prototype.readFully = function(length) {
var buffer = new NSPR.lib.buffer(length);
var read;
var response = "";
var buffer;
var n = length;

while (((read = NSPR.lib.PR_Read(this.fd, buffer, length)) == -1) &&
(NSPR.lib.PR_GetError() == NSPR.lib.PR_WOULD_BLOCK_ERROR))
{
if (!this.waitForInput(-1))
return null;
while ((buffer = this.readString(n)) != null) {
response += buffer;
n -= buffer.length;
}

if (read != length) {
if (response.length != length) {
throw "Assertion error on read fully (" + read + ", " + length + ")!";
}

return buffer;
return response;
};

ConvergenceClientSocket.prototype.close = function() {
Expand All @@ -186,4 +187,4 @@ ConvergenceClientSocket.prototype.waitForInput = function(timeoutMillis) {
}

return true;
};
};
6 changes: 3 additions & 3 deletions client/chrome/content/sockets/ConvergenceNotarySocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ ConvergenceNotarySocket.prototype.writeBytes = function(buffer, length) {
return this.connection.writeBytes(buffer, length);
};

ConvergenceNotarySocket.prototype.readString = function() {
return this.connection.readString();
ConvergenceNotarySocket.prototype.readString = function(length) {
return this.connection.readString(length);
};

ConvergenceNotarySocket.prototype.readFully = function(length) {
Expand All @@ -31,4 +31,4 @@ ConvergenceNotarySocket.prototype.readFully = function(length) {

ConvergenceNotarySocket.prototype.close = function() {
return this.connection.close();
};
};
10 changes: 5 additions & 5 deletions server/convergence/NotaryResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ def signResponse(self, response):
return json.dumps(response)

def sendResponse(self, code, recordRows):
self.request.setHeader("Content-Type", "application/json")
self.request.setResponseCode(code)

fingerprintList = []

if recordRows is not None:
Expand All @@ -52,9 +49,12 @@ def sendResponse(self, code, recordRows):
'timestamp' : timestamp }

fingerprintList.append(fingerprint)
result = self.signResponse({'fingerprintList' : fingerprintList})

result = {'fingerprintList' : fingerprintList}
self.request.setHeader('Content-Type', 'application/json')
self.request.setHeader('Content-Length', str(len(result)))
self.request.setResponseCode(code)

self.request.write(self.signResponse(result))
self.request.write(result)
self.request.finish()