-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps.h
More file actions
212 lines (184 loc) · 6 KB
/
https.h
File metadata and controls
212 lines (184 loc) · 6 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
// EZ-HTTPS Copyright 2023 Jim Rogers (jimrogerz@gmail.com).
//
// 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.
#ifndef LIB_HTTPS_H
#define LIB_HTTPS_H
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/version.hpp>
#include <boost/certify/extensions.hpp>
#include <boost/certify/https_verification.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
/** Network adapter for HTTP. */
class HttpAdapter {
public:
// Connects to the HTTP server over TLS.
virtual absl::Status Connect() = 0;
// Executes a request.
virtual absl::StatusOr<
boost::beast::http::response<boost::beast::http::string_body>>
Execute(const boost::beast::http::request<boost::beast::http::string_body>
&request) = 0;
// Returns the hostname.
virtual std::string Hostname() = 0;
// Disconnects from the HTTP server.
virtual void Disconnect() = 0;
};
/** Concrete implementation of HttpAdapter. */
class HttpAdapterImpl : public HttpAdapter {
public:
HttpAdapterImpl(absl::string_view hostname)
: hostname_(std::string(hostname)),
ssl_ctx_(
boost::asio::ssl::context(boost::asio::ssl::context::tls_client)) {
ssl_ctx_.set_verify_mode(
boost::asio::ssl::context::verify_peer |
boost::asio::ssl::context::verify_fail_if_no_peer_cert);
ssl_ctx_.set_default_verify_paths();
boost::certify::enable_native_https_server_verification(ssl_ctx_);
}
absl::StatusOr<boost::beast::http::response<boost::beast::http::string_body>>
Execute(const boost::beast::http::request<boost::beast::http::string_body>
&request) override;
absl::Status Connect() override;
std::string Hostname() override { return hostname_; }
void Disconnect() override { stream_ptr_->next_layer().close(error_); }
private:
std::string hostname_;
boost::asio::io_context ctx_;
boost::asio::ssl::context ssl_ctx_;
boost::system::error_code error_;
std::unique_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>
stream_ptr_;
absl::Status Error() {
return this->error_ ? absl::InternalError(this->error_.message())
: absl::OkStatus();
}
};
/**
* Performs GET, PUT, POST, and DELETE requests over TLS and
* decompresses responses.
*
* Example usage:
*
* #include "https.h"
*
* Https https("api.openai.com");
*
* auto status = https.Connect();
* if (!status.ok()) {
* std::cerr << "Unable to connect: " << status << std::endl;
* return 1;
* }
*
* absl::string_view body = absl::StrFormat(
* "{"
* "\"prompt\": \"%s\","
* "\"n\": %d,"
* "\"size\": \"%s\","
* "\"response_format\": \"%s\""
* "}",
* prompt, num_images, size, image_count == 1 ? "b64_json" : "url");
*
* auto response = https.SetPath("/v1/images/generations")
* .SetContentType("application/json")
* .SetAuthorization(absl::StrCat("Bearer ", kApiKey));
* .Post(body);
*
* https.Close();
*
* if (!response.ok()) {
* std::cerr << "Unable to post request: " << response.status() << std::endl;
* return 1;
* }
*
* std::cout << response->body() << std::endl;
*/
class Https {
public:
Https(std::shared_ptr<HttpAdapter> adapter)
: path_("/"), content_type_(""), authorization_(""),
verb_(boost::beast::http::verb::get), adapter_(adapter) {}
Https(absl::string_view hostname)
: Https(std::make_shared<HttpAdapterImpl>(hostname)) {}
// The path and query of the request.
Https &SetPath(absl::string_view path) {
path_ = std::string(path);
return *this;
}
// The content type.
Https &SetContentType(absl::string_view content_type) {
content_type_ = content_type;
return *this;
}
// The authorization header.
Https &SetAuthorization(absl::string_view authorization) {
authorization_ = authorization;
return *this;
}
// Option to keep the connection alive.
Https &KeepAlive() {
keep_alive_ = true;
return *this;
}
// Connects to the remote server over TLS.
absl::Status Connect() { return adapter_->Connect(); }
// Gets the resource.
absl::StatusOr<boost::beast::http::response<boost::beast::http::string_body>>
Get() {
verb_ = boost::beast::http::verb::get;
return this->Execute();
}
// Posts to a resource.
absl::StatusOr<boost::beast::http::response<boost::beast::http::string_body>>
Post(absl::string_view body) {
body_ = body;
verb_ = boost::beast::http::verb::post;
return this->Execute();
}
// Puts to a resource.
absl::StatusOr<boost::beast::http::response<boost::beast::http::string_body>>
Put(absl::string_view body) {
body_ = body;
verb_ = boost::beast::http::verb::put;
return this->Execute();
}
// Deletes a resource.
absl::StatusOr<boost::beast::http::response<boost::beast::http::string_body>>
Delete() {
verb_ = boost::beast::http::verb::delete_;
return this->Execute();
}
// Closes the connection.
void Close() { adapter_->Disconnect(); }
private:
std::string path_;
std::string content_type_;
std::string authorization_;
std::string body_;
boost::beast::http::verb verb_;
std::shared_ptr<HttpAdapter> adapter_;
bool keep_alive_;
absl::StatusOr<boost::beast::http::response<boost::beast::http::string_body>>
Execute();
};
#endif // LIB_HTTPS_H