-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
228 lines (201 loc) · 7 KB
/
Copy pathClient.cpp
File metadata and controls
228 lines (201 loc) · 7 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "Client.h"
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
#include "HTTPResponseParser.h"
Client::Client(const std::string &url) { url_parser_.Parse(url); }
bool Client::Connect() {
if (url_parser_.GetProtocol() != "http") {
std::cerr << "Sorry , working only with HTTP protocol" << std::endl;
return false;
}
std::cout << "Protocol = " << url_parser_.GetProtocol()
<< " ,Host= " << url_parser_.GetHost()
<< " , Port = " << url_parser_.GetPort()
<< " ,path=" << url_parser_.GetPath() << std::endl;
addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int status;
if (status = getaddrinfo(url_parser_.GetHost().c_str(), "http", &hints,
&res) != 0) {
std::cerr << "getaddrinfo:" << gai_strerror(status) << std::endl;
return false;
}
char ipstr[INET6_ADDRSTRLEN];
memset(ipstr , 0 , sizeof(char) * INET6_ADDRSTRLEN );
for (addrinfo *p = res; p != NULL; p = p->ai_next) {
void *addr;
std::string ipver = "";
if (p->ai_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "Addres in IPv4 : ";
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "Addres in IPv6 :";
}
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
std::cout << ipver << ipstr << std::endl;
}
socket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (socket_ < 0) {
std::cerr << "Can't create socket" << std::endl;
return false;
}
int port = 80;
if (url_parser_.GetPort() != "") {
std::istringstream str(url_parser_.GetPort());
str >> port;
}
if (res->ai_family == AF_INET) {
((struct sockaddr_in *)(res->ai_addr))->sin_port = htons(port);
} else {
((struct sockaddr_in6 *)(res->ai_addr))->sin6_port = htons(port);
}
if (connect(socket_, res->ai_addr, res->ai_addrlen) < 0) {
std::cerr << "Can't connect" << std::endl;
return false;
} else {
std::cout << " Connected OK \n" << std::endl;
}
return true;
};
const std::string Client::GenerateHTTPRequest(const std::string &host,
const std::string &path) {
std::ostringstream str;
str << "GET " << path << " HTTP/1.1\r\nHost: " << host << "\r\n\r\n\r\n";
return str.str();
}
const std::string Client::GenerateFileName(const std::string &host,
const std::string &path) {
std::string path_copy = path;
std::replace(path_copy.begin(), path_copy.end(), '/', '_');
std::ostringstream str;
str << "data_from_" << host << path_copy;
return str.str();
}
void Client::LoadData() {
std::string http_request =
GenerateHTTPRequest(url_parser_.GetHost(), url_parser_.GetPath());
std::cout << "Request send to server : \n" << http_request << std::endl;
send(socket_, http_request.c_str(), http_request.length(), 0);
const int buffer_size = 10000;
char buffer[buffer_size];
memset(buffer, 0, sizeof(char) * buffer_size);
std::string filename =
GenerateFileName(url_parser_.GetHost(), url_parser_.GetPath());
std::fstream file;
file.open(filename, std::ios_base::out | std::ios_base::in);
int counter = 0;
if (file.is_open()) {
while (1) {
std::ostringstream cntr;
cntr << counter;
file.close();
file.open(filename + cntr.str(), std::ios_base::out | std::ios_base::in);
if (!file.is_open()) {
file.clear();
file.open(filename + cntr.str(), std::ios_base::out);
filename += cntr.str();
break;
}
++counter;
}
} else {
file.clear();
file.open(filename, std::ios_base::out);
}
int data_length = 1;
counter = 0;
HTTPResponseParser http_parser;
int data_length_not_readed = 0;
int content_length = 0;
while ( 1 ) {
data_length = recv(socket_, buffer, 10000, 0);
if (data_length == 0 )
break;
std::string buf = std::string(buffer, buffer + data_length);
if (counter == 0) {
size_t it = buf.find("\r\n\r\n");
if (it != std::string::npos) {
std::string HTTPHeader(buf.begin(), buf.begin() + it + 2);
std::cout << "Http response header: \n\n" << HTTPHeader << std::endl;
http_parser.Parse(HTTPHeader);
if (http_parser.Redirected()) break;
} else
throw std::runtime_error("Invalid HTTP response header");
std::string buff_without_header(buf.begin() + it + 4,
buf.begin() + data_length);
if (buff_without_header.size() != 0) {
if (!http_parser.IsChunkedEncoding()) {
file << buff_without_header;
} else {
std::string str = http_parser.ParseIfChuckedBuff(buff_without_header);
data_length_not_readed = http_parser.GetChunkSize();
if (str == "") break;
file << str;
}
}
content_length = http_parser.GetContentLength();
if (buff_without_header.size() == http_parser.GetContentLength()) break;
content_length -= buff_without_header.size();
} else {
buf = std::string(buf.begin(), buf.begin() + data_length);
if (http_parser.IsChunkedEncoding()) {
if (data_length_not_readed == 0) {
std::string str = http_parser.ParseIfChuckedBuff(buf);
data_length_not_readed = http_parser.GetChunkSize();
if (str == "") break;
file << str;
} else {
if (data_length_not_readed > data_length) {
file << buf;
data_length_not_readed -= data_length;
} else {
file << std::string(buf.begin(),
buf.begin() + data_length_not_readed);
if (std::string(buf.begin() + data_length_not_readed + 2, buf.end())
.size() != 0) {
std::string str = http_parser.ParseIfChuckedBuff(std::string(
buf.begin() + data_length_not_readed + 2, buf.end()));
data_length_not_readed = http_parser.GetChunkSize();
if (str == "" && data_length_not_readed == 0) break;
file << str;
} else
data_length_not_readed = 0;
}
}
} else {
content_length -= buf.size();
file << buf;
if (content_length == 0) break;
}
}
++counter;
}
close(socket_);
if (http_parser.Redirected()) {
std::cout << "\nRedirected URL : " << http_parser.GetRedirectedURL() << "\n"
<< std::endl;
file.close();
std::remove(filename.c_str());
if (http_parser.OnlyPathRedirected())
url_parser_.SetPath(http_parser.GetRedirectedURL());
else
url_parser_.Parse(http_parser.GetRedirectedURL());
Connect();
LoadData();
}
};