-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpconnection.cpp
159 lines (122 loc) · 4.45 KB
/
httpconnection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <QtNetwork/QTcpSocket>
#include <QFileInfo>
#include <QFile>
#include <QStringList>
#include <QDir>
#include <QThread>
#include <QDebug>
#include "httpconnection.h"
#include "dispatcher.h"
HTTPConnection::HTTPConnection(int socketDescriptor, QObject *parent) :
QObject(parent), socket(this), eventLoop(this)
{
if(!socket.setSocketDescriptor(socketDescriptor)){
qDebug() << socket.errorString() << "Cannot set sd: " << socketDescriptor;
emit closed();
}
}
void HTTPConnection::close()
{
socket.waitForBytesWritten(500);
socket.disconnectFromHost();
if (socket.state() == QAbstractSocket::UnconnectedState || socket.waitForDisconnected(1000)){
qDebug("Disconnected!");
}
emit closed();
}
void HTTPConnection::start()
{
connect(&socket, SIGNAL(readyRead()), this, SLOT(read()));
connect(&parser, SIGNAL(parsed(HTTPRequest)), this,
SLOT(processRequestData(HTTPRequest)));
connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
SLOT(onError(QAbstractSocket::SocketError)));
connect(&parser, SIGNAL(parseError(QString)), this,
SLOT(onParseError(QString)));
connect(this, SIGNAL(closed()), &eventLoop, SLOT(quit()));
eventLoop.exec();
}
void HTTPConnection::read()
{
parser << socket.readAll();
}
void HTTPConnection::write(uchar *data, int size)
{
qDebug() << Q_FUNC_INFO << "Writing" << size << "bytes";
socket.write((const char*)data, size);
}
void HTTPConnection::write(HTTPResponse response)
{
//TODO: for status codes that doesn't make sense, don't set the body
if("200" != response.getStatusCode() && response.getBody().isEmpty()){
response.setBody(response.getStatusCode() + " " + response.getReasonPhrase());
}
QByteArray partialResponse = response.getPartial();
qDebug() << Q_FUNC_INFO << "Writing" << partialResponse.size() << "bytes";
socket.write(partialResponse);
//TODO: in case of error (-1) or if not all data has been sent queue the data
}
void HTTPConnection::processRequestData(HTTPRequest requestData)
{
requestData.remoteAddress = socket.peerAddress();
if(socket.localPort() != requestData.port){
requestData.port = socket.localPort();
}
qDebug() << "Request data:\n\tMethod:"
<< requestData.method << "\n\tUrl:"
<< requestData.url << "\n\tProtocol:"
<< requestData.protocol << "\n\tVer:"
<< requestData.protocolVersion << "\n\tHost:"
<< requestData.host << "\n\tPort:"
<< requestData.port << "\n\tRemote addr:"
<< requestData.remoteAddress << "\n\tCookies:"
<< requestData.cookieJar
<< "\n\tFields:" << requestData.fields
<< "\n\tContent-Length:" << requestData.contentLength
<< "\n\tpost:" << requestData.postData;
//TODO: integrate FastCGI
//TOOD: implement other methods
if("GET" != requestData.method && "POST" != requestData.method){
HTTPResponse response;
response.setStatusCode(501);
response.setReasonPhrase("Not Implemented");
write(response);
close();
return;
}
Dispatcher dispatcher;
HTTPRequestHandler *requestHandler = dispatcher.getHTTPRequestHandler(requestData.url.path());
if(NULL == requestHandler){
HTTPResponse response;
response.setStatusCode(500);
response.setReasonPhrase("Internal Server Error");
write(response);
close();
return;
}
connect(requestHandler, SIGNAL(redirect(HTTPRequest)), this,
SLOT(processRequestData(HTTPRequest)));
connect(requestHandler, SIGNAL(responseWritten(HTTPResponse)), this,
SLOT(write(HTTPResponse)));
connect(requestHandler, SIGNAL(rawDataWritten(uchar*, int)), this,
SLOT(write(uchar*, int)));
connect(requestHandler, SIGNAL(endOfWriting()), this,
SLOT(close()));
connect(requestHandler, SIGNAL(endOfWriting()), requestHandler,
SLOT(deleteLater()));
requestHandler->createResponse(requestData);
}
void HTTPConnection::onParseError(const QString &reason)
{
HTTPResponse response;
response.setStatusCode(400);
response.setReasonPhrase("Bad Request");
response.setBody(reason);
write(response);
close();
}
void HTTPConnection::onError(QAbstractSocket::SocketError socketError)
{
qDebug() << socketError << ":" << socket.errorString();
close();
}