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
7namespace SC
8{
11
13struct SC_COMPILER_EXPORT HttpParser
14{
16 enum class Method
17 {
18 HttpGET,
19 HttpPUT,
20 HttpPOST,
21 };
22 Method method = Method::HttpGET;
23
24 size_t tokenStart = 0;
25 size_t tokenLength = 0;
26 uint32_t statusCode = 0;
27 uint64_t contentLength = 0;
28
30 enum class State
31 {
32 Parsing,
33 Result,
34 Finished,
35 };
36
38 enum class Token
39 {
40 Method,
41 Url,
42 Version,
43 HeaderName,
44 HeaderValue,
45 HeadersEnd,
46 StatusCode,
47 StatusString,
48 Body
49 };
50
51 Token token = Token::HeadersEnd;
52 State state = State::Parsing;
53
55 enum class Type
56 {
57 Request,
58 Response
59 };
60 Type type = Type::Request;
61
67 Result parse(Span<const char> data, size_t& readBytes, Span<const char>& parsedData);
68
70 enum class HeaderType
71 {
72 ContentLength = 0
73 };
74
76 [[nodiscard]] bool matchesHeader(HeaderType headerName) const;
77
78 private:
79 size_t globalStart = 0;
80 size_t globalLength = 0;
81 int topLevelCoroutine = 0;
82 int nestedParserCoroutine = 0;
83 bool parsedContentLength = false;
84 size_t matchIndex = 0;
85
86 static constexpr size_t numMatches = 1;
87
88 size_t matchingHeader[numMatches] = {0};
89 bool matchingHeaderValid[numMatches] = {false};
90 uint64_t number = 0;
91
92 [[nodiscard]] bool parseHeaderName(char currentChar);
93 [[nodiscard]] bool parseHeaderValue(char currentChar);
94 [[nodiscard]] bool parseStatusCode(char currentChar);
95 [[nodiscard]] bool parseNumberValue(char currentChar);
96 [[nodiscard]] bool parseHeadersEnd(char currentChar);
97 [[nodiscard]] bool parseMethod(char currentChar);
98 [[nodiscard]] bool parseUrl(char currentChar);
99
100 template <bool spaces>
101 [[nodiscard]] bool parseVersion(char currentChar);
102
103 template <bool (HttpParser::*Func)(char), Token currentResult>
104 Result process(Span<const char>& data, size_t& readBytes, Span<const char>& parsedData);
105};
106
108} // 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:14
State
State of the parser.
Definition HttpParser.h:31
Token
One possible Token reported by the parser.
Definition HttpParser.h:39
HeaderType
Header types.
Definition HttpParser.h:71
Method
Method of the current request / response.
Definition HttpParser.h:17
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:56
bool matchesHeader(HeaderType headerName) const
Check if current result matches this HeaderType.
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