WPILibC++  2018.4.1-20180728210220-1136-g75a6720
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
HttpParser.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_HTTPPARSER_H_
9 #define WPIUTIL_WPI_HTTPPARSER_H_
10 
11 #include <stdint.h>
12 
13 #include "wpi/Signal.h"
14 #include "wpi/SmallString.h"
15 #include "wpi/StringRef.h"
16 #include "wpi/http_parser.h"
17 
18 namespace wpi {
19 
25 class HttpParser {
26  public:
27  enum Type {
28  kRequest = HTTP_REQUEST,
29  kResponse = HTTP_RESPONSE,
30  kBoth = HTTP_BOTH
31  };
32 
37  static uint32_t GetParserVersion();
38 
43  explicit HttpParser(Type type);
44 
50  void Reset(Type type);
51 
57  void SetMaxLength(size_t len) { m_maxLength = len; }
58 
65  return in.drop_front(
66  http_parser_execute(&m_parser, &m_settings, in.data(), in.size()));
67  }
68 
72  unsigned int GetMajor() const { return m_parser.http_major; }
73 
77  unsigned int GetMinor() const { return m_parser.http_minor; }
78 
83  unsigned int GetStatusCode() const { return m_parser.status_code; }
84 
88  http_method GetMethod() const {
89  return static_cast<http_method>(m_parser.method);
90  }
91 
96  bool HasError() const { return m_parser.http_errno != HPE_OK; }
97 
101  http_errno GetError() const {
102  return static_cast<http_errno>(m_parser.http_errno);
103  }
104 
110  void Abort() { m_aborted = true; }
111 
118  bool IsUpgrade() const { return m_parser.upgrade; }
119 
126  bool ShouldKeepAlive() const { return http_should_keep_alive(&m_parser); }
127 
132  void Pause(bool paused) { http_parser_pause(&m_parser, paused); }
133 
137  bool IsBodyFinal() const { return http_body_is_final(&m_parser); }
138 
142  StringRef GetUrl() const { return m_urlBuf; }
143 
148 
155 
163 
170 
180 
190 
200 
207 
212 
213  private:
214  http_parser m_parser;
215  http_parser_settings m_settings;
216 
217  size_t m_maxLength = 1024;
218  enum { kStart, kUrl, kStatus, kField, kValue } m_state = kStart;
219  SmallString<128> m_urlBuf;
220  SmallString<32> m_fieldBuf;
221  SmallString<128> m_valueBuf;
222 
223  bool m_aborted = false;
224 };
225 
226 } // namespace wpi
227 
228 #endif // WPIUTIL_WPI_HTTPPARSER_H_
unsigned int GetStatusCode() const
Get HTTP status code.
Definition: HttpParser.h:83
Definition: http_parser.h:310
bool HasError() const
Determine if an error occurred.
Definition: HttpParser.h:96
http_errno GetError() const
Get error number.
Definition: HttpParser.h:101
http_method GetMethod() const
Get HTTP method.
Definition: HttpParser.h:88
unsigned int GetMinor() const
Get HTTP minor version.
Definition: HttpParser.h:77
static uint32_t GetParserVersion()
Returns the library version.
bool IsBodyFinal() const
Checks if this is the final chunk of the body.
Definition: HttpParser.h:137
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const noexcept
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
namespace to hold default to_json function
Definition: SmallString.h:21
void SetMaxLength(size_t len)
Set the maximum accepted length for URLs, field names, and field values.
Definition: HttpParser.h:57
sig::Signal< StringRef > url
URL callback.
Definition: HttpParser.h:154
sig::Signal< bool > headersComplete
Headers complete callback.
Definition: HttpParser.h:179
void Abort()
Abort the parse.
Definition: HttpParser.h:110
sig::Signal chunkComplete
Chunk complete callback.
Definition: HttpParser.h:211
sig::Signal< bool > messageComplete
Headers complete callback.
Definition: HttpParser.h:199
Definition: http_parser.h:279
unsigned int GetMajor() const
Get HTTP major version.
Definition: HttpParser.h:72
StringRef Execute(StringRef in)
Executes the parser.
Definition: HttpParser.h:64
sig::Signal< StringRef, bool > body
Body data callback.
Definition: HttpParser.h:189
sig::Signal< StringRef > status
Status callback.
Definition: HttpParser.h:162
bool ShouldKeepAlive() const
If this returns false in the headersComplete or messageComplete callback, then this should be the las...
Definition: HttpParser.h:126
StringRef GetUrl() const
Get URL.
Definition: HttpParser.h:142
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const noexcept
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:603
unsigned short http_major
READ-ONLY.
Definition: http_parser.h:292
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
sig::Signal< uint64_t > chunkHeader
Chunk header callback.
Definition: HttpParser.h:206
sig::Signal messageBegin
Message begin callback.
Definition: HttpParser.h:147
sig::Signal< StringRef, StringRef > header
Header field callback.
Definition: HttpParser.h:169
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const noexcept
size - Get the string size.
Definition: StringRef.h:138
HttpParser(Type type)
Constructor.
HTTP protocol parser.
Definition: HttpParser.h:25
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
bool IsUpgrade() const
Determine if an upgrade header was present and the parser has exited because of that.
Definition: HttpParser.h:118
void Reset(Type type)
Reset the parser to initial state.
void Pause(bool paused)
Pause the parser.
Definition: HttpParser.h:132