Permalink
Browse files

http, feat: support statusCode, statusMessage, writeHeaders.

  • Loading branch information...
xicilion committed Sep 27, 2017
1 parent 731c1fd commit 1429a5f7df8a5d195bc19723a3fb75980eec6336
@@ -75,16 +75,21 @@ class HttpResponse : public HttpResponse_base {
public:
// HttpResponse_base
virtual result_t get_status(int32_t& retVal);
virtual result_t set_status(int32_t newVal);
virtual result_t get_statusCode(int32_t& retVal);
virtual result_t set_statusCode(int32_t newVal);
virtual result_t get_statusMessage(exlib::string& retVal);
virtual result_t set_statusMessage(exlib::string newVal);
virtual result_t writeHead(int32_t statusCode, exlib::string statusMessage, v8::Local<v8::Object> headers);
virtual result_t writeHead(int32_t statusCode, v8::Local<v8::Object> headers);
virtual result_t get_cookies(obj_ptr<List_base>& retVal);
virtual result_t addCookie(HttpCookie_base* cookie);
virtual result_t redirect(exlib::string url);
virtual result_t sendHeader(Stream_base* stm, AsyncEvent* ac);
public:
obj_ptr<HttpMessage> m_message;
int32_t m_status;
int32_t m_statusCode;
exlib::string m_statusMessage;
obj_ptr<List_base> m_cookies;
};
@@ -28,8 +28,12 @@ class HttpResponse_base : public HttpMessage_base {
public:
// HttpResponse_base
static result_t _new(obj_ptr<HttpResponse_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
virtual result_t get_status(int32_t& retVal) = 0;
virtual result_t set_status(int32_t newVal) = 0;
virtual result_t get_statusCode(int32_t& retVal) = 0;
virtual result_t set_statusCode(int32_t newVal) = 0;
virtual result_t get_statusMessage(exlib::string& retVal) = 0;
virtual result_t set_statusMessage(exlib::string newVal) = 0;
virtual result_t writeHead(int32_t statusCode, exlib::string statusMessage, v8::Local<v8::Object> headers) = 0;
virtual result_t writeHead(int32_t statusCode, v8::Local<v8::Object> headers) = 0;
virtual result_t get_cookies(obj_ptr<List_base>& retVal) = 0;
virtual result_t addCookie(HttpCookie_base* cookie) = 0;
virtual result_t redirect(exlib::string url) = 0;
@@ -41,8 +45,11 @@ class HttpResponse_base : public HttpMessage_base {
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_status(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_status(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_get_statusCode(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_statusCode(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_get_statusMessage(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_statusMessage(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_writeHead(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_cookies(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_addCookie(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_redirect(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -61,14 +68,16 @@ namespace fibjs {
inline ClassInfo& HttpResponse_base::class_info()
{
static ClassData::ClassMethod s_method[] = {
{ "writeHead", s_writeHead, false },
{ "addCookie", s_addCookie, false },
{ "redirect", s_redirect, false },
{ "sendHeader", s_sendHeader, false },
{ "sendHeaderSync", s_sendHeader, false }
};
static ClassData::ClassProperty s_property[] = {
{ "status", s_get_status, s_set_status, false },
{ "statusCode", s_get_statusCode, s_set_statusCode, false },
{ "statusMessage", s_get_statusMessage, s_set_statusMessage, false },
{ "cookies", s_get_cookies, block_set, false }
};
@@ -102,29 +111,75 @@ void HttpResponse_base::__new(const T& args)
CONSTRUCT_RETURN();
}
inline void HttpResponse_base::s_get_status(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
inline void HttpResponse_base::s_get_statusCode(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
int32_t vr;
METHOD_INSTANCE(HttpResponse_base);
PROPERTY_ENTER();
hr = pInst->get_status(vr);
hr = pInst->get_statusCode(vr);
METHOD_RETURN();
}
inline void HttpResponse_base::s_set_status(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args)
inline void HttpResponse_base::s_set_statusCode(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args)
{
METHOD_INSTANCE(HttpResponse_base);
PROPERTY_ENTER();
PROPERTY_VAL(int32_t);
hr = pInst->set_status(v0);
hr = pInst->set_statusCode(v0);
PROPERTY_SET_LEAVE();
}
inline void HttpResponse_base::s_get_statusMessage(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
exlib::string vr;
METHOD_INSTANCE(HttpResponse_base);
PROPERTY_ENTER();
hr = pInst->get_statusMessage(vr);
METHOD_RETURN();
}
inline void HttpResponse_base::s_set_statusMessage(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args)
{
METHOD_INSTANCE(HttpResponse_base);
PROPERTY_ENTER();
PROPERTY_VAL(exlib::string);
hr = pInst->set_statusMessage(v0);
PROPERTY_SET_LEAVE();
}
inline void HttpResponse_base::s_writeHead(const v8::FunctionCallbackInfo<v8::Value>& args)
{
METHOD_INSTANCE(HttpResponse_base);
METHOD_ENTER();
METHOD_OVER(3, 2);
ARG(int32_t, 0);
ARG(exlib::string, 1);
OPT_ARG(v8::Local<v8::Object>, 2, v8::Object::New(isolate));
hr = pInst->writeHead(v0, v1, v2);
METHOD_OVER(2, 1);
ARG(int32_t, 0);
OPT_ARG(v8::Local<v8::Object>, 1, v8::Object::New(isolate));
hr = pInst->writeHead(v0, v1);
METHOD_VOID();
}
inline void HttpResponse_base::s_get_cookies(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
{
obj_ptr<List_base> vr;
@@ -457,7 +457,7 @@ result_t HttpClient::request(exlib::string method, exlib::string url, SeekableSt
obj_ptr<Url> u = new Url();
obj_ptr<UrlObject_base> u1;
hr = pThis->m_retVal->get_status(status);
hr = pThis->m_retVal->get_statusCode(status);
if (hr < 0)
return hr;
@@ -180,7 +180,7 @@ result_t HttpFileHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
asyncInvoke* pThis = (asyncInvoke*)pState;
if (qstrchr(pThis->m_url.c_str(), '%')) {
pThis->m_rep->set_status(400);
pThis->m_rep->set_statusCode(400);
return pThis->done(CALL_RETURN_NULL);
}
@@ -331,7 +331,7 @@ result_t HttpFileHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
diff = d.diff(d1);
if (diff > -1000 && diff < 1000) {
pThis->m_rep->set_status(304);
pThis->m_rep->set_statusCode(304);
return pThis->done(CALL_RETURN_NULL);
}
}
@@ -367,7 +367,7 @@ result_t HttpFileHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
}
}
m_rep->set_status(404);
m_rep->set_statusCode(404);
return done(CALL_RETURN_NULL);
}
@@ -167,7 +167,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
int32_t s;
int32_t err_idx = -1;
pThis->m_rep->get_status(s);
pThis->m_rep->get_statusCode(s);
if (s == 400) {
err_idx = 0;
pThis->m_pThis->m_stats->inc(HTTP_ERROR_400);
@@ -195,7 +195,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
d.now();
pThis->m_pThis->m_stats->add(HTTP_TOTAL_TIME, (int32_t)d.diff(pThis->m_d));
pThis->m_rep->get_status(s);
pThis->m_rep->get_statusCode(s);
if (s == 200) {
pThis->m_rep->hasHeader("Last-Modified", t);
if (!t) {
@@ -310,7 +310,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
m_req->set_lastError(err);
errorLog("HttpHandler: " + err);
m_rep->set_status(500);
m_rep->set_statusCode(500);
return 0;
}
@@ -320,7 +320,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
m_pThis->m_stats->inc(HTTP_PENDDING);
m_rep->set_keepAlive(false);
m_rep->set_status(400);
m_rep->set_statusCode(400);
set(check_error);
m_d.now();
return 0;
@@ -220,7 +220,7 @@ result_t HttpResponse::clear()
m_message->clear();
m_cookies.Release();
m_status = 200;
m_statusCode = 200;
return 0;
}
@@ -296,11 +296,16 @@ result_t HttpResponse::sendTo(Stream_base* stm, AsyncEvent* ac)
m_cookies.Release();
}
int32_t pos = shortcut[m_status / 100 - 1] + m_status % 100;
exlib::string strCommand;
exlib::string statusMessage = m_statusMessage;
if (statusMessage.empty()) {
int32_t pos = shortcut[m_statusCode / 100 - 1] + m_statusCode % 100;
statusMessage.assign(status_lines[pos], status_lines_size[pos]);
}
get_protocol(strCommand);
strCommand.append(status_lines[pos], status_lines_size[pos]);
strCommand.append(statusMessage);
return m_message->sendTo(stm, strCommand, ac);
}
@@ -341,7 +346,7 @@ result_t HttpResponse::readFrom(Stream_base* stm, AsyncEvent* ac)
&& qisdigit(pThis->m_strLine[12])))
return CHECK_ERROR(Runtime::setError("HttpResponse: bad protocol: " + pThis->m_strLine));
pThis->m_pThis->set_status(atoi(pThis->m_strLine.c_str() + 8));
pThis->m_pThis->set_statusCode(atoi(pThis->m_strLine.c_str() + 8));
pThis->m_strLine.resize(8);
hr = pThis->m_pThis->set_protocol(pThis->m_strLine);
@@ -378,13 +383,13 @@ result_t HttpResponse::get_response(obj_ptr<Message_base>& retVal)
return CHECK_ERROR(CALL_E_INVALID_CALL);
}
result_t HttpResponse::get_status(int32_t& retVal)
result_t HttpResponse::get_statusCode(int32_t& retVal)
{
retVal = m_status;
retVal = m_statusCode;
return 0;
}
result_t HttpResponse::set_status(int32_t newVal)
result_t HttpResponse::set_statusCode(int32_t newVal)
{
if (newVal < 100 || newVal >= 600)
newVal = 500;
@@ -394,7 +399,34 @@ result_t HttpResponse::set_status(int32_t newVal)
newVal = 500;
}
m_status = newVal;
m_statusCode = newVal;
return 0;
}
result_t HttpResponse::get_statusMessage(exlib::string& retVal)
{
retVal = m_statusMessage;
return 0;
}
result_t HttpResponse::set_statusMessage(exlib::string newVal)
{
m_statusMessage = newVal;
return 0;
}
result_t HttpResponse::writeHead(int32_t statusCode, exlib::string statusMessage, v8::Local<v8::Object> headers)
{
set_statusCode(statusCode);
set_statusMessage(statusMessage);
addHeader(headers);
return 0;
}
result_t HttpResponse::writeHead(int32_t statusCode, v8::Local<v8::Object> headers)
{
set_statusCode(statusCode);
addHeader(headers);
return 0;
}
@@ -446,7 +478,7 @@ result_t HttpResponse::addCookie(HttpCookie_base* cookie)
result_t HttpResponse::redirect(exlib::string url)
{
m_status = 302;
m_statusCode = 302;
setHeader("Location", url);
return 0;
}
@@ -478,7 +510,7 @@ result_t HttpResponse::sendHeader(Stream_base* stm, AsyncEvent* ac)
m_cookies.Release();
}
int32_t pos = shortcut[m_status / 100 - 1] + m_status % 100;
int32_t pos = shortcut[m_statusCode / 100 - 1] + m_statusCode % 100;
exlib::string strCommand;
get_protocol(strCommand);
@@ -174,7 +174,7 @@ result_t WebSocket_base::_new(exlib::string url, exlib::string protocol, exlib::
result_t hr;
int32_t status;
pThis->m_httprep->get_status(status);
pThis->m_httprep->get_statusCode(status);
if (status != 101) {
pThis->m_this->endConnect(1002, "server error.");
return CHECK_ERROR(Runtime::setError("websocket: server error."));
@@ -141,7 +141,7 @@ result_t WebSocketHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
6, (const char*)output, 20, out);
pThis->m_httprep->set_status(101);
pThis->m_httprep->set_statusCode(101);
pThis->m_httprep->addHeader("Sec-WebSocket-Accept", out);
pThis->m_httprep->addHeader("Upgrade", "websocket");
pThis->m_httprep->set_upgrade(true);
@@ -88,7 +88,7 @@ result_t ws_base::connect(exlib::string url, exlib::string origin,
result_t hr;
int32_t status;
pThis->m_httprep->get_status(status);
pThis->m_httprep->get_statusCode(status);
if (status != 101)
return CHECK_ERROR(Runtime::setError("websocket: server error."));
View
@@ -6,7 +6,23 @@ interface HttpResponse : HttpMessage
HttpResponse();
/*! @brief 查询和设置响应消息的返回状态 */
Integer status;
Integer statusCode;
/*! @brief 查询和设置响应消息的返回消息 */
String statusMessage;
/*! @brief 设置响应消息的返回状态,返回消息,并添加响应头
@param statusCode 指定响应消息的返回状态
@param statusMessage 指定响应消息的返回消息
@param headers 指定响应消息添加的响应头
*/
writeHead(Integer statusCode, String statusMessage, Object headers = {});
/*! @brief 设置响应消息的返回状态,返回消息,并添加响应头
@param statusCode 指定响应消息的返回状态
@param headers 指定响应消息添加的响应头
*/
writeHead(Integer statusCode, Object headers = {});
/*! @brief 返回当前消息的 HttpCookie 对象列表 */
readonly List cookies;
Oops, something went wrong.

0 comments on commit 1429a5f

Please sign in to comment.