Skip to content

Commit

Permalink
Multipart message view (#862)
Browse files Browse the repository at this point in the history
* multipart::message_view
* Make multipart::message_view non-returnable
* Store message headers by reference
  • Loading branch information
bgs99 authored Aug 13, 2024
1 parent 1cf1942 commit 18cfb34
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
endif()

# Set required C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Default to build type "Release" unless tests are being built
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/multipart.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ The structure of a multipart request is typically consistent of:<br>
- `--<boundary>--`<br><br>

## Multipart messages in Crow
Crow supports multipart requests and responses though `crow::multipart::message`.<br>
Crow supports multipart requests and responses though `crow::multipart::message` and `crow::multipart::message_view`, where `crow::multipart::message` owns the contents of the message and `crow::multipart::message_view` stores views into its parts.<br>
A message can be created either by defining the headers, boundary, and individual parts and using them to create the message. or simply by reading a `crow::request`.<br><br>

Once a multipart message has been made, the individual parts can be accessed throughout `msg.parts`, `parts` is an `std::vector`.<br><br>

<span class="tag">[:octicons-feed-tag-16: v1.0](https://github.com/CrowCpp/Crow/releases/v1.0)</span>


Part headers are organized in a similar way to request and response headers, and can be retrieved via `crow::multipart::get_header_object("header-key")`. This function returns a `crow::multipart::header` object.<br><br>
Part headers are organized in a similar way to request and response headers, and can be retrieved via `crow::multipart::get_header_object("header-key")`. This function returns a `crow::multipart::header` object for owning message and `crow::multipart::header_view` for non-owning message.<br><br>

The message's individual body parts can be accessed by name using `msg.get_part_by_name("part-name")`.<br><br>

Expand Down
2 changes: 1 addition & 1 deletion examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ int main()
// Take a multipart/form-data request and print out its body
CROW_ROUTE(app, "/multipart")
([](const crow::request& req) {
crow::multipart::message msg(req);
crow::multipart::message_view msg(req);
CROW_LOG_INFO << "body of the first part " << msg.parts[0].body;
return "it works!";
});
Expand Down
4 changes: 2 additions & 2 deletions examples/example_file_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int main()

CROW_ROUTE(app, "/uploadfile")
.methods(crow::HTTPMethod::Post)([](const crow::request& req) {
crow::multipart::message file_message(req);
crow::multipart::message_view file_message(req);
for (const auto& part : file_message.part_map)
{
const auto& part_name = part.first;
Expand All @@ -27,7 +27,7 @@ int main()
CROW_LOG_ERROR << "Part with name \"InputFile\" should have a file";
return crow::response(400);
}
const std::string outfile_name = params_it->second;
const std::string outfile_name{params_it->second};

for (const auto& part_header : part_value.headers)
{
Expand Down
1 change: 1 addition & 0 deletions include/crow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "crow/parser.h"
#include "crow/http_response.h"
#include "crow/multipart.h"
#include "crow/multipart_view.h"
#include "crow/routing.h"
#include "crow/middleware.h"
#include "crow/middleware_context.h"
Expand Down
6 changes: 4 additions & 2 deletions include/crow/ci_map.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#pragma once

#include <string_view>
#include <locale>
#include <unordered_map>

#include "crow/utility.h"

namespace crow
{
/// Hashing function for ci_map (unordered_multimap).
struct ci_hash
{
size_t operator()(const std::string& key) const
size_t operator()(const std::string_view key) const
{
std::size_t seed = 0;
std::locale locale;
Expand All @@ -31,7 +33,7 @@ namespace crow
/// Equals function for ci_map (unordered_multimap).
struct ci_key_eq
{
bool operator()(const std::string& l, const std::string& r) const
bool operator()(const std::string_view l, const std::string_view r) const
{
return utility::string_equals(l, r);
}
Expand Down
Loading

0 comments on commit 18cfb34

Please sign in to comment.