Cuberite
A lightweight, fast and extensible game server for Minecraft
HTTPServerConnection.h
Go to the documentation of this file.
1 
2 // HTTPConnection.h
3 
4 // Declares the cHTTPConnection class representing a single persistent connection in the HTTP server.
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "../OSSupport/Network.h"
13 #include "HTTPMessageParser.h"
14 
15 
16 
17 
18 
19 // fwd:
20 class cHTTPServer;
23 
24 
25 
26 
28  public cTCPLink::cCallbacks,
30 {
31 public:
33  cHTTPServerConnection(cHTTPServer & a_HTTPServer);
34 
35  // Force a virtual destructor in all descendants
36  virtual ~cHTTPServerConnection() override;
37 
41  void SendStatusAndReason(int a_StatusCode, const AString & a_Reason);
42 
45  void SendNeedAuth(const AString & a_Realm);
46 
48  void Send(const cHTTPOutgoingResponse & a_Response);
49 
51  void Send(const void * a_Data, size_t a_Size);
52 
54  void Send(const AString & a_Data) { Send(a_Data.data(), a_Data.size()); }
55 
58  void FinishResponse(void);
59 
61  void Terminate(void);
62 
63 protected:
64  typedef std::map<AString, AString> cNameValueMap;
65 
68 
71 
74  std::unique_ptr<cHTTPIncomingRequest> m_CurrentRequest;
75 
78 
79 
80  // cTCPLink::cCallbacks overrides:
82  virtual void OnLinkCreated(cTCPLinkPtr a_Link) override;
83 
85  virtual void OnReceivedData(const char * a_Data, size_t a_Size) override;
86 
88  virtual void OnRemoteClosed(void) override;
89 
91  virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override;
92 
93  // cHTTPMessageParser::cCallbacks overrides:
94  virtual void OnError(const AString & a_ErrorDescription) override;
95  virtual void OnFirstLine(const AString & a_FirstLine) override;
96  virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override;
97  virtual void OnHeadersFinished(void) override;
98  virtual void OnBodyData(const void * a_Data, size_t a_Size) override;
99  virtual void OnBodyFinished(void) override;
100 
101  // Overridable:
103  virtual void SendData(const void * a_Data, size_t a_Size);
104 
107  void SendData(const AString & a_Data)
108  {
109  SendData(a_Data.data(), a_Data.size());
110  }
111 } ;
112 
113 typedef std::vector<cHTTPServerConnection *> cHTTPServerConnections;
114 
115 
116 
117 
118 
std::vector< cHTTPServerConnection * > cHTTPServerConnections
std::shared_ptr< cTCPLink > cTCPLinkPtr
Definition: Network.h:25
std::string AString
Definition: StringUtils.h:11
Stores outgoing response headers and serializes them to an HTTP data stream.
Definition: HTTPMessage.h:71
Provides storage for an incoming HTTP request.
Definition: HTTPMessage.h:90
std::map< AString, AString > cNameValueMap
virtual void OnFirstLine(const AString &a_FirstLine) override
Called when the first line of the request or response is fully parsed.
cTCPLinkPtr m_Link
The network link attached to this connection.
cHTTPServerConnection(cHTTPServer &a_HTTPServer)
Creates a new instance, connected to the specified HTTP server instance.
virtual void OnHeadersFinished(void) override
Called when all the headers have been parsed.
void Terminate(void)
Terminates the connection; finishes any request being currently processed.
virtual void OnBodyFinished(void) override
Called when the entire body has been reported by OnBodyData().
void SendData(const AString &a_Data)
Sends the raw data over the link.
virtual void OnBodyData(const void *a_Data, size_t a_Size) override
Called for each chunk of the incoming body data.
cHTTPMessageParser m_Parser
The parser responsible for reading the requests.
void Send(const AString &a_Data)
Sends the data as the response (may be called multiple times)
void SendNeedAuth(const AString &a_Realm)
Sends the "401 unauthorized" reply together with instructions on authorizing, using the specified rea...
virtual ~cHTTPServerConnection() override
virtual void OnRemoteClosed(void) override
The socket has been closed for any reason.
cHTTPServer & m_HTTPServer
The parent webserver that is to be notified of events on this connection.
virtual void OnHeaderLine(const AString &a_Key, const AString &a_Value) override
Called when a single header line is parsed.
void FinishResponse(void)
Indicates that the current response is finished, gets ready for receiving another request (HTTP 1....
virtual void OnError(int a_ErrorCode, const AString &a_ErrorMsg) override
An error has occurred on the socket.
virtual void OnReceivedData(const char *a_Data, size_t a_Size) override
Data is received from the client.
std::unique_ptr< cHTTPIncomingRequest > m_CurrentRequest
The request being currently received Valid only between having parsed the headers and finishing recei...
void SendStatusAndReason(int a_StatusCode, const AString &a_Reason)
Sends HTTP status code together with a_Reason (used for HTTP errors).
virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
The link instance has been created, remember it.
void Send(const cHTTPOutgoingResponse &a_Response)
Sends the headers contained in a_Response.
virtual void SendData(const void *a_Data, size_t a_Size)
Called to send raw data over the link.