SpiecsEngine
 
Loading...
Searching...
No Matches
HttpResponse.cpp
Go to the documentation of this file.
1/**
2* @file HttpResponse.cpp.
3* @brief The HttpResponse Class Implementation.
4* @author Spices & Muduo.
5*/
6
7#include "Pchheader.h"
8#include "HttpResponse.h"
9#include "../Net/Buffer.h"
10
11namespace Spices {
12
13namespace Net {
14
16 {
18
19 m_StatusCode = code;
20 }
21
22 void HttpResponse::SetStatusMessage(const std::string& message)
23 {
25
26 m_StatusMessage = message;
27 }
28
30 {
32
34 }
35
37 {
39
40 return m_CloseConnection;
41 }
42
43 void HttpResponse::SetContentType(const std::string& contentType)
44 {
46
47 AddHeader("Content-Type", contentType);
48 }
49
50 void HttpResponse::AddHeader(const std::string& name, const std::string& value)
51 {
53
54 m_Header[name] = value;
55 }
56
57 void HttpResponse::SetBody(const std::string& body)
58 {
60
61 m_Body = body;
62 }
63
65 {
67
68 std::stringstream ss;
69 ss << "HTTP/1.1 " << m_StatusCode << m_StatusMessage << "\r\n";
70
71 output->Append(ss.str());
72
74 {
75 output->Append("Connection: close\r\n");
76 }
77 else
78 {
79 std::stringstream v;
80 v << "Content-Length: " << m_Body.size() << "\r\n" << "Connection: Keep-Alive\r\n";
81
82 output->Append(v.str());
83 }
84
85 for (const auto& header : m_Header)
86 {
87 std::stringstream v;
88 v << header.first << ": " << header.second << "\r\n";
89
90 output->Append(v.str());
91 }
92
93 output->Append("\r\n");
94 output->Append(m_Body);
95
96 }
97
98}
99
100}
#define SPICES_PROFILE_ZONE
void Append(const std::string &msg)
Append Message to this buffer.
Definition Buffer.cpp:41
Wrapper of readwrite buffer.
Definition Buffer.h:19
void SetCloseConnection(bool on)
Set CloseConnection.
void SetContentType(const std::string &contentType)
Set ContentType.
std::string m_StatusMessage
StatusMessage.
StatusCode m_StatusCode
StatusCode.
bool m_CloseConnection
CloseConnection.
void SetBody(const std::string &body)
Set body.
void SetStatusCode(StatusCode code)
Set StatusCode.
void SetStatusMessage(const std::string &message)
Set StatusMessage.
void AddHeader(const std::string &name, const std::string &value)
Add a header.
std::string m_Body
Body.
void AppendToBuffer(Buffer *output)
Append this response to Buffer.
bool CloseConnection() const
Get CloseConnection.