Cryptography Reference
In-Depth Information
free( auth_string );
return -1;
}
free( proxy_credentials );
free( auth_string );
}
sprintf( connect_command, “\r\n” );
if ( send( connection, connect_command,
strlen( connect_command ), 0 ) == -1 )
{
return -1;
}
// Have to read the response!
while ( ( received = recv( connection, recv_buf,
BUFFER_SIZE, 0 ) ) > 0 )
{
if ( http_status == 0 )
{
if ( !strncmp( recv_buf, “HTTP”, 4 ) )
{
http_status = atoi( recv_buf + 9 );
printf( “interpreted http status code %d\n”, http_status );
}
}
if ( !strcmp( recv_buf + ( received - 4 ), “\r\n\r\n” ) )
{
break;
}
}
return ( http_status == 200 );
}
This ought to look pretty familiar; the fi rst half is the http_get function
from Chapter 1 with a few details changed. If you're so inclined, you can prob-
ably see a way to consolidate these both into a single function. Notice that you
still connect on port 80 to the proxy; the CONNECT command sent includes the
desired port of 443.
Because CONNECT is an HTTP command, the proxy starts by returning an HTTP
response. At the very least, you have to read it in its entirety so that the fi rst
recv command you invoke inside tls_connect doesn't start reading an HTTP
response when it's expecting a ServerHello message. Of course, it's probably
worthwhile to have a look at the response code as well, as in Listing 10-2. If you
mistyped the password, or failed to provide a password to an authenticating
proxy, you get a 407 error code. If this is the case, you should abort the connec-
tion attempt and report an error to the user.
Search WWH ::




Custom Search