Cuberite
A lightweight, fast and extensible game server for Minecraft
HTTPMessage.h
Go to the documentation of this file.
1 
2 // HTTPMessage.h
3 
4 // Declares the cHTTPMessage class representing the common ancestor for HTTP request and response classes
5 
6 
7 
8 
9 
10 #pragma once
11 
12 
13 
14 
15 
17 {
18 public:
19  enum eStatus
20  {
21  HTTP_OK = 200,
23  } ;
24 
25  enum eKind
26  {
29  } ;
30 
31  cHTTPMessage(eKind a_Kind);
32 
33  // Force a virtual destructor in all descendants
34  virtual ~cHTTPMessage() {}
35 
37  virtual void AddHeader(const AString & a_Key, const AString & a_Value);
38 
39  void SetContentType (const AString & a_ContentType) { m_ContentType = a_ContentType; }
40  void SetContentLength(size_t a_ContentLength) { m_ContentLength = a_ContentLength; }
41 
42  const AString & GetContentType (void) const { return m_ContentType; }
43  size_t GetContentLength(void) const { return m_ContentLength; }
44 
45 protected:
46 
47  using cNameValueMap = std::map<AString, AString>;
48 
49 
51 
54 
57 
62 } ;
63 
64 
65 
66 
67 
70  public cHTTPMessage
71 {
73 
74 public:
75 
77 
80  void AppendToData(AString & a_DataStream) const;
81 } ;
82 
83 
84 
85 
86 
89  public cHTTPMessage
90 {
92 
93 public:
94 
96  class cUserData
97  {
98  public:
99  // Force a virtual destructor in descendants:
100  virtual ~cUserData() {}
101  };
102  using cUserDataPtr = std::shared_ptr<cUserData>;
103 
104 
106  cHTTPIncomingRequest(const AString & a_Method, const AString & a_URL);
107 
109  const AString & GetMethod(void) const { return m_Method; }
110 
112  const AString & GetURL(void) const { return m_URL; }
113 
115  AString GetURLPath(void) const;
116 
118  bool HasAuth(void) const { return m_HasAuth; }
119 
121  const AString & GetAuthUsername(void) const { return m_AuthUsername; }
122 
124  const AString & GetAuthPassword(void) const { return m_AuthPassword; }
125 
126  bool DoesAllowKeepAlive(void) const { return m_AllowKeepAlive; }
127 
129  void SetUserData(cUserDataPtr a_UserData) { m_UserData = std::move(a_UserData); }
130 
133 
136  virtual void AddHeader(const AString & a_Key, const AString & a_Value) override;
137 
138 protected:
139 
142 
145 
147  bool m_HasAuth;
148 
151 
154 
158 
161 };
std::string AString
Definition: StringUtils.h:11
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:16
std::map< AString, AString > cNameValueMap
Definition: HTTPMessage.h:47
eKind m_Kind
Definition: HTTPMessage.h:50
cHTTPMessage(eKind a_Kind)
Definition: HTTPMessage.cpp:26
virtual ~cHTTPMessage()
Definition: HTTPMessage.h:34
size_t GetContentLength(void) const
Definition: HTTPMessage.h:43
void SetContentLength(size_t a_ContentLength)
Definition: HTTPMessage.h:40
virtual void AddHeader(const AString &a_Key, const AString &a_Value)
Adds a header into the internal map of headers.
Definition: HTTPMessage.cpp:36
AString m_ContentType
Type of the content; parsed by AddHeader(), set directly by SetContentLength()
Definition: HTTPMessage.h:56
AStringMap m_Headers
Map of headers, with their keys lowercased.
Definition: HTTPMessage.h:53
size_t m_ContentLength
Length of the content that is to be received.
Definition: HTTPMessage.h:61
void SetContentType(const AString &a_ContentType)
Definition: HTTPMessage.h:39
const AString & GetContentType(void) const
Definition: HTTPMessage.h:42
Stores outgoing response headers and serializes them to an HTTP data stream.
Definition: HTTPMessage.h:71
void AppendToData(AString &a_DataStream) const
Appends the response to the specified datastream - response line and headers.
Definition: HTTPMessage.cpp:81
Provides storage for an incoming HTTP request.
Definition: HTTPMessage.h:90
virtual void AddHeader(const AString &a_Key, const AString &a_Value) override
Adds the specified header into the internal list of headers.
const AString & GetMethod(void) const
Returns the method used in the request.
Definition: HTTPMessage.h:109
const AString & GetAuthUsername(void) const
Returns the username that the request presented.
Definition: HTTPMessage.h:121
void SetUserData(cUserDataPtr a_UserData)
Attaches any kind of data to this request, to be later retrieved by GetUserData().
Definition: HTTPMessage.h:129
cUserDataPtr m_UserData
Any data attached to the request by the class client.
Definition: HTTPMessage.h:160
cHTTPIncomingRequest(const AString &a_Method, const AString &a_URL)
Creates a new instance of the class, containing the method and URL provided by the client.
std::shared_ptr< cUserData > cUserDataPtr
Definition: HTTPMessage.h:102
bool m_AllowKeepAlive
Set to true if the request indicated that it supports keepalives.
Definition: HTTPMessage.h:157
AString GetURLPath(void) const
Returns the path part of the URL.
bool DoesAllowKeepAlive(void) const
Definition: HTTPMessage.h:126
cUserDataPtr GetUserData(void)
Returns the data attached to this request by the class client.
Definition: HTTPMessage.h:132
AString m_Method
Method of the request (GET / PUT / POST / ...)
Definition: HTTPMessage.h:141
bool m_HasAuth
Set to true if the request contains auth data that was understood by the parser.
Definition: HTTPMessage.h:147
AString m_URL
Full URL of the request.
Definition: HTTPMessage.h:144
AString m_AuthUsername
The username used for auth.
Definition: HTTPMessage.h:150
const AString & GetAuthPassword(void) const
Returns the password that the request presented.
Definition: HTTPMessage.h:124
bool HasAuth(void) const
Returns true if the request has had the Auth header present.
Definition: HTTPMessage.h:118
AString m_AuthPassword
The password used for auth.
Definition: HTTPMessage.h:153
const AString & GetURL(void) const
Returns the URL used in the request.
Definition: HTTPMessage.h:112
Base class for anything that can be used as the UserData for the request.
Definition: HTTPMessage.h:97