@@ -165,6 +165,48 @@ can_reuse_connection(
165
165
return true ;
166
166
}
167
167
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
+
168
210
class any_stream
169
211
{
170
212
public:
@@ -870,8 +912,13 @@ connect_http_proxy(
870
912
request.set (field::user_agent, " Boost.Http.Io" );
871
913
}
872
914
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
+ }
875
922
876
923
auto serializer = http_proto::serializer{ http_proto_ctx };
877
924
auto parser = http_proto::response_parser{ http_proto_ctx };
@@ -1000,8 +1047,10 @@ create_request(
1000
1047
1001
1048
if (vm.count (" user" ))
1002
1049
{
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);
1005
1054
}
1006
1055
1007
1056
if (vm.count (" compressed" ) && http_proto_has_zlib)
0 commit comments