Cryptography Reference
In-Depth Information
return -1;
}
if ( proxy_user )
{
int credentials_len = strlen( proxy_user ) + strlen( proxy_password ) + 1;
char *proxy_credentials = malloc( credentials_len );
char *auth_string = malloc( ( ( credentials_len * 4 ) / 3 ) + 1 );
sprintf( proxy_credentials, “%s:%s”, proxy_user, proxy_password );
base64_encode( proxy_credentials, credentials_len, auth_string );
sprintf( get_command, “Proxy-Authorization: BASIC %s\r\n”, auth_string );
if ( send( connection, get_command, strlen( get_command ), 0 ) == -1 )
{
free( proxy_credentials );
free( auth_string );
return -1;
}
free( proxy_credentials );
free( auth_string );
}
sprintf( get_command, “Connection: close\r\n\r\n” );
Now, if you invoke your http main routine with just a URL, it tries to connect
directly to the target host; if you invoke it with parameters:
./http -p http://user:password@proxy-host:80/ http://some.server.com/path
You connect through an authenticating proxy and request the same page.
Implementing an HTTP Server
Because you probably also want to examine server-side SSL, develop a server-
side HTTP application — what is usually referred to as a web server — and add
SSL support to it, as well. The operation of a web server is pretty straightfor-
ward. It starts by establishing a socket on which to listen for new requests.
By default, it listens on port 80, the standard HTTP port. When a new request
is received, it reads an HTTP request, as described earlier, from the client,
forms an HTTP response that either satisfi es the request or describes an error
condition, and either closes the connection (in the case of HTTP 1.0) or looks
for another request (in the case of HTTP 1.1+).
The main routine in Listing 1-17 illustrates the outer shell of an HTTP
server — or any other internet protocol server, for that matter.
Listing 1-17: “webserver.c” main routine
#define HTTP_PORT 80
int main( int argc, char *argv[ ] )
{
(Continued)
 
Search WWH ::




Custom Search