Merge pull request #168 from an-tao/dev

Add some necessary headers in preflight responses for CORS
This commit is contained in:
An Tao 2019-06-01 07:35:21 +08:00 committed by GitHub
commit 06e4ed43b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 7 deletions

View File

@ -221,9 +221,10 @@ int main()
// return;
accb();
});
app().registerPostHandlingAdvice(
[](const drogon::HttpRequestPtr &, const drogon::HttpResponsePtr &) {
app().registerPostHandlingAdvice([](const drogon::HttpRequestPtr &,
const drogon::HttpResponsePtr &resp) {
LOG_DEBUG << "postHandling1";
resp->addHeader("Access-Control-Allow-Origin", "*");
});
app().registerPreRoutingAdvice([](const drogon::HttpRequestPtr &req) {
LOG_DEBUG << "preRouting observer";

View File

@ -174,8 +174,9 @@ class HttpRequest
/// Get the Json object of the request
/**
* The content type of the request must be 'application/json', otherwise
* the method returns an empty object.
* The content type of the request must be 'application/json', and the query
* string (the part after the question mark in the URI) must be empty,
* otherwise the method returns an empty shared_ptr object.
*/
virtual const std::shared_ptr<Json::Value> jsonObject() const = 0;
const std::shared_ptr<Json::Value> getJsonObject() const

View File

@ -514,6 +514,10 @@ void HttpControllersRouter::doPreHandlingAdvices(
}
methods.resize(methods.length() - 1);
resp->addHeader("ALLOW", methods);
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->addHeader("Access-Control-Allow-Methods", methods);
resp->addHeader("Access-Control-Allow-Headers",
"x-requested-with,content-type");
callback(resp);
return;
}

View File

@ -30,7 +30,7 @@ void HttpRequestImpl::parseParameters() const
std::transform(type.begin(), type.end(), type.begin(), tolower);
if (_method == Get ||
(_method == Post &&
(type == "" ||
(type.empty() ||
type.find("application/x-www-form-urlencoded") != std::string::npos)))
{
std::string::size_type pos = 0;

View File

@ -379,6 +379,10 @@ void HttpSimpleControllersRouter::doPreHandlingAdvices(
}
methods.resize(methods.length() - 1);
resp->addHeader("ALLOW", methods);
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->addHeader("Access-Control-Allow-Methods", methods);
resp->addHeader("Access-Control-Allow-Headers",
"x-requested-with,content-type");
callback(resp);
return;
}