Skip to content

Commit

Permalink
Fix CORS for new HTTP PATCH method (#477)
Browse files Browse the repository at this point in the history
* Fix CORS for new HTTP PATCH method, also fix HttpRequestImpl::appendToBuffer, to be able to send PATCH requests

* Fix simple_example_test to work with updated CORS
  • Loading branch information
itgenie98 authored Jun 15, 2020
1 parent fda6a44 commit 2607f35
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/simple_example_test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void doTest(const HttpClientPtr &client,
// LOG_DEBUG << resp->getBody();
auto allow = resp->getHeader("allow");
if (resp->statusCode() == k200OK &&
allow == "GET,HEAD,POST,PUT,DELETE,OPTIONS")
allow == "GET,HEAD,POST,PUT,DELETE,OPTIONS,PATCH")
{
outputGood(req, isHttps);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/HttpAppFrameworkImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(
{
auto resp = HttpResponse::newHttpResponse();
resp->setContentTypeCode(ContentType::CT_TEXT_PLAIN);
resp->addHeader("ALLOW", "GET,HEAD,POST,PUT,DELETE,OPTIONS");
resp->addHeader("ALLOW", "GET,HEAD,POST,PUT,DELETE,OPTIONS,PATCH");
resp->setExpiredTime(0);
callback(resp);
return;
Expand Down
4 changes: 4 additions & 0 deletions lib/src/HttpControllersRouter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,10 @@ void HttpControllersRouter::doPreHandlingAdvices(
{
methods.append("DELETE,");
}
if (routerItem.binders_[Patch] && routerItem.binders_[Patch]->isCORS_)
{
methods.append("PATCH,");
}
methods.resize(methods.length() - 1);
resp->addHeader("ALLOW", methods);
auto &origin = req->getHeader("Origin");
Expand Down
3 changes: 3 additions & 0 deletions lib/src/HttpRequestImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ void HttpRequestImpl::appendToBuffer(trantor::MsgBuffer *output) const
case Options:
output->append("OPTIONS ");
break;
case Patch:
output->append("PATCH ");
break;
default:
return;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/HttpSimpleControllersRouter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ void HttpSimpleControllersRouter::doPreHandlingAdvices(
{
methods.append("DELETE,");
}
if (routerItem.binders_[Patch] && routerItem.binders_[Patch]->isCORS_)
{
methods.append("PATCH,");
}
methods.resize(methods.length() - 1);
resp->addHeader("ALLOW", methods);

Expand Down
4 changes: 4 additions & 0 deletions lib/src/WebsocketControllersRouter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ void WebsocketControllersRouter::doControllerHandler(
{
methods.append("DELETE,");
}
if (routerItem.binders_[Patch] && routerItem.binders_[Patch]->isCORS_)
{
methods.append("PATCH,");
}
methods.resize(methods.length() - 1);
resp->addHeader("ALLOW", methods);

Expand Down

0 comments on commit 2607f35

Please sign in to comment.