WPILibC++  2020.3.2-60-g3011ebe
HttpServerConnection.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_HTTPSERVERCONNECTION_H_
9 #define WPIUTIL_WPI_HTTPSERVERCONNECTION_H_
10 
11 #include <memory>
12 
13 #include "wpi/ArrayRef.h"
14 #include "wpi/HttpParser.h"
15 #include "wpi/StringRef.h"
16 #include "wpi/Twine.h"
17 #include "wpi/uv/Stream.h"
18 
19 namespace wpi {
20 
21 class raw_ostream;
22 
24  public:
25  explicit HttpServerConnection(std::shared_ptr<uv::Stream> stream);
26  virtual ~HttpServerConnection() = default;
27 
28  protected:
36  virtual void ProcessRequest() = 0;
37 
56  virtual void BuildCommonHeaders(raw_ostream& os);
57 
70  void BuildHeader(raw_ostream& os, int code, const Twine& codeText,
71  const Twine& contentType, uint64_t contentLength,
72  const Twine& extra = Twine{});
73 
85  void SendData(ArrayRef<uv::Buffer> bufs, bool closeAfter = false);
86 
97  void SendResponse(int code, const Twine& codeText, const Twine& contentType,
98  StringRef content, const Twine& extraHeader = Twine{});
99 
114  void SendStaticResponse(int code, const Twine& codeText,
115  const Twine& contentType, StringRef content,
116  bool gzipped, const Twine& extraHeader = Twine{});
117 
127  void SendError(int code, const Twine& message = Twine{});
128 
130  HttpParser m_request{HttpParser::kRequest};
131 
133  bool m_keepAlive = false;
134 
136  bool m_acceptGzip = false;
137 
140 
143 
146 
149 };
150 
151 } // namespace wpi
152 
153 #endif // WPIUTIL_WPI_HTTPSERVERCONNECTION_H_
wpi::HttpServerConnection::m_dataConn
sig::ScopedConnection m_dataConn
The header reader connection.
Definition: HttpServerConnection.h:142
wpi::uv::Stream
Stream handle.
Definition: Stream.h:69
wpi::HttpServerConnection::m_acceptGzip
bool m_acceptGzip
If gzip is an acceptable encoding for responses.
Definition: HttpServerConnection.h:136
wpi::HttpServerConnection::ProcessRequest
virtual void ProcessRequest()=0
Process an incoming HTTP request.
wpi::HttpServerConnection::BuildHeader
void BuildHeader(raw_ostream &os, int code, const Twine &codeText, const Twine &contentType, uint64_t contentLength, const Twine &extra=Twine{})
Build HTTP response header, along with other header information like mimetype.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::HttpServerConnection::m_endConn
sig::ScopedConnection m_endConn
The end stream connection.
Definition: HttpServerConnection.h:145
wpi::HttpServerConnection::m_request
HttpParser m_request
The HTTP request.
Definition: HttpServerConnection.h:130
wpi::HttpParser
HTTP protocol parser.
Definition: HttpParser.h:25
wpi::HttpServerConnection::SendData
void SendData(ArrayRef< uv::Buffer > bufs, bool closeAfter=false)
Send data to client.
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
wpi::HttpServerConnection::SendError
void SendError(int code, const Twine &message=Twine{})
Send error header and message.
wpi::HttpServerConnection::BuildCommonHeaders
virtual void BuildCommonHeaders(raw_ostream &os)
Build common response headers.
wpi::HttpServerConnection
Definition: HttpServerConnection.h:23
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
wpi::HttpServerConnection::SendStaticResponse
void SendStaticResponse(int code, const Twine &codeText, const Twine &contentType, StringRef content, bool gzipped, const Twine &extraHeader=Twine{})
Send HTTP response from static data, along with other header information like mimetype.
wpi::HttpServerConnection::m_stream
uv::Stream & m_stream
The underlying stream for the connection.
Definition: HttpServerConnection.h:139
wpi::sig::ScopedConnection
ScopedConnection is a RAII version of Connection It disconnects the slot from the signal upon destruc...
Definition: Signal.h:257
wpi::sig::Connection
A Connection object allows interaction with an ongoing slot connection.
Definition: Signal.h:198
wpi::HttpServerConnection::m_keepAlive
bool m_keepAlive
Whether the connection should be kept alive.
Definition: HttpServerConnection.h:133
wpi::HttpServerConnection::SendResponse
void SendResponse(int code, const Twine &codeText, const Twine &contentType, StringRef content, const Twine &extraHeader=Twine{})
Send HTTP response, along with other header information like mimetype.
wpi::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:47
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
wpi::HttpServerConnection::m_messageCompleteConn
sig::Connection m_messageCompleteConn
The message complete connection.
Definition: HttpServerConnection.h:148