forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.cpp
More file actions
116 lines (98 loc) · 3.42 KB
/
Copy pathservice.cpp
File metadata and controls
116 lines (98 loc) · 3.42 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
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memdisk>
#include <net/openssl/init.hpp>
static SSL_CTX* init_ssl_context()
{
auto& disk = fs::memdisk();
disk.init_fs([] (fs::error_t err, auto& fs) {
assert(!err);
err = fs.print_subtree("/certs");
assert(err == fs::no_error && "Need certificate bundle folder present");
});
auto ents = disk.fs().ls("/certs");
// initialize client context
openssl::init();
return openssl::create_client(ents, true);
}
#include <service>
#include <net/http/client.hpp>
using namespace std::literals::string_literals;
static void begin_http(net::Inet& inet)
{
using namespace http;
static Basic_client basic{inet.tcp()};
const auto url{"http://www.google.com"s};
INFO("HTTP", "GET %s", url.c_str());
basic.get(url, {}, [url](Error err, Response_ptr res, Connection&)
{
if(not err) {
printf("\n%s - Got Response!\n%s\n", url.c_str(), res->to_string().c_str());
}
else {
printf("\n%s - No response: %s\n", url.c_str(), err.to_string().c_str());
printf("Make sure the virtual machine can reach internet.\n");
}
});
auto* ctx = init_ssl_context();
assert(ctx != nullptr);
static Client client{inet.tcp(), ctx};
const auto url_sec{"https://www.google.com"s};
INFO("HTTPS", "(Secure) GET %s", url_sec.c_str());
client.get("https://www.google.com", {}, [url = url_sec](Error err, Response_ptr res, Connection&)
{
if(not err) {
printf("\n%s - Got Response!\n%s\n", url.c_str(), res->to_string().c_str());
}
else {
printf("\n%s - No response: %s\n", url.c_str(), err.to_string().c_str());
printf("Make sure the virtual machine can reach internet.\n");
}
});
Client::Options options;
options.follow_redirect = 0;
const auto url_mis{"https://www.facebok.com"s};
client.get(url_mis, {}, [url = url_mis](Error err, Response_ptr res, Connection&)
{
if(not err) {
std::cout << "\n" << url << " - Got response!\n" << res->status_line() << "\n" << res->header() << "\n";
}
else {
printf("\n%s - No response: %s\n", url.c_str(), err.to_string().c_str());
printf("Make sure the virtual machine can reach internet.\n");
}
}, options);
options.follow_redirect = 1;
client.get(url_mis, {}, [url = url_mis](Error err, Response_ptr res, Connection&)
{
if(not err) {
std::cout << "\n" << url << " - Got response!\n" << res->status_line() << "\n" << res->header() << "\n";
}
else {
printf("\n%s - No response: %s\n", url.c_str(), err.to_string().c_str());
printf("Make sure the virtual machine can reach internet.\n");
}
}, options);
}
void Service::start()
{
auto& inet = net::Super_stack::get(0);
inet.on_config(
[] (auto& inet) {
begin_http(inet);
});
}