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 bool connectionKeepAlive = true;
29
31 enum class State
32 {
33 Parsing,
34 Result,
35 Finished,
36 };
37
39 enum class Token
40 {
41 Method,
42 Url,
43 Version,
44 HeaderName,
45 HeaderValue,
46 HeadersEnd,
47 StatusCode,
48 StatusString,
49 Body
50 };
51
52 Token token = Token::HeadersEnd;
53 State state = State::Parsing;
54
56 enum class Type
57 {
58 Request,
59 Response
60 };
61 Type type = Type::Request;
62
68 Result parse(Span<const char> data, size_t& readBytes, Span<const char>& parsedData);
69
71 enum class HeaderType
72 {
73 ContentLength = 0,
74 Connection = 1
75 };
76
78 [[nodiscard]] bool matchesHeader(HeaderType headerName) const;
79
80 private:
81 size_t globalStart = 0;
82 size_t globalLength = 0;
83 int topLevelCoroutine = 0;
84 int nestedParserCoroutine = 0;
85 bool parsedContentLength = false;
86 bool parsedConnection = false;
87 size_t matchIndex = 0;
88
89 static constexpr size_t numMatches = 2;
90
91 size_t matchingHeader[numMatches] = {0};
92 bool matchingHeaderValid[numMatches] = {false};
93 uint64_t number = 0;
94
95 [[nodiscard]] bool parseHeaderName(char currentChar);
96 [[nodiscard]] bool parseHeaderValue(char currentChar);
97 [[nodiscard]] bool parseStatusCode(char currentChar);
98 [[nodiscard]] bool parseNumberValue(char currentChar);
99 [[nodiscard]] bool parseConnectionValue(char currentChar);
100 [[nodiscard]] bool parseHeadersEnd(char currentChar);
101 [[nodiscard]] bool parseMethod(char currentChar);
102 [[nodiscard]] bool parseUrl(char currentChar);
103
104 template <bool spaces>
105 [[nodiscard]] bool parseVersion(char currentChar);
106
107 template <bool (HttpParser::*Func)(char), Token currentResult>
108 Result process(Span<const char>& data, size_t& readBytes, Span<const char>& parsedData);
109};
110
112} // 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:32
Token
One possible Token reported by the parser.
Definition HttpParser.h:40
HeaderType
Header types.
Definition HttpParser.h:72
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:57
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