WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
HttpParser.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <string_view>
10
12#include "wpi/util/Signal.h"
14
15namespace wpi::net {
16
17/**
18 * HTTP protocol parser. Performs incremental parsing with callbacks for each
19 * part of the HTTP protocol. As this is incremental, it's suitable for use
20 * with event based frameworks that provide arbitrary chunks of data.
21 */
23 public:
29
30 /**
31 * Returns the library version. Bits 16-23 contain the major version number,
32 * bits 8-15 the minor version number and bits 0-7 the patch level.
33 */
34 static uint32_t GetParserVersion();
35
36 /**
37 * Constructor.
38 * @param type Type of parser (request or response or both)
39 */
40 explicit HttpParser(Type type);
41
42 /**
43 * Reset the parser to initial state.
44 * This allows reusing the same parser object from request to request.
45 * @param type Type of parser (request or response or both)
46 */
47 void Reset(Type type);
48
49 /**
50 * Set the maximum accepted length for URLs, field names, and field values.
51 * The default is 1024.
52 * @param len maximum length
53 */
54 void SetMaxLength(size_t len) { m_maxLength = len; }
55
56 /**
57 * Executes the parser. An empty input is treated as EOF.
58 * @param in input data
59 * @return Trailing input data after the parse.
60 */
61 std::string_view Execute(std::string_view in) {
62 in.remove_prefix(
63 http_parser_execute(&m_parser, &m_settings, in.data(), in.size()));
64 return in;
65 }
66
67 /**
68 * Get HTTP major version.
69 */
70 unsigned int GetMajor() const { return m_parser.http_major; }
71
72 /**
73 * Get HTTP minor version.
74 */
75 unsigned int GetMinor() const { return m_parser.http_minor; }
76
77 /**
78 * Get HTTP status code. Valid only on responses. Valid in and after
79 * the OnStatus() callback has been called.
80 */
81 unsigned int GetStatusCode() const { return m_parser.status_code; }
82
83 /**
84 * Get HTTP method. Valid only on requests.
85 */
87 return static_cast<http_method>(m_parser.method);
88 }
89
90 /**
91 * Determine if an error occurred.
92 * @return False if no error.
93 */
94 bool HasError() const { return m_parser.http_errno != HPE_OK; }
95
96 /**
97 * Get error number.
98 */
100 return static_cast<http_errno>(m_parser.http_errno);
101 }
102
103 /**
104 * Abort the parse. Call this from a callback handler to indicate an error.
105 * This will result in GetError() returning one of the callback-related
106 * errors (e.g. HPE_CB_message_begin).
107 */
108 void Abort() { m_aborted = true; }
109
110 /**
111 * Determine if an upgrade header was present and the parser has exited
112 * because of that. Should be checked when Execute() returns in addition to
113 * checking GetError().
114 * @return True if upgrade header, false otherwise.
115 */
116 bool IsUpgrade() const { return m_parser.upgrade; }
117
118 /**
119 * If this returns false in the headersComplete or messageComplete
120 * callback, then this should be the last message on the connection.
121 * If you are the server, respond with the "Connection: close" header.
122 * If you are the client, close the connection.
123 */
124 bool ShouldKeepAlive() const { return http_should_keep_alive(&m_parser); }
125
126 /**
127 * Pause the parser.
128 * @param paused True to pause, false to unpause.
129 */
130 void Pause(bool paused) { http_parser_pause(&m_parser, paused); }
131
132 /**
133 * Checks if this is the final chunk of the body.
134 */
135 bool IsBodyFinal() const { return http_body_is_final(&m_parser); }
136
137 /**
138 * Get URL. Valid in and after the url callback has been called.
139 */
140 std::string_view GetUrl() const { return m_urlBuf.str(); }
141
142 /**
143 * Message begin callback.
144 */
146
147 /**
148 * URL callback.
149 *
150 * The parameter to the callback is the complete URL string.
151 */
153
154 /**
155 * Status callback.
156 *
157 * The parameter to the callback is the complete status string.
158 * GetStatusCode() can be used to get the numeric status code.
159 */
161
162 /**
163 * Header field callback.
164 *
165 * The parameters to the callback are the field name and field value.
166 */
168
169 /**
170 * Headers complete callback.
171 *
172 * The parameter to the callback is whether the connection should be kept
173 * alive. If this is false, then this should be the last message on the
174 * connection. If you are the server, respond with the "Connection: close"
175 * header. If you are the client, close the connection.
176 */
178
179 /**
180 * Body data callback.
181 *
182 * The parameters to the callback is the data chunk and whether this is the
183 * final chunk of data in the message. Note this callback will be called
184 * multiple times arbitrarily (e.g. it's possible that it may be called with
185 * just a few characters at a time).
186 */
188
189 /**
190 * Headers complete callback.
191 *
192 * The parameter to the callback is whether the connection should be kept
193 * alive. If this is false, then this should be the last message on the
194 * connection. If you are the server, respond with the "Connection: close"
195 * header. If you are the client, close the connection.
196 */
198
199 /**
200 * Chunk header callback.
201 *
202 * The parameter to the callback is the chunk size.
203 */
205
206 /**
207 * Chunk complete callback.
208 */
210
211 private:
212 http_parser m_parser;
213 http_parser_settings m_settings;
214
215 size_t m_maxLength = 1024;
216 enum { kStart, kUrl, kStatus, kField, kValue } m_state = kStart;
220
221 bool m_aborted = false;
222};
223
224} // namespace wpi::net
This file defines the SmallString class.
bool HasError() const
Determine if an error occurred.
Definition HttpParser.hpp:94
Type
Definition HttpParser.hpp:24
@ kBoth
Definition HttpParser.hpp:27
@ kRequest
Definition HttpParser.hpp:25
@ kResponse
Definition HttpParser.hpp:26
std::string_view Execute(std::string_view in)
Executes the parser.
Definition HttpParser.hpp:61
wpi::util::sig::Signal< std::string_view, bool > body
Body data callback.
Definition HttpParser.hpp:187
wpi::util::sig::Signal messageBegin
Message begin callback.
Definition HttpParser.hpp:145
wpi::util::sig::Signal< uint64_t > chunkHeader
Chunk header callback.
Definition HttpParser.hpp:204
static uint32_t GetParserVersion()
Returns the library version.
void Pause(bool paused)
Pause the parser.
Definition HttpParser.hpp:130
wpi::util::sig::Signal< std::string_view, std::string_view > header
Header field callback.
Definition HttpParser.hpp:167
bool IsUpgrade() const
Determine if an upgrade header was present and the parser has exited because of that.
Definition HttpParser.hpp:116
wpi::util::sig::Signal< bool > headersComplete
Headers complete callback.
Definition HttpParser.hpp:177
std::string_view GetUrl() const
Get URL.
Definition HttpParser.hpp:140
bool ShouldKeepAlive() const
If this returns false in the headersComplete or messageComplete callback, then this should be the las...
Definition HttpParser.hpp:124
void Abort()
Abort the parse.
Definition HttpParser.hpp:108
void Reset(Type type)
Reset the parser to initial state.
unsigned int GetMajor() const
Get HTTP major version.
Definition HttpParser.hpp:70
HttpParser(Type type)
Constructor.
bool IsBodyFinal() const
Checks if this is the final chunk of the body.
Definition HttpParser.hpp:135
wpi::util::sig::Signal< std::string_view > status
Status callback.
Definition HttpParser.hpp:160
unsigned int GetStatusCode() const
Get HTTP status code.
Definition HttpParser.hpp:81
http_method GetMethod() const
Get HTTP method.
Definition HttpParser.hpp:86
wpi::util::sig::Signal chunkComplete
Chunk complete callback.
Definition HttpParser.hpp:209
wpi::util::sig::Signal< std::string_view > url
URL callback.
Definition HttpParser.hpp:152
void SetMaxLength(size_t len)
Set the maximum accepted length for URLs, field names, and field values.
Definition HttpParser.hpp:54
http_errno GetError() const
Get error number.
Definition HttpParser.hpp:99
wpi::util::sig::Signal< bool > messageComplete
Headers complete callback.
Definition HttpParser.hpp:197
unsigned int GetMinor() const
Get HTTP minor version.
Definition HttpParser.hpp:75
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.hpp:27
Definition raw_socket_ostream.hpp:9
size_t http_parser_execute(http_parser *parser, const http_parser_settings *settings, const char *data, size_t len)
http_method
Definition http_parser.hpp:194
http_errno
Definition http_parser.hpp:269
int http_should_keep_alive(const http_parser *parser)
int http_body_is_final(const http_parser *parser)
void http_parser_pause(http_parser *parser, int paused)
@ HTTP_RESPONSE
Definition http_parser.hpp:201
@ HTTP_REQUEST
Definition http_parser.hpp:201
@ HTTP_BOTH
Definition http_parser.hpp:201
SignalBase< detail::NullMutex, T... > Signal
Specialization of SignalBase to be used in single threaded contexts.
Definition Signal.h:809
Definition http_parser.hpp:310
Definition http_parser.hpp:279