Skip to content

Commit 18cfb34

Browse files
authored
Multipart message view (#862)
* multipart::message_view * Make multipart::message_view non-returnable * Store message headers by reference
1 parent 1cf1942 commit 18cfb34

File tree

9 files changed

+391
-9
lines changed

9 files changed

+391
-9
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
1919
endif()
2020

2121
# Set required C++ standard
22-
set(CMAKE_CXX_STANDARD 11)
22+
set(CMAKE_CXX_STANDARD 17)
2323
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
2424

2525
# Default to build type "Release" unless tests are being built

docs/guides/multipart.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ The structure of a multipart request is typically consistent of:<br>
1717
- `--<boundary>--`<br><br>
1818

1919
## Multipart messages in Crow
20-
Crow supports multipart requests and responses though `crow::multipart::message`.<br>
20+
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>
2121
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>
2222

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

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

2727

28-
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>
28+
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>
2929

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

examples/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ int main()
210210
// Take a multipart/form-data request and print out its body
211211
CROW_ROUTE(app, "/multipart")
212212
([](const crow::request& req) {
213-
crow::multipart::message msg(req);
213+
crow::multipart::message_view msg(req);
214214
CROW_LOG_INFO << "body of the first part " << msg.parts[0].body;
215215
return "it works!";
216216
});

examples/example_file_upload.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main()
66

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

3232
for (const auto& part_header : part_value.headers)
3333
{

include/crow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "crow/parser.h"
1717
#include "crow/http_response.h"
1818
#include "crow/multipart.h"
19+
#include "crow/multipart_view.h"
1920
#include "crow/routing.h"
2021
#include "crow/middleware.h"
2122
#include "crow/middleware_context.h"

include/crow/ci_map.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#pragma once
22

3+
#include <string_view>
34
#include <locale>
45
#include <unordered_map>
6+
57
#include "crow/utility.h"
68

79
namespace crow
810
{
911
/// Hashing function for ci_map (unordered_multimap).
1012
struct ci_hash
1113
{
12-
size_t operator()(const std::string& key) const
14+
size_t operator()(const std::string_view key) const
1315
{
1416
std::size_t seed = 0;
1517
std::locale locale;
@@ -31,7 +33,7 @@ namespace crow
3133
/// Equals function for ci_map (unordered_multimap).
3234
struct ci_key_eq
3335
{
34-
bool operator()(const std::string& l, const std::string& r) const
36+
bool operator()(const std::string_view l, const std::string_view r) const
3537
{
3638
return utility::string_equals(l, r);
3739
}

0 commit comments

Comments
 (0)