My Project
 All Classes Functions Variables Pages
HttpResponse.h
1 /*
2  * HttpResponse.h
3  *
4  * Encapsulate a response to be sent to the Http client.
5  *
6  * Created on: Sep 2, 2017
7  * Author: kolban
8  */
9 
10 #ifndef COMPONENTS_CPP_UTILS_HTTPRESPONSE_H_
11 #define COMPONENTS_CPP_UTILS_HTTPRESPONSE_H_
12 #include <string>
13 #include <map>
14 #include "HttpRequest.h"
15 
16 class HttpResponse {
17 public:
18  static const int HTTP_STATUS_CONTINUE;
19  static const int HTTP_STATUS_SWITCHING_PROTOCOL;
20  static const int HTTP_STATUS_OK;
21  static const int HTTP_STATUS_MOVED_PERMANENTLY;
22  static const int HTTP_STATUS_BAD_REQUEST;
23  static const int HTTP_STATUS_UNAUTHORIZED;
24  static const int HTTP_STATUS_FORBIDDEN;
25  static const int HTTP_STATUS_NOT_FOUND;
26  static const int HTTP_STATUS_METHOD_NOT_ALLOWED;
27  static const int HTTP_STATUS_INTERNAL_SERVER_ERROR;
28  static const int HTTP_STATUS_NOT_IMPLEMENTED;
29  static const int HTTP_STATUS_SERVICE_UNAVAILABLE;
30 
31  HttpResponse(HttpRequest* httpRequest);
32  virtual ~HttpResponse();
33 
34  void addHeader(std::string name, std::string value); // Add a header to be sent to the client.
35  void close(); // Close the request/response.
36  std::string getHeader(std::string name); // Get a named header.
37  std::map<std::string, std::string> getHeaders(); // Get all headers.
38  void sendData(std::string data); // Send data to the client.
39  void sendData(uint8_t* pData, size_t size); // Send data to the client.
40  void setStatus(int status, std::string message); // Set the response status.
41  void sendFile(std::string fileName, size_t bufSize = 4 * 1024); // Send file contents if exists.
42 
43 private:
44  bool m_headerCommitted; // Has the header been sent?
45  HttpRequest* m_request; // The request associated with this response.
46  std::map<std::string, std::string> m_responseHeaders; // The headers to be sent with the response.
47  int m_status; // The status to be sent with the response.
48  std::string m_statusMessage; // The status message to be sent with the response.
49 
50  void sendHeader(); // Send the header to the client.
51 
52 };
53 
54 #endif /* COMPONENTS_CPP_UTILS_HTTPRESPONSE_H_ */
void setStatus(int status, std::string message)
Set the status code that is to be sent back to the client. When a client makes a request, the response contains a status. This call sets the status that will be sent back to the client.
Definition: HttpResponse.cpp:179
std::string getHeader(std::string name)
Get the value of the named header.
Definition: HttpResponse.cpp:71
void sendData(std::string data)
Send data to the partner. Send some data to the partner. If we haven't yet sent the HTTP header then ...
Definition: HttpResponse.cpp:88
void addHeader(std::string name, std::string value)
Add a header to the response message. If the response has already been committed then ignore this req...
Definition: HttpResponse.cpp:46
Definition: HttpRequest.h:19
void close()
Close the response. We close the response. If we haven't yet sent the header, we send that now and th...
Definition: HttpResponse.cpp:57
Definition: HttpResponse.h:16