My Project
 All Classes Functions Variables Pages
HttpRequest.h
1 /*
2  * HTTPRequest.h
3  *
4  * Created on: Aug 30, 2017
5  * Author: kolban
6  */
7 
8 #ifndef COMPONENTS_CPP_UTILS_HTTPREQUEST_H_
9 #define COMPONENTS_CPP_UTILS_HTTPREQUEST_H_
10 #include <string>
11 #include <map>
12 #include <vector>
13 #include "Socket.h"
14 #include "WebSocket.h"
15 #include "HttpParser.h"
16 
17 #undef close
18 
19 class HttpRequest {
20 public:
22  virtual ~HttpRequest();
23  static const char HTTP_HEADER_ACCEPT[];
24  static const char HTTP_HEADER_ALLOW[];
25  static const char HTTP_HEADER_CONNECTION[];
26  static const char HTTP_HEADER_CONTENT_LENGTH[];
27  static const char HTTP_HEADER_CONTENT_TYPE[];
28  static const char HTTP_HEADER_COOKIE[];
29  static const char HTTP_HEADER_HOST[];
30  static const char HTTP_HEADER_LAST_MODIFIED[];
31  static const char HTTP_HEADER_ORIGIN[];
32  static const char HTTP_HEADER_SEC_WEBSOCKET_ACCEPT[];
33  static const char HTTP_HEADER_SEC_WEBSOCKET_PROTOCOL[];
34  static const char HTTP_HEADER_SEC_WEBSOCKET_KEY[];
35  static const char HTTP_HEADER_SEC_WEBSOCKET_VERSION[];
36  static const char HTTP_HEADER_UPGRADE[];
37  static const char HTTP_HEADER_USER_AGENT[];
38 
39  static const char HTTP_METHOD_CONNECT[];
40  static const char HTTP_METHOD_DELETE[];
41  static const char HTTP_METHOD_GET[];
42  static const char HTTP_METHOD_HEAD[];
43  static const char HTTP_METHOD_OPTIONS[];
44  static const char HTTP_METHOD_PATCH[];
45  static const char HTTP_METHOD_POST[];
46  static const char HTTP_METHOD_PUT[];
47 
48  void close(); // Close the connection to the client.
49  void dump(); // Diagnostic dump of the Http request.
50  std::string getBody(); // Get the body of the request.
51  std::string getHeader(std::string name); // Get the value of a named header.
52  std::map<std::string, std::string> getHeaders(); // Get all the headers.
53  std::string getMethod(); // Get the request method.
54  std::string getPath(); // Get the request path.
55  std::map<std::string, std::string> getQuery(); // Get the query part of the request.
56  Socket getSocket(); // Get the underlying TCP/IP socket.
57  std::string getVersion(); // Get the HTTP version.
58  WebSocket* getWebSocket(); // Get the WebSocket reference if this is a web socket.
59  bool isClosed(); // Has the connection been closed?
60  bool isWebsocket(); // Is this request to create a web socket?
61  std::map<std::string, std::string> parseForm(); // Parse the body as a form.
62  std::vector<std::string> pathSplit();
63  std::string urlDecode(std::string str); // Decode a URL.
64 private:
65  Socket m_clientSocket; // The socket connected to the client.
66  bool m_isClosed; // Is the client connection closed?
67  HttpParser m_parser; // The parse to parse HTTP data.
68  WebSocket* m_pWebSocket; // A possible reference to a WebSocket object instance.
69 
70 };
71 
72 #endif /* COMPONENTS_CPP_UTILS_HTTPREQUEST_H_ */
bool isWebsocket()
Determine if this request represents a WebSocket.
Definition: HttpRequest.cpp:294
Encapsulate a socket.
Definition: Socket.h:62
Socket getSocket()
Get the underlying socket.
Definition: HttpRequest.cpp:266
std::map< std::string, std::string > getQuery()
Get the query part of the request. The query is a set of name = value pairs. The return is a map keye...
Definition: HttpRequest.cpp:210
std::vector< std::string > pathSplit()
Return the constituent parts of the path. If we imagine a path as composed of parts separated by slas...
Definition: HttpRequest.cpp:346
Definition: WebSocket.h:60
std::string urlDecode(std::string str)
Decode a URL/form.
Definition: HttpRequest.cpp:366
std::string getBody()
Get the body of the HttpRequest.
Definition: HttpRequest.cpp:174
std::map< std::string, std::string > parseForm()
Parse the request message as a form.
Definition: HttpRequest.cpp:303
void dump()
Dump the HttpRequest for debugging purposes.
Definition: HttpRequest.cpp:160
Definition: HttpParser.h:14
void close()
Close the HttpRequest.
Definition: HttpRequest.cpp:148
Definition: HttpRequest.h:19
HttpRequest(Socket s)
Create an HTTP Request instance.
Definition: HttpRequest.cpp:95
bool isClosed()
Determine if the request is closed.
Definition: HttpRequest.cpp:285
std::string getHeader(std::string name)
Get the named header.
Definition: HttpRequest.cpp:184