[Mplayer-cvslog] CVS: main/libmpdemux http.h,1.3,1.4 http.c,1.4,1.5

Bertrand Baudet bertrand at mplayer.dev.hu
Tue Nov 20 23:14:24 CET 2001


Update of /cvsroot/mplayer/main/libmpdemux
In directory mplayer:/var/tmp.root/cvs-serv21930

Modified Files:
	http.h http.c 
Log Message:
Removed the field number limitation.
We can add as much field as we want.


Index: http.h
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/http.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- http.h	26 Oct 2001 18:26:06 -0000	1.3
+++ http.h	20 Nov 2001 22:14:16 -0000	1.4
@@ -7,7 +7,10 @@
 #ifndef __HTTP_H
 #define __HTTP_H
 
-#define HTTP_FIELD_MAX	20
+typedef struct HTTP_field_type {
+	char *field_name;
+	struct HTTP_field_type *next;
+} HTTP_field_t;
 
 typedef struct {
 	char *protocol;
@@ -16,10 +19,13 @@
 	int status_code;
 	char *reason_phrase;
 	int http_minor_version;
-	char *fields[HTTP_FIELD_MAX];
+	// Field variables
+	HTTP_field_t *first_field;
+	HTTP_field_t *last_field;
 	int field_nb;
 	char *field_search;
-	int search_pos;
+	HTTP_field_t *field_search_pos;
+	// Body varibles
 	char *body;
 	int body_size;
 	char *buffer;
@@ -35,7 +41,7 @@
 char* 		http_build_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_field( HTTP_header_t *http_hdr, const char *field_name );
 void		http_set_method( HTTP_header_t *http_hdr, const char *method );
 void		http_set_uri( HTTP_header_t *http_hdr, const char *uri );
 

Index: http.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/http.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- http.c	26 Oct 2001 18:26:06 -0000	1.4
+++ http.c	20 Nov 2001 22:14:16 -0000	1.5
@@ -23,7 +23,7 @@
 
 void
 http_free( HTTP_header_t *http_hdr ) {
-	int i;
+	HTTP_field_t *field, *field2free;
 	if( http_hdr==NULL ) return;
 	if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
 	if( http_hdr->uri!=NULL ) free( http_hdr->uri );
@@ -32,9 +32,14 @@
 	if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
 	if( http_hdr->method!=NULL ) free( http_hdr->method );
 	if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
-	for( i=0 ; i<http_hdr->field_nb ; i++ ) 
-		if( http_hdr->fields[i]!=NULL ) free( http_hdr->fields[i] );
+	field = http_hdr->first_field;
+	while( field!=NULL ) {
+		field2free = field;
+		field = field->next;
+		free( field2free );
+	}
 	free( http_hdr );
+	http_hdr = NULL;
 }
 
 int
@@ -168,8 +173,8 @@
 char *
 http_build_request( HTTP_header_t *http_hdr ) {
 	char *ptr;
-	int i;
 	int len;
+	HTTP_field_t *field;
 	if( http_hdr==NULL ) return NULL;
 
 	if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
@@ -177,8 +182,11 @@
 
 	// Compute the request length
 	len = strlen(http_hdr->method)+strlen(http_hdr->uri)+12;	// Method line
-	for( i=0 ; i<http_hdr->field_nb ; i++ ) 			// Fields
-		len += strlen(http_hdr->fields[i])+2;
+	field = http_hdr->first_field; 					// Fields
+	while( field!=NULL ) {
+		len += strlen(field->field_name)+2;
+		field = field->next;
+	}
 	len += 2;							// CRLF
 	if( http_hdr->body!=NULL ) {
 		len += http_hdr->body_size;
@@ -196,8 +204,11 @@
 
 	ptr = http_hdr->buffer;
 	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] );
+	field = http_hdr->first_field;
+	while( field!=NULL ) {
+		ptr += sprintf( ptr, "%s\r\n", field->field_name );
+		field = field->next;
+	}
 	ptr += sprintf( ptr, "\r\n" );
 	if( http_hdr->body!=NULL ) {
 		memcpy( ptr, http_hdr->body, http_hdr->body_size );
@@ -208,9 +219,8 @@
 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);
+	http_hdr->field_search_pos = http_hdr->first_field;
+	http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 );
 	if( http_hdr->field_search==NULL ) {
 		printf("Memory allocation failed\n");
 		return NULL;
@@ -223,35 +233,49 @@
 http_get_next_field( HTTP_header_t *http_hdr ) {
 	char *ptr;
 	int i;
+	HTTP_field_t *field;
 	if( http_hdr==NULL ) return NULL;
 
-	for( i=http_hdr->search_pos ; i<http_hdr->field_nb ; i++ ) {
-		ptr = strstr( http_hdr->fields[i], ":" );
+	field = http_hdr->field_search_pos;
+	while( field!=NULL ) { 
+		ptr = strstr( field->field_name, ":" );
 		if( ptr==NULL ) return NULL;
-		if( !strncasecmp( http_hdr->fields[i], http_hdr->field_search, ptr-http_hdr->fields[i] ) ) {
+		if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
 			ptr++;	// Skip the column
 			while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
-			http_hdr->search_pos = i+1;
+			http_hdr->field_search_pos = field->next;
 			return ptr;	// return the value without the field name
 		}
+		field = field->next;
 	}
 	return NULL;
 }
 
 void
-http_set_field( HTTP_header_t *http_hdr, const char *field ) {
-	int pos;
-	if( http_hdr==NULL || field==NULL ) return;
+http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
+	HTTP_field_t *new_field;
+	if( http_hdr==NULL || field_name==NULL ) return;
 
-	pos = http_hdr->field_nb;
-
-	http_hdr->fields[pos] = (char*)malloc(strlen(field)+1);
-	if( http_hdr->fields[pos]==NULL ) {
+	new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t));
+	if( new_field==NULL ) {
 		printf("Memory allocation failed\n");
 		return;
 	}
+	new_field->next = NULL;
+	new_field->field_name = (char*)malloc(strlen(field_name)+1);
+	if( new_field->field_name==NULL ) {
+		printf("Memory allocation failed\n");
+		return;
+	}
+	strcpy( new_field->field_name, field_name );
+
+	if( http_hdr->last_field==NULL ) {
+		http_hdr->first_field = new_field;
+	} else {
+		http_hdr->last_field->next = new_field;
+	}
+	http_hdr->last_field = new_field;
 	http_hdr->field_nb++;
-	strcpy( http_hdr->fields[pos], field );
 }
 
 void
@@ -280,7 +304,8 @@
 
 void
 http_debug_hdr( HTTP_header_t *http_hdr ) {
-	int i;
+	HTTP_field_t *field;
+	int i = 0;
 	if( http_hdr==NULL ) return;
 
 	printf("protocol: %s\n", http_hdr->protocol );
@@ -291,6 +316,9 @@
 	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] );
+	field = http_hdr->first_field;
+	while( field!=NULL ) {
+		printf(" %d - %s\n", i++, field->field_name );
+		field = field->next;
+	}
 }




More information about the MPlayer-cvslog mailing list