Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
HttpHeaders.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 HttpHeaderKeyValue
16{
17 StringSpan name;
18 StringSpan value;
19 bool hasValue = false;
20};
21
23struct SC_HTTP_EXPORT HttpCookieIterator
24{
25 explicit HttpCookieIterator(StringSpan cookieHeader);
26
27 bool next(HttpHeaderKeyValue& pair);
28
29 private:
30 StringSpan header;
31 size_t cursor = 0;
32};
33
35struct SC_HTTP_EXPORT HttpSetCookieAttributeIterator
36{
37 explicit HttpSetCookieAttributeIterator(StringSpan attributes);
38
39 bool next(HttpHeaderKeyValue& attribute);
40
41 private:
42 StringSpan attributes;
43 size_t cursor = 0;
44};
45
47struct SC_HTTP_EXPORT HttpSetCookieView
48{
49 StringSpan name;
50 StringSpan value;
51 StringSpan attributes;
52
53 StringSpan path;
54 StringSpan domain;
55 StringSpan expires;
56 StringSpan maxAge;
57 StringSpan sameSite;
58
59 bool hasMaxAge = false;
60 bool secure = false;
61 bool httpOnly = false;
62
63 Result parse(StringSpan setCookieHeader);
64};
65
67struct SC_HTTP_EXPORT HttpSetCookieBuilder
68{
69 StringSpan name;
70 StringSpan value;
71 StringSpan path;
72 StringSpan domain;
73 StringSpan expires;
74 StringSpan maxAge;
75 StringSpan sameSite;
76
77 bool secure = false;
78 bool httpOnly = false;
79
80 Result writeTo(Span<char> storage, StringSpan& output) const;
81};
82
85SC_HTTP_EXPORT StringSpan HttpContentTypeTextHtmlUtf8();
86SC_HTTP_EXPORT StringSpan HttpContentTypeApplicationJson();
87SC_HTTP_EXPORT StringSpan HttpContentTypeApplicationOctetStream();
88
90struct SC_HTTP_EXPORT HttpCacheControlBuilder
91{
92 uint32_t maxAgeSeconds = 0;
93
94 bool hasMaxAge = false;
95 bool noStore = false;
96 bool noCache = false;
97 bool publicCache = false;
98 bool privateCache = false;
99 bool mustRevalidate = false;
100 bool immutable = false;
101
102 Result writeTo(Span<char> storage, StringSpan& output) const;
103};
104
106struct SC_HTTP_EXPORT HttpAuthorizationView
107{
108 StringSpan scheme;
109 StringSpan credentials;
110
111 Result parse(StringSpan authorizationHeader);
112 bool isBearer() const;
113 bool isBasic() const;
114};
115
117SC_HTTP_EXPORT Result HttpParseBearerToken(StringSpan authorizationHeader, StringSpan& token);
118
120SC_HTTP_EXPORT Result HttpParseBasicCredentials(StringSpan authorizationHeader, Span<char> storage,
121 StringSpan& username, StringSpan& password);
122
125
127SC_HTTP_EXPORT Result HttpWriteBasicAuthorization(StringSpan base64Credentials, Span<char> storage, StringSpan& output);
128
130} // namespace SC
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition PrimitiveTypes.h:29
SC_HTTP_EXPORT StringSpan HttpContentTypeTextPlainUtf8()
Common Content-Type header values.
SC_HTTP_EXPORT Result HttpParseBasicCredentials(StringSpan authorizationHeader, Span< char > storage, StringSpan &username, StringSpan &password)
Parses Basic Authorization credentials into caller-provided decoded storage.
SC_HTTP_EXPORT Result HttpWriteBasicAuthorization(StringSpan base64Credentials, Span< char > storage, StringSpan &output)
Writes Basic <base64Credentials> into caller-provided storage.
SC_HTTP_EXPORT Result HttpParseBearerToken(StringSpan authorizationHeader, StringSpan &token)
Parses a Bearer Authorization header and returns the raw token slice.
SC_HTTP_EXPORT Result HttpWriteBearerAuthorization(StringSpan token, Span< char > storage, StringSpan &output)
Writes Bearer <token> into caller-provided storage.
Zero-copy view of an Authorization header split into scheme and credentials.
Definition HttpHeaders.h:107
Writes a Cache-Control header value into caller-provided storage.
Definition HttpHeaders.h:91
Iterates a request Cookie header as semicolon-delimited name/value pairs.
Definition HttpHeaders.h:24
A zero-copy key/value view used by lightweight HTTP header helpers.
Definition HttpHeaders.h:16
Iterates semicolon-delimited Set-Cookie attributes after the first name/value pair.
Definition HttpHeaders.h:36
Writes a Set-Cookie header value into caller-provided storage.
Definition HttpHeaders.h:68
Zero-copy view of one Set-Cookie header value.
Definition HttpHeaders.h:48
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
An read-only view over a string (to avoid including Strings library when parsing is not needed).
Definition StringSpan.h:37