[Mplayer-cvslog] CVS: main http.c,1.1,1.2 http.h,1.1,1.2

Bertrand Baudet bertrand at users.sourceforge.net
Tue May 29 18:57:50 CEST 2001


Update of /cvsroot/mplayer/main
In directory usw-pr-cvs1:/tmp/cvs-serv3848

Modified Files:
	http.c http.h 
Log Message:
Bugs fix, improvements...


Index: http.c
===================================================================
RCS file: /cvsroot/mplayer/main/http.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** http.c	2001/05/25 13:58:32	1.1
--- http.c	2001/05/29 16:57:48	1.2
***************
*** 1,2 ****
--- 1,8 ----
+ /* 
+  * HTTP Helper
+  * by Bertrand Baudet <bertrand_baudet at yahoo.com>
+  * (C) 2001, MPlayer team.
+  */
+ 
  #include <stdio.h>
  #include <stdlib.h>
***************
*** 21,29 ****
  	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] );
--- 27,36 ----
  	if( http_hdr==NULL ) return;
  	if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
  	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 );
+ 	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] );
***************
*** 31,63 ****
  }
  
! 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;
  		}
  	}
--- 38,99 ----
  }
  
! int
! http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
! 	char *ptr = NULL;
! 	if( http_hdr==NULL || response==NULL ) return -1;
! 	ptr = (char*)malloc( http_hdr->buffer_size+length );
! 	if( ptr==NULL ) {
! 		printf("Memory allocation failed\n");
! 		return -1;
! 	}
! 	if( http_hdr->buffer_size==0 ) {
! 		// Buffer empty, copy response into it.
! 		memcpy( ptr, response, length );
! 		http_hdr->buffer_size = length;
! 	} else {
! 		// Buffer not empty, grow buffer, copy and append the response.
! 		memcpy( ptr, http_hdr->buffer, http_hdr->buffer_size );
! 		free( http_hdr->buffer );
! 		memcpy( ptr+http_hdr->buffer_size, response, length );
! 		http_hdr->buffer_size += length;
! 	}
! 	http_hdr->buffer = ptr;
! 	return http_hdr->buffer_size;
! }
! 
! int
! http_is_header_entired( HTTP_header_t *http_hdr ) {
! 	if( http_hdr==NULL ) return -1;
! 
! 	if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL ) return 0;
! 	else return 1;
! }
! 
! int
! http_response_parse( HTTP_header_t *http_hdr ) {
  	char *hdr_ptr, *ptr;
  	char *field=NULL;
  	int pos_hdr_sep, len;
! 	if( http_hdr==NULL ) return -1;
! 	if( http_hdr->is_parsed ) return 0;
  
  	// Get the protocol
! 	hdr_ptr = strstr( http_hdr->buffer, " " );
  	if( hdr_ptr==NULL ) {
! 		printf("Malformed answer. No space separator found.\n");
! 		return -1;
  	}
! 	len = hdr_ptr-http_hdr->buffer;
  	http_hdr->protocol = (char*)malloc(len+1);
  	if( http_hdr->protocol==NULL ) {
  		printf("Memory allocation failed\n");
! 		return -1;
  	}
! 	strncpy( http_hdr->protocol, http_hdr->buffer, len );
! 	http_hdr->protocol[len]='\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. Unable to get HTTP minor version.\n");
! 			return -1;
  		}
  	}
***************
*** 65,70 ****
  	// Get the status code
  	if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
! 		printf("Malformed answer\n");
! 		return NULL;
  	}
  	hdr_ptr += 4;
--- 101,106 ----
  	// Get the status code
  	if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
! 		printf("Malformed answer. Unable to get status code.\n");
! 		return -1;
  	}
  	hdr_ptr += 4;
***************
*** 73,78 ****
  	ptr = strstr( hdr_ptr, "\r\n" );
  	if( hdr_ptr==NULL ) {
! 		printf("Malformed answer\n");
! 		return NULL;
  	}
  	len = ptr-hdr_ptr;
--- 109,114 ----
  	ptr = strstr( hdr_ptr, "\r\n" );
  	if( hdr_ptr==NULL ) {
! 		printf("Malformed answer. Unable to get the reason phrase.\n");
! 		return -1;
  	}
  	len = ptr-hdr_ptr;
***************
*** 80,84 ****
  	if( http_hdr->reason_phrase==NULL ) {
  		printf("Memory allocation failed\n");
! 		return NULL;
  	}
  	strncpy( http_hdr->reason_phrase, hdr_ptr, len );
--- 116,120 ----
  	if( http_hdr->reason_phrase==NULL ) {
  		printf("Memory allocation failed\n");
! 		return -1;
  	}
  	strncpy( http_hdr->reason_phrase, hdr_ptr, len );
***************
*** 86,102 ****
  
  	// 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;
--- 122,138 ----
  
  	// Set the position of the header separator: \r\n\r\n
! 	ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
  	if( ptr==NULL ) {
! 		printf("Header may be incomplete. No CRLF CRLF found.\n");
! 		return -1;
  	}
! 	pos_hdr_sep = ptr-http_hdr->buffer;
  
! 	hdr_ptr = strstr( http_hdr->buffer, "\r\n" )+2;
  	do {
  		ptr = strstr( hdr_ptr, "\r\n");
  		if( ptr==NULL ) {
  			printf("No CRLF found\n");
! 			return -1;
  		}
  		len = ptr-hdr_ptr;
***************
*** 104,108 ****
  		if( field==NULL ) {
  			printf("Memory allocation failed\n");
! 			return NULL;
  		}
  		strncpy( field, hdr_ptr, len );
--- 140,144 ----
  		if( field==NULL ) {
  			printf("Memory allocation failed\n");
! 			return -1;
  		}
  		strncpy( field, hdr_ptr, len );
***************
*** 110,146 ****
  		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++ ) 
--- 146,199 ----
  		http_set_field( http_hdr, field );
  		hdr_ptr = ptr+2;
! 	} while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
  	
  	if( field!=NULL ) free( field );
  
! 	if( pos_hdr_sep+4<http_hdr->buffer_size ) {
  		// Response has data!
! 		int data_length = http_hdr->buffer_size-(pos_hdr_sep+4);
  		http_hdr->body = (char*)malloc( data_length );
  		if( http_hdr->body==NULL ) {
! 			printf("Memory allocation failed\n");
! 			return -1;
  		}
! 		memcpy( http_hdr->body, http_hdr->buffer+pos_hdr_sep+4, data_length );
  		http_hdr->body_size = data_length;
  	}
  
! 	http_hdr->is_parsed = 1;
! 	return 0;
  }
  
  char *
! http_build_request( HTTP_header_t *http_hdr ) {
  	char *ptr;
  	int i;
+ 	int len;
  	if( http_hdr==NULL ) return NULL;
  
  	if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
  	if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
  
! 	// 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;
! 	len += 2;							// CRLF
! 	if( http_hdr->body!=NULL ) {
! 		len += http_hdr->body_size;
! 	}
! 	if( http_hdr->buffer!=NULL ) {
! 		free( http_hdr->buffer );
! 		http_hdr->buffer = NULL;
! 	}
! 	http_hdr->buffer = (char*)malloc(len);
! 	if( http_hdr->buffer==NULL ) {
! 		printf("Memory allocation failed\n");
! 		return NULL;
! 	}
! 	http_hdr->buffer_size = len;
! 
! 	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++ ) 
***************
*** 150,155 ****
  		memcpy( ptr, http_hdr->body, http_hdr->body_size );
  	}
! 
! 	return request;	
  }
  
--- 203,207 ----
  		memcpy( ptr, http_hdr->body, http_hdr->body_size );
  	}
! 	return http_hdr->buffer;	
  }
  
***************
*** 230,233 ****
--- 282,286 ----
  http_debug_hdr( HTTP_header_t *http_hdr ) {
  	int i;
+ 	if( http_hdr==NULL ) return;
  
  	printf("protocol: %s\n", http_hdr->protocol );
***************
*** 241,244 ****
  	for( i=0 ; i<http_hdr->field_nb ; i++ )
  		printf(" %d - %s\n", i, http_hdr->fields[i] );
- 
  }
--- 294,296 ----

Index: http.h
===================================================================
RCS file: /cvsroot/mplayer/main/http.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** http.h	2001/05/25 13:58:32	1.1
--- http.h	2001/05/29 16:57:48	1.2
***************
*** 1,2 ****
--- 1,8 ----
+ /* 
+  * HTTP Helper
+  * by Bertrand Baudet <bertrand_baudet at yahoo.com>
+  * (C) 2001, MPlayer team.
+  */
+ 
  #ifndef __HTTP_H
  #define __HTTP_H
***************
*** 17,26 ****
  	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 );
--- 23,37 ----
  	char *body;
  	int body_size;
+ 	char *buffer;
+ 	int buffer_size;
+ 	int is_parsed;
  } HTTP_header_t;
  
  HTTP_header_t*	http_new_header();
  void		http_free( HTTP_header_t *http_hdr );
! int		http_response_append( HTTP_header_t *http_hdr, char *data, int length );
! int		http_response_parse( HTTP_header_t *http_hdr );
! int		http_is_header_entired( HTTP_header_t *http_hdr );
! 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 );


_______________________________________________
Mplayer-cvslog mailing list
Mplayer-cvslog at lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/mplayer-cvslog



More information about the MPlayer-cvslog mailing list