-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwget.cpp
138 lines (129 loc) · 3.24 KB
/
wget.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
#include "wget.h"
#include <QHostInfo>
Wget::Wget(QObject *parent) : QObject(parent)
{
m_pSocket = new QTcpSocket(this);
connect(m_pSocket,&QTcpSocket::stateChanged,this,[this](const QAbstractSocket::SocketState state){
if( state == QAbstractSocket::UnconnectedState ) {
if( m_buffer.size() > 0 ) parsHead(m_buffer);
m_buffer.clear();
m_running = false;
emit signal_complete();
}
});
connect(m_pSocket,&QTcpSocket::readyRead,this,&Wget::slot_readyRead);
}
void Wget::get(QString url)
{
QString addr;
QByteArray path;
uint16_t port = 80;
QByteArray buff;
if( url.indexOf("http://",Qt::CaseInsensitive) == 0 ) url.remove(0,7);
for(uint32_t i = 0; i < (unsigned)url.size(); i++){
if(addr.isEmpty()){
if( url[i] == ':' ){
port = 0;
addr = buff;
buff.clear();
continue;
}
if( url[i] == '/' ){
addr = buff;
buff.clear();
}
}else{
if( url[i] == '/' and port == 0 ){
port = buff.toUInt();
buff.clear();
}
}
buff.append( url.at(i) );
}
path.append( buff );
QHostInfo info = QHostInfo::fromName( addr );
for(auto elem:info.addresses()){
m_pSocket->connectToHost( elem, port );
m_pSocket->waitForConnected(1000);
if( m_pSocket->isOpen() ) break;
}
if( m_pSocket->isOpen() && m_pSocket->state() == QAbstractSocket::ConnectedState ){
QByteArray ba;
ba.append("GET " + path + " HTTP/1.1\r\nHost: " + addr + ":" + QString::number(port) + "\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0\r\n\r\n");
m_pSocket->write(ba);
m_pSocket->waitForBytesWritten(100);
m_running = true;
}else{
emit signal_complete();
}
}
void Wget::slot_readyRead()
{
while(m_pSocket->bytesAvailable()) m_buffer.append( m_pSocket->read(1024) );
}
void Wget::parsHead(const QByteArray &data)
{
clearData();
QByteArray buffF1;
QByteArray buffF2;
QByteArray buff;
QString param;
bool first = true;
bool headF = true;
for(unsigned int i=0;i<(unsigned)data.length();i++){
if(data[i] == '\r') continue;
if(param.isEmpty() and data[i] == ':' and data[i+1] == ' '){
param = buff;
buff.clear();
i++;
continue;
}
if(first and data[i] == ' ' and ( buffF1.size() == 0 or buffF2.size() == 0) ){
if(buffF1.size() == 0){
buffF1.append(buff);
}else{
buffF2.append(buff);
}
buff.clear();
continue;
}
if(data[i] == '\n'){
if(first){
bool ok;
buffF2.toInt(&ok);
if(ok){
m_packet.response.proto = buffF1;
m_packet.response.code = buffF2.toInt();
m_packet.response.reason = buff;
}else{
m_packet.request.type = buffF1;
m_packet.request.url = buffF2;
m_packet.request.proto = buff;
}
buffF1.clear();
buffF2.clear();
first = false;
continue;
}
if(headF and param.isEmpty()){
headF = false;
continue;
}
if(headF){
if(param.contains("content-length",Qt::CaseInsensitive)) m_packet.contLen = buff.toUInt();
if(param.contains("content-type",Qt::CaseInsensitive)) m_packet.contType = buff;
buff.clear();
param.clear();
continue;
}
}
buff.append(data.at(i));
}
m_packet.body = buff;
//if(head.contType == "application/sdp") head.sdp = parsSDP(buff);
}
void Wget::clearData()
{
m_packet.body.clear();
m_packet.contType.clear();
}