-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpserver.cpp
57 lines (47 loc) · 1.14 KB
/
httpserver.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
#include "httpserver.h"
#include <QTcpSocket>
#include <QHttpRequestHeader>
HttpServer::HttpServer(QObject *parent) :
QTcpServer(parent)
{
if (listen())
qDebug() << "HttpServer listening on port" << serverPort();
else
qDebug() << "HttpServer listening failed";
connect(&m_socket, SIGNAL(readyRead()), this, readData());
}
void HttpServer::incomingConnection(int handle)
{
m_socket = m_http->nextPendingConnection();
}
void HttpServer::readData()
{
QByteArray ba = m_socket->readAll();
buffer = buffer + ba;
int eoh = buffer.indexOf("\r\n\r\n");
if (eoh > 0)
{
if (ba[0] == 'G')
{
buffer = buffer.mid(eoh+4);
} else if (ba[0] == 'P') {
QHttpRequestHeader header(QString(buffer.left(eoh+4)));
QByteArray data = buffer = buffer.mid(eoh+4, header.contentLength());
// MouseEvent: (Type, GlobalX, GlobalY, Button, Buttons, KeyboardModifiers);
// KeyEvent (Type, Key, KeyboardModifiers);
while(data.length())
{
int nl = data.indexOf("\r\n");
QByteArray line;
if (nl > 0)
{
line = data.left(nl);
data = data.mid(nl+2);
} else if (nl==-1) {
line = data;
data.empty();
}
}
}
}
}