forked from cpp-netlib/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_wget.cpp
More file actions
58 lines (48 loc) · 1.56 KB
/
Copy pathsimple_wget.cpp
File metadata and controls
58 lines (48 loc) · 1.56 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
// Copyright (c) Glyn Matthews 2009-2013.
// Copyright 2012 Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//[ simple_wget_main
/*`
This is a very basic clone of wget. It's missing a lot of
features, such as content-type detection, but it does the
fundamental things the same.
It demonstrates the use of the `uri` and the `http::client`.
*/
#include <network/http/client.hpp>
#include <string>
#include <fstream>
#include <iostream>
namespace http = network::http;
namespace {
std::string get_filename(const std::string& path) {
auto index = path.find_last_of('/');
auto filename = path.substr(index + 1);
return filename.empty() ? "index.html" : filename;
}
} // namespace
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " url" << std::endl;
return 1;
}
try {
http::client client;
http::request request{network::uri{std::string{argv[1]}}};
request.version("1.0");
request.append_header("Connection", "close");
request.append_header("User-Agent", "cpp-netlib simple_wget example");
auto future_response = client.get(request);
auto response = future_response.get();
auto filename = get_filename(request.path());
std::cout << "Saving to: " << filename << std::endl;
std::ofstream ofs(filename.c_str());
ofs << response.body() << std::endl;
}
catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}