Sending custom HTTP request headers

HTTP Request Headers

Using the Header Re-Write System in Awesomium, you can send custom headers for certain HTTP requests.

This can be useful for sharing certain in-app information with a remote web-server-- for example, EVE Online uses this feature in their in-game web-browser to make certain websites "aware" of player data so that those sites can display relevant information.

For more information about HTTP requests and headers, please see this article: What's in an HTTP Request?

Header Re-Write System

Using the header re-write system is a two step process: simply define a header (a collection of key/values to send with the HTTP request) and then set a rule to apply the header to certain URLs.

Define a Header

The first step is to define a set of header fields and give the collection a name:

// Define our header fields
Awesomium::HeaderDefinition myHeader;
myHeader["Header-Field-A"] = "value goes here";
myHeader["Header-Field-B"] = "value goes here";

// Register our header fields as "myHeader"
webView->setHeaderDefinition("myHeader", myHeader);

Set a Rule to Apply the Header

The second step is to set a rule to apply the set of header fields:

// Wildcard rule matches all domains, "myHeader" will
// be applied to all HTTP requests
webView->addHeaderRewriteRule(L"http://*", "myHeader");

Using the Header in PHP

You can access the value of a certain header in PHP by using the following:

echo $_SERVER["HTTP_HEADER_FIELD_A"];

As you can see above, to retrieve the value of an HTTP header in PHP, we use $_SERVER["HTTP_x"], where x is the HTTP header name with all the "-" characters replaced with the "_" character.