Giter VIP home page Giter VIP logo

Comments (8)

bamkrs avatar bamkrs commented on May 18, 2024 1

Wouldn't a call like

#include "oatpp-zlib.hpp" // Unimplemented module.

...


ENDPOINT("GET", "/zip", zip)
{

  auto content = "huge data";
  return createZippedResponse(Status::CODE_200, content);
}

Provided as a convenience function by oatpp-zlib more intuitive? Less prone to error by users.

However, having the abstraction layer to intercept and iE decompress the data sounds great.

from oatpp.

lganzzzo avatar lganzzzo commented on May 18, 2024 1

Hey!

The full support for Content-Encoding and Accept-Encoding headers is added.- commit 8a4fd7e .

The additional data-processing layer was introduced, so No changes should be done for user endpoints.

The API

In the AppComponents file:

#include "oatpp-zlib/EncoderProvider.hpp"

...

  OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {

    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component

    /* Create HttpProcessor::Components */
    auto components = std::make_shared<oatpp::web::server::HttpProcessor::Components>(router);

    /* Add content encoders */
    auto encoders = std::make_shared<oatpp::web::protocol::http::encoding::ProviderCollection>();

    encoders->add(std::make_shared<oatpp::zlib::DeflateEncoderProvider>());
    encoders->add(std::make_shared<oatpp::zlib::GzipEncoderProvider>());

    components->contentEncodingProviders = encoders;

    /* Add content decoders */
    auto decoders = std::make_shared<oatpp::web::protocol::http::encoding::ProviderCollection>();

    decoders->add(std::make_shared<oatpp::zlib::DeflateDecoderProvider>());
    decoders->add(std::make_shared<oatpp::zlib::GzipDecoderProvider>());

    components->bodyDecoder = std::make_shared<oatpp::web::protocol::http::incoming::SimpleBodyDecoder>(decoders);

    /* return HttpConnectionHandler */
    return std::make_shared<oatpp::web::server::HttpConnectionHandler>(components);
    
  }());

...

Cheers,
Leonid

from oatpp.

bamkrs avatar bamkrs commented on May 18, 2024 1

Great work as always Leonid!

from oatpp.

lganzzzo avatar lganzzzo commented on May 18, 2024

Hello @jfixemer ,

Brief

Currently, in order to add support for Content-Encoding header on the framework level (so that the user gets it almost for free), we have to add a new abstraction layer for data processing, plus the dependency on the zlib in the main oatpp module.
While adding an abstraction layer to make Content-Encoding feature pluggable may be a good idea. zlib should not go as a dependency of the main oatpp module. Instead, it should be added as an oatpp module ex. oatpp-zlib (as you've mentioned).

Effort

  • Creating a new convenience abstraction layer in oatpp to make Content-Encoding feature pluggable is a matter of discussion and investigation.
  • Creating a C++ wrapper for zlib should be pretty simple and straightforward. It depends on the functionality that should be covered.

Workaround

Let's say that we don't have the convenience abstraction layer, but we do have the oatpp-zlib module (or whatever convenience API over zlib).
Then the endpoint code could look something like:

#include "oatpp-zlib.hpp" // Unimplemented module.

...

/* It is a hypothetical code. NOT a real example. */
ENDPOINT("GET", "/huge-document", getHugeDocument) {
  auto compressedData = oatpp::zlib::compress("my-huge-data", "gzip");
  auto response = createResponse(Status::CODE_200, compressedData);
  response->putHeader("Content-Encoding", "gzip");
  return response;
}

So this is the current situation.
Please let me know if you have any questions.

Regards,
Leonid

from oatpp.

jfixemer avatar jfixemer commented on May 18, 2024

Thanks for the hints and feedback!

from oatpp.

lganzzzo avatar lganzzzo commented on May 18, 2024

so for receiving data: ...

Yes, you are correct.

Or the same but with ifs moved to decompress method:

#include "oatpp-zlib.hpp" // Unimplemented module.

...


ENDPOINT("POST", "/data", setData,
         HEADER(String, encoding, "Content-Encoding"),
         BODY_STRING(String, input))
{

  auto content = oatpp::zlib::decompress(imput->toLowerCase(), encoding); // hypothetical call
  // TODO - process content. Ex.: map content to DTO with ObjectMapper.
  return createResponse(Status::CODE_200, "OK");
}

I guess the big downside is that if I wanted to use DTO mapping to parse JSON then I have to do so explicitly after the decompression (I can't use the BODY_DTO(...))

Yes, you are right. Unfortunately, in this case, you won't be able to use BODY_DTO.

Regards,
Leonid

from oatpp.

lganzzzo avatar lganzzzo commented on May 18, 2024

Thanks!

from oatpp.

jfixemer avatar jfixemer commented on May 18, 2024

Excellent!!

Much better API than the workaround.

Thanks

from oatpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.