Repository navigation
Expand file tree
/
Copy pathdownload.cpp
More file actions
88 lines (75 loc) · 2.28 KB
/
Copy pathdownload.cpp
File metadata and controls
88 lines (75 loc) · 2.28 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
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <map>
#include <string>
#include <boost/shared_ptr.hpp>
#include "download.h"
#include "http/HttpResponse.h"
int main(int argc, char* argv[]) {
bool displayHeaders = false;
std::string host;
ushort port;
std::string uri;
char c;
while ((c = getopt(argc, argv, "d")) != -1) {
switch (c) {
case 'd':
displayHeaders = true;
break;
}
}
// Grab hostname
if (optind < argc) {
host = argv[optind++];
} else {
// Print usage statement, as no hostname was given.
//std::cout << "Simple Web Client" << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << "download [-d] hostname [port [uri]]" << std::endl;
exit(0);
}
// Grab port
if (optind < argc) {
bool valid = true;
int len = strlen(argv[optind]);
for (int i = 0; i < len; i++) {
if (!isdigit(argv[optind][i])) {
valid = false;
}
}
if (valid) {
port = atoi(argv[optind]);
} else {
std::cout << "Invalid port entered. Assuming port 80.\n" << std::endl;
port = 80;
}
optind++;
} else {
port = 80;
}
// Grab uir
if (optind < argc) {
uri = argv[optind++];
} else {
uri = "/";
}
boost::shared_ptr<http::HttpResponse> response = http::load(host, port, uri);
//std::cout << "Headers:" << std::endl;
if (response) {
if (displayHeaders) {
//std::cout << "Request: \n";
std::cout << response->getRequest();
//std::cout << "Status: " << response->getStatusCode() << " (" << response->getStatus() << ")\n" << std::endl;
std::cout << response->getStatus() << std::endl;
for (std::map<std::string, std::string>::const_iterator itr = response->getHeaders().begin(); itr != response->getHeaders().end(); itr++) {
std::cout << itr->first << ": " << itr->second << "\n";
}
std::cout << std::endl;
}
std::cout << response->getContent() << std::endl;
}
exit(0);
}