Permalink
Browse files

http, break: deprecated property crossDomain, add method enableCrossO…

…rigin.
  • Loading branch information...
xicilion committed Sep 26, 2017
1 parent ca05229 commit d5d1196ac9557d61cc0a60128ed5ff2211fb94fb
@@ -40,8 +40,7 @@ class HttpHandler : public HttpHandler_base {
public:
// HttpHandler_base
virtual result_t get_crossDomain(bool& retVal);
virtual result_t set_crossDomain(bool newVal);
virtual result_t enableCrossOrigin(exlib::string allowHeaders);
virtual result_t get_forceGZIP(bool& retVal);
virtual result_t set_forceGZIP(bool newVal);
virtual result_t get_maxHeadersCount(int32_t& retVal);
@@ -59,6 +58,7 @@ class HttpHandler : public HttpHandler_base {
obj_ptr<Handler_base> m_err_hdlrs[3];
bool m_crossDomain;
exlib::string m_allowHeaders;
bool m_forceGZIP;
int32_t m_maxHeadersCount;
int32_t m_maxBodySize;
@@ -37,8 +37,7 @@ class HttpServer : public HttpServer_base {
public:
// HttpServer_base
virtual result_t onerror(v8::Local<v8::Object> hdlrs);
virtual result_t get_crossDomain(bool& retVal);
virtual result_t set_crossDomain(bool newVal);
virtual result_t enableCrossOrigin(exlib::string allowHeaders);
virtual result_t get_forceGZIP(bool& retVal);
virtual result_t set_forceGZIP(bool newVal);
virtual result_t get_maxHeadersCount(int32_t& retVal);
@@ -37,8 +37,7 @@ class HttpsServer : public HttpsServer_base {
public:
// HttpServer_base
virtual result_t onerror(v8::Local<v8::Object> hdlrs);
virtual result_t get_crossDomain(bool& retVal);
virtual result_t set_crossDomain(bool newVal);
virtual result_t enableCrossOrigin(exlib::string allowHeaders);
virtual result_t get_forceGZIP(bool& retVal);
virtual result_t set_forceGZIP(bool newVal);
virtual result_t get_maxHeadersCount(int32_t& retVal);
@@ -26,8 +26,7 @@ class HttpHandler_base : public HandlerEx_base {
public:
// HttpHandler_base
static result_t _new(Handler_base* hdlr, obj_ptr<HttpHandler_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
virtual result_t get_crossDomain(bool& retVal) = 0;
virtual result_t set_crossDomain(bool newVal) = 0;
virtual result_t enableCrossOrigin(exlib::string allowHeaders) = 0;
virtual result_t get_forceGZIP(bool& retVal) = 0;
virtual result_t set_forceGZIP(bool newVal) = 0;
virtual result_t get_maxHeadersCount(int32_t& retVal) = 0;
@@ -43,8 +42,7 @@ class HttpHandler_base : public HandlerEx_base {
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_crossDomain(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_crossDomain(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_enableCrossOrigin(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_forceGZIP(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_forceGZIP(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_get_maxHeadersCount(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
@@ -61,8 +59,11 @@ class HttpHandler_base : public HandlerEx_base {
namespace fibjs {
inline ClassInfo& HttpHandler_base::class_info()
{
static ClassData::ClassMethod s_method[] = {
{ "enableCrossOrigin", s_enableCrossOrigin, false }
};
static ClassData::ClassProperty s_property[] = {
{ "crossDomain", s_get_crossDomain, s_set_crossDomain, false },
{ "forceGZIP", s_get_forceGZIP, s_set_forceGZIP, false },
{ "maxHeadersCount", s_get_maxHeadersCount, s_set_maxHeadersCount, false },
{ "maxBodySize", s_get_maxBodySize, s_set_maxBodySize, false },
@@ -71,7 +72,7 @@ inline ClassInfo& HttpHandler_base::class_info()
static ClassData s_cd = {
"HttpHandler", false, s__new, NULL,
0, NULL, 0, NULL, ARRAYSIZE(s_property), s_property, NULL, NULL,
ARRAYSIZE(s_method), s_method, 0, NULL, ARRAYSIZE(s_property), s_property, NULL, NULL,
&HandlerEx_base::class_info()
};
@@ -101,27 +102,18 @@ void HttpHandler_base::__new(const T& args)
CONSTRUCT_RETURN();
}
inline void HttpHandler_base::s_get_crossDomain(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
inline void HttpHandler_base::s_enableCrossOrigin(const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool vr;
METHOD_INSTANCE(HttpHandler_base);
PROPERTY_ENTER();
METHOD_ENTER();
hr = pInst->get_crossDomain(vr);
METHOD_OVER(1, 0);
METHOD_RETURN();
}
OPT_ARG(exlib::string, 0, "Content-Type");
inline void HttpHandler_base::s_set_crossDomain(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args)
{
METHOD_INSTANCE(HttpHandler_base);
PROPERTY_ENTER();
PROPERTY_VAL(bool);
hr = pInst->set_crossDomain(v0);
hr = pInst->enableCrossOrigin(v0);
PROPERTY_SET_LEAVE();
METHOD_VOID();
}
inline void HttpHandler_base::s_get_forceGZIP(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
@@ -29,8 +29,7 @@ class HttpServer_base : public TcpServer_base {
static result_t _new(int32_t port, Handler_base* hdlr, obj_ptr<HttpServer_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
static result_t _new(exlib::string addr, int32_t port, Handler_base* hdlr, obj_ptr<HttpServer_base>& retVal, v8::Local<v8::Object> This = v8::Local<v8::Object>());
virtual result_t onerror(v8::Local<v8::Object> hdlrs) = 0;
virtual result_t get_crossDomain(bool& retVal) = 0;
virtual result_t set_crossDomain(bool newVal) = 0;
virtual result_t enableCrossOrigin(exlib::string allowHeaders) = 0;
virtual result_t get_forceGZIP(bool& retVal) = 0;
virtual result_t set_forceGZIP(bool newVal) = 0;
virtual result_t get_maxHeadersCount(int32_t& retVal) = 0;
@@ -48,8 +47,7 @@ class HttpServer_base : public TcpServer_base {
public:
static void s__new(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_onerror(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_crossDomain(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_crossDomain(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_enableCrossOrigin(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_get_forceGZIP(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
static void s_set_forceGZIP(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args);
static void s_get_maxHeadersCount(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args);
@@ -69,11 +67,11 @@ namespace fibjs {
inline ClassInfo& HttpServer_base::class_info()
{
static ClassData::ClassMethod s_method[] = {
{ "onerror", s_onerror, false }
{ "onerror", s_onerror, false },
{ "enableCrossOrigin", s_enableCrossOrigin, false }
};
static ClassData::ClassProperty s_property[] = {
{ "crossDomain", s_get_crossDomain, s_set_crossDomain, false },
{ "forceGZIP", s_get_forceGZIP, s_set_forceGZIP, false },
{ "maxHeadersCount", s_get_maxHeadersCount, s_set_maxHeadersCount, false },
{ "maxBodySize", s_get_maxBodySize, s_set_maxBodySize, false },
@@ -136,27 +134,18 @@ inline void HttpServer_base::s_onerror(const v8::FunctionCallbackInfo<v8::Value>
METHOD_VOID();
}
inline void HttpServer_base::s_get_crossDomain(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
inline void HttpServer_base::s_enableCrossOrigin(const v8::FunctionCallbackInfo<v8::Value>& args)
{
bool vr;
METHOD_INSTANCE(HttpServer_base);
PROPERTY_ENTER();
METHOD_ENTER();
hr = pInst->get_crossDomain(vr);
METHOD_OVER(1, 0);
METHOD_RETURN();
}
OPT_ARG(exlib::string, 0, "Content-Type");
inline void HttpServer_base::s_set_crossDomain(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& args)
{
METHOD_INSTANCE(HttpServer_base);
PROPERTY_ENTER();
PROPERTY_VAL(bool);
hr = pInst->enableCrossOrigin(v0);
hr = pInst->set_crossDomain(v0);
PROPERTY_SET_LEAVE();
METHOD_VOID();
}
inline void HttpServer_base::s_get_forceGZIP(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& args)
@@ -132,8 +132,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
Variant origin;
if (pThis->m_req->firstHeader("origin",
origin)
if (pThis->m_req->firstHeader("origin", origin)
!= CALL_RETURN_NULL) {
pThis->m_rep->setHeader("Access-Control-Allow-Credentials", "true");
pThis->m_rep->setHeader("Access-Control-Allow-Origin", origin);
@@ -142,7 +141,8 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
if (!qstricmp(str.c_str(), "options")) {
pThis->m_rep->setHeader("Access-Control-Allow-Methods", "*");
pThis->m_rep->setHeader("Access-Control-Allow-Headers", "CONTENT-TYPE");
pThis->m_rep->setHeader("Access-Control-Allow-Headers",
pThis->m_pThis->m_allowHeaders);
pThis->m_rep->setHeader("Access-Control-Max-Age", "1728000");
return 0;
@@ -199,8 +199,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
if (s == 200) {
pThis->m_rep->hasHeader("Last-Modified", t);
if (!t) {
pThis->m_rep->addHeader("Cache-Control",
"no-cache, no-store");
pThis->m_rep->addHeader("Cache-Control", "no-cache, no-store");
pThis->m_rep->addHeader("Expires", "-1");
}
}
@@ -223,8 +222,7 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
if (len > 128 && len < 1024 * 1024 * 64) {
Variant hdr;
if (pThis->m_req->firstHeader("Accept-Encoding",
hdr)
if (pThis->m_req->firstHeader("Accept-Encoding", hdr)
!= CALL_RETURN_NULL) {
exlib::string str = hdr.string();
int32_t type = 0;
@@ -235,31 +233,26 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
type = 2;
if (type != 0) {
if (pThis->m_rep->firstHeader("Content-Type",
hdr)
if (pThis->m_rep->firstHeader("Content-Type", hdr)
!= CALL_RETURN_NULL) {
str = hdr.string();
if (qstricmp(str.c_str(), "text/", 5)
&& qstricmp(str.c_str(),
"application/x-javascript")
&& qstricmp(str.c_str(),
"application/json"))
&& qstricmp(str.c_str(), "application/x-javascript")
&& qstricmp(str.c_str(), "application/json"))
type = 0;
} else
type = 0;
}
if (type != 0) {
if (pThis->m_rep->firstHeader("Content-Encoding",
hdr)
if (pThis->m_rep->firstHeader("Content-Encoding", hdr)
!= CALL_RETURN_NULL)
type = 0;
}
if (type != 0) {
pThis->m_rep->addHeader("Content-Encoding",
type == 1 ? "gzip" : "deflate");
pThis->m_rep->addHeader("Content-Encoding", type == 1 ? "gzip" : "deflate");
pThis->m_rep->get_body(pThis->m_body);
pThis->m_body->rewind();
@@ -269,11 +262,9 @@ result_t HttpHandler::invoke(object_base* v, obj_ptr<Handler_base>& retVal,
pThis->set(zip);
if (type == 1)
return zlib_base::gzipTo(pThis->m_body,
pThis->m_zip, pThis);
return zlib_base::gzipTo(pThis->m_body, pThis->m_zip, pThis);
else
return zlib_base::deflateTo(pThis->m_body,
pThis->m_zip, -1, pThis);
return zlib_base::deflateTo(pThis->m_body, pThis->m_zip, -1, pThis);
}
}
}
@@ -396,15 +387,10 @@ result_t HttpHandler::onerror(v8::Local<v8::Object> hdlrs)
return 0;
}
result_t HttpHandler::get_crossDomain(bool& retVal)
result_t HttpHandler::enableCrossOrigin(exlib::string allowHeaders)
{
retVal = m_crossDomain;
return 0;
}
result_t HttpHandler::set_crossDomain(bool newVal)
{
m_crossDomain = newVal;
m_crossDomain = true;
m_allowHeaders = allowHeaders;
return 0;
}
@@ -91,14 +91,9 @@ result_t HttpServer::onerror(v8::Local<v8::Object> hdlrs)
return m_hdlr->onerror(hdlrs);
}
result_t HttpServer::get_crossDomain(bool& retVal)
result_t HttpServer::enableCrossOrigin(exlib::string allowHeaders)
{
return m_hdlr->get_crossDomain(retVal);
}
result_t HttpServer::set_crossDomain(bool newVal)
{
return m_hdlr->set_crossDomain(newVal);
return m_hdlr->enableCrossOrigin(allowHeaders);
}
result_t HttpServer::get_forceGZIP(bool& retVal)
@@ -140,14 +140,9 @@ result_t HttpsServer::onerror(v8::Local<v8::Object> hdlrs)
return m_hdlr->onerror(hdlrs);
}
result_t HttpsServer::get_crossDomain(bool& retVal)
result_t HttpsServer::enableCrossOrigin(exlib::string allowHeaders)
{
return m_hdlr->get_crossDomain(retVal);
}
result_t HttpsServer::set_crossDomain(bool newVal)
{
return m_hdlr->set_crossDomain(newVal);
return m_hdlr->enableCrossOrigin(allowHeaders);
}
result_t HttpsServer::get_forceGZIP(bool& retVal)
@@ -17,8 +17,10 @@ interface HttpHandler : HandlerEx
*/
HttpHandler(Handler hdlr);
/*! @brief 查询和设置是否允许跨域请求,缺省为 false */
Boolean crossDomain;
/*! @brief 允许跨域请求
@param allowHeaders 指定接受的 http 头字段
*/
enableCrossOrigin(String allowHeaders = "Content-Type");
/*! @brief 查询和设置是否允强制使用 gzip 压缩输出,缺省为 false */
Boolean forceGZIP;
View
@@ -46,8 +46,10 @@ interface HttpServer : TcpServer
*/
onerror(Object hdlrs);
/*! @brief 查询和设置是否允许跨域请求,缺省为 false */
Boolean crossDomain;
/*! @brief 允许跨域请求
@param allowHeaders 指定接受的 http 头字段
*/
enableCrossOrigin(String allowHeaders = "Content-Type");
/*! @brief 查询和设置是否允强制使用 gzip 压缩输出,缺省为 false */
Boolean forceGZIP;

0 comments on commit d5d1196

Please sign in to comment.