WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
HTTPResponse.cpp
1#include "HTTPResponse.h"
2
3using namespace std;
4
5namespace framework
6{
8 {
9 return format("{:%a, %d %b %Y %H:%M:%OS} GMT", chrono::system_clock::now());
10 }
11
12 HTTPResponse::HTTPResponse()
13 {
14 this->setDefault();
15 }
16
17 HTTPResponse& HTTPResponse::operator = (const web::HTTPBuilder& builder)
18 {
19 this->builder = builder;
20
21 return *this;
22 }
23
24 void HTTPResponse::setHTTPVersion(const string& version)
25 {
26 builder.HTTPVersion(version);
27 }
28
29 void HTTPResponse::setResponseCode(web::responseCodes code)
30 {
31 builder.responseCode(code);
32 }
33
34 void HTTPResponse::addHeader(const string& name, const string& value)
35 {
36 builder.headers
37 (
38 name, value
39 );
40 }
41
42 void HTTPResponse::addBody(const string& body)
43 {
44 this->body = body;
45 }
46
47 void HTTPResponse::addBody(const json::JSONBuilder& json)
48 {
49 builder.headers
50 (
51 "Content-Type", "application/json"
52 );
53
54 this->body = json.build();
55 }
56
57 void HTTPResponse::addBody(string&& body) noexcept
58 {
59 this->body = move(body);
60 }
61
62 HTTPResponse& HTTPResponse::appendBody(const std::string& body)
63 {
64 this->body += body;
65
66 return *this;
67 }
68
69 void HTTPResponse::addCookie(const std::string& name, const std::string& value)
70 {
71 builder.headers
72 (
73 "Set-Cookie", name + "=" + value
74 );
75 }
76
78 {
79 builder.clear();
80
81 builder.responseCode(web::responseCodes::ok);
82
83 body.clear();
84
85 this->setIsValid(true);
86 }
87
88 void HTTPResponse::setIsValid(bool isValid)
89 {
90 this->isValid = isValid;
91 }
92
93 HTTPResponse::operator bool() const
94 {
95 return isValid;
96 }
97
98 streams::IOSocketStream& operator << (streams::IOSocketStream& stream, HTTPResponse& response)
99 {
100 string result;
101
102 response.builder.headers
103 (
105 "Server", "WebFramework-Server"
106 );
107
108 if (response.body.size())
109 {
110 result = response.builder.build(response.body);
111 }
112 else
113 {
114 result = response.builder.build();
115 }
116
117 stream << result;
118
119 return stream;
120 }
121}
HTTPBuilder wrapper.
HTTPResponse & operator=(const web::HTTPBuilder &builder)
Assign operator for HTTPBuilder.
HTTPResponse & appendBody(const std::string &body)
void addCookie(const std::string &name, const std::string &value)
Add cookie to HTTP response.
void addBody(const std::string &body)
static std::string getFullDate()
Adds full date GMT to response.
void addHeader(const std::string &name, const std::string &value)
Set additional HTTP header.
void setDefault()
Clears HTTPResponse.
void setResponseCode(web::responseCodes code)
Set HTTP response code.
void setHTTPVersion(const std::string &version)