Skip to content
Browse files

Changed to use req.end and res.end instead of req.close and res.close…

…; fixed bug with error handling for attempt to read a directory in serve_static_file.
  • Loading branch information...
1 parent c94791a commit 9e7151c0057d483c76618b3a7ad7dadc6dd9b0c0 @gjritter committed
Showing with 43 additions and 33 deletions.
  1. +15 −5 lib/nerve.js
  2. +28 −28 test/test_nerve.js
View
20 lib/nerve.js
@@ -24,7 +24,7 @@
}
this.writeHeader(response_data.status_code || 200, headers);
this.write(response_data.content || response_data, 'binary');
- this.close();
+ this.end();
};
function match_request(matcher, req) {
@@ -88,12 +88,22 @@
function serve_static_file(pathname, res) {
path.exists(pathname, function (exists) {
if (exists) {
- fs.readFile(pathname, 'binary', function (err, data) {
- if(err) {
+ fs.stat(pathname, function (err, stats) {
+ if (err) {
res.respond({content: '<html><head><title>Exception</title></head><body><h1>Exception</h1><pre>' + sys.inspect(err) + '</pre></body></html>', status_code: 501});
- sys.p(err);
} else {
- res.respond({content: data, headers: {'Content-Type': mime.mime_type(pathname)}});
+ if (stats.isFile()) {
+ fs.readFile(pathname, 'binary', function (err, data) {
+ if(err) {
+ res.respond({content: '<html><head><title>Exception</title></head><body><h1>Exception</h1><pre>' + sys.inspect(err) + '</pre></body></html>', status_code: 501});
+ sys.p(err);
+ } else {
+ res.respond({content: data, headers: {'Content-Type': mime.mime_type(pathname)}});
+ }
+ });
+ } else {
+ res.respond({content: '<html><head><title>Error</title></head><body><h1>Not a file</h1></body></html>', status_code: 501});
+ }
}
});
} else {
View
56 test/test_nerve.js
@@ -78,7 +78,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'Hello, World!');
});
- req.close();
+ req.end();
}());
(function test_unmatched_request_string() {
@@ -88,7 +88,7 @@
req.addListener( 'response', function (res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_get_to_get_matcher() {
@@ -100,7 +100,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'GET matcher');
});
- req.close();
+ req.end();
}());
(function test_post_to_get_matcher() {
@@ -110,7 +110,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_put_to_get_matcher() {
@@ -120,7 +120,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_delete_to_get_matcher() {
@@ -130,7 +130,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_get_to_get_string_matcher() {
@@ -142,7 +142,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'GET string matcher');
});
- req.close();
+ req.end();
}());
(function test_post_to_get_string_matcher() {
@@ -152,7 +152,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_put_to_get_string_matcher() {
@@ -162,7 +162,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_delete_to_get_string_matcher() {
@@ -172,7 +172,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_nonmatching_get_to_get_string_matcher() {
@@ -182,7 +182,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_post_to_post_string_matcher() {
@@ -194,7 +194,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'POST string matcher');
});
- req.close();
+ req.end();
}());
(function test_get_to_post_string_matcher() {
@@ -204,7 +204,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_put_to_post_string_matcher() {
@@ -214,7 +214,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_delete_to_post_string_matcher() {
@@ -224,7 +224,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_nonmatching_post_to_post_string_matcher() {
@@ -234,7 +234,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_put_to_put_string_matcher() {
@@ -246,7 +246,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'PUT string matcher');
});
- req.close();
+ req.end();
}());
(function test_get_to_put_string_matcher() {
@@ -256,7 +256,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_post_to_put_string_matcher() {
@@ -266,7 +266,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_delete_to_put_string_matcher() {
@@ -276,7 +276,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_nonmatching_put_to_put_string_matcher() {
@@ -286,7 +286,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_del_to_del_string_matcher() {
@@ -298,7 +298,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'DEL string matcher');
});
- req.close();
+ req.end();
}());
(function test_get_to_del_string_matcher() {
@@ -308,7 +308,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_post_to_del_string_matcher() {
@@ -318,7 +318,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_put_to_del_string_matcher() {
@@ -328,7 +328,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_nonmatching_del_to_del_string_matcher() {
@@ -338,7 +338,7 @@
req.addListener( 'response', function(res) {
assert_not_found(res);
});
- req.close();
+ req.end();
}());
(function test_static_file() {
@@ -350,7 +350,7 @@
test.assertEquals('text/html', res.headers['content-type']);
assert_response(res, 'hello world\n');
});
- req.close();
+ req.end();
}());
(function test_read_dir() {
@@ -361,7 +361,7 @@
test.assertEquals(501, res.statusCode);
receive_callback();
});
- req.close();
+ req.end();
}());
// assert that all callbacks were called within the alloted time and exit

1 comment on commit 9e7151c

@gjritter
Owner

Thanks FSX for pointing out the change from close -> end!

Please sign in to comment.
Something went wrong with that request. Please try again.