AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
form.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-rest-common-export.hpp"
23
24#include <filesystem>
25#include <istream>
26#include <memory>
27#include <optional>
28#include <string>
29#include <string_view>
30#include <vector>
31
32#include "codec/mediaType.hpp"
33
34#include "rest/headers.hpp"
35
36namespace anch::rest {
37
46 struct ANCH_REST_COMMON_EXPORT FormLimits {
48 std::size_t maxSize;
49
51 std::size_t maxParts;
52
54 std::size_t maxPartSize;
55
57 std::size_t maxHeadersSize;
58
61 std::size_t maxMemoryPartSize;
62
64 std::filesystem::path temporaryDirectory;
65 };
66
79 class ANCH_REST_COMMON_EXPORT Part {
80
82 struct FileContent;
83
84 // Attributes +
85 private:
87 std::string _name;
88
90 std::optional<std::string> _filename;
91
93 anch::codec::MediaType _mediaType;
94
96 anch::rest::Headers _headers;
97
99 mutable std::string _content;
100
102 std::shared_ptr<const FileContent> _file;
103
105 mutable bool _loaded;
106 // Attributes -
107
108 // Constructors +
109 public:
115
122 Part(std::string_view name, std::string content);
123
132 Part(std::string_view name, std::string_view filename, anch::codec::MediaType mediaType, std::string content);
133
146 static Part fromFile(std::string_view name, const std::filesystem::path& path, anch::codec::MediaType mediaType,
147 std::optional<std::string_view> filename = std::nullopt);
148
161 static Part fromTemporaryFile(std::string_view name, const std::filesystem::path& path, anch::codec::MediaType mediaType,
162 std::optional<std::string_view> filename);
163 // Constructors -
164
165 // Methods +
166 public:
174 std::string_view content() const;
175
181 std::unique_ptr<std::istream> open() const;
182
188 std::size_t size() const noexcept;
189
195 bool isFile() const noexcept;
196
202 bool isInMemory() const noexcept;
203
209 std::optional<std::filesystem::path> getPath() const;
210
218 bool operator==(const Part& other) const;
219 // Methods -
220
221 // Accessors +
222 public:
228 const std::string& getName() const noexcept;
229
235 void setName(std::string_view name);
236
242 std::optional<std::string_view> getFilename() const noexcept;
243
249 const anch::codec::MediaType& getMediaType() const noexcept;
250
256 void setMediaType(anch::codec::MediaType mediaType);
257
265
271 const anch::rest::Headers& getHeaders() const noexcept;
272 // Accessors -
273
274 };
275
287 class ANCH_REST_COMMON_EXPORT Form {
288
289 // Attributes +
290 private:
292 std::vector<anch::rest::Part> _parts;
293 // Attributes -
294
295 // Constructors +
296 public:
301 // Constructors -
302
303 // Methods +
304 public:
313 Form& add(std::string_view name, std::string value);
314
325 Form& addFile(std::string_view name, std::string_view filename, anch::codec::MediaType mediaType, std::string content);
326
335
343 bool has(std::string_view name) const noexcept;
344
352 std::optional<std::string_view> field(std::string_view name) const;
353
361 std::vector<std::string_view> fields(std::string_view name) const;
362
370 const anch::rest::Part* part(std::string_view name) const noexcept;
371
379 std::vector<const anch::rest::Part*> parts(std::string_view name) const;
380
386 std::size_t size() const noexcept;
387
393 bool empty() const noexcept;
394
400 std::vector<anch::rest::Part>::const_iterator begin() const noexcept;
401
407 std::vector<anch::rest::Part>::const_iterator end() const noexcept;
408 // Methods -
409
410 // Accessors +
411 public:
417 const std::vector<anch::rest::Part>& getParts() const noexcept;
418 // Accessors -
419
420 };
421
422}
Definition mediaType.hpp:41
Form & add(anch::rest::Part part)
std::optional< std::string_view > field(std::string_view name) const
std::vector< const anch::rest::Part * > parts(std::string_view name) const
const std::vector< anch::rest::Part > & getParts() const noexcept
std::size_t size() const noexcept
std::vector< anch::rest::Part >::const_iterator end() const noexcept
std::vector< anch::rest::Part >::const_iterator begin() const noexcept
Form & add(std::string_view name, std::string value)
const anch::rest::Part * part(std::string_view name) const noexcept
bool empty() const noexcept
bool has(std::string_view name) const noexcept
std::vector< std::string_view > fields(std::string_view name) const
Form & addFile(std::string_view name, std::string_view filename, anch::codec::MediaType mediaType, std::string content)
Definition headers.hpp:62
Definition form.hpp:79
void setName(std::string_view name)
static Part fromFile(std::string_view name, const std::filesystem::path &path, anch::codec::MediaType mediaType, std::optional< std::string_view > filename=std::nullopt)
std::optional< std::filesystem::path > getPath() const
anch::rest::Headers & getHeaders() noexcept
void setMediaType(anch::codec::MediaType mediaType)
std::size_t size() const noexcept
std::unique_ptr< std::istream > open() const
static Part fromTemporaryFile(std::string_view name, const std::filesystem::path &path, anch::codec::MediaType mediaType, std::optional< std::string_view > filename)
bool isFile() const noexcept
bool isInMemory() const noexcept
const anch::codec::MediaType & getMediaType() const noexcept
const std::string & getName() const noexcept
Part(std::string_view name, std::string content)
Part(std::string_view name, std::string_view filename, anch::codec::MediaType mediaType, std::string content)
std::string_view content() const
std::optional< std::string_view > getFilename() const noexcept
AnCH codec library namespace.
Definition csv.hpp:31
AnCH REST library namespace.
Definition auth.hpp:29
AnCH framework base namespace.
Definition app.hpp:30
Definition form.hpp:46
std::size_t maxParts
Definition form.hpp:51
std::size_t maxPartSize
Definition form.hpp:54
std::size_t maxSize
Definition form.hpp:48
std::size_t maxMemoryPartSize
Definition form.hpp:61
std::size_t maxHeadersSize
Definition form.hpp:57
std::filesystem::path temporaryDirectory
Definition form.hpp:64