-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathMultipartStreamParser.h
More file actions
90 lines (77 loc) · 1.93 KB
/
MultipartStreamParser.h
File metadata and controls
90 lines (77 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
*
* @file MultipartStreamParser.h
* @author Nitromelon
*
* Copyright 2024, Nitromelon. All rights reserved.
* https://github.com/drogonframework/drogon
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Drogon
*
*/
#pragma once
#include <drogon/exports.h>
#include <drogon/RequestStream.h>
#include <string>
namespace drogon
{
class DROGON_EXPORT MultipartStreamParser
{
public:
enum class ExceptionType
{
kNoException = 0,
kServerCancel = 1,
};
public:
MultipartStreamParser(const std::string &contentType);
void parse(const char *data,
size_t length,
const RequestStreamReader::MultipartHeaderCallback &headerCb,
const RequestStreamReader::StreamDataCallback &dataCb);
bool isFinished() const
{
return isFinished_;
}
bool isValid() const
{
return isValid_;
}
ExceptionType exceptionType() const {
return exception_type_;
}
private:
const std::string dash_ = "--";
const std::string crlf_ = "\r\n";
std::string boundary_;
std::string dashBoundaryCrlf_;
std::string crlfDashBoundary_;
struct Buffer
{
public:
std::string_view view() const;
void append(const char *data, size_t length);
size_t size() const;
void eraseFront(size_t length);
void clear();
private:
std::string buffer_;
size_t bufHead_{0};
size_t bufTail_{0};
} buffer_;
enum class Status
{
kExpectFirstBoundary = 0,
kExpectNewEntry = 1,
kExpectHeader = 2,
kExpectBody = 3,
kExpectEndOrNewEntry = 4,
} status_{Status::kExpectFirstBoundary};
ExceptionType exception_type_{ExceptionType::kNoException};
MultipartHeader currentHeader_;
bool isValid_{true};
bool isFinished_{false};
};
} // namespace drogon