changeset 870:f641c96e431b

Some simple code to handle HTTP requests/responses.
author bertrand
date Fri, 25 May 2001 13:58:32 +0000
parents e350849ff400
children ab94c4cf96d8
files http.c http.h
diffstat 2 files changed, 277 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http.c	Fri May 25 13:58:32 2001 +0000
@@ -0,0 +1,244 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "http.h"
+
+HTTP_header_t *
+http_new_header() {
+	HTTP_header_t *http_hdr;
+
+	http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t));
+	if( http_hdr==NULL ) return NULL;
+	memset( http_hdr, 0, sizeof(HTTP_header_t) );
+
+	return http_hdr;
+}
+
+void
+http_free( HTTP_header_t *http_hdr ) {
+	int i;
+	if( http_hdr==NULL ) return;
+	if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
+	if( http_hdr->method!=NULL ) free( http_hdr->method );
+	if( http_hdr->uri!=NULL ) free( http_hdr->uri );
+	if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
+	if( http_hdr->body!=NULL ) free( http_hdr->body );
+	if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
+	for( i=0 ; i<http_hdr->field_nb ; i++ ) 
+		if( http_hdr->fields[i]!=NULL ) free( http_hdr->fields[i] );
+	free( http_hdr );
+}
+
+HTTP_header_t *
+http_new_response( char *response, int length ) {
+	HTTP_header_t *http_hdr;
+	char *hdr_ptr, *ptr;
+	char *field=NULL;
+	int pos_hdr_sep, len;
+	if( response==NULL ) return NULL;
+	
+	http_hdr = http_new_header();
+	if( http_hdr==NULL ) return NULL;
+
+	// Get the protocol
+	hdr_ptr = strstr( response, " " );
+	if( hdr_ptr==NULL ) {
+		printf("Malformed answer\n");
+		return NULL;
+	}
+	len = hdr_ptr-response;
+	http_hdr->protocol = (char*)malloc(len+1);
+	if( http_hdr->protocol==NULL ) {
+		printf("Memory allocation failed\n");
+		return NULL;
+	}
+	strncpy( http_hdr->protocol, response, len );
+	http_hdr->protocol[len+1]='\0';
+	if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
+		if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
+			printf("Malformed answer\n");
+			return NULL;
+		}
+	}
+
+	// Get the status code
+	if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
+		printf("Malformed answer\n");
+		return NULL;
+	}
+	hdr_ptr += 4;
+
+	// Get the reason phrase
+	ptr = strstr( hdr_ptr, "\r\n" );
+	if( hdr_ptr==NULL ) {
+		printf("Malformed answer\n");
+		return NULL;
+	}
+	len = ptr-hdr_ptr;
+	http_hdr->reason_phrase = (char*)malloc(len+1);
+	if( http_hdr->reason_phrase==NULL ) {
+		printf("Memory allocation failed\n");
+		return NULL;
+	}
+	strncpy( http_hdr->reason_phrase, hdr_ptr, len );
+	http_hdr->reason_phrase[len]='\0';
+
+	// Set the position of the header separator: \r\n\r\n
+	ptr = strstr(response, "\r\n\r\n");
+	if( ptr==NULL ) {
+		printf("Malformed answer\n");
+		return NULL;
+	}
+	pos_hdr_sep = ptr-response;
+
+	hdr_ptr = strstr( response, "\r\n" )+2;
+	do {
+		ptr = strstr( hdr_ptr, "\r\n");
+		if( ptr==NULL ) {
+			printf("No CRLF found\n");
+			return NULL;
+		}
+		len = ptr-hdr_ptr;
+		field = (char*)realloc(field, len+1);
+		if( field==NULL ) {
+			printf("Memory allocation failed\n");
+			return NULL;
+		}
+		strncpy( field, hdr_ptr, len );
+		field[len]='\0';
+		http_set_field( http_hdr, field );
+		hdr_ptr = ptr+2;
+	} while( hdr_ptr<(response+pos_hdr_sep) );
+	
+	if( field!=NULL ) free( field );
+
+	if( pos_hdr_sep+4<length ) {
+		// Response has data!
+		int data_length = length-(pos_hdr_sep+4);
+		http_hdr->body = (char*)malloc( data_length );
+		if( http_hdr->body==NULL ) {
+			printf("Failed to allocate memory\n");
+			return NULL;
+		}
+		memcpy( http_hdr->body, response+pos_hdr_sep+4, data_length );
+		http_hdr->body_size = data_length;
+	}
+
+	return http_hdr;
+}
+
+char *
+http_get_request( HTTP_header_t *http_hdr ) {
+	char *request;
+	char *ptr;
+	int i;
+	if( http_hdr==NULL ) return NULL;
+
+	request = (char*)malloc(1024);		// FIXME
+	if( request==NULL ) return NULL;
+
+	if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
+	if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
+
+	ptr = request;
+	ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, http_hdr->uri, http_hdr->http_minor_version );
+	for( i=0 ; i<http_hdr->field_nb ; i++ ) 
+		ptr += sprintf( ptr, "%s\r\n", http_hdr->fields[i] );
+	ptr += sprintf( ptr, "\r\n" );
+	if( http_hdr->body!=NULL ) {
+		memcpy( ptr, http_hdr->body, http_hdr->body_size );
+	}
+
+	return request;	
+}
+
+char *
+http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
+	if( http_hdr==NULL || field_name==NULL ) return NULL;
+	http_hdr->search_pos = 0;
+	if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
+	http_hdr->field_search = (char*)malloc(strlen(field_name)+1);
+	if( http_hdr->field_search==NULL ) {
+		printf("Memory allocation failed\n");
+		return NULL;
+	}
+	strcpy( http_hdr->field_search, field_name );
+	return http_get_next_field( http_hdr );
+}
+
+char *
+http_get_next_field( HTTP_header_t *http_hdr ) {
+	char *ptr;
+	int i;
+	if( http_hdr==NULL ) return NULL;
+
+	for( i=http_hdr->search_pos ; i<http_hdr->field_nb ; i++ ) {
+		ptr = strstr( http_hdr->fields[i], ":" );
+		if( ptr==NULL ) return NULL;
+		if( !strncasecmp( http_hdr->fields[i], http_hdr->field_search, ptr-http_hdr->fields[i] ) ) {
+			ptr++;	// Skip the column
+			while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
+			http_hdr->search_pos = i+1;
+			return ptr;	// return the value without the field name
+		}
+	}
+	return NULL;
+}
+
+void
+http_set_field( HTTP_header_t *http_hdr, const char *field ) {
+	int pos;
+	if( http_hdr==NULL || field==NULL ) return;
+
+	pos = http_hdr->field_nb;
+
+	http_hdr->fields[pos] = (char*)malloc(strlen(field)+1);
+	if( http_hdr->fields[pos]==NULL ) {
+		printf("Memory allocation failed\n");
+		return;
+	}
+	http_hdr->field_nb++;
+	strcpy( http_hdr->fields[pos], field );
+}
+
+void
+http_set_method( HTTP_header_t *http_hdr, const char *method ) {
+	if( http_hdr==NULL || method==NULL ) return;
+
+	http_hdr->method = (char*)malloc(strlen(method)+1);
+	if( http_hdr->method==NULL ) {
+		printf("Memory allocation failed\n");
+		return;
+	}
+	strcpy( http_hdr->method, method );
+}
+
+void
+http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
+	if( http_hdr==NULL || uri==NULL ) return;
+
+	http_hdr->uri = (char*)malloc(strlen(uri)+1);
+	if( http_hdr->uri==NULL ) {
+		printf("Memory allocation failed\n");
+		return;
+	}
+	strcpy( http_hdr->uri, uri );
+}
+
+void
+http_debug_hdr( HTTP_header_t *http_hdr ) {
+	int i;
+
+	printf("protocol: %s\n", http_hdr->protocol );
+	printf("http minor version: %d\n", http_hdr->http_minor_version );
+	printf("uri: %s\n", http_hdr->uri );
+	printf("method: %s\n", http_hdr->method );
+	printf("status code: %d\n", http_hdr->status_code );
+	printf("reason phrase: %s\n", http_hdr->reason_phrase );
+
+	printf("Fields:\n");
+	for( i=0 ; i<http_hdr->field_nb ; i++ )
+		printf(" %d - %s\n", i, http_hdr->fields[i] );
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http.h	Fri May 25 13:58:32 2001 +0000
@@ -0,0 +1,33 @@
+#ifndef __HTTP_H
+#define __HTTP_H
+
+#define HTTP_FIELD_MAX	20
+
+typedef struct {
+	char *protocol;
+	char *method;
+	char *uri;
+	int status_code;
+	char *reason_phrase;
+	int http_minor_version;
+	char *fields[HTTP_FIELD_MAX];
+	int field_nb;
+	char *field_search;
+	int search_pos;
+	char *body;
+	int body_size;
+} HTTP_header_t;
+
+HTTP_header_t*	http_new_header();
+void		http_free( HTTP_header_t *http_hdr );
+HTTP_header_t*	http_new_response( char *data, int length );
+char* 		http_get_request( HTTP_header_t *http_hdr );
+char* 		http_get_field( HTTP_header_t *http_hdr, const char *field_name );
+char*		http_get_next_field( HTTP_header_t *http_hdr );
+void		http_set_field( HTTP_header_t *http_hdr, const char *field );
+void		http_set_method( HTTP_header_t *http_hdr, const char *method );
+void		http_set_uri( HTTP_header_t *http_hdr, const char *uri );
+
+void		http_debug_hdr( HTTP_header_t *http_hdr );
+
+#endif // __HTTP_H