Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
HttpParser.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Foundation/Span.h"
6#include "../Foundation/StringSpan.h"
7
8namespace SC
9{
12
14struct SC_COMPILER_EXPORT HttpParser
15{
17 enum class Method
18 {
19 HttpGET,
20 HttpPUT,
21 HttpPOST,
22 };
23 Method method = Method::HttpGET;
24
25 size_t tokenStart = 0;
26 size_t tokenLength = 0;
27 uint32_t statusCode = 0;
28 uint64_t contentLength = 0;
29 bool connectionKeepAlive = true;
30
32 enum class State
33 {
34 Parsing,
35 Result,
36 Finished,
37 };
38
40 enum class Token
41 {
42 Method,
43 Url,
44 Version,
45 HeaderName,
46 HeaderValue,
47 HeadersEnd,
48 StatusCode,
49 StatusString,
50 Body
51 };
52
53 Token token = Token::HeadersEnd;
54 State state = State::Parsing;
55
57 enum class Type
58 {
59 Request,
60 Response
61 };
62 Type type = Type::Request;
63
69 Result parse(Span<const char> data, size_t& readBytes, Span<const char>& parsedData);
70
72 enum class HeaderType
73 {
74 ContentLength = 0,
75 Connection = 1,
76 ContentType = 2,
77 };
78
81
83 [[nodiscard]] bool matchesHeader(HeaderType headerName) const;
84
85 private:
86 size_t globalStart = 0;
87 size_t globalLength = 0;
88 int topLevelCoroutine = 0;
89 int nestedParserCoroutine = 0;
90 bool parsedContentLength = false;
91 bool parsedConnection = false;
92 size_t matchIndex = 0;
93
94 static constexpr size_t numMatches = 2;
95
96 size_t matchingHeader[numMatches] = {0};
97 bool matchingHeaderValid[numMatches] = {false};
98 uint64_t number = 0;
99
100 [[nodiscard]] bool parseHeaderName(char currentChar);
101 [[nodiscard]] bool parseHeaderValue(char currentChar);
102 [[nodiscard]] bool parseStatusCode(char currentChar);
103 [[nodiscard]] bool parseNumberValue(char currentChar);
104 [[nodiscard]] bool parseConnectionValue(char currentChar);
105 [[nodiscard]] bool parseHeadersEnd(char currentChar);
106 [[nodiscard]] bool parseMethod(char currentChar);
107 [[nodiscard]] bool parseUrl(char currentChar);
108
109 template <bool spaces>
110 [[nodiscard]] bool parseVersion(char currentChar);
111
112 template <bool (HttpParser::*Func)(char), Token currentResult>
113 Result process(Span<const char>& data, size_t& readBytes, Span<const char>& parsedData);
114};
115
117} // namespace SC
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:38
Incremental HTTP request or response parser.
Definition HttpParser.h:15
State
State of the parser.
Definition HttpParser.h:33
Token
One possible Token reported by the parser.
Definition HttpParser.h:41
StringSpan contentTypeValue
Value of Content-Type header.
Definition HttpParser.h:79
HeaderType
Header types.
Definition HttpParser.h:73
Method
Method of the current request / response.
Definition HttpParser.h:18
Result parse(Span< const char > data, size_t &readBytes, Span< const char > &parsedData)
Parse an incoming chunk of bytes, returning actually parsed span.
Type
Type of the stream to be parsed (Request or Response)
Definition HttpParser.h:58
bool matchesHeader(HeaderType headerName) const
Check if current result matches this HeaderType.
StringSpan multipartBoundary
Extracted multipart boundary (if Content-Type is multipart/form-data)
Definition HttpParser.h:80
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:13
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37