forked from NOLFXceptMe/http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPClient.cpp
More file actions
504 lines (408 loc) · 10.5 KB
/
Copy pathHTTPClient.cpp
File metadata and controls
504 lines (408 loc) · 10.5 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/* HTTPClient.cpp */
/* An HTTP Server written in C++ */
/* Sweta Yamini 06CS3008
Naveen Kumar Molleti 06CS3009 */
/*
Connect to server
Prepare request
Send request
Receive response
Close connection
*/
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<errno.h>
#include<fcntl.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include"HTTPClient.h"
extern int errno;
using namespace std;
const string HTTPClient::dnsSvrIp = "127.0.0.1";
HTTPClient::HTTPClient(string method, string url, string fileName, string proxy):svrUrl(url), svrPort(80), m_fileName(fileName)
{
if(method == "GET")
m_httpMethod = GET;
else if(method == "PUT")
m_httpMethod = PUT;
else
cerr<<"Unsupported HTTP Method"<<endl;
if(proxy == "direct"){
isProxy = false;
m_httpProxy = "";
}else{
isProxy = true;
m_httpProxy = proxy;
}
}
HTTPClient::~HTTPClient()
{
close(sockfd);
}
int HTTPClient::run()
{
string funcName = "run: ";
m_httpRequest = new HTTPRequest();
m_httpResponse = new HTTPResponse();
if(prepareRequest()){
cerr<<funcName<<"Failed to prepare request"<<endl;
return -1;
}
m_httpRequest->printRequest();
if(initSocket()){
cerr<<funcName<<"Failed to initialize socket"<<endl;
return -1;
}
if(sendRequest()){
cerr<<funcName<<"Failed to send request"<<endl;
}
if(handleResponse()){
cerr<<funcName<<"Failed to receive response"<<endl;
}
delete m_httpRequest;
delete m_httpResponse;
return 0;
}
int HTTPClient::parseProxyURL()
{
if(parseServerURL())
return -1;
size_t posColon = m_httpProxy.find_first_of(":");
svrIp = m_httpProxy.substr(0, posColon);
svrPort = atoi(m_httpProxy.substr(posColon+1).c_str());
m_remoteFileName = svrUrl;
return 0;
}
string HTTPClient::getIp(const string& svrName)
{
string funcName = "getIp: ";
int sockfd_dns;
socklen_t dnsServLen;
struct sockaddr_in dnsServAddr;
struct timeval recvTimeout;
size_t nameLength = svrName.length();
char *buf = new char[nameLength];
memset(buf, '\0', nameLength);
memcpy(buf, svrName.data(), nameLength);
recvTimeout.tv_sec = 2;
recvTimeout.tv_usec = 0;
if((sockfd_dns = socket(AF_INET, SOCK_DGRAM, 0))<0){
cerr<<funcName<<"Unable to open dns socket"<<endl;
return "";
}
setsockopt(sockfd_dns, SOL_SOCKET, SO_RCVTIMEO, &recvTimeout, sizeof(struct timeval));
dnsServAddr.sin_family = AF_INET;
dnsServAddr.sin_port = htons(dnsPort);
dnsServAddr.sin_addr.s_addr = inet_addr(dnsSvrIp.c_str());
if((sendto(sockfd_dns, buf,nameLength , 0,
(struct sockaddr *)&dnsServAddr, sizeof(dnsServAddr)))<0){
cerr<<funcName<<"Sending request failed"<<endl;
}
memset(buf, '\0', nameLength); /* Clear the same buffer and use for getting no. of addresses that will be received */
int data_bytes;
dnsServLen = sizeof(dnsServAddr);
if((data_bytes = recvfrom(sockfd_dns, buf, nameLength, 0, (struct sockaddr *)&dnsServAddr, &dnsServLen))<0){
if(errno == ETIMEDOUT || errno == EAGAIN){
cerr<<"DNS Server did not respond"<<endl;
}
return "";
}
int n_addr = atoi(buf);
if(!n_addr){
printf("Couldn't find IP address for given DNS name\n");
return "";
}
delete buf;
buf = new char[512]; /* 512 bytes */
if((data_bytes = recvfrom(sockfd_dns, buf, 512, 0, (struct sockaddr *)&dnsServAddr, &dnsServLen))<0){
cerr<<"Data receive failed"<<endl;
return "";
}
struct in_addr *addr_list = (struct in_addr *)buf;
delete buf;
return inet_ntoa(addr_list[0]);
}
int HTTPClient::parseServerURL()
{
size_t posParserOld = 0, posParserNew = 0;
string resType = "http://";
string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string svrName;
/* Pass over http:// if any */
if((posParserOld = svrUrl.find_first_of(resType)) != string::npos){
posParserOld += resType.length();
}
/* Extract string till the next / */
string svrNamePort = "";
if((posParserNew = svrUrl.find_first_of("/", posParserOld)) != string::npos){
svrNamePort = svrUrl.substr(posParserOld, posParserNew-posParserOld);
/* Parse into Server DNS Name/IP and Port(if any) */
if((posParserOld = svrNamePort.find_first_of(":")) != string::npos){
svrName = svrNamePort.substr(0, posParserOld);
svrPort = atoi(svrNamePort.substr(posParserOld+1).c_str());
}else{
svrName = svrNamePort;
}
m_host = svrName;
if(!isProxy){
bool isIp = (strpbrk(svrName.c_str(), alphabet.c_str()) == NULL);
if(isIp){
svrIp = svrName;
}else{
svrIp = getIp(svrName);
}
if(svrIp == "")
return -1;
}
}else{
cerr<<"Parse Error!"<<endl;
return -1;
}
posParserOld = posParserNew;
m_remoteFileName = svrUrl.substr(posParserOld);
return 0;
}
int HTTPClient::prepareRequest()
{
string funcName = "prepareRequest: ";
ifstream ifs;
ostringstream os;
size_t contentLength;
if(isProxy){
if(parseProxyURL())
return -1;
}else{
if(parseServerURL())
return -1;
}
m_httpRequest->setMethod(m_httpMethod);
m_httpRequest->setProtocol(HTTP1_0);
m_httpRequest->setURL(m_remoteFileName);
m_httpRequest->setHTTPHeader("Host", m_host);
m_httpRequest->setHTTPHeader("User-Agent", "Awesome HTTP Client");
m_httpRequest->setHTTPHeader("Content-Type", getMimeType(m_fileName));
m_httpRequest->setHTTPHeader("Connection", "close");
switch(m_httpMethod){
case PUT:
/* Open file and write to m_httpRequest->m_requestBody */
ifs.open(m_fileName.c_str(), ifstream::in);
if(ifs.is_open()){
ifs.seekg(0, ifstream::end);
contentLength = ifs.tellg();
ifs.seekg(0, ifstream::beg);
os<<contentLength;
if(m_httpRequest->copyFromFile(ifs, contentLength)){
cerr<<funcName<<"Failed to copy file to Request Body"<<endl;
return -1;
}
m_httpRequest->setHTTPHeader("Content-Length", os.str());
}else{
cerr<<"Unable to read from file"<<endl;
return -1;
}
break;
default:
break;
}
m_httpRequest->prepareRequest();
return 0;
}
int HTTPClient::initSocket()
{
string funcName = "initSocket: ";
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))<0){
cerr<<funcName<<"Failed to create socket"<<endl;
return -1;
}
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(svrPort);
servAddr.sin_addr.s_addr = inet_addr(svrIp.c_str());
if((connect(sockfd, (struct sockaddr *)&servAddr, sizeof(servAddr)))<0){
cerr<<funcName<<"Failed to connect"<<endl;
return -1;
}
return 0;
}
int HTTPClient::sendRequest()
{
string funcName = "sendRequest: ";
size_t requestSize = m_httpRequest->getRequestSize();
string* requestDataPtr = m_httpRequest->getRequestDataPtr();
char *buf = new char[requestSize];
memset(buf, '\0', requestSize);
memcpy(buf, requestDataPtr->data(), requestSize);
if((send(sockfd, buf, requestSize, 0))<0){
cerr<<funcName<<"Sending request failed"<<endl;
}
delete buf;
return 0;
}
int HTTPClient::handleResponse()
{
string funcName = "handleResponse: ";
if(recvResponse()){
cerr<<funcName<<"Failed to receive response"<<endl;
return -1;
}
m_httpResponse->printResponse();
if(parseResponse()){
cerr<<funcName<<"Parsing HTTP Response failed"<<endl;
return -1;
}
if(processResponse()){
cerr<<funcName<<"Parsing HTTP Responsefailed"<<endl;
return -1;
}
return 0;
}
int HTTPClient::recvResponse()
{
string funcName = "recvResponse: ";
int recvLength;
char* buf = new char[buf_sz];
memset(buf, '\0', buf_sz);
if(!(recvLength = recv(sockfd, buf, buf_sz, 0))){
cerr<<funcName<<"Failed to receive response(blocking)"<<endl;
return -1;
}
m_httpResponse->addData(buf, recvLength);
while(1){
memset(buf, '\0', buf_sz);
recvLength = recv(sockfd, buf, buf_sz, MSG_DONTWAIT);
if(recvLength < 0){
if(errno == EWOULDBLOCK || errno == EAGAIN){
cerr<<funcName<<"End of response"<<endl;
}
cerr<<funcName<<"Failed receiving response (nonblocking)"<<endl;
return -1;
}
m_httpResponse->addData(buf, recvLength);
if(recvLength<buf_sz)
break;
}
delete buf;
return 0;
}
int HTTPClient::parseResponse()
{
string funcName = "parseResponse: ";
if(m_httpResponse->parseResponse()){
cerr<<funcName<<"Failed parsing response"<<endl;
return -1;
}
return 0;
}
int HTTPClient::processResponse()
{
string funcName = "processResponse: ";
ofstream ofs;
switch(m_httpMethod){
case GET:
ofs.open(m_fileName.c_str(), ofstream::out|ofstream::trunc);
if(ofs.is_open()){
if(m_httpResponse->copyToFile(ofs))
cerr<<funcName<<"Failed to write to file"<<endl;
}else{
cerr<<funcName<<"Failed to write to file"<<endl;
return -1;
}
ofs.close();
break;
default:
break;
}
return 0;
}
string HTTPClient::getMimeType(string fileName)
{
size_t extPos = fileName.find_last_of(".");
string extension;
string mimeType = "text/plain, charset=us-ascii";
if(extPos == string::npos){
extension = "";
}else{
extension = fileName.substr(extPos+1);
}
/* Compare and return mimetype */
switch(extension[0]){
case 'b':
if(extension == "bmp")
mimeType = "image/bmp";
if(extension == "bin")
mimeType = "application/octet-stream";
break;
case 'c':
if(extension == "csh")
mimeType = "application/csh";
if(extension == "css")
mimeType = "text/css";
break;
case 'd':
if(extension == "doc")
mimeType = "application/msword";
if(extension == "dtd")
mimeType = "application/xml-dtd";
break;
case 'e':
if(extension == "exe")
mimeType = "application/octet-stream";
break;
case 'h':
if(extension == "html" || extension == "htm")
mimeType = "text/html";
break;
case 'i':
if(extension == "ico")
mimeType = "image/x-icon";
break;
case 'g':
if(extension == "gif")
mimeType = "image/gif";
break;
case 'j':
if(extension == "jpeg" || extension == "jpg")
mimeType = "image/jpeg";
break;
case 'l':
if(extension == "latex")
mimeType = "application/x-latex";
break;
case 'p':
if(extension == "png")
mimeType = "image/png";
if(extension == "pgm")
mimeType = "image/x-portable-graymap";
break;
case 'r':
if(extension == "rtf")
mimeType = "text/rtf";
break;
case 's':
if(extension == "svg")
mimeType = "image/svg+xml";
if(extension == "sh")
mimeType = "application/x-sh";
break;
case 't':
if(extension == "tar")
mimeType = "application/x-tar";
if(extension == "tex")
mimeType = "application/x-tex";
if(extension == "tif" || extension == "tiff")
mimeType = "image/tiff";
if(extension == "txt")
mimeType = "text/plain";
break;
case 'x':
if(extension == "xml")
mimeType = "application/xml";
break;
default:
break;
}
return mimeType;
}