RESTinio 0.6.1 Released!
2019.11.13
This version contains a set of helpers those simplify work with HTTP-fields.
There were only tools for checking the presence of HTTP-fields and accessing the raw, unparsed values of those fields in previous versions of RESTinio. Checking the format and the correctness of HTTP-fields values was the task of a user.
Starting from v.0.6.1, RESTinio provides a set of helpers that parses HTTP-fields values. For example, access to the content of the Content-Type field can be obtained that way:
#include <restinio/all.hpp> #include <restinio/helpers/http_field_parsers/content-type.hpp> ... auto on_request(const restinio::request_handle_t & req) { namespace hfp = restinio::http_field_parsers; // Get the content of Content-Type field and parse it. const auto content_type = hfp::content_type_value_t::try_parse( // value_of will throw if there is no Content-Type. req->header().value_of(restinio::http_field::content_type)); if(content_type) { // Now we can access values of Content-Type directly. if("text" == content_type->media_type.type && "plain" == content_type->media_type.subtype) { // It is text/plain content. Try to detect charset for it. const auto charset = hfp::find_first( content_type->media_type.parameters, "charset"); if(charset) { ... // Handling existing charset value. } else { ... // Handling missing charset value. } ... // Some processing. } else if("application" == content_type->media_type.type && "json" == content_type->media_type.subtype) { // It is application/json content. ... // Some processing. } } else { // There is some error with parsing of Content-Type value. ... // Some error handling code. } }
The support for Accept, Cache-Control, Content-Encoding, Contend-Disposition, Content-Type HTTP-fields is implemented that way in v.0.6.1. Helpers for parsing other HTTP-fields are going to be added in the future versions of RESTinio. You can influence the speed of that process, telling us which HTTP-fields you interested in the most.
A set of tools for working with multipart bodies is introduced into RESTinio in v.0.6.1. Thus, the enumerate_parts() function from restinio::multipart_body allows to enumerate all parts of a multipart body:
#include <restinio/all.hpp> #include <restinio/helpers/multipart_body.hpp> ... auto on_post(const restinio::request_handle_t & req) { using namespace restinio::multipart_body; const auto result = enumerate_parts( *req, [](parsed_part_t part) { ... // Some actions with the current part. return handling_result_t::continue_enumeration; }, "multipart", "form-data" ); if(result) { ... // Producing positive response. } else { ... // Producing negative response. } return restinio::request_accepted(); }
One of the purposes of requests with multipart bodies is uploading of files to the server. RESTinio v.0.6.1 provides some tools for simplification of that task (but the implementation of the file upload operation is still a user's responsibility). Helper function enumerate_parts_with_files from restinio::file_upload namespace allows to enumerate all parts of a multipart body where are files uploaded in correspondence with RFC1867:
#include <restinio/all.hpp> #include <restinio/helpers/file_upload.hpp> ... auto on_post(const restinio::request_handle_t & req) { using namespace restinio::file_upload; const auto result = enumerate_parts_with_files( *req, [](part_description_t part) { ... // Some actions with the current uploaded file. return handling_result_t::continue_enumeration; } ); if(result) { ... // Producing positive response. } else { ... // Producing negative response. } return restinio::request_accepted(); }
A new file_upload example is added to the list of standard RESTinio's examples. That example shows the usage of the enumerate_parts_with_files function.
We tried to make all those features convenient for our users. But we understand that it is the very first version of the new tools and, because of that, there could be some flaws in the design and implementation. So we ask all interested users to try the new version of RESTinio and share your impressions with us.
The new version of RESTinio is available in the main repository on github. It also can be obtained via vcpkg and conan dependency management systems.
Doxygen documentation is also available: RESTinio-0.6 API Reference.
If you miss something in RESTinio and want to see some new features let us know via Issues on github or Google-group, or by writing to info@stiffstream.com. You can also order the addition of some specific functionality to RESTinio.