Next: , Previous: , Up: Steps in Implementing an Insecure HTTP Client   [Index]


1.3.1.4 Implementing a ‘display_result’ Function

Finally, output the response from the server. To keep things simple, just dump the contents of the response on stdout. An HTTP response has a standard format, just like an HTTP request. The response is the token ‘HTTP/1.0’ or ‘HTTP/1.1’ depending on which version the server understands (which does not necessarily have to match the client’s version), followed by a space, followed by a numeric code indicating the status of the request—errored, rejected, processed, and so on—followed by a textual, human-readable description of the meaning of the status code.

Some of the more common status codes are shown in Table 1-1.

StatusMeaning
200Everything was OK, requested document follows
302Requested document exists, but has been moved – new location follows
403Forbidden: requested document exists, but you are not authorized to view it
404Requested document not found
500Internal server error

Table 1.1: Common status codes

Status codes are described in RFC 2616.

The response status line is followed by a CRLF, and a series of colon-separated, CRLF delimited headers, a standalong CRL/blank line end-of-headers marker, and the document itself.

For testing purposes, you don’t care about the response itself, as long as you got one. Therefore, don’t make any efforts to parse these responses—just dump their contents, verbatim, on stdout, as shown in Listing 1-7.

/**
 * Receive all data available on a connection and dump it to stdout
 */

void display_result( int connection )
{
  int received = 0;

  static char recv_buf[ BUFFER_SIZE + 1 ];

  while ( ( received = recv( connection, recv_buf, BUFFER_SIZE, 0 ) ) > 0 )
  {
    recv_buf[ received ] = '\0';
    printf( "%s", recv_buf );
  }

  printf( "\n" );
}

Listing 1.4: "http.c" display_result

This is all that’s required to implement a bare-bones web client. Note, however, that because the socket created was a cleartext socket, everything that’s transmitted between the client and the server is observable, in plaintext, to every host in between. In general, if you want to protect the transmission from eavesdroppers, you establish SSL context—that is, secure the line—prior to sending the ‘GET’ command.


Next: , Previous: , Up: Steps in Implementing an Insecure HTTP Client   [Index]