Skip to content

Commit

Permalink
Merge pull request #182 from oatpp/http_encoding_decoding_layer
Browse files Browse the repository at this point in the history
Http encoding decoding layer
  • Loading branch information
lganzzzo committed Jan 18, 2020
2 parents f47d84d + ad3dabb commit 8a4fd7e
Show file tree
Hide file tree
Showing 50 changed files with 947 additions and 729 deletions.
15 changes: 7 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,10 @@ add_library(oatpp
oatpp/web/protocol/http/outgoing/Body.hpp
oatpp/web/protocol/http/outgoing/BufferBody.cpp
oatpp/web/protocol/http/outgoing/BufferBody.hpp
oatpp/web/protocol/http/outgoing/ChunkedBody.cpp
oatpp/web/protocol/http/outgoing/ChunkedBody.hpp
oatpp/web/protocol/http/outgoing/ChunkedBufferBody.cpp
oatpp/web/protocol/http/outgoing/ChunkedBufferBody.hpp
oatpp/web/protocol/http/outgoing/CommunicationUtils.cpp
oatpp/web/protocol/http/outgoing/CommunicationUtils.hpp
oatpp/web/protocol/http/outgoing/DtoBody.cpp
oatpp/web/protocol/http/outgoing/DtoBody.hpp
oatpp/web/protocol/http/outgoing/MultipartBody.cpp
oatpp/web/protocol/http/outgoing/MultipartBody.hpp
oatpp/web/protocol/http/outgoing/StreamingBody.cpp
oatpp/web/protocol/http/outgoing/StreamingBody.hpp
oatpp/web/protocol/http/outgoing/Request.cpp
oatpp/web/protocol/http/outgoing/Request.hpp
oatpp/web/protocol/http/outgoing/Response.cpp
Expand All @@ -211,6 +205,11 @@ add_library(oatpp
oatpp/web/protocol/http/outgoing/ResponseFactory.hpp
oatpp/web/protocol/http/encoding/Chunked.cpp
oatpp/web/protocol/http/encoding/Chunked.hpp
oatpp/web/protocol/http/encoding/ProviderCollection.cpp
oatpp/web/protocol/http/encoding/ProviderCollection.hpp
oatpp/web/protocol/http/encoding/EncoderProvider.hpp
oatpp/web/protocol/http/utils/CommunicationUtils.cpp
oatpp/web/protocol/http/utils/CommunicationUtils.hpp
oatpp/web/server/AsyncHttpConnectionHandler.cpp
oatpp/web/server/AsyncHttpConnectionHandler.hpp
oatpp/web/server/HttpConnectionHandler.cpp
Expand Down
5 changes: 4 additions & 1 deletion src/oatpp/codegen/codegen_define_ApiClient_.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ __body = OATPP_MACRO_FIRSTARG PARAM_LIST;
// BODY_DTO MACRO

#define OATPP_MACRO_API_CLIENT_BODY_DTO(TYPE, PARAM_LIST) \
__body = oatpp::web::protocol::http::outgoing::DtoBody::createShared(OATPP_MACRO_FIRSTARG PARAM_LIST, m_objectMapper.get());
__body = oatpp::web::protocol::http::outgoing::BufferBody::createShared( \
m_objectMapper->writeToString(OATPP_MACRO_FIRSTARG PARAM_LIST), \
m_objectMapper->getInfo().http_content_type \
);

// BODY_STRING MACRO

Expand Down
19 changes: 17 additions & 2 deletions src/oatpp/core/data/share/MemoryLabel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class MemoryLabel {
, m_size(0)
{}

/**
* nullptr constructor.
*/
MemoryLabel(std::nullptr_t)
: m_memoryHandle(nullptr)
, m_data(nullptr)
, m_size(0)
{}

/**
* Constructor.
* @param str
Expand Down Expand Up @@ -150,6 +159,8 @@ class StringKeyLabel : public MemoryLabel {
public:

StringKeyLabel() : MemoryLabel() {};

StringKeyLabel(std::nullptr_t) : MemoryLabel() {}

StringKeyLabel(const std::shared_ptr<base::StrBuffer>& memHandle, p_char8 data, v_buff_size size);
StringKeyLabel(const char* constText);
Expand All @@ -172,7 +183,9 @@ class StringKeyLabelCI : public MemoryLabel {
public:

StringKeyLabelCI() : MemoryLabel() {};


StringKeyLabelCI(std::nullptr_t) : MemoryLabel() {}

StringKeyLabelCI(const std::shared_ptr<base::StrBuffer>& memHandle, p_char8 data, v_buff_size size);
StringKeyLabelCI(const char* constText);
StringKeyLabelCI(const oatpp::String& str);
Expand All @@ -194,7 +207,9 @@ class StringKeyLabelCI : public MemoryLabel {
*/
class StringKeyLabelCI_FAST : public MemoryLabel {
public:


StringKeyLabelCI_FAST(std::nullptr_t) : MemoryLabel() {}

StringKeyLabelCI_FAST(const std::shared_ptr<base::StrBuffer>& memHandle, p_char8 data, v_buff_size size);
StringKeyLabelCI_FAST(const char* constText);
StringKeyLabelCI_FAST(const oatpp::String& str);
Expand Down
6 changes: 3 additions & 3 deletions src/oatpp/core/data/stream/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,9 @@ v_io_size transfer(const base::ObjectHandle<ReadCallback>& readCallback,
v_int32 procRes = data::buffer::Processor::Error::PROVIDE_DATA_IN;
v_io_size progress = 0;

while(transferSize == 0 || progress < transferSize) {
while(procRes != data::buffer::Processor::Error::FINISHED) {

if(procRes == data::buffer::Processor::Error::PROVIDE_DATA_IN) {
if(procRes == data::buffer::Processor::Error::PROVIDE_DATA_IN && inData.bytesLeft == 0) {

v_buff_size desiredToRead = processor->suggestInputStreamReadSize();

Expand Down Expand Up @@ -638,7 +638,7 @@ async::CoroutineStarter transferAsync(const base::ObjectHandle<ReadCallback>& re

Action act() override {

if(m_transferSize != 0 && m_progress >= m_transferSize) {
if(m_procRes == data::buffer::Processor::Error::FINISHED) {
return finish();
}

Expand Down
1 change: 0 additions & 1 deletion src/oatpp/web/client/ApiClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

#include "oatpp/web/protocol/http/incoming/Response.hpp"

#include "oatpp/web/protocol/http/outgoing/DtoBody.hpp"
#include "oatpp/web/protocol/http/outgoing/BufferBody.hpp"

#include "oatpp/encoding/Base64.hpp"
Expand Down
2 changes: 2 additions & 0 deletions src/oatpp/web/protocol/http/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ 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 Header::ACCEPT_ENCODING = "Accept-Encoding";

const char* const Range::UNIT_BYTES = "bytes";
const char* const ContentRange::UNIT_BYTES = "bytes";

Expand Down
1 change: 1 addition & 0 deletions src/oatpp/web/protocol/http/Http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ class Header {
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
static const char* const ACCEPT_ENCODING; // Accept-Encoding
};

class Range {
Expand Down
21 changes: 21 additions & 0 deletions src/oatpp/web/protocol/http/encoding/Chunked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,26 @@ v_int32 DecoderChunked::iterate(data::buffer::InlineReadData& dataIn, data::buff

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ChunkedEncoderProvider

oatpp::String ChunkedEncoderProvider::getEncodingName() {
return "chunked";
}

std::shared_ptr<data::buffer::Processor> ChunkedEncoderProvider::getProcessor() {
return std::make_shared<EncoderChunked>();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ChunkedDecoderProvider

oatpp::String ChunkedDecoderProvider::getEncodingName() {
return "chunked";
}

std::shared_ptr<data::buffer::Processor> ChunkedDecoderProvider::getProcessor() {
return std::make_shared<DecoderChunked>();
}

}}}}}
42 changes: 41 additions & 1 deletion src/oatpp/web/protocol/http/encoding/Chunked.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#ifndef oatpp_web_protocol_http_encoding_Chunked_hpp
#define oatpp_web_protocol_http_encoding_Chunked_hpp

#include "EncoderProvider.hpp"
#include "oatpp/core/data/stream/BufferStream.hpp"
#include "oatpp/core/data/buffer/Processor.hpp"

namespace oatpp { namespace web { namespace protocol { namespace http { namespace encoding {

Expand Down Expand Up @@ -99,6 +99,46 @@ class DecoderChunked : public data::buffer::Processor {

};

/**
* EncoderProvider for "chunked" encoding.
*/
class ChunkedEncoderProvider : public EncoderProvider {
public:

/**
* Get encoding name.
* @return
*/
oatpp::String getEncodingName() override;

/**
* Get &id:oatpp::data::buffer::Processor; for chunked encoding.
* @return - &id:oatpp::data::buffer::Processor;
*/
std::shared_ptr<data::buffer::Processor> getProcessor() override;

};

/**
* EncoderProvider for "chunked" decoding.
*/
class ChunkedDecoderProvider : public EncoderProvider {
public:

/**
* Get encoding name.
* @return
*/
oatpp::String getEncodingName() override;

/**
* Get &id:oatpp::data::buffer::Processor; for chunked decoding.
* @return - &id:oatpp::data::buffer::Processor;
*/
std::shared_ptr<data::buffer::Processor> getProcessor() override;

};

}}}}}

#endif //oatpp_web_protocol_http_encoding_Chunked_hpp
60 changes: 60 additions & 0 deletions src/oatpp/web/protocol/http/encoding/EncoderProvider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/***************************************************************************
*
* 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_web_protocol_http_encoding_EncoderProvider_hpp
#define oatpp_web_protocol_http_encoding_EncoderProvider_hpp

#include "oatpp/core/data/buffer/Processor.hpp"
#include "oatpp/core/Types.hpp"

namespace oatpp { namespace web { namespace protocol { namespace http { namespace encoding {

/**
* Provider of encoding or decoding &id:oatpp::data::buffer::Processor;.
*/
class EncoderProvider {
public:

/**
* Default virtual destructor.
*/
virtual ~EncoderProvider() = default;

/**
* Get name of the encoding. This name is used in HTTP headers.
* @return - name of the encoding. Ex.: "gzip" or "deflate" or "chunked".
*/
virtual oatpp::String getEncodingName() = 0;

/**
* Get &id:oatpp::data::buffer::Processor; for decoding/encoding.
* @return - &id:oatpp::data::buffer::Processor;
*/
virtual std::shared_ptr<data::buffer::Processor> getProcessor() = 0;

};

}}}}}

#endif // oatpp_web_protocol_http_encoding_EncoderProvider_hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,33 @@
*
***************************************************************************/

#include "./DtoBody.hpp"
#include "ProviderCollection.hpp"

namespace oatpp { namespace web { namespace protocol { namespace http { namespace outgoing {
namespace oatpp { namespace web { namespace protocol { namespace http { namespace encoding {

DtoBody::DtoBody(const oatpp::data::mapping::type::AbstractObjectWrapper& dto,
oatpp::data::mapping::ObjectMapper* objectMapper)
: ChunkedBufferBody(oatpp::data::stream::ChunkedBuffer::createShared())
, m_dto(dto)
, m_objectMapper(objectMapper)
{}

std::shared_ptr<DtoBody> DtoBody::createShared(const oatpp::data::mapping::type::AbstractObjectWrapper& dto,
oatpp::data::mapping::ObjectMapper* objectMapper) {
return Shared_Http_Outgoing_DtoBody_Pool::allocateShared(dto, objectMapper);
void ProviderCollection::add(const std::shared_ptr<EncoderProvider>& provider) {
m_providers[provider->getEncodingName()] = provider;
}

void DtoBody::declareHeaders(Headers& headers) {
if(m_dto) {
m_objectMapper->write(m_buffer, m_dto);
std::shared_ptr<EncoderProvider> ProviderCollection::get(const data::share::StringKeyLabelCI& encoding) const {
auto it = m_providers.find(encoding);
if(it != m_providers.end()) {
return it->second;
}
ChunkedBufferBody::declareHeaders(headers);
headers.putIfNotExists_LockFree(Header::CONTENT_TYPE, m_objectMapper->getInfo().http_content_type);
return nullptr;
}

v_buff_size DtoBody::getKnownSize() {
return m_buffer->getSize();
std::shared_ptr<EncoderProvider> ProviderCollection::get(const std::unordered_set<data::share::StringKeyLabelCI>& encodings) const {

for(const auto& encoding : encodings) {
auto provider = get(encoding);
if(provider) {
return provider;
}
}

return nullptr;

}

}}}}}
}}}}}

0 comments on commit 8a4fd7e

Please sign in to comment.