Frames

check if finished2

0
1
2
1#include "HttpResponse.h"
2#include "TcpSocket.h"
3
4
5HttpResponse::HttpResponse(TcpSocket *_socket)
6 :QObject(),
7 m_buffer(),
8 m_socket(_socket),
9 m_header(),
10 m_statusCode(200),
11 m_cookies(),
12 m_sessionId(),
13 m_hasFinished(false)
14{
15}
16
17HttpResponse::HttpResponse(const HttpResponse &in)
18 :QObject(),
19 m_buffer(in.m_buffer),
20 m_socket(in.m_socket),
21 m_header(in.m_header),
22 m_statusCode(in.m_statusCode),
23 m_cookies(in.m_cookies),
24 m_sessionId(in.m_sessionId),
25 m_hasFinished(in.m_hasFinished)
26{
27
28}
29
30void HttpResponse::operator=(const HttpResponse &in)
31{
32 m_buffer = in.m_buffer;
33 m_socket = in.m_socket;
34 m_header = in.m_header;
35 m_statusCode = in.m_statusCode;
36 m_sessionId = in.m_sessionId;
37 m_cookies = in.m_cookies;
38 m_hasFinished = in.m_hasFinished;
39}
40
41HttpResponse::~HttpResponse()
42{
43 //qDebug() << "released";
44}
45
46void HttpResponse::addCookie(const QString &key, const QVariant &value)
47{
48 m_cookies.insert(key, value);
49}
50
51HttpResponse& HttpResponse::operator<<(const QByteArray &in)
52{
53 m_buffer.append(in);
54 return *this;
55}
56
57HttpResponse& HttpResponse::operator<<(const char *in)
58{
59 m_buffer.append(in);
60 return *this;
61}
62
63HttpResponse& HttpResponse::operator<<(const QString &in)
64{
65 m_buffer.append(in);
66 return *this;
67}
68
69void HttpResponse::write(const char* in,const size_t size)
70{
71 m_buffer.append(in,size);
72}
73
74void HttpResponse::finish(const QString &typeOverride )
75{
76 if (!m_hasFinished)
77 {
78 unsigned int bufferSize = static_cast<unsigned int>(m_buffer.size());
79
80 QString headerString;
81
82 switch (m_statusCode)
83 {
84 case 200:
85 headerString = "HTTP/1.1 " % QString::number(m_statusCode) % " Ok\r\n"
86 "Content-Length: " % QString::number(bufferSize) % "\r\n"
87 "Content-Type: " % typeOverride % "\r\n";
88 break;
89 case 302:
90 headerString = "HTTP/1.1 302 Found\r\n"
91 "Connection:keep-alive\r\n";
92 break;
93 case 301:
94 headerString = "HTTP/1.1 301 Moved Permanently\r\n"
95 "Connection:keep-alive\r\n";
96 break;
97 case 304:
98 headerString = "HTTP/1.1 304 Not Modified\r\n";
99 break;
100 case 400:
101 headerString = "HTTP/1.1 400 Bad Request\r\n";
102 break;
103 case 401:
104 headerString = "HTTP/1.1 401 Unauthorized\r\n";
105 break;
106 case 404:
107 headerString = "HTTP/1.1 404 Not Found\r\n"
108 "Content-Type: text/html; charset=\"utf-8\"\r\n";
109 break;
110 case 415:
111 headerString = "HTTP/1.1 415 Unsupported Media Type\r\n";
112 break;
113 case 422:
114 headerString = "HTTP/1.1 422 Unprocessable Entity\r\n";
115 break;
116 case 500:
117 headerString = "HTTP/1.1 500 Internal Server Error\r\n";
118 break;
119 case 503:
120 headerString = "HTTP/1.1 503 Service Unavilable\r\n";
121 break;
122 default:
123 qDebug() << "unimplemented http status code";
124 }
125
126 headerString = headerString % m_header.toString();
127
128 QString cookieString("Set-Cookie: ");
129
130 if (!m_sessionId.isEmpty())
131 {
132 cookieString.append("ssid=").append(m_sessionId);
133 for(QHash<QString, QVariant>::Iterator iter = m_cookies.begin(); iter != m_cookies.end(); ++iter)
134 {
135 cookieString.append("&").append(iter.key()).append("=").append(iter.value().toString());
136 }
137 }
138 else if(m_cookies.count() > 0)
139 {
140 QHash<QString, QVariant>::Iterator iter = m_cookies.begin();
141
142 cookieString.append(iter.key()).append("=").append(iter.value().toString());
143
144 for(++iter; iter != m_cookies.end(); ++iter)
145 {
146 cookieString.append("&").append(iter.key()).append("=").append(iter.value().toString());
147 }
148 }
149
150 if ((!m_sessionId.isEmpty()) || (m_cookies.count() > 0))
151 {
152 headerString = headerString % cookieString % "; Path=/\r\n";
153 }
154
155 headerString = headerString % "\r\n";
156
157 m_socket->write(headerString.toUtf8());
158 m_socket->write(m_buffer);
159 m_hasFinished = true;
160 }
161}
162
163void HttpResponse::redirectTo(QSharedPointer<QString> url)
164{
165 setStatusCode(302);
166 setHeader("Location", url);
167 finish();
168}
169
170
Watch expressions
m_hasFinished