From f89496563b77a66d3e4455a00ba441e209a27a88 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Mon, 21 Dec 2020 02:46:01 +0200 Subject: [PATCH] Update README news. --- README.md | 7 ++- changelog/1.2.5.md | 132 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 19d20764..f6a9a45a 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,11 @@ **News** -- The new `1.2.0` version is now released. See [changelog](changelog/1.2.0.md) for details. -- Check out the new oatpp ORM - read more [here](https://oatpp.io/docs/components/orm/#declare-dbclient). +- Hey, meet the new oatpp version `1.2.5`. +It's now in the master, and soon will receive its release tag! +See [changelog](changelog/1.2.5.md) for details. +*please wait a release tag before updating your CIs*. +- Check out the new oatpp ORM - read more [here](https://oatpp.io/docs/components/orm/). --- diff --git a/changelog/1.2.5.md b/changelog/1.2.5.md index e6b36df0..cbbb7c71 100644 --- a/changelog/1.2.5.md +++ b/changelog/1.2.5.md @@ -1,4 +1,132 @@ -# Oat++ 1.2.0 +# Oat++ 1.2.5 -Feel free to ask questions - [Chat on Gitter](https://gitter.im/oatpp-framework/Lobby) +Previous release - [1.2.0](1.2.0.md) +Feel free to ask questions - [Chat on Gitter!](https://gitter.im/oatpp-framework/Lobby) + +Contents: + +- [Introduce ResponseInterceptor](#introduce-responseinterceptor) +- [Enable Global CORS](#enable-global-cors) +- [Headers Multimap](#headers-multimap) +- [Better Router API](#better-router-api) + +## Introduce ResponseInterceptor + +### Declare Response Interceptor + +```cpp +#include "oatpp/web/server/interceptor/ResponseInterceptor.hpp" + +class MyResponseInterceptor : public ResponseInterceptor { +public: + + std::shared_ptr intercept(const std::shared_ptr& request, + const std::shared_ptr& response) override + { + // TODO modify response or create a new one + return response; // return modified response + // returning nullptr will result in an error + } + +}; +``` + +### Register global request interceptor + +```cpp + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionHandler)([] { + + OATPP_COMPONENT(std::shared_ptr, router); + + auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router); + + /* Add MyResponseInterceptor */ + connectionHandler->addResponseInterceptor(std::make_shared()); + + return connectionHandler; + + }()); +``` + +## Enable Global CORS + +To enable global CORS for all endpoints: + +- Add **Request** Interceptor - `oatpp::web::server::interceptor::AllowOptionsGlobal` to `ConnectionHandler`. +- Add **Response** Interceptor - `atpp::web::server::interceptor::AllowCorsGlobal` to `ConnectionHandler`. + +```cpp +#include "oatpp/web/server/interceptor/AllowCorsGlobal.hpp" + +... + + OATPP_CREATE_COMPONENT(std::shared_ptr, serverConnectionHandler)([] { + + OATPP_COMPONENT(std::shared_ptr, router); // get Router component + + auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router); + + /* Add CORS-enabling interceptors */ + connectionHandler->addRequestInterceptor(std::make_shared()); + connectionHandler->addResponseInterceptor(std::make_shared()); + + return connectionHandler; + + }()); +``` + +## Headers Multimap + +Now headers are stored using [std::multimap](https://en.cppreference.com/w/cpp/container/multimap) and can store multiple entries with the same key. + +Put multiple headers: + +```cpp +auto response = createResponse(Status::CODE_200, ""); +response->putHeader("Set-Cookie", "..."); +response->putHeader("Set-Cookie", "..."); +return response; +``` + +Log all "Set-Cookie" headers: + +```cpp + const auto& map = headers.getAll(); + auto itlow = map.lower_bound("Set-Cookie"); + auto itup = map.upper_bound("Set-Cookie"); + + for(auto it = itlow; it != itup; it ++) { + oatpp::String value = it->second.toString(); + OATPP_LOGD("Header", "Set-Cookie: %s", value->c_str()); + } +``` + +## Better Router API + +Now Router class is a template and can store any value-types and not only `RequestHandler`s. + +Example use-case - check if endpoint should require authorization: + +**Add Routs** + +```cpp +oatpp::web::server::HttpRouterTemplate authEndpoints; + +authEndpoint.route("POST", "login", false); // DO NOT require auth for /login path +authEndpoint.route("POST", "auth", false); // DO NOT require auth for /auth path + +authEndpoint.route("GET", "*", true); // require auth for all GET +authEndpoint.route("POST", "*", true); // require auth for all POST + +authEndpoint.route("OPTIONS", "*", false); // DO NOT require auth for OPTIONS +``` + +**Check Auth** + +```cpp +auto r = authEndpoints.getRoute(request->getStartingLine().method, request->getStartingLine().path); +if(r && r.getEndpoint() == true) { + // check auth +} +```