Skip to content

Commit 5fa0968

Browse files
committed
Burl: HTTP basic access authentication support
1 parent 21528bd commit 5fa0968

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

example/client/burl/main.cpp

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,48 @@ can_reuse_connection(
165165
return true;
166166
}
167167

168+
void
169+
base64_encode(std::string& dest, core::string_view src)
170+
{
171+
// Adapted from Boost.Beast project
172+
char const* in = static_cast<char const*>(src.data());
173+
static char constexpr tab[] = {
174+
"ABCDEFGHIJKLMNOP"
175+
"QRSTUVWXYZabcdef"
176+
"ghijklmnopqrstuv"
177+
"wxyz0123456789+/"
178+
};
179+
180+
for(auto n = src.size() / 3; n--;)
181+
{
182+
dest.push_back(tab[ (in[0] & 0xfc) >> 2]);
183+
dest.push_back(tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]);
184+
dest.push_back(tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)]);
185+
dest.push_back(tab[ in[2] & 0x3f]);
186+
in += 3;
187+
}
188+
189+
switch(src.size() % 3)
190+
{
191+
case 2:
192+
dest.push_back(tab[ (in[0] & 0xfc) >> 2]);
193+
dest.push_back(tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]);
194+
dest.push_back(tab[ (in[1] & 0x0f) << 2]);
195+
dest.push_back('=');
196+
break;
197+
198+
case 1:
199+
dest.push_back(tab[ (in[0] & 0xfc) >> 2]);
200+
dest.push_back(tab[((in[0] & 0x03) << 4)]);
201+
dest.push_back('=');
202+
dest.push_back('=');
203+
break;
204+
205+
case 0:
206+
break;
207+
}
208+
}
209+
168210
class any_stream
169211
{
170212
public:
@@ -870,8 +912,13 @@ connect_http_proxy(
870912
request.set(field::user_agent, "Boost.Http.Io");
871913
}
872914

873-
// TODO
874-
// request.set(field::proxy_authorization, "");
915+
if(proxy.has_userinfo())
916+
{
917+
auto credentials = proxy.encoded_userinfo().decode();
918+
auto basic_auth = std::string{ "Basic " };
919+
base64_encode(basic_auth, credentials);
920+
request.set(field::proxy_authorization, basic_auth);
921+
}
875922

876923
auto serializer = http_proto::serializer{ http_proto_ctx };
877924
auto parser = http_proto::response_parser{ http_proto_ctx };
@@ -1000,8 +1047,10 @@ create_request(
10001047

10011048
if(vm.count("user"))
10021049
{
1003-
// TODO: use base64 encoding for basic authentication
1004-
request.set(field::authorization, vm.at("user").as<std::string>());
1050+
auto credentials = vm.at("user").as<std::string>();
1051+
auto basic_auth = std::string{ "Basic " };
1052+
base64_encode(basic_auth, credentials);
1053+
request.set(field::authorization, basic_auth);
10051054
}
10061055

10071056
if(vm.count("compressed") && http_proto_has_zlib)

0 commit comments

Comments
 (0)