Cryptography Reference
In-Depth Information
Listing C-3: “https.c” http_get with SSLv2 support
int http_get( int connection, const char *path, const char *host,
SSLParameters *ssl_context )
{
static char get_command[ MAX_GET_COMMAND ];
sprintf( get_command, “GET /%s HTTP/1.1\r\n”, path );
if ( ssl_send( connection, get_command, strlen( get_command ),
0, ssl_context ) == -1 )
{
return -1;
}
sprintf( get_command, “Host: %s\r\n”, host );
if ( ssl_send( connection, get_command, strlen( get_command ),
0, ssl_context ) == -1 )
{
return -1;
}
strcpy( get_command, “Connection: Close\r\n\r\n” );
if ( ssl_send( connection, get_command, strlen( get_command ),
0, ssl_context ) == -1 )
{
return -1;
}
return 0;
}
...
int display_result( int connection, SSLParameters *ssl_context )
{
int received = 0;
static char recv_buf[ BUFFER_SIZE + 1 ];
// Can't exit when return value is 0 like in http.c, since empty SSL
// messages can be sent (openssl does this)
while ( ( received = ssl_recv( connection, recv_buf, BUFFER_SIZE,
0, ssl_context ) ) >= 0 )
{
recv_buf[ received ] = '\0';
printf( “data: %s”, recv_buf );
}
printf( “\n” );
}
The application fl ow is exactly the same as before — in fact, other than the
extra call to ssl_connect after the actual socket is connected, you could make
this completely transparent by defi ning a few macros.
Search WWH ::




Custom Search