C string interpolation

I am building a simple http status check in C. I have part of the network, but I have problems with string manipulations. Here's how it works:

$ ./client http://domain.com/path.html#anchor 200 

This utility simply displays the status of a given page on the command line. I need to parse a given string in the host name and request path. I also created the string "template" with this definition:

 #define HTTP_GET_MSG "GET %s HTTP/1.1\nUser-Agent: my-agent-0.01\nHost: %s\n\n" 

I would like to know how can I go about interpolating the parsed url (host and path) to this specific line before send() to its socket?

+4
source share
1 answer

A simple approach is to use sprintf:

 char req[ SOME_SUITABLE_SIZE ]; sprintf( req, HTTP_GET_MSG, host, path ); 

but it will be disgusting for a buffer overflow unless you check the length of the host and path in advance. If your system has snprintf , you can avoid this:

 snprintf( req, SOME_SUITABLE_SIZE, HTTP_GET_MSG, host, path ); 
+5
source

All Articles