Sane C++ Libraries
C++ Platform Abstraction Libraries
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{
9struct SC_COMPILER_EXPORT HttpParser;
10} // namespace SC
11
14
17{
19 enum class Method
20 {
21 HttpGET,
22 HttpPUT,
23 HttpPOST,
24 };
26
27 size_t tokenStart = 0;
28 size_t tokenLength = 0;
31
33 enum class State
34 {
35 Parsing,
36 Result,
37 Finished,
38 };
39
41 enum class Result
42 {
43 Method,
44 Url,
45 Version,
51 Body
52 };
53
56
58 enum class Type
59 {
60 Request,
62 };
64
70 [[nodiscard]] SC::Result parse(Span<const char> data, size_t& readBytes, Span<const char>& parsedData);
71
73 enum class HeaderType
74 {
75 ContentLength = 0
76 };
77
79 [[nodiscard]] bool matchesHeader(HeaderType headerName) const;
80
81 private:
82 size_t globalStart = 0;
83 size_t globalLength = 0;
84 int topLevelCoroutine = 0;
85 int nestedParserCoroutine = 0;
86 bool parsedContentLength = false;
87 size_t matchIndex = 0;
88
89 static constexpr size_t numMatches = 1;
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 parseHeadersEnd(char currentChar);
100 [[nodiscard]] bool parseMethod(char currentChar);
101 [[nodiscard]] bool parseUrl(char currentChar);
102
103 template <bool spaces>
104 [[nodiscard]] bool parseVersion(char currentChar);
105
106 template <bool (HttpParser::*Func)(char), Result currentResult>
107 [[nodiscard]] SC::Result process(Span<const char>& data, size_t& readBytes, Span<const char>& parsedData);
108};
109
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
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:17
Type type
Type of http stream (request or response)
Definition: HttpParser.h:63
SC::Result parse(Span< const char > data, size_t &readBytes, Span< const char > &parsedData)
Parse an incoming chunk of bytes, returning actually parsed span.
State
State of the parser.
Definition: HttpParser.h:34
@ Parsing
Parser is parsing.
@ Result
Parser is reporting a result.
@ Finished
Parser has finished.
size_t tokenStart
Offset in bytes to start of parsed token.
Definition: HttpParser.h:27
Method method
Http method.
Definition: HttpParser.h:25
Result
One possible Result reported by the parser.
Definition: HttpParser.h:42
@ Url
Http url has been found.
@ HeaderName
Name of an http header has been found.
@ StatusString
Http status string has been found.
@ Version
Http version number has been found.
@ HeaderValue
Value of an http header has been found.
@ Method
Http method has been found.
@ StatusCode
Http status code has been found.
@ Body
Start of http body has been found.
@ HeadersEnd
Lash http header has been found.
State state
Current state of the parser.
Definition: HttpParser.h:55
Result result
Last found result.
Definition: HttpParser.h:54
HeaderType
Header types.
Definition: HttpParser.h:74
@ ContentLength
Content-Length header.
Method
Method of the current request / response.
Definition: HttpParser.h:20
uint32_t statusCode
Parsed http status code.
Definition: HttpParser.h:29
size_t tokenLength
Length in bytes of parsed token.
Definition: HttpParser.h:28
Type
Type of the stream to be parsed (Request or Response)
Definition: HttpParser.h:59
@ Request
Stream to be parsed is an Http request from a client.
@ Response
Stream to be parsed is an http response from a server.
bool matchesHeader(HeaderType headerName) const
Check if current result matches this HeaderType.
uint64_t contentLength
Content-Length of http request.
Definition: HttpParser.h:30
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20