Permalink
Browse files

Handle remote urls properly.

This includes respecting port, and watching for errors
from response and request.
  • Loading branch information...
1 parent 12bd555 commit 320a990922171089c643aa30cb7c1ba32a6f958e @nikhilm committed Dec 14, 2010
Showing with 11 additions and 2 deletions.
  1. +11 −2 server.js
View
@@ -79,12 +79,19 @@ function handleUrl(request, response) {
var size = 0;
var error = false;
var parts = url.parse(remote);
- var client = http.createClient(80, parts.host);
+ var client = http.createClient(parseInt(parts.port)||80, parts.hostname);
+ client.on('error', function() {
+ getOut(500, response);
+ });
client.request('GET', parts.pathname || '/', {
- 'host' : parts.host
+ 'host' : parts.hostname
, 'user-agent' : 'Mugshot FaceDetector'
})
.on('response', function(remoteresponse) {
+ if( remoteresponse.statusCode != 200 ) {
+ getOut(500, "Remote server failed with status code " + remoteresponse.statusCode, response);
+ return;
+ }
remoteresponse.on('data', function(data) {
if( size > MAX_SIZE ) {
error = true;
@@ -120,6 +127,8 @@ function handleUrl(request, response) {
else
getOut(500, response);
});;
+ }).on('error', function() {
+ getOut(500, response);
}).end();
});
});

0 comments on commit 320a990

Please sign in to comment.