Thread: Tokenize HTTP server response

  1. #1
    Registered User
    Join Date
    Mar 2009

    Tokenize HTTP server response

    I am writing a simple but secure HTTP/1.0 client, for an assignment. The client should send a GET request to some server, and parse the server response into tokens, such as:

    Initial line

    I've found much documentation about socket programming, but nothing about how to properly tokenize the server response. Can someone point me in the right direction. This is the only way I could get things to somewhat work:
    int clientResponse(int socket, char *version, char *code, char *message, char *server, char *lastModified, char *date, char *contentType, char *contentLength, char *body){
    	char buf[MAX_FILE_SIZE];	// buffer to hold the incoming socket message
    	char *ptrPosition;			// pointer used for seeking
    	int nr=0;					// number of bytes read from socket
    	int numScan=0;				// number of bytes parsed in buffer
    	int numTokens=0;			// number of tokens found
    	char *headerName;			// temp buffer for header field-name
    	char *headerValue;			// temp buffer for header field-value
    	// get message
    	if((nr = getMessage(socket, buf, MAX_FILE_SIZE)) <= 0){
    		return -1;
    	// get starting position of message
    	ptrPosition = buf;
    	if((headerName = malloc(BUF_SIZE)) == NULL){
    		return -2;	
    	if((headerValue = malloc(BUF_SIZE)) == NULL){
    		return -2;
    	// tokenize initial line
    	if((numTokens = sscanf(ptrPosition, "%s %s %255[^\r\n]\r\n%n", version, code, message, &numScan)) != 3){
    		return -3;
    	// move pointer position
    	ptrPosition += numScan;
    	// tokenize headers
    	while((numTokens = sscanf(ptrPosition, "%255[^:]: %255[^\r\n]\r\n%n", headerName, headerValue, &numScan)) == 2){
    		// check for body
    		if(headerName[0] == '<'){
    			strcpy(body, ptrPosition);
    		// move position
    		ptrPosition += numScan;
    		// set appropriate header token
    		if(strcmp(headerName, "Date") == 0){
    			strcpy(date, headerValue);
    		if(strcmp(headerName, "Server") == 0){
    			strcpy(server, headerValue);
    		if(strcmp(headerName, "Last-Modified") == 0){
    			strcpy(lastModified, headerValue);
    		if(strcmp(headerName, "Content-Length") == 0){
    			strcpy(contentLength, headerValue);
    		if(strcmp(headerName, "Content-Type") == 0){
    			strcpy(contentType, headerValue);
    	// deallocate memory
    	return nr;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Moved to Networking/Device Communication forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    The first occurrence of the character ":" separates key from value in the header of HTTP responses. Find that character using strchr() and mark its position. Everything up to the marker is the key, marker + 2 (trailing whitespace) is the value.


    PS: on a general note, your function above is trying to do too much stuff at a time: get response, tokenize response, parse tokens. Each of these steps deserves its own function.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPv6 multicast client fails to receive server response
    By perfect in forum Networking/Device Communication
    Replies: 10
    Last Post: 06-21-2009, 11:30 PM
  2. Help with Parsing HTTP response from server
    By NuNn in forum C Programming
    Replies: 4
    Last Post: 02-19-2009, 03:02 PM
  3. HTTP Response from web browser
    By zort15 in forum C# Programming
    Replies: 0
    Last Post: 06-25-2007, 08:35 AM
  4. HTTP Server?
    By Nebbuchadnezzar in forum Tech Board
    Replies: 9
    Last Post: 07-30-2005, 03:46 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

Tags for this Thread