WPILibC++  2018.4.1-1232-gce8c71b
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
109  void SendError(int code, const Twine& message = Twine{});
110 
112  HttpParser m_request{HttpParser::kRequest};
113 
115  bool m_keepAlive = false;
116 
119 };
120 
121 } // namespace wpi
122 
123 #endif // WPIUTIL_WPI_HTTPSERVERCONNECTION_H_
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
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.
bool m_keepAlive
Whether the connection should be kept alive.
Definition: HttpServerConnection.h:115
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
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.
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Stream handle.
Definition: Stream.h:68
Definition: HttpServerConnection.h:23
void SendError(int code, const Twine &message=Twine{})
Send error header and message.
virtual void BuildCommonHeaders(raw_ostream &os)
Build common response headers.
uv::Stream & m_stream
The underlying stream for the connection.
Definition: HttpServerConnection.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void SendData(ArrayRef< uv::Buffer > bufs, bool closeAfter=false)
Send data to client.
HTTP protocol parser.
Definition: HttpParser.h:25
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
HttpParser m_request
The HTTP request.
Definition: HttpServerConnection.h:112
virtual void ProcessRequest()=0
Process an incoming HTTP request.