Merge branch 'master' into minor_optimization

This commit is contained in:
lganzzzo 2019-10-04 00:53:33 +03:00
commit ac0747e4f8
26 changed files with 1501 additions and 617 deletions

View File

@ -5,6 +5,12 @@
add_library(oatpp
oatpp/algorithm/CRC.cpp
oatpp/algorithm/CRC.hpp
oatpp/codegen/api_controller/base_define.hpp
oatpp/codegen/api_controller/base_undef.hpp
oatpp/codegen/api_controller/auth_define.hpp
oatpp/codegen/api_controller/auth_undef.hpp
oatpp/codegen/api_controller/cors_define.hpp
oatpp/codegen/api_controller/cors_undef.hpp
oatpp/codegen/codegen_define_ApiClient_.hpp
oatpp/codegen/codegen_define_ApiController_.hpp
oatpp/codegen/codegen_define_DTO_.hpp

View File

@ -0,0 +1,67 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#define AUTHORIZATION(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_AUTHORIZATION, OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO, TYPE, (__VA_ARGS__))
// AUTHORIZATION MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getHeader(oatpp::web::protocol::http::Header::AUTHORIZATION); \
std::shared_ptr<oatpp::web::server::handler::AuthorizationObject> __param_aosp_val_##NAME = ApiController::handleDefaultAuthorization(__param_str_val_##NAME); \
TYPE NAME = std::static_pointer_cast<TYPE::element_type>(__param_aosp_val_##NAME);
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_2(TYPE, NAME, AUTH_HANDLER) \
auto __param_str_val_##NAME = __request->getHeader(oatpp::web::protocol::http::Header::AUTHORIZATION); \
std::shared_ptr<oatpp::web::server::handler::AuthorizationHandler> __auth_handler_##NAME = AUTH_HANDLER; \
std::shared_ptr<oatpp::web::server::handler::AuthorizationObject> __param_aosp_val_##NAME = __auth_handler_##NAME->handleAuthorization(__param_str_val_##NAME); \
TYPE NAME = std::static_pointer_cast<TYPE::element_type>(__param_aosp_val_##NAME);
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_1(TYPE, NAME) \
auto __param_obj_##NAME = ApiController::getDefaultAuthorizationHandler(); \
if(__param_obj_##NAME) { \
info->headers.add(oatpp::web::protocol::http::Header::AUTHORIZATION, oatpp::String::Class::getType()); \
info->headers[oatpp::web::protocol::http::Header::AUTHORIZATION].description = __param_obj_##NAME ->getScheme(); \
info->authorization = __param_obj_##NAME ->getScheme(); \
} else { \
throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "No authorization handler set up in controller before controller was added to router or swagger-doc."); \
}
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_2(TYPE, NAME, AUTH_HANDLER) \
std::shared_ptr<oatpp::web::server::handler::AuthorizationHandler> __auth_handler_##NAME = AUTH_HANDLER; \
if(__auth_handler_##NAME) { \
info->headers.add(oatpp::web::protocol::http::Header::AUTHORIZATION, oatpp::String::Class::getType()); \
info->headers[oatpp::web::protocol::http::Header::AUTHORIZATION].description = __auth_handler_##NAME->getScheme(); \
info->authorization = __auth_handler_##NAME->getScheme(); \
} else { \
throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "Invalid authorization handler given (or not set up) in AUTHORIZATION(TYPE, NAME, AUTH_HANDLER) before controller was added to router or swagger-doc."); \
}
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)

View File

@ -0,0 +1,35 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#undef AUTHORIZATION
// AUTHORIZATION MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_1
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_2
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO

View File

@ -0,0 +1,522 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#define OATPP_MACRO_API_CONTROLLER_PARAM_MACRO(MACRO, INFO, TYPE, PARAM_LIST) MACRO(TYPE, PARAM_LIST)
#define OATPP_MACRO_API_CONTROLLER_PARAM_INFO(MACRO, INFO, TYPE, PARAM_LIST) INFO(TYPE, PARAM_LIST)
#define OATPP_MACRO_API_CONTROLLER_PARAM_TYPE(MACRO, INFO, TYPE, PARAM_LIST) TYPE
#define OATPP_MACRO_API_CONTROLLER_PARAM_NAME(MACRO, INFO, TYPE, PARAM_LIST) OATPP_MACRO_FIRSTARG PARAM_LIST
#define OATPP_MACRO_API_CONTROLLER_PARAM_TYPE_STR(MACRO, INFO, TYPE, PARAM_LIST) #TYPE
#define OATPP_MACRO_API_CONTROLLER_PARAM_NAME_STR(MACRO, INFO, TYPE, PARAM_LIST) OATPP_MACRO_FIRSTARG_STR PARAM_LIST
#define OATPP_MACRO_API_CONTROLLER_PARAM(MACRO, INFO, TYPE, PARAM_LIST) (MACRO, INFO, TYPE, PARAM_LIST)
#define REQUEST(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_REQUEST, OATPP_MACRO_API_CONTROLLER_REQUEST_INFO, TYPE, (__VA_ARGS__))
#define HEADER(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_HEADER, OATPP_MACRO_API_CONTROLLER_HEADER_INFO, TYPE, (__VA_ARGS__))
#define PATH(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_PATH, OATPP_MACRO_API_CONTROLLER_PATH_INFO, TYPE, (__VA_ARGS__))
#define QUERIES(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_QUERIES, OATPP_MACRO_API_CONTROLLER_QUERIES_INFO, TYPE, (__VA_ARGS__))
#define QUERY(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_QUERY, OATPP_MACRO_API_CONTROLLER_QUERY_INFO, TYPE, (__VA_ARGS__))
#define BODY_STRING(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_BODY_STRING, OATPP_MACRO_API_CONTROLLER_BODY_STRING_INFO, TYPE, (__VA_ARGS__))
#define BODY_DTO(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_BODY_DTO, OATPP_MACRO_API_CONTROLLER_BODY_DTO_INFO, TYPE, (__VA_ARGS__))
//////////////////////////////////////////////////////////////////////////
#define OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(MACRO, TYPE, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_SELECTOR(MACRO, (__VA_ARGS__)) (TYPE, __VA_ARGS__))
//////////////////////////////////////////////////////////////////////////
// REQUEST MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_REQUEST(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST = __request;
#define OATPP_MACRO_API_CONTROLLER_REQUEST_INFO(TYPE, PARAM_LIST)
// HEADER MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_HEADER_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getHeader(#NAME); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing HEADER parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid HEADER parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_HEADER_2(TYPE, NAME, QUALIFIER) \
auto __param_str_val_##NAME = __request->getHeader(QUALIFIER); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Missing HEADER parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid HEADER parameter '") + \
QUALIFIER + \
"'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_HEADER(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_HEADER_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_HEADER_INFO_1(TYPE, NAME) \
info->headers.add(#NAME, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_HEADER_INFO_2(TYPE, NAME, QUALIFIER) \
info->headers.add(QUALIFIER, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_HEADER_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_HEADER_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// PATH MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_PATH_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getPathVariable(#NAME); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing PATH parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid PATH parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_PATH_2(TYPE, NAME, QUALIFIER) \
auto __param_str_val_##NAME = __request->getPathVariable(QUALIFIER); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Missing PATH parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid PATH parameter '") + \
QUALIFIER + \
"'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_PATH(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_PATH_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_PATH_INFO_1(TYPE, NAME) \
info->pathParams.add(#NAME, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_PATH_INFO_2(TYPE, NAME, QUALIFIER) \
info->pathParams.add(QUALIFIER, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_PATH_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_PATH_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// QUERIES MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_QUERIES(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST = __request->getQueryParameters();
#define OATPP_MACRO_API_CONTROLLER_QUERIES_INFO(TYPE, PARAM_LIST)
// QUERY MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_QUERY_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getQueryParameter(#NAME); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing QUERY parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid QUERY parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_QUERY_2(TYPE, NAME, QUALIFIER) \
auto __param_str_val_##NAME = __request->getQueryParameter(QUALIFIER); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Missing QUERY parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid QUERY parameter '") + \
QUALIFIER + \
"'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_QUERY(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_QUERY_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_QUERY_INFO_1(TYPE, NAME) \
info->queryParams.add(#NAME, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_QUERY_INFO_2(TYPE, NAME, QUALIFIER) \
info->queryParams.add(QUALIFIER, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_QUERY_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_QUERY_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// BODY_STRING MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_BODY_STRING(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST = __request->readBodyToString();
// __INFO
#define OATPP_MACRO_API_CONTROLLER_BODY_STRING_INFO(TYPE, PARAM_LIST) \
info->body.name = OATPP_MACRO_FIRSTARG_STR PARAM_LIST; \
info->body.type = oatpp::data::mapping::type::__class::String::getType();
// BODY_DTO MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_BODY_DTO(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST; \
__request->readBodyToDto(OATPP_MACRO_FIRSTARG PARAM_LIST, getDefaultObjectMapper().get()); \
if(!OATPP_MACRO_FIRSTARG PARAM_LIST) { \
return ApiController::handleError(Status::CODE_400, "Missing valid body parameter '" OATPP_MACRO_FIRSTARG_STR PARAM_LIST "'"); \
}
// __INFO
#define OATPP_MACRO_API_CONTROLLER_BODY_DTO_INFO(TYPE, PARAM_LIST) \
info->body.name = OATPP_MACRO_FIRSTARG_STR PARAM_LIST; \
info->body.type = TYPE::Class::getType();
// FOR EACH // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_FIRST(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_TYPE X OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_REST(INDEX, COUNT, X) \
, OATPP_MACRO_API_CONTROLLER_PARAM_TYPE X OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_PUT(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_MACRO X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_FIRST(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_REST(INDEX, COUNT, X) \
, OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_INFO(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_INFO X
// ENDPOINT_INFO MACRO // ------------------------------------------------------
#define ENDPOINT_INFO(NAME) \
\
std::shared_ptr<Endpoint::Info> Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME() { \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
Z__ENDPOINT_ADD_INFO_##NAME(info); \
return info; \
} \
\
const std::shared_ptr<Endpoint::Info> Z__ENDPOINT_ADDITIONAL_INFO_##NAME = Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME(); \
\
void Z__ENDPOINT_ADD_INFO_##NAME(const std::shared_ptr<Endpoint::Info>& info)
// ENDPOINT MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS(NAME, METHOD, PATH) \
\
template<class T> \
static typename std::shared_ptr<Handler<T>> Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(T* controller) { \
auto handler = std::static_pointer_cast<Handler<T>>(controller->getEndpointHandler(#NAME)); \
if(!handler) { \
handler = Handler<T>::createShared(controller, &T::Z__PROXY_METHOD_##NAME, nullptr); \
controller->setEndpointHandler(#NAME, handler); \
} \
return handler; \
} \
\
std::shared_ptr<Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
std::shared_ptr<Endpoint::Info> info = getEndpointInfo(#NAME); \
if(!info){ \
info = Endpoint::Info::createShared(); \
setEndpointInfo(#NAME, info); \
} \
return info; \
}
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_0(NAME, METHOD, PATH) \
\
EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
info->name = #NAME; \
info->path = PATH; \
info->method = METHOD; \
return info; \
}; \
\
const std::shared_ptr<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \
Z__CREATE_ENDPOINT_INFO_##NAME);
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_0(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_0(NAME, METHOD, PATH) \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__PROXY_METHOD_##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
{ \
(void)__request; \
return NAME(); \
} \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> NAME()
////////////////////
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_1(NAME, METHOD, PATH, ...) \
\
EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
info->name = #NAME; \
info->path = PATH; \
info->method = METHOD; \
OATPP_MACRO_FOREACH(OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_INFO, __VA_ARGS__) \
return info; \
}; \
\
const std::shared_ptr<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \
Z__CREATE_ENDPOINT_INFO_##NAME);
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_1(NAME, METHOD, PATH, ...) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_1(NAME, METHOD, PATH, __VA_ARGS__) \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__PROXY_METHOD_##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
{ \
OATPP_MACRO_FOREACH(OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_PUT, __VA_ARGS__) \
return NAME( \
OATPP_MACRO_FOREACH_FIRST_AND_REST( \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_FIRST, \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_REST, \
__VA_ARGS__ \
) \
); \
} \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> NAME(\
OATPP_MACRO_FOREACH_FIRST_AND_REST( \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_FIRST, \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_REST, \
__VA_ARGS__ \
) \
)
// Chooser
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_0(METHOD, PATH, NAME) \
OATPP_MACRO_EXPAND(OATPP_MACRO_API_CONTROLLER_ENDPOINT_0(NAME, METHOD, PATH))
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_1(METHOD, PATH, NAME, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_API_CONTROLLER_ENDPOINT_1(NAME, METHOD, PATH, __VA_ARGS__))
/**
* Codegen macoro to be used in `oatpp::web::server::api::ApiController` to generate Endpoint.
* @param METHOD - Http method ("GET", "POST", "PUT", etc.).
* @param PATH - Path to endpoint (without host).
* @param NAME - Name of the generated method.
* @return - std::shared_ptr to &id:oatpp::web::protocol::http::outgoing::Response;.
*/
#define ENDPOINT(METHOD, PATH, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_, (__VA_ARGS__)) (METHOD, PATH, __VA_ARGS__))
/**
* Endpoint interceptor
*/
#define ENDPOINT_INTERCEPTOR(ENDPOINT_NAME, NAME) \
\
Handler<oatpp::web::server::api::ApiController>::Method \
Z__INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME = Z__INTERCEPTOR_METHOD_SET_##ENDPOINT_NAME ##_ ##NAME(this); \
\
template<class T> \
Handler<oatpp::web::server::api::ApiController>::Method Z__INTERCEPTOR_METHOD_SET_##ENDPOINT_NAME ##_ ##NAME (T* controller) { \
return static_cast<Handler<oatpp::web::server::api::ApiController>::Method>( \
Z__ENDPOINT_HANDLER_GET_INSTANCE_##ENDPOINT_NAME(controller)->setMethod(&T::Z__PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME) \
); \
} \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request) { \
return Z__USER_PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME(this, request); \
} \
\
template<class T> \
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__USER_PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME( \
T* controller, \
const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request \
) { \
auto intercepted = static_cast<typename Handler<T>::Method>(Z__INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME); \
return Z__USER_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME <T> (intercepted, request); \
} \
\
template<class T> \
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__USER_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME( \
typename Handler<T>::Method intercepted, \
const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request \
)
// ENDPOINT ASYNC MACRO // ------------------------------------------------------
/*
* 1 - Method to obtain endpoint call function ptr
* 2 - Endpoint info singleton
*/
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL_DEFAULTS(NAME, METHOD, PATH) \
\
template<class T> \
static typename std::shared_ptr<Handler<T>> Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(T* controller) { \
auto handler = std::static_pointer_cast<Handler<T>>(controller->getEndpointHandler(#NAME)); \
if(!handler) { \
handler = Handler<T>::createShared(controller, nullptr, &T::Z__PROXY_METHOD_##NAME); \
controller->setEndpointHandler(#NAME, handler); \
} \
return handler; \
} \
\
std::shared_ptr<Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
std::shared_ptr<Endpoint::Info> info = getEndpointInfo(#NAME); \
if(!info){ \
info = Endpoint::Info::createShared(); \
setEndpointInfo(#NAME, info); \
} \
return info; \
}
/*
* 1 - Endpoint info instance
* 2 - Endpoint instance
*/
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL(NAME, METHOD, PATH) \
\
EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
info->name = #NAME; \
info->path = PATH; \
info->method = METHOD; \
return info; \
}; \
\
const std::shared_ptr<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
Z__ENDPOINT_HANDLER_GET_INSTANCE_##NAME(this), \
Z__CREATE_ENDPOINT_INFO_##NAME);
/**
* Codegen macoro to be used in `oatpp::web::server::api::ApiController` to generate Asynchronous Endpoint.
* @param METHOD - Http method ("GET", "POST", "PUT", etc.).
* @param PATH - Path to endpoint (without host).
* @param NAME - Name of the generated method.
* @return - &id:oatpp::async::Action;.
*/
#define ENDPOINT_ASYNC(METHOD, PATH, NAME) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL_DEFAULTS(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL(NAME, METHOD, PATH) \
\
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>&> \
Z__PROXY_METHOD_##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
{ \
return NAME::startForResult(this, __request); \
} \
\
class NAME : public HandlerCoroutine<NAME, __ControllerType>
/**
* Auxiliary codegen macro for `ENDPOINT_ASYNC` to generate correct constructor for Asynchronous Endpoint Coroutine.
* @NAME - Name of the endpoint. Exact the same name as was passed to `ENDPOINT_ASYNC` macro.
*/
#define ENDPOINT_ASYNC_INIT(NAME) \
public: \
\
NAME(__ControllerType* pController, \
const std::shared_ptr<IncomingRequest>& pRequest) \
: HandlerCoroutine(pController, pRequest) \
{}
/**
* Endpoint interceptor
*/
#define ENDPOINT_INTERCEPTOR_ASYNC(ENDPOINT_NAME, NAME) \
\
Handler<oatpp::web::server::api::ApiController>::MethodAsync \
Z__INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME = Z__INTERCEPTOR_METHOD_SET_##ENDPOINT_NAME ##_ ##NAME(this); \
\
template<class T> \
Handler<oatpp::web::server::api::ApiController>::MethodAsync Z__INTERCEPTOR_METHOD_SET_##ENDPOINT_NAME ##_ ##NAME (T* controller) { \
return static_cast<Handler<oatpp::web::server::api::ApiController>::MethodAsync>( \
Z__ENDPOINT_HANDLER_GET_INSTANCE_##ENDPOINT_NAME(controller)->setMethodAsync(&T::Z__PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME) \
); \
} \
\
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>&> \
Z__PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request) { \
return Z__USER_PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME(this, request); \
} \
\
template<class T> \
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>&> \
Z__USER_PROXY_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME( \
T* controller, \
const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request \
) { \
auto intercepted = static_cast<typename Handler<T>::MethodAsync>(Z__INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME); \
return Z__USER_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME <T> (intercepted, request); \
} \
\
template<class T> \
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>&> \
Z__USER_INTERCEPTOR_METHOD_##ENDPOINT_NAME ##_ ##NAME( \
typename Handler<T>::MethodAsync intercepted, \
const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request \
)

View File

@ -0,0 +1,144 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#undef OATPP_MACRO_API_CONTROLLER_PARAM_MACRO
#undef OATPP_MACRO_API_CONTROLLER_PARAM_INFO
#undef OATPP_MACRO_API_CONTROLLER_PARAM_TYPE
#undef OATPP_MACRO_API_CONTROLLER_PARAM_NAME
#undef OATPP_MACRO_API_CONTROLLER_PARAM_TYPE_STR
#undef OATPP_MACRO_API_CONTROLLER_PARAM_NAME_STR
#undef OATPP_MACRO_API_CONTROLLER_PARAM
#undef REQUEST
#undef HEADER
#undef PATH
#undef QUERIES
#undef QUERY
#undef BODY_STRING
#undef BODY_DTO
// INIT // ------------------------------------------------------
#undef REST_CONTROLLER_INIT
#undef OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR
// REQUEST MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_REQUEST
#undef OATPP_MACRO_API_CONTROLLER_REQUEST_INFO
// HEADER MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_HEADER_1
#undef OATPP_MACRO_API_CONTROLLER_HEADER_2
#undef OATPP_MACRO_API_CONTROLLER_HEADER
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_HEADER_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_HEADER_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_HEADER_INFO
// PATH MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_PATH_1
#undef OATPP_MACRO_API_CONTROLLER_PATH_2
#undef OATPP_MACRO_API_CONTROLLER_PATH
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_PATH_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_PATH_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_PATH_INFO
// QUERIES MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_QUERIES
#undef OATPP_MACRO_API_CONTROLLER_QUERIES_INFO
// QUERY MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_QUERY_1
#undef OATPP_MACRO_API_CONTROLLER_QUERY_2
#undef OATPP_MACRO_API_CONTROLLER_QUERY
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_QUERY_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_QUERY_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_QUERY_INFO
// BODY_STRING MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_BODY_STRING
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_BODY_STRING_INFO
// BODY_DTO MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_BODY_DTO
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_BODY_DTO_INFO
// FOR EACH // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_PUT
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_INFO
// ENDPOINT_INFO MACRO // ------------------------------------------------------
#undef ENDPOINT_INFO
// ENDPOINT MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_0
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_0
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_1
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_1
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_0
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_1
#undef ENDPOINT
#undef ENDPOINT_INTERCEPTOR
// ENDPOINT ASYNC MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL_DEFAULTS
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL
#undef ENDPOINT_ASYNC
#undef ENDPOINT_ASYNC_INIT
#undef ENDPOINT_INTERCEPTOR_ASYNC

View File

@ -0,0 +1,138 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
* Benedikt-Alexander Mokroß <bam@icognize.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_ORIGIN "*"
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_METHODS "GET, POST, OPTIONS"
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization"
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE "1728000"
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY(ARG_ORIGIN, ARG_METHODS, ARG_HEADERS, ARG_MAX_AGE) \
resp->putHeaderIfNotExists(oatpp::web::protocol::http::Header::CORS_ORIGIN, ARG_ORIGIN); \
resp->putHeaderIfNotExists(oatpp::web::protocol::http::Header::CORS_METHODS, ARG_METHODS); \
resp->putHeaderIfNotExists(oatpp::web::protocol::http::Header::CORS_HEADERS, ARG_HEADERS);\
resp->putHeaderIfNotExists(oatpp::web::protocol::http::Header::CORS_MAX_AGE, ARG_MAX_AGE);
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_1(ENDPOINTNAME, ...) \
ENDPOINT_INTERCEPTOR(ENDPOINTNAME, CORS) { \
auto resp = (this->*intercepted)(request); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_ORIGIN, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_METHODS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
} \
ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_ENDPOINT_##ENDPOINTNAME) { \
auto resp = createResponse(Status::CODE_204, ""); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_ORIGIN, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_METHODS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_2(ENDPOINTNAME, ORIGIN) \
ENDPOINT_INTERCEPTOR(ENDPOINTNAME, CORS) { \
auto resp = (this->*intercepted)(request); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
ORIGIN, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_METHODS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
} \
ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_ENDPOINT_##ENDPOINTNAME) { \
auto resp = createResponse(Status::CODE_204, ""); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
ORIGIN, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_METHODS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_3(ENDPOINTNAME, ORIGIN, METHODS) \
ENDPOINT_INTERCEPTOR(ENDPOINTNAME, CORS) { \
auto resp = (this->*intercepted)(request); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
ORIGIN, \
METHODS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
} \
ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_ENDPOINT_##ENDPOINTNAME) { \
auto resp = createResponse(Status::CODE_204, ""); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
ORIGIN, \
METHODS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_4(ENDPOINTNAME, ORIGIN, METHODS, HEADERS) \
ENDPOINT_INTERCEPTOR(ENDPOINTNAME, CORS) { \
auto resp = (this->*intercepted)(request); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
ORIGIN, \
METHODS, \
HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
} \
ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_ENDPOINT_##ENDPOINTNAME) { \
auto resp = createResponse(Status::CODE_204, ""); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY( \
ORIGIN, \
METHODS, \
HEADERS, \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE \
) \
return resp; \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_5(ENDPOINTNAME, ORIGIN, METHODS, HEADERS, MAX_AGE) \
ENDPOINT_INTERCEPTOR(ENDPOINTNAME, CORS) { \
auto resp = (this->*intercepted)(request); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY(ORIGIN, METHODS, HEADERS, MAX_AGE) \
return resp; \
} \
ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_ENDPOINT_##ENDPOINTNAME) { \
auto resp = createResponse(Status::CODE_204, ""); \
OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY(ORIGIN, METHODS, HEADERS, MAX_AGE) \
return resp; \
}
#define ADDCORS(...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_, (__VA_ARGS__)) (__VA_ARGS__))

View File

@ -0,0 +1,37 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
* Benedikt-Alexander Mokroß <bam@icognize.de>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#undef ADDCORS
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_1
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_2
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_3
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_4
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_5
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_ORIGIN
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_METHODS
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_HEADERS
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_BODY_DEFAULT_MAX_AGE

View File

@ -45,467 +45,6 @@
#include "oatpp/core/macro/basic.hpp"
#include "oatpp/core/macro/codegen.hpp"
#define OATPP_MACRO_API_CONTROLLER_PARAM_MACRO(MACRO, INFO, TYPE, PARAM_LIST) MACRO(TYPE, PARAM_LIST)
#define OATPP_MACRO_API_CONTROLLER_PARAM_INFO(MACRO, INFO, TYPE, PARAM_LIST) INFO(TYPE, PARAM_LIST)
#define OATPP_MACRO_API_CONTROLLER_PARAM_TYPE(MACRO, INFO, TYPE, PARAM_LIST) TYPE
#define OATPP_MACRO_API_CONTROLLER_PARAM_NAME(MACRO, INFO, TYPE, PARAM_LIST) OATPP_MACRO_FIRSTARG PARAM_LIST
#define OATPP_MACRO_API_CONTROLLER_PARAM_TYPE_STR(MACRO, INFO, TYPE, PARAM_LIST) #TYPE
#define OATPP_MACRO_API_CONTROLLER_PARAM_NAME_STR(MACRO, INFO, TYPE, PARAM_LIST) OATPP_MACRO_FIRSTARG_STR PARAM_LIST
#define OATPP_MACRO_API_CONTROLLER_PARAM(MACRO, INFO, TYPE, PARAM_LIST) (MACRO, INFO, TYPE, PARAM_LIST)
#define REQUEST(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_REQUEST, OATPP_MACRO_API_CONTROLLER_REQUEST_INFO, TYPE, (__VA_ARGS__))
#define HEADER(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_HEADER, OATPP_MACRO_API_CONTROLLER_HEADER_INFO, TYPE, (__VA_ARGS__))
#define PATH(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_PATH, OATPP_MACRO_API_CONTROLLER_PATH_INFO, TYPE, (__VA_ARGS__))
#define QUERIES(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_QUERIES, OATPP_MACRO_API_CONTROLLER_QUERIES_INFO, TYPE, (__VA_ARGS__))
#define QUERY(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_QUERY, OATPP_MACRO_API_CONTROLLER_QUERY_INFO, TYPE, (__VA_ARGS__))
#define BODY_STRING(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_BODY_STRING, OATPP_MACRO_API_CONTROLLER_BODY_STRING_INFO, TYPE, (__VA_ARGS__))
#define BODY_DTO(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_BODY_DTO, OATPP_MACRO_API_CONTROLLER_BODY_DTO_INFO, TYPE, (__VA_ARGS__))
#define AUTHORIZATION(TYPE, ...) \
OATPP_MACRO_API_CONTROLLER_PARAM(OATPP_MACRO_API_CONTROLLER_AUTHORIZATION, OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO, TYPE, (__VA_ARGS__))
//////////////////////////////////////////////////////////////////////////
#define OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(MACRO, TYPE, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_SELECTOR(MACRO, (__VA_ARGS__)) (TYPE, __VA_ARGS__))
//////////////////////////////////////////////////////////////////////////
// REQUEST MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_REQUEST(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST = __request;
#define OATPP_MACRO_API_CONTROLLER_REQUEST_INFO(TYPE, PARAM_LIST)
// HEADER MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_HEADER_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getHeader(#NAME); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing HEADER parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid HEADER parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_HEADER_2(TYPE, NAME, QUALIFIER) \
auto __param_str_val_##NAME = __request->getHeader(QUALIFIER); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Missing HEADER parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid HEADER parameter '") + \
QUALIFIER + \
"'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_HEADER(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_HEADER_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_HEADER_INFO_1(TYPE, NAME) \
info->headers.add(#NAME, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_HEADER_INFO_2(TYPE, NAME, QUALIFIER) \
info->headers.add(QUALIFIER, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_HEADER_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_HEADER_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// PATH MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_PATH_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getPathVariable(#NAME); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing PATH parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid PATH parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_PATH_2(TYPE, NAME, QUALIFIER) \
auto __param_str_val_##NAME = __request->getPathVariable(QUALIFIER); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Missing PATH parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid PATH parameter '") + \
QUALIFIER + \
"'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_PATH(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_PATH_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_PATH_INFO_1(TYPE, NAME) \
info->pathParams.add(#NAME, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_PATH_INFO_2(TYPE, NAME, QUALIFIER) \
info->pathParams.add(QUALIFIER, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_PATH_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_PATH_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// QUERIES MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_QUERIES(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST = __request->getQueryParameters();
#define OATPP_MACRO_API_CONTROLLER_QUERIES_INFO(TYPE, PARAM_LIST)
// QUERY MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_QUERY_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getQueryParameter(#NAME); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing QUERY parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid QUERY parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_QUERY_2(TYPE, NAME, QUALIFIER) \
auto __param_str_val_##NAME = __request->getQueryParameter(QUALIFIER); \
if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Missing QUERY parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
TYPE NAME = TYPE::Class::parseFromString(__param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid QUERY parameter '") + \
QUALIFIER + \
"'. Expected type is '" #TYPE "'"); \
}
#define OATPP_MACRO_API_CONTROLLER_QUERY(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_QUERY_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_QUERY_INFO_1(TYPE, NAME) \
info->queryParams.add(#NAME, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_QUERY_INFO_2(TYPE, NAME, QUALIFIER) \
info->queryParams.add(QUALIFIER, TYPE::Class::getType());
#define OATPP_MACRO_API_CONTROLLER_QUERY_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_QUERY_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// BODY_STRING MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_BODY_STRING(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST = __request->readBodyToString();
// __INFO
#define OATPP_MACRO_API_CONTROLLER_BODY_STRING_INFO(TYPE, PARAM_LIST) \
info->body.name = OATPP_MACRO_FIRSTARG_STR PARAM_LIST; \
info->body.type = oatpp::data::mapping::type::__class::String::getType();
// BODY_DTO MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_BODY_DTO(TYPE, PARAM_LIST) \
TYPE OATPP_MACRO_FIRSTARG PARAM_LIST; \
__request->readBodyToDto(OATPP_MACRO_FIRSTARG PARAM_LIST, getDefaultObjectMapper().get()); \
if(!OATPP_MACRO_FIRSTARG PARAM_LIST) { \
return ApiController::handleError(Status::CODE_400, "Missing valid body parameter '" OATPP_MACRO_FIRSTARG_STR PARAM_LIST "'"); \
}
// __INFO
#define OATPP_MACRO_API_CONTROLLER_BODY_DTO_INFO(TYPE, PARAM_LIST) \
info->body.name = OATPP_MACRO_FIRSTARG_STR PARAM_LIST; \
info->body.type = TYPE::Class::getType();
// AUTHORIZATION MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_1(TYPE, NAME) \
auto __param_str_val_##NAME = __request->getHeader(oatpp::web::protocol::http::Header::AUTHORIZATION); \
std::shared_ptr<oatpp::web::server::handler::AuthorizationObject> __param_aosp_val_##NAME = ApiController::handleDefaultAuthorization(__param_str_val_##NAME); \
TYPE NAME = std::static_pointer_cast<TYPE::element_type>(__param_aosp_val_##NAME);
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_2(TYPE, NAME, AUTH_HANDLER) \
auto __param_str_val_##NAME = __request->getHeader(oatpp::web::protocol::http::Header::AUTHORIZATION); \
std::shared_ptr<oatpp::web::server::handler::AuthorizationHandler> __auth_handler_##NAME = AUTH_HANDLER; \
std::shared_ptr<oatpp::web::server::handler::AuthorizationObject> __param_aosp_val_##NAME = __auth_handler_##NAME->handleAuthorization(__param_str_val_##NAME); \
TYPE NAME = std::static_pointer_cast<TYPE::element_type>(__param_aosp_val_##NAME);
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// __INFO
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_1(TYPE, NAME) \
auto __param_obj_##NAME = ApiController::getDefaultAuthorizationHandler(); \
if(__param_obj_##NAME) { \
info->headers.add(oatpp::web::protocol::http::Header::AUTHORIZATION, oatpp::String::Class::getType()); \
info->headers[oatpp::web::protocol::http::Header::AUTHORIZATION].description = __param_obj_##NAME ->getScheme(); \
info->authorization = __param_obj_##NAME ->getScheme(); \
} else { \
throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "No authorization handler set up in controller before controller was added to router or swagger-doc."); \
}
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_2(TYPE, NAME, AUTH_HANDLER) \
std::shared_ptr<oatpp::web::server::handler::AuthorizationHandler> __auth_handler_##NAME = AUTH_HANDLER; \
if(__auth_handler_##NAME) { \
info->headers.add(oatpp::web::protocol::http::Header::AUTHORIZATION, oatpp::String::Class::getType()); \
info->headers[oatpp::web::protocol::http::Header::AUTHORIZATION].description = __auth_handler_##NAME->getScheme(); \
info->authorization = __auth_handler_##NAME->getScheme(); \
} else { \
throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "Invalid authorization handler given (or not set up) in AUTHORIZATION(TYPE, NAME, AUTH_HANDLER) before controller was added to router or swagger-doc."); \
}
#define OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO(TYPE, PARAM_LIST) \
OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_, TYPE, OATPP_MACRO_UNFOLD_VA_ARGS PARAM_LIST)
// FOR EACH // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_FIRST(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_TYPE X OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_REST(INDEX, COUNT, X) \
, OATPP_MACRO_API_CONTROLLER_PARAM_TYPE X OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_PUT(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_MACRO X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_FIRST(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_REST(INDEX, COUNT, X) \
, OATPP_MACRO_API_CONTROLLER_PARAM_NAME X
#define OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_INFO(INDEX, COUNT, X) \
OATPP_MACRO_API_CONTROLLER_PARAM_INFO X
// ENDPOINT_INFO MACRO // ------------------------------------------------------
#define ENDPOINT_INFO(NAME) \
\
std::shared_ptr<Endpoint::Info> Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME() { \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
Z__ENDPOINT_ADD_INFO_##NAME(info); \
return info; \
} \
\
const std::shared_ptr<Endpoint::Info> Z__ENDPOINT_ADDITIONAL_INFO_##NAME = Z__ENDPOINT_CREATE_ADDITIONAL_INFO_##NAME(); \
\
void Z__ENDPOINT_ADD_INFO_##NAME(const std::shared_ptr<Endpoint::Info>& info)
// ENDPOINT MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS(NAME, METHOD, PATH) \
\
template<class T> \
static typename Handler<T>::Method Z__ENDPOINT_METHOD_##NAME(T* controller) { \
(void)controller; \
return &T::Z__PROXY_METHOD_##NAME; \
} \
\
std::shared_ptr<Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
std::shared_ptr<Endpoint::Info> info = getEndpointInfo(#NAME); \
if(!info){ \
info = Endpoint::Info::createShared(); \
setEndpointInfo(#NAME, info); \
} \
return info; \
}
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_0(NAME, METHOD, PATH) \
\
EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
info->name = #NAME; \
info->path = PATH; \
info->method = METHOD; \
return info; \
}; \
\
const std::shared_ptr<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
this, \
Z__ENDPOINT_METHOD_##NAME(this), \
nullptr, \
Z__CREATE_ENDPOINT_INFO_##NAME);
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_0(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_0(NAME, METHOD, PATH) \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__PROXY_METHOD_##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
{ \
(void)__request; \
return NAME(); \
} \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> NAME()
////////////////////
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_1(NAME, METHOD, PATH, ...) \
\
EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
info->name = #NAME; \
info->path = PATH; \
info->method = METHOD; \
OATPP_MACRO_FOREACH(OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_INFO, __VA_ARGS__) \
return info; \
}; \
\
const std::shared_ptr<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
this, \
Z__ENDPOINT_METHOD_##NAME(this), \
nullptr, \
Z__CREATE_ENDPOINT_INFO_##NAME);
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_1(NAME, METHOD, PATH, ...) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_1(NAME, METHOD, PATH, __VA_ARGS__) \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> \
Z__PROXY_METHOD_##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
{ \
OATPP_MACRO_FOREACH(OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_PUT, __VA_ARGS__) \
return NAME( \
OATPP_MACRO_FOREACH_FIRST_AND_REST( \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_FIRST, \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL_REST, \
__VA_ARGS__ \
) \
); \
} \
\
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> NAME(\
OATPP_MACRO_FOREACH_FIRST_AND_REST( \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_FIRST, \
OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL_REST, \
__VA_ARGS__ \
) \
)
// Chooser
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_0(METHOD, PATH, NAME) \
OATPP_MACRO_EXPAND(OATPP_MACRO_API_CONTROLLER_ENDPOINT_0(NAME, METHOD, PATH))
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_1(METHOD, PATH, NAME, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_API_CONTROLLER_ENDPOINT_1(NAME, METHOD, PATH, __VA_ARGS__))
/**
* Codegen macoro to be used in `oatpp::web::server::api::ApiController` to generate Endpoint.
* @param METHOD - Http method ("GET", "POST", "PUT", etc.).
* @param PATH - Path to endpoint (without host).
* @param NAME - Name of the generated method.
* @return - std::shared_ptr to &id:oatpp::web::protocol::http::outgoing::Response;.
*/
#define ENDPOINT(METHOD, PATH, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_, (__VA_ARGS__)) (METHOD, PATH, __VA_ARGS__))
// ENDPOINT ASYNC MACRO // ------------------------------------------------------
/*
* 1 - Method to obtain endpoint call function ptr
* 2 - Endpoint info singleton
*/
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL_DEFAULTS(NAME, METHOD, PATH) \
template<class T> \
static typename Handler<T>::MethodAsync Z__ENDPOINT_METHOD_##NAME(T* controller) { \
(void)controller; \
return &T::Z__PROXY_METHOD_##NAME; \
} \
\
std::shared_ptr<Endpoint::Info> Z__EDNPOINT_INFO_GET_INSTANCE_##NAME() { \
std::shared_ptr<Endpoint::Info> info = getEndpointInfo(#NAME); \
if(!info){ \
info = Endpoint::Info::createShared(); \
setEndpointInfo(#NAME, info); \
} \
return info; \
}
/*
* 1 - Endpoint info instance
* 2 - Endpoint instance
*/
#define OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL(NAME, METHOD, PATH) \
\
EndpointInfoBuilder Z__CREATE_ENDPOINT_INFO_##NAME = [this](){ \
auto info = Z__EDNPOINT_INFO_GET_INSTANCE_##NAME(); \
info->name = #NAME; \
info->path = PATH; \
info->method = METHOD; \
return info; \
}; \
\
const std::shared_ptr<Endpoint> Z__ENDPOINT_##NAME = createEndpoint(m_endpoints, \
this, \
nullptr, \
Z__ENDPOINT_METHOD_##NAME(this), \
Z__CREATE_ENDPOINT_INFO_##NAME);
/**
* Codegen macoro to be used in `oatpp::web::server::api::ApiController` to generate Asynchronous Endpoint.
* @param METHOD - Http method ("GET", "POST", "PUT", etc.).
* @param PATH - Path to endpoint (without host).
* @param NAME - Name of the generated method.
* @return - &id:oatpp::async::Action;.
*/
#define ENDPOINT_ASYNC(METHOD, PATH, NAME) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL_DEFAULTS(NAME, METHOD, PATH) \
OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL(NAME, METHOD, PATH) \
\
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>&> \
Z__PROXY_METHOD_##NAME(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& __request) \
{ \
return NAME::startForResult(this, __request); \
} \
\
class NAME : public HandlerCoroutine<NAME, __ControllerType>
/**
* Auxiliary codegen macro for `ENDPOINT_ASYNC` to generate correct constructor for Asynchronous Endpoint Coroutine.
* @NAME - Name of the endpoint. Exact the same name as was passed to `ENDPOINT_ASYNC` macro.
*/
#define ENDPOINT_ASYNC_INIT(NAME) \
public: \
\
NAME(__ControllerType* pController, \
const std::shared_ptr<IncomingRequest>& pRequest) \
: HandlerCoroutine(pController, pRequest) \
{}
#include "./api_controller/base_define.hpp"
#include "./api_controller/auth_define.hpp"
#include "./api_controller/cors_define.hpp"

View File

@ -42,130 +42,6 @@
* </ul>
*/
#undef OATPP_MACRO_API_CONTROLLER_PARAM_MACRO
#undef OATPP_MACRO_API_CONTROLLER_PARAM_INFO
#undef OATPP_MACRO_API_CONTROLLER_PARAM_TYPE
#undef OATPP_MACRO_API_CONTROLLER_PARAM_NAME
#undef OATPP_MACRO_API_CONTROLLER_PARAM_TYPE_STR
#undef OATPP_MACRO_API_CONTROLLER_PARAM_NAME_STR
#undef OATPP_MACRO_API_CONTROLLER_PARAM
#undef REQUEST
#undef HEADER
#undef PATH
#undef QUERIES
#undef QUERY
#undef BODY_STRING
#undef BODY_DTO
#undef AUTHORIZATION
// INIT // ------------------------------------------------------
#undef REST_CONTROLLER_INIT
#undef OATPP_MACRO_API_CONTROLLER_MACRO_SELECTOR
// REQUEST MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_REQUEST
#undef OATPP_MACRO_API_CONTROLLER_REQUEST_INFO
// HEADER MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_HEADER_1
#undef OATPP_MACRO_API_CONTROLLER_HEADER_2
#undef OATPP_MACRO_API_CONTROLLER_HEADER
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_HEADER_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_HEADER_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_HEADER_INFO
// PATH MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_PATH_1
#undef OATPP_MACRO_API_CONTROLLER_PATH_2
#undef OATPP_MACRO_API_CONTROLLER_PATH
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_PATH_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_PATH_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_PATH_INFO
// QUERIES MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_QUERIES
#undef OATPP_MACRO_API_CONTROLLER_QUERIES_INFO
// QUERY MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_QUERY_1
#undef OATPP_MACRO_API_CONTROLLER_QUERY_2
#undef OATPP_MACRO_API_CONTROLLER_QUERY
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_QUERY_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_QUERY_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_QUERY_INFO
// BODY_STRING MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_BODY_STRING
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_BODY_STRING_INFO
// BODY_DTO MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_BODY_DTO
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_BODY_DTO_INFO
// AUTHORIZATION MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_1
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_2
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION
// __INFO
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_1
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO_2
#undef OATPP_MACRO_API_CONTROLLER_AUTHORIZATION_INFO
// FOR EACH // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_DECL
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_PUT
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_CALL
#undef OATPP_MACRO_API_CONTROLLER_FOR_EACH_PARAM_INFO
// ENDPOINT_INFO MACRO // ------------------------------------------------------
#undef ENDPOINT_INFO
// ENDPOINT MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_DEFAULTS
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_0
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_0
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_DECL_1
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_1
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_0
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_MACRO_1
#undef ENDPOINT
// ENDPOINT ASYNC MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL_DEFAULTS
#undef OATPP_MACRO_API_CONTROLLER_ENDPOINT_ASYNC_DECL
#undef ENDPOINT_ASYNC
#undef ENDPOINT_ASYNC_INIT
#include "./api_controller/base_undef.hpp"
#include "./api_controller/auth_undef.hpp"
#include "./api_controller/cors_undef.hpp"

View File

@ -121,7 +121,13 @@ const char* const Header::HOST = "Host";
const char* const Header::USER_AGENT = "User-Agent";
const char* const Header::SERVER = "Server";
const char* const Header::UPGRADE = "Upgrade";
const char* const Header::CORS_ORIGIN = "Access-Control-Allow-Origin";
const char* const Header::CORS_METHODS = "Access-Control-Allow-Methods";
const char* const Header::CORS_HEADERS = "Access-Control-Allow-Headers";
const char* const Header::CORS_MAX_AGE = "Access-Control-Max-Age";
const char* const Range::UNIT_BYTES = "bytes";
const char* const ContentRange::UNIT_BYTES = "bytes";

View File

@ -497,6 +497,10 @@ public:
static const char* const USER_AGENT; // "User-Agent"
static const char* const SERVER; // "Server"
static const char* const UPGRADE; // "Upgrade"
static const char* const CORS_ORIGIN; // Access-Control-Allow-Origin
static const char* const CORS_METHODS; // Access-Control-Allow-Methods
static const char* const CORS_HEADERS; // Access-Control-Allow-Headers
static const char* const CORS_MAX_AGE; // Access-Control-Max-Age
};
class Range {

View File

@ -89,6 +89,19 @@ std::shared_ptr<const http::incoming::BodyDecoder> Request::getBodyDecoder() con
return m_bodyDecoder;
}
void Request::putHeader(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value) {
m_headers[key] = value;
}
bool Request::putHeaderIfNotExists(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value) {
auto it = m_headers.find(key);
if(it == m_headers.end()) {
m_headers.insert({key, value});
return true;
}
return false;
}
oatpp::String Request::getHeader(const oatpp::data::share::StringKeyLabelCI_FAST& headerName) const{
auto it = m_headers.find(headerName);
if(it != m_headers.end()) {

View File

@ -126,10 +126,25 @@ public:
*/
std::shared_ptr<const http::incoming::BodyDecoder> getBodyDecoder() const;
/**
* Add http header.
* @param key - &id:oatpp::data::share::StringKeyLabelCI_FAST;.
* @param value - &id:oatpp::data::share::StringKeyLabel;.
*/
void putHeader(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Add http header if not already exists.
* @param key - &id:oatpp::data::share::StringKeyLabelCI_FAST;.
* @param value - &id:oatpp::data::share::StringKeyLabel;.
* @return - `true` if header was added.
*/
bool putHeaderIfNotExists(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Get header value
* @param headerName
* @return header value
* @param headerName - &id:oatpp::data::share::StringKeyLabelCI_FAST;.
* @return - &id:oatpp::String;.
*/
oatpp::String getHeader(const oatpp::data::share::StringKeyLabelCI_FAST& headerName) const;

View File

@ -60,6 +60,14 @@ bool Response::putHeaderIfNotExists(const oatpp::data::share::StringKeyLabelCI_F
return false;
}
oatpp::String Response::getHeader(const oatpp::data::share::StringKeyLabelCI_FAST& headerName) const {
auto it = m_headers.find(headerName);
if(it != m_headers.end()) {
return it->second.toString();
}
return nullptr;
}
void Response::setConnectionUpgradeHandler(const std::shared_ptr<oatpp::network::server::ConnectionHandler>& handler) {
m_connectionUpgradeHandler = handler;
}

View File

@ -100,6 +100,13 @@ public:
*/
bool putHeaderIfNotExists(const oatpp::data::share::StringKeyLabelCI_FAST& key, const oatpp::data::share::StringKeyLabel& value);
/**
* Get header value
* @param headerName - &id:oatpp::data::share::StringKeyLabelCI_FAST;.
* @return - &id:oatpp::String;.
*/
oatpp::String getHeader(const oatpp::data::share::StringKeyLabelCI_FAST& headerName) const;
/**
* Set connection upgreade header. <br>
* Use it together with corresponding headers being set when Response is created as: <br>

View File

@ -48,6 +48,14 @@ std::shared_ptr<ApiController::Endpoint::Info> ApiController::getEndpointInfo(co
return m_endpointInfo[endpointName];
}
void ApiController::setEndpointHandler(const std::string& endpointName, const std::shared_ptr<RequestHandler>& handler) {
m_endpointHandlers[endpointName] = handler;
}
std::shared_ptr<ApiController::RequestHandler> ApiController::getEndpointHandler(const std::string& endpointName) {
return m_endpointHandlers[endpointName];
}
void ApiController::setErrorHandler(const std::shared_ptr<handler::ErrorHandler>& errorHandler){
m_errorHandler = errorHandler;
}

View File

@ -105,6 +105,11 @@ public:
*/
typedef oatpp::collection::LinkedList<std::shared_ptr<Endpoint>> Endpoints;
/**
* Convenience typedef for &id:oatpp::web::server::HttpRequestHandler;.
*/
typedef oatpp::web::server::HttpRequestHandler RequestHandler;
/**
* Convenience typedef for &id:oatpp::web::server::handler::AuthorizationHandler;.
*/
@ -181,7 +186,7 @@ protected:
* Handler which subscribes to specific URL in Router and delegates calls endpoints
*/
template<class T>
class Handler : public oatpp::web::server::HttpRequestHandler {
class Handler : public RequestHandler {
public:
typedef std::shared_ptr<OutgoingResponse> (T::*Method)(const std::shared_ptr<protocol::http::incoming::Request>&);
typedef oatpp::async::CoroutineStarterForResult<const std::shared_ptr<OutgoingResponse>&>
@ -217,8 +222,55 @@ protected:
}
throw oatpp::web::protocol::http::HttpError(Status::CODE_500, "Using Async model for non async endpoint");
}
Method setMethod(Method method) {
auto prev = m_method;
m_method = method;
return prev;
}
Method getMethod() {
return m_method;
}
MethodAsync setMethodAsync(MethodAsync methodAsync) {
auto prev = m_methodAsync;
m_methodAsync = methodAsync;
return prev;
}
MethodAsync getMethodAsync() {
return m_methodAsync;
}
};
protected:
/*
* Set endpoint info by endpoint name. (Endpoint name is the 'NAME' parameter of the ENDPOINT macro)
* Info should be set before call to addEndpointsToRouter();
*/
void setEndpointInfo(const std::string& endpointName, const std::shared_ptr<Endpoint::Info>& info);
/*
* Get endpoint info by endpoint name. (Endpoint name is the 'NAME' parameter of the ENDPOINT macro)
*/
std::shared_ptr<Endpoint::Info> getEndpointInfo(const std::string& endpointName);
/*
* Set endpoint Request handler.
* @param endpointName
* @param handler
*/
void setEndpointHandler(const std::string& endpointName, const std::shared_ptr<RequestHandler>& handler);
/*
* Get endpoint Request handler.
* @param endpointName
* @return
*/
std::shared_ptr<RequestHandler> getEndpointHandler(const std::string& endpointName);
protected:
std::shared_ptr<Endpoints> m_endpoints;
@ -226,6 +278,7 @@ protected:
std::shared_ptr<handler::AuthorizationHandler> m_defaultAuthorizationHandler;
std::shared_ptr<oatpp::data::mapping::ObjectMapper> m_defaultObjectMapper;
std::unordered_map<std::string, std::shared_ptr<Endpoint::Info>> m_endpointInfo;
std::unordered_map<std::string, std::shared_ptr<RequestHandler>> m_endpointHandlers;
public:
ApiController(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& defaultObjectMapper)
: m_endpoints(Endpoints::createShared())
@ -236,16 +289,14 @@ public:
template<class T>
static std::shared_ptr<Endpoint> createEndpoint(const std::shared_ptr<Endpoints>& endpoints,
T* controller,
typename Handler<T>::Method method,
typename Handler<T>::MethodAsync methodAsync,
const EndpointInfoBuilder& infoBuilder){
auto handler = Handler<T>::createShared(controller, method, methodAsync);
const std::shared_ptr<Handler<T>>& handler,
const EndpointInfoBuilder& infoBuilder)
{
auto endpoint = Endpoint::createShared(handler, infoBuilder);
endpoints->pushBack(endpoint);
return endpoint;
}
/**
* Subscribes all created endpoint-handlers to corresponding URLs in Router
*/
@ -255,17 +306,6 @@ public:
* Get list of Endpoints created via ENDPOINT macro
*/
std::shared_ptr<Endpoints> getEndpoints();
/**
* Set endpoint info by endpoint name. (Endpoint name is the 'NAME' parameter of the ENDPOINT macro)
* Info should be set before call to addEndpointsToRouter();
*/
void setEndpointInfo(const std::string& endpointName, const std::shared_ptr<Endpoint::Info>& info);
/**
* Get endpoint info by endpoint name. (Endpoint name is the 'NAME' parameter of the ENDPOINT macro)
*/
std::shared_ptr<Endpoint::Info> getEndpointInfo(const std::string& endpointName);
/**
* [under discussion]

View File

@ -51,14 +51,16 @@ add_executable(oatppAllTests
oatpp/web/FullAsyncTest.hpp
oatpp/web/FullTest.cpp
oatpp/web/FullTest.hpp
oatpp/web/FullAsyncClientTest.cpp
oatpp/web/FullAsyncClientTest.hpp
oatpp/web/app/Client.hpp
oatpp/web/app/BearerAuthorizationController.hpp
oatpp/web/app/BasicAuthorizationController.hpp
oatpp/web/app/Controller.hpp
oatpp/web/app/ControllerAsync.hpp
oatpp/web/app/DTOs.hpp
oatpp/web/FullAsyncClientTest.cpp
oatpp/web/FullAsyncClientTest.hpp
oatpp/web/app/ControllerWithInterceptors.hpp
oatpp/web/app/ControllerWithInterceptorsAsync.hpp
)
target_link_libraries(oatppAllTests PRIVATE oatpp PRIVATE oatpp-test)

View File

@ -154,6 +154,7 @@ public:
} else {
OATPP_LOGE("[FullAsyncClientTest::ClientCoroutine_getRootAsync::handleError()]", "Error. %s", error->what());
}
OATPP_ASSERT(!"Error");
return error;
}
@ -195,6 +196,7 @@ public:
} else {
OATPP_LOGE("[FullAsyncClientTest::ClientCoroutine_postBodyAsync::handleError()]", "Error. %s", error->what());
}
OATPP_ASSERT(!"Error");
return error;
}
@ -242,6 +244,7 @@ public:
OATPP_LOGE("[FullAsyncClientTest::ClientCoroutine_echoBodyAsync::handleError()]", "Error. %s", error->what());
}
}
OATPP_ASSERT(!"Error");
return error;
}

View File

@ -26,6 +26,7 @@
#include "oatpp/web/app/Client.hpp"
#include "oatpp/web/app/ControllerWithInterceptorsAsync.hpp"
#include "oatpp/web/app/ControllerAsync.hpp"
#include "oatpp/web/client/HttpRequestExecutor.hpp"
@ -143,6 +144,7 @@ void FullAsyncTest::onRun() {
oatpp::test::web::ClientServerTestRunner runner;
runner.addController(app::ControllerAsync::createShared());
runner.addController(app::ControllerWithInterceptorsAsync::createShared());
runner.run([this, &runner] {
@ -256,6 +258,13 @@ void FullAsyncTest::onRun() {
}
{ // test interceptor GET
auto response = client->getInterceptors(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Hello World Async!!!");
}
if((i + 1) % iterationsStep == 0) {
auto ticks = oatpp::base::Environment::getMicroTickCount() - lastTick;
lastTick = oatpp::base::Environment::getMicroTickCount();

View File

@ -26,6 +26,7 @@
#include "oatpp/web/app/Client.hpp"
#include "oatpp/web/app/ControllerWithInterceptors.hpp"
#include "oatpp/web/app/Controller.hpp"
#include "oatpp/web/app/BasicAuthorizationController.hpp"
#include "oatpp/web/app/BearerAuthorizationController.hpp"
@ -140,6 +141,7 @@ void FullTest::onRun() {
oatpp::test::web::ClientServerTestRunner runner;
runner.addController(app::Controller::createShared());
runner.addController(app::ControllerWithInterceptors::createShared());
runner.addController(app::DefaultBasicAuthorizationController::createShared());
runner.addController(app::BasicAuthorizationController::createShared());
runner.addController(app::BearerAuthorizationController::createShared());
@ -168,6 +170,84 @@ void FullTest::onRun() {
OATPP_ASSERT(value == "Hello World!!!");
}
{ // test simple GET with CORS
auto response = client->getCors(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Ping");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "*");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, POST, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization");
}
{ // test simple OPTIONS with CORS
auto response = client->optionsCors(connection);
OATPP_ASSERT(response->getStatusCode() == 204);
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "*");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, POST, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization");
}
{ // test simple GET with CORS
auto response = client->getCorsOrigin(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Pong");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "127.0.0.1");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, POST, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization");
}
{ // test simple GET with CORS
auto response = client->getCorsOriginMethods(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Ping");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "127.0.0.1");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization");
}
{ // test simple GET with CORS
auto response = client->getCorsOriginMethodsHeader(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Pong");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "127.0.0.1");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "X-PWNT");
}
{ // test GET with path parameter
auto response = client->getWithParams("my_test_param", connection);
OATPP_ASSERT(response->getStatusCode() == 200);
@ -353,6 +433,13 @@ void FullTest::onRun() {
}
{ // test interceptors
auto response = client->getInterceptors(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Hello World!!!");
}
if((i + 1) % iterationsStep == 0) {
auto ticks = oatpp::base::Environment::getMicroTickCount() - lastTick;
lastTick = oatpp::base::Environment::getMicroTickCount();

View File

@ -42,6 +42,11 @@ public:
API_CLIENT_INIT(Client)
API_CALL("GET", "/", getRoot)
API_CALL("GET", "/cors", getCors)
API_CALL("OPTIONS", "/cors", optionsCors)
API_CALL("GET", "/cors-origin", getCorsOrigin)
API_CALL("GET", "/cors-origin-methods", getCorsOriginMethods)
API_CALL("GET", "/cors-origin-methods-headers", getCorsOriginMethodsHeader)
API_CALL("GET", "params/{param}", getWithParams, PATH(String, param))
API_CALL("GET", "queries", getWithQueries, QUERY(String, name), QUERY(Int32, age))
API_CALL("GET", "queries/map", getWithQueriesMap, QUERY(String, key1), QUERY(Int32, key2), QUERY(Float32, key3))
@ -60,6 +65,8 @@ public:
API_CALL("GET", "chunked/{text-value}/{num-iterations}", getChunked, PATH(String, text, "text-value"), PATH(Int32, numIterations, "num-iterations"))
API_CALL("POST", "test/multipart/{chunk-size}", multipartTest, PATH(Int32, chunkSize, "chunk-size"), BODY(std::shared_ptr<MultipartBody>, body))
API_CALL("GET", "test/interceptors", getInterceptors)
API_CALL_ASYNC("GET", "/", getRootAsync)
API_CALL_ASYNC("GET", "/", getRootAsyncWithCKA, HEADER(String, connection, "Connection"))
API_CALL_ASYNC("GET", "params/{param}", getWithParamsAsync, PATH(String, param))

View File

@ -60,12 +60,40 @@ public:
}
#include OATPP_CODEGEN_BEGIN(ApiController)
/*ENDPOINT_INTERCEPTOR(root, inter1) {
OATPP_LOGD("aaa", "inter1");
return (this->*prevMethod)(request);
}*/
/*ENDPOINT_INTERCEPTOR(root, inter2) {
OATPP_LOGD("aaa", "inter2");
return (this->*prevMethod)(request);
}*/
ENDPOINT("GET", "/", root) {
//OATPP_LOGV(TAG, "GET '/'");
return createResponse(Status::CODE_200, "Hello World!!!");
}
ADDCORS(cors);
ENDPOINT("GET", "/cors", cors) {
return createResponse(Status::CODE_200, "Ping");
}
ADDCORS(corsOrigin, "127.0.0.1");
ENDPOINT("GET", "/cors-origin", corsOrigin) {
return createResponse(Status::CODE_200, "Pong");
}
ADDCORS(corsOriginMethods, "127.0.0.1", "GET, OPTIONS");
ENDPOINT("GET", "/cors-origin-methods", corsOriginMethods) {
return createResponse(Status::CODE_200, "Ping");
}
ADDCORS(corsOriginMethodsHeaders, "127.0.0.1", "GET, OPTIONS", "X-PWNT");
ENDPOINT("GET", "/cors-origin-methods-headers", corsOriginMethodsHeaders) {
return createResponse(Status::CODE_200, "Pong");
}
ENDPOINT("GET", "params/{param}", getWithParams,
PATH(String, param)) {
//OATPP_LOGV(TAG, "GET params/%s", param->c_str());

View File

@ -60,7 +60,16 @@ public:
}
#include OATPP_CODEGEN_BEGIN(ApiController)
// ENDPOINT_INTERCEPTOR_ASYNC(Root, inter1) {
// OATPP_LOGD("aaa", "inter1");
// return (this->*prevMethod)(request);
// }
// ENDPOINT_INTERCEPTOR(root, inter2) {
// OATPP_LOGD("aaa", "inter2");
// return (this->*prevMethod)(request);
// }
ENDPOINT_ASYNC("GET", "/", Root) {
ENDPOINT_ASYNC_INIT(Root)

View File

@ -0,0 +1,114 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#ifndef oatpp_test_web_app_ControllerWithInterceptors_hpp
#define oatpp_test_web_app_ControllerWithInterceptors_hpp
#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/core/utils/ConversionUtils.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp"
#include <sstream>
namespace oatpp { namespace test { namespace web { namespace app {
namespace multipart = oatpp::web::mime::multipart;
class ControllerWithInterceptors : public oatpp::web::server::api::ApiController {
private:
static constexpr const char* TAG = "test::web::app::ControllerWithInterceptors";
public:
ControllerWithInterceptors(const std::shared_ptr<ObjectMapper>& objectMapper)
: oatpp::web::server::api::ApiController(objectMapper)
{}
public:
static std::shared_ptr<ControllerWithInterceptors> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){
return std::make_shared<ControllerWithInterceptors>(objectMapper);
}
#include OATPP_CODEGEN_BEGIN(ApiController)
ENDPOINT_INTERCEPTOR(interceptor, inter1) {
/* assert order of interception */
OATPP_ASSERT(request->getHeader("header-in-inter2") == "inter2");
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
/********************************/
request->putHeader("header-in-inter1", "inter1");
auto response = (this->*intercepted)(request);
response->putHeader("header-out-inter1", "inter1");
return response;
}
ENDPOINT_INTERCEPTOR(interceptor, inter2) {
/* assert order of interception */
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
/********************************/
request->putHeader("header-in-inter2", "inter2");
auto response = (this->*intercepted)(request);
response->putHeader("header-out-inter2", "inter2");
return response;
}
ENDPOINT_INTERCEPTOR(interceptor, inter3) {
request->putHeader("header-in-inter3", "inter3");
auto response = (this->*intercepted)(request);
response->putHeader("header-out-inter3", "inter3");
return response;
}
ENDPOINT_INTERCEPTOR(interceptor, asserter) {
auto response = (this->*intercepted)(request);
OATPP_ASSERT(response->getHeader("header-out-inter1") == "inter1");
OATPP_ASSERT(response->getHeader("header-out-inter2") == "inter2");
OATPP_ASSERT(response->getHeader("header-out-inter3") == "inter3");
return response;
}
ENDPOINT("GET", "test/interceptors", interceptor,
REQUEST(std::shared_ptr<IncomingRequest>, request))
{
OATPP_ASSERT(request->getHeader("header-in-inter1") == "inter1");
OATPP_ASSERT(request->getHeader("header-in-inter2") == "inter2");
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
return createResponse(Status::CODE_200, "Hello World!!!");
}
#include OATPP_CODEGEN_END(ApiController)
};
}}}}
#endif /* oatpp_test_web_app_ControllerWithInterceptors_hpp */

View File

@ -0,0 +1,160 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#ifndef oatpp_test_web_app_ControllerWithInterceptorsAsync_hpp
#define oatpp_test_web_app_ControllerWithInterceptorsAsync_hpp
#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/core/utils/ConversionUtils.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp"
#include <sstream>
namespace oatpp { namespace test { namespace web { namespace app {
namespace multipart = oatpp::web::mime::multipart;
class ControllerWithInterceptorsAsync : public oatpp::web::server::api::ApiController {
private:
static constexpr const char* TAG = "test::web::app::ControllerWithInterceptorsAsync";
public:
ControllerWithInterceptorsAsync(const std::shared_ptr<ObjectMapper>& objectMapper)
: oatpp::web::server::api::ApiController(objectMapper)
{}
public:
static std::shared_ptr<ControllerWithInterceptorsAsync> createShared(const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)){
return std::make_shared<ControllerWithInterceptorsAsync>(objectMapper);
}
#include OATPP_CODEGEN_BEGIN(ApiController)
ENDPOINT_INTERCEPTOR_ASYNC(Interceptor, inter1) {
/* assert order of interception */
OATPP_ASSERT(request->getHeader("header-in-inter2") == "inter2");
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
/********************************/
request->putHeader("header-in-inter1", "inter1");
return (this->*intercepted)(request);
}
ENDPOINT_INTERCEPTOR_ASYNC(Interceptor, inter2) {
/* assert order of interception */
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
/********************************/
request->putHeader("header-in-inter2", "inter2");
return (this->*intercepted)(request);
}
ENDPOINT_INTERCEPTOR_ASYNC(Interceptor, inter3) {
class IterceptorCoroutine : public oatpp::async::CoroutineWithResult<IterceptorCoroutine, const std::shared_ptr<OutgoingResponse>&> {
private:
ControllerWithInterceptorsAsync* m_this;
Handler<ControllerWithInterceptorsAsync>::MethodAsync m_intercepted;
std::shared_ptr<IncomingRequest> m_request;
public:
IterceptorCoroutine(ControllerWithInterceptorsAsync* _this,
const Handler<ControllerWithInterceptorsAsync>::MethodAsync& intercepted,
const std::shared_ptr<IncomingRequest>& request)
: m_this(_this)
, m_intercepted(intercepted)
, m_request(request)
{}
oatpp::async::Action act() override {
m_request->putHeader("header-in-inter3", "inter3");
return (m_this->*m_intercepted)(m_request).callbackTo(&IterceptorCoroutine::onResponse);
}
oatpp::async::Action onResponse(const std::shared_ptr<OutgoingResponse>& response) {
response->putHeader("header-out-inter3", "inter3");
return this->_return(response);
}
};
return IterceptorCoroutine::startForResult(this, intercepted, request);
}
ENDPOINT_INTERCEPTOR_ASYNC(Interceptor, asserter) {
class IterceptorCoroutine : public oatpp::async::CoroutineWithResult<IterceptorCoroutine, const std::shared_ptr<OutgoingResponse>&> {
private:
ControllerWithInterceptorsAsync* m_this;
Handler<ControllerWithInterceptorsAsync>::MethodAsync m_intercepted;
std::shared_ptr<IncomingRequest> m_request;
public:
IterceptorCoroutine(ControllerWithInterceptorsAsync* _this,
const Handler<ControllerWithInterceptorsAsync>::MethodAsync& intercepted,
const std::shared_ptr<IncomingRequest>& request)
: m_this(_this)
, m_intercepted(intercepted)
, m_request(request)
{}
oatpp::async::Action act() override {
return (m_this->*m_intercepted)(m_request).callbackTo(&IterceptorCoroutine::onResponse);
}
oatpp::async::Action onResponse(const std::shared_ptr<OutgoingResponse>& response) {
OATPP_ASSERT(response->getHeader("header-out-inter3") == "inter3");
return this->_return(response);
}
};
return IterceptorCoroutine::startForResult(this, intercepted, request);
}
ENDPOINT_ASYNC("GET", "test/interceptors", Interceptor) {
ENDPOINT_ASYNC_INIT(Interceptor)
Action act() {
OATPP_ASSERT(request->getHeader("header-in-inter1") == "inter1");
OATPP_ASSERT(request->getHeader("header-in-inter2") == "inter2");
OATPP_ASSERT(request->getHeader("header-in-inter3") == "inter3");
return _return(controller->createResponse(Status::CODE_200, "Hello World Async!!!"));
}
};
#include OATPP_CODEGEN_END(ApiController)
};
}}}}
#endif /* oatpp_test_web_app_ControllerWithInterceptorsAsync_hpp */