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#include "HttpExport.h"
8
9namespace SC
10{
13
15struct SC_HTTP_EXPORT HttpParser
16{
18 enum class Method
19 {
20 HttpGET,
21 HttpPUT,
22 HttpPOST,
23 };
24 Method method = Method::HttpGET;
25
26 size_t tokenStart = 0;
27 size_t tokenLength = 0;
28 uint32_t statusCode = 0;
29 uint64_t contentLength = 0;
30 bool connectionKeepAlive = true;
31
33 enum class State
34 {
35 Parsing,
36 Result,
37 Finished,
38 };
39
41 enum class Token
42 {
43 Method,
44 Url,
45 Version,
46 HeaderName,
47 HeaderValue,
48 HeadersEnd,
49 StatusCode,
50 StatusString,
51 Body
52 };
53
54 Token token = Token::HeadersEnd;
55 State state = State::Parsing;
56
58 enum class Type
59 {
60 Request,
61 Response
62 };
63 Type type = Type::Request;
64
70 Result parse(Span<const char> data, size_t& readBytes, Span<const char>& parsedData);
71
73 enum class HeaderType
74 {
75 ContentLength = 0,
76 Connection = 1,
77 ContentType = 2,
78 };
79
81 [[nodiscard]] bool matchesHeader(HeaderType headerName) const;
82
83 private:
84 size_t globalStart = 0;
85 size_t globalLength = 0;
86 int topLevelCoroutine = 0;
87 int nestedParserCoroutine = 0;
88 bool parsedContentLength = false;
89 bool parsedConnection = false;
90 size_t matchIndex = 0;
91
92 static constexpr size_t numMatches = static_cast<size_t>(HeaderType::ContentType) + 1;
93
94 size_t matchingHeader[numMatches] = {0};
95 bool matchingHeaderValid[numMatches] = {false};
96 uint64_t number = 0;
97
98 [[nodiscard]] bool parseHeaderName(char currentChar);
99 [[nodiscard]] bool parseHeaderValue(char currentChar);
100 [[nodiscard]] bool parseStatusCode(char currentChar);
101 [[nodiscard]] bool parseNumberValue(char currentChar);
102 [[nodiscard]] bool parseConnectionValue(char currentChar);
103 [[nodiscard]] bool parseHeadersEnd(char currentChar);
104 [[nodiscard]] bool parseMethod(char currentChar);
105 [[nodiscard]] bool parseUrl(char currentChar);
106
107 template <bool spaces>
108 [[nodiscard]] bool parseVersion(char currentChar);
109
110 template <bool (HttpParser::*Func)(char), Token currentResult>
111 Result process(Span<const char>& data, size_t& readBytes, Span<const char>& parsedData);
112};
113
115} // namespace SC
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:33
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
Incremental HTTP request or response parser.
Definition HttpParser.h:16
State
State of the parser.
Definition HttpParser.h:34
Token
One possible Token reported by the parser.
Definition HttpParser.h:42
HeaderType
Header types.
Definition HttpParser.h:74
Method
Method of the current request / response.
Definition HttpParser.h:19
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:59
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