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
|
#include "gopherclient.hpp"
#include "ioutil.hpp"
GopherClient::GopherClient(QObject *parent) : QObject(parent)
{
connect(&socket, &QTcpSocket::connected, this, &GopherClient::on_connected);
connect(&socket, &QTcpSocket::readyRead, this, &GopherClient::on_readRead);
connect(&socket, &QTcpSocket::disconnected, this, &GopherClient::on_finished);
}
GopherClient::~GopherClient()
{
}
bool GopherClient::startRequest(const QUrl &url)
{
if(isInProgress())
return false;
if(url.scheme() != "gopher")
return false;
// Second char on the URL path denotes the Gopher type
// See https://tools.ietf.org/html/rfc4266
QString type = url.path().mid(1, 1);
mime = "application/octet-stream";
if(type == "") mime = "text/gophermap";
else if(type == "0") mime = "text/plain";
else if(type == "1") mime = "text/gophermap";
else if(type == "g") mime = "image/gif";
else if(type == "I") mime = "image/unknown";
else if(type == "h") mime = "text/html";
else if(type == "s") mime = "audio/unknown";
qDebug() << url << "→" << mime;
this->requested_url = url;
this->was_cancelled = false;
socket.connectToHost(url.host(), url.port(70));
return true;
}
bool GopherClient::isInProgress() const
{
return socket.isOpen();
}
bool GopherClient::cancelRequest()
{
was_cancelled = true;
socket.close();
body.clear();
return true;
}
void GopherClient::on_connected()
{
auto blob = (requested_url.path().mid(2) + "\r\n").toUtf8();
IoUtil::writeAll(socket, blob);
}
void GopherClient::on_readRead()
{
body.append(socket.readAll());
emit this->requestProgress(body.size());
}
void GopherClient::on_finished()
{
if(not was_cancelled)
{
emit this->requestComplete(this->body, mime);
was_cancelled = true;
}
body.clear();
}
|