Skip to content

Instantly share code, notes, and snippets.

@prantlf
Created September 15, 2013 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prantlf/6571166 to your computer and use it in GitHub Desktop.
Save prantlf/6571166 to your computer and use it in GitHub Desktop.
Use case for the BASE64 encoding
/*
USE CASE: Set the Authorization header value in the HTTP response
to the Basic Authentication challenge.
EXAMPLE: Request headers:
WWW-Authenticate: Basic realm="WallyWorld"
Response headers:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
*/
// Username and password to compute the credentials.
string username = ...,
password = ...;
// Compute the header value by concatenating and converting complete strings.
string credentials = "Basic " +
base64::encode<char>(username + ":" + password);
request << header("Authorization", credentials);
// Compute the header value by appending the complete encoded basic
// credentials to the credentials value.
string credentials = "Basic ";
base64::encode(username + ":" + password,
back_inserter(credentials));
request << header("Authorization", credentials);
// Compute the header value by appending the complete encoded basic
// credentials to the credentials stream.
stringstream credentials;
credentials << "Basic ";
base64::encode(username + ":" + password,
ostream_iterator<char>(credentials));
request << header("Authorization", credentials.str());
// Compute the header value by appending the encoded basic credentials
// to the credentials value. Use stateful methods to encode by chunks.
string credentials = "Basic ";
back_insert_iterator<string> appender(credentials);
base64::state<char> rest;
base64::encode(username, appender, rest);
base64::encode(":", appender, rest);
base64::encode(password, appender, rest);
base64::encode_rest(appender, rest);
request << header("Authorization", credentials);
// Compute the header value by appending the encoded basic credentials
// to the credentials stream. Use stateful methods to encode by chunks.
stringstream credentials;
credentials << "Basic ";
ostream_iterator<char> appender(credentials);
base64::encode(username, appender, rest);
base64::encode(":", appender, rest);
base64::encode(password, appender, rest);
base64::encode_rest(appender, rest);
request << header("Authorization", credentials.str());
// Compute the header value by writing the encoded basic credentials
// to the credentials stream. Use manipulators to encode by chunks.
stringstream credentials;
credentials << "Basic " <<
base64::io::encode(username) <<
base64::io::encode(":") <<
base64::io::encode(password) <<
base64::encode_rest<char>;
request << header("Authorization", credentials.str());
@prantlf
Copy link
Author

prantlf commented Sep 15, 2013

Using the method overloads with state may seem too cumbersome in this example, because they're to be used more when all the input is not available at the same time, like the credentials here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment