Permalink
Browse files

http, feat: HttpResopnse support any statusCode.

  • Loading branch information...
xicilion committed Sep 28, 2017
1 parent 91915c0 commit f8e5511b2a358f770e9b56f821a24c081bb80bfb
Showing with 26 additions and 10 deletions.
  1. +15 −10 fibjs/src/http/HttpResponse.cpp
  2. +11 −0 test/http_test.js
@@ -300,8 +300,21 @@ result_t HttpResponse::sendTo(Stream_base* stm, AsyncEvent* ac)
exlib::string statusMessage;
if (m_statusMessage.empty()) {
int32_t pos = shortcut[m_statusCode / 100 - 1] + m_statusCode % 100;
statusMessage.assign(status_lines[pos], status_lines_size[pos]);
int32_t statusCode = m_statusCode;
if (statusCode >= 100 && statusCode < 600) {
int32_t n = statusCode / 100;
if (shortcut[n - 1] + statusCode % 100 < shortcut[n]) {
int32_t pos = shortcut[statusCode / 100 - 1] + statusCode % 100;
statusMessage.assign(status_lines[pos], status_lines_size[pos]);
}
}
if (statusMessage.empty()) {
char buf[16];
sprintf(buf, " %d Unknown", statusCode);
statusMessage = buf;
}
} else {
char buf[16];
@@ -397,14 +410,6 @@ result_t HttpResponse::get_statusCode(int32_t& retVal)
result_t HttpResponse::set_statusCode(int32_t newVal)
{
if (newVal < 100 || newVal >= 600)
newVal = 500;
else {
int32_t n = newVal / 100;
if (shortcut[n - 1] + newVal % 100 >= shortcut[n])
newVal = 500;
}
m_statusCode = newVal;
return 0;
}
View
@@ -822,6 +822,17 @@ describe("http", () => {
assert.equal(ms.read(), 'HTTP/1.1 404 File Not Found\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n');
});
it("unknown statusCode", () => {
var ms = new io.MemoryStream();
var rep = new http.Response();
rep.statusCode = 210;
rep.sendTo(ms);
ms.rewind();
assert.equal(ms.read().toString(), 'HTTP/1.1 210 Unknown\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n');
});
it("statusMessage", () => {
var ms = new io.MemoryStream();

0 comments on commit f8e5511

Please sign in to comment.