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 HttpHEAD,
24 HttpPATCH,
25 HttpDELETE,
26 HttpOPTIONS,
27 };
28 Method method = Method::HttpGET;
29
30 size_t tokenStart = 0;
31 size_t tokenLength = 0;
32 uint32_t statusCode = 0;
33 uint64_t contentLength = 0;
34 bool connectionKeepAlive = true;
35
37 enum class State
38 {
39 Parsing,
40 Result,
41 Finished,
42 };
43
45 enum class Token
46 {
47 Method,
48 Url,
49 Version,
50 HeaderName,
51 HeaderValue,
52 HeadersEnd,
53 StatusCode,
54 StatusString,
55 Body
56 };
57
58 Token token = Token::HeadersEnd;
59 State state = State::Parsing;
60
62 enum class Type
63 {
64 Request,
65 Response
66 };
67 Type type = Type::Request;
68
74 Result parse(Span<const char> data, size_t& readBytes, Span<const char>& parsedData);
75
77 enum class HeaderType
78 {
79 ContentLength = 0,
80 Connection = 1,
81 ContentType = 2,
82 };
83
85 [[nodiscard]] bool matchesHeader(HeaderType headerName) const;
86
87 private:
88 size_t globalStart = 0;
89 size_t globalLength = 0;
90 int topLevelCoroutine = 0;
91 int nestedParserCoroutine = 0;
92 bool parsedContentLength = false;
93 bool parsedConnection = false;
94 size_t matchIndex = 0;
95
96 static constexpr size_t numMatches = static_cast<size_t>(HeaderType::ContentType) + 1;
97
98 size_t matchingHeader[numMatches] = {0};
99 bool matchingHeaderValid[numMatches] = {false};
100 uint64_t number = 0;
101
102 [[nodiscard]] bool parseHeaderName(char currentChar);
103 [[nodiscard]] bool parseHeaderValue(char currentChar);
104 [[nodiscard]] bool parseStatusCode(char currentChar);
105 [[nodiscard]] bool parseNumberValue(char currentChar);
106 [[nodiscard]] bool parseConnectionValue(char currentChar);
107 [[nodiscard]] bool parseHeadersEnd(char currentChar);
108 [[nodiscard]] bool parseMethod(char currentChar);
109 [[nodiscard]] bool parseUrl(char currentChar);
110
111 template <bool spaces>
112 [[nodiscard]] bool parseVersion(char currentChar);
113
114 template <bool (HttpParser::*Func)(char), Token currentResult>
115 Result process(Span<const char>& data, size_t& readBytes, Span<const char>& parsedData);
116};
117
119} // 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:38
Token
One possible Token reported by the parser.
Definition HttpParser.h:46
HeaderType
Header types.
Definition HttpParser.h:78
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:63
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