[Mplayerxp-cvslog] SF.net SVN: mplayerxp:[535] mplayerxp
Brought to you by:
olov
From: <nic...@us...> - 2012-12-10 17:44:23
|
Revision: 535 http://mplayerxp.svn.sourceforge.net/mplayerxp/?rev=535&view=rev Author: nickols_k Date: 2012-12-10 17:44:11 +0000 (Mon, 10 Dec 2012) Log Message: ----------- convert struct HTTP_header_t into class HTTP_Header Modified Paths: -------------- mplayerxp/Makefile mplayerxp/libmpstream/asf_streaming.cpp mplayerxp/libmpstream/cddb.cpp mplayerxp/libmpstream/cookies.cpp mplayerxp/libmpstream/cookies.h mplayerxp/libmpstream/http.cpp mplayerxp/libmpstream/http.h mplayerxp/libmpstream/network.cpp mplayerxp/libmpstream/network.h Modified: mplayerxp/Makefile =================================================================== --- mplayerxp/Makefile 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/Makefile 2012-12-10 17:44:11 UTC (rev 535) @@ -103,7 +103,7 @@ -rm -f mp_config.h mp_config.mak mp_conf_lavc.h version.h -rm -f cpuinfo help_mp.h -dep: +dep: version.h $(CXX) -MM $(CXXFLAGS) $(SRCS) 1>.depend $(DO_MAKE) Modified: mplayerxp/libmpstream/asf_streaming.cpp =================================================================== --- mplayerxp/libmpstream/asf_streaming.cpp 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/asf_streaming.cpp 2012-12-10 17:44:11 UTC (rev 535) @@ -500,18 +500,17 @@ } static int -asf_header_check( HTTP_header_t *http_hdr ) { - ASF_obj_header_t *objh; - if( http_hdr==NULL ) return -1; - if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1; +asf_header_check( HTTP_Header& http_hdr ) { + ASF_obj_header_t *objh; + if( http_hdr.body==NULL || http_hdr.body_size<sizeof(ASF_obj_header_t) ) return -1; - objh = (ASF_obj_header_t*)http_hdr->body; - if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0; - return -1; + objh = (ASF_obj_header_t*)http_hdr.body; + if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0; + return -1; } static ASF_StreamType_e -asf_http_networking_type(char *content_type, char *features, HTTP_header_t *http_hdr ) { +asf_http_networking_type(char *content_type, char *features, HTTP_Header& http_hdr ) { if( content_type==NULL ) return ASF_Unknown_e; if( !strcasecmp(content_type, "application/octet-stream") || !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request @@ -530,7 +529,7 @@ // so we could used mime type to know the stream type, // but guess what? All of them are not well configured. // So we have to check for an asf header :(, but it works :p - if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) { + if( http_hdr.body_size>sizeof(ASF_obj_header_t) ) { if( asf_header_check( http_hdr )==0 ) { MSG_V("=====> ASF Plain text\n"); return ASF_PlainText_e; @@ -563,9 +562,8 @@ return ASF_Unknown_e; } -static HTTP_header_t * -asf_http_request(networking_t *networking) { - HTTP_header_t *http_hdr; +static HTTP_Header* asf_http_request(networking_t *networking) { + HTTP_Header* http_hdr = new(zeromem) HTTP_Header; URL_t *url = NULL; URL_t *server_url = NULL; asf_http_networking_t *asf_http_ctrl; @@ -583,38 +581,37 @@ if( url==NULL || asf_http_ctrl==NULL ) return NULL; // Common header for all requests. - http_hdr = http_new_header(); - http_set_field( http_hdr, "Accept: */*" ); - http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" ); - http_add_basic_authentication( http_hdr, url->username, url->password ); + http_hdr->set_field("Accept: */*" ); + http_hdr->set_field("User-Agent: NSPlayer/4.1.0.3856" ); + http_hdr->add_basic_authentication(url->username, url->password ); // Check if we are using a proxy if( !strcasecmp( url->protocol, "http_proxy" ) ) { server_url = url_new( (url->file)+1 ); if( server_url==NULL ) { MSG_ERR("Invalid proxy URL\n"); - http_free( http_hdr ); + delete http_hdr; return NULL; } - http_set_uri( http_hdr, server_url->url ); + http_hdr->set_uri(server_url->url ); sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port ); url_free( server_url ); } else { - http_set_uri( http_hdr, url->file ); + http_hdr->set_uri(url->file ); sprintf( str, "Host: %.220s:%d", url->hostname, url->port ); } - http_set_field( http_hdr, str ); - http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" ); + http_hdr->set_field(str ); + http_hdr->set_field("Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" ); sprintf(str, "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u", offset_hi, offset_lo, asf_http_ctrl->request, length ); - http_set_field( http_hdr, str ); + http_hdr->set_field( str ); switch( asf_http_ctrl->networking_type ) { case ASF_Live_e: case ASF_Prerecorded_e: - http_set_field( http_hdr, "Pragma: xPlayStrm=1" ); + http_hdr->set_field("Pragma: xPlayStrm=1" ); ptr = str; ptr += sprintf( ptr, "Pragma: stream-switch-entry="); if(asf_http_ctrl->n_audio > 0) { @@ -643,9 +640,9 @@ ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable); } } - http_set_field( http_hdr, str ); + http_hdr->set_field(str ); sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream ); - http_set_field( http_hdr, str ); + http_hdr->set_field( str ); break; case ASF_Redirector_e: break; @@ -656,34 +653,34 @@ MSG_ERR("Unknown asf stream type\n"); } - http_set_field( http_hdr, "Connection: Close" ); - http_build_request( http_hdr ); + http_hdr->set_field("Connection: Close" ); + http_hdr->build_request( ); return http_hdr; } static int -asf_http_parse_response(asf_http_networking_t *asf_http_ctrl, HTTP_header_t *http_hdr ) { +asf_http_parse_response(asf_http_networking_t *asf_http_ctrl, HTTP_Header& http_hdr ) { char *content_type, *pragma; char features[64] = "\0"; size_t len; - if( http_response_parse(http_hdr)<0 ) { + if( http_hdr.response_parse()<0 ) { MSG_ERR("Failed to parse HTTP response\n"); return -1; } - switch( http_hdr->status_code ) { + switch( http_hdr.status_code ) { case 200: break; case 401: // Authentication required return ASF_Authenticate_e; default: - MSG_ERR("Server return %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase); + MSG_ERR("Server return %d:%s\n", http_hdr.status_code, http_hdr.get_reason_phrase()); return -1; } - content_type = http_get_field( http_hdr, "Content-Type"); + content_type = http_hdr.get_field("Content-Type"); - pragma = http_get_field( http_hdr, "Pragma"); + pragma = http_hdr.get_field("Pragma"); while( pragma!=NULL ) { char *comma_ptr=NULL; char *end; @@ -714,14 +711,14 @@ if( pragma[0]==' ' ) pragma++; } } while( comma_ptr!=NULL ); - pragma = http_get_next_field( http_hdr ); + pragma = http_hdr.get_next_field(); } asf_http_ctrl->networking_type = asf_http_networking_type( content_type, features, http_hdr ); return 0; } static MPXP_Rc asf_http_networking_start(Tcp& tcp, networking_t *networking) { - HTTP_header_t *http_hdr=NULL; + HTTP_Header *http_hdr=NULL; URL_t *url = networking->url; asf_http_networking_t *asf_http_ctrl; char buffer[BUFFER_SIZE]; @@ -762,25 +759,25 @@ } i += r; } - http_free( http_hdr ); - http_hdr = http_new_header(); + delete http_hdr; + http_hdr = new(zeromem) HTTP_Header; do { i = tcp.read((uint8_t*)buffer, BUFFER_SIZE); if( i<=0 ) { perror("read"); - http_free( http_hdr ); + delete http_hdr; return MPXP_False; } - http_response_append( http_hdr, buffer, i ); - } while( !http_is_header_entire( http_hdr ) ); + http_hdr->response_append(buffer,i); + } while( !http_hdr->is_header_entire()); if( mp_conf.verbose>0 ) { http_hdr->buffer[http_hdr->buffer_size]='\0'; MSG_DBG2("Response [%s]\n", http_hdr->buffer ); } - ret = asf_http_parse_response(asf_http_ctrl, http_hdr); + ret = asf_http_parse_response(asf_http_ctrl, *http_hdr); if( ret<0 ) { MSG_ERR("Failed to parse header\n"); - http_free( http_hdr ); + delete http_hdr; return MPXP_False; } switch( asf_http_ctrl->networking_type ) { @@ -789,7 +786,7 @@ case ASF_PlainText_e: if( http_hdr->body_size>0 ) { if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); + delete http_hdr; return MPXP_False; } } @@ -810,7 +807,7 @@ case ASF_Redirector_e: if( http_hdr->body_size>0 ) { if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); + delete http_hdr; return MPXP_False; } } @@ -818,7 +815,7 @@ done = 1; break; case ASF_Authenticate_e: - if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return MPXP_False; + if( http_authenticate( *http_hdr, url, &auth_retry)<0 ) return MPXP_False; asf_http_ctrl->networking_type = ASF_Unknown_e; done = 0; break; @@ -826,7 +823,7 @@ default: MSG_ERR("Unknown ASF networking type\n"); tcp.close(); - http_free( http_hdr ); + delete http_hdr; return MPXP_False; } // Check if we got a redirect. @@ -842,7 +839,7 @@ } networking->status = networking_playing_e; - http_free( http_hdr ); + delete http_hdr; return MPXP_Ok; } Modified: mplayerxp/libmpstream/cddb.cpp =================================================================== --- mplayerxp/libmpstream/cddb.cpp 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/cddb.cpp 2012-12-10 17:44:11 UTC (rev 535) @@ -141,53 +141,51 @@ return ((n % 0xff) << 24 | t << 8 | tot_trks); } -static int __FASTCALL__ cddb_http_request(const char *command, int (*reply_parser)(HTTP_header_t*,cddb_data_t*), cddb_data_t *cddb_data) { - char request[4096]; - int ret = 0; - Tcp tcp(cddb_data->libinput,-1); - URL_t *url; - HTTP_header_t *http_hdr; +static int __FASTCALL__ cddb_http_request(const char *command, int (*reply_parser)(HTTP_Header&,cddb_data_t*), cddb_data_t *cddb_data) { + char request[4096]; + int ret = 0; + Tcp tcp(cddb_data->libinput,-1); + URL_t *url; - if( reply_parser==NULL || command==NULL || cddb_data==NULL ) return -1; + if( reply_parser==NULL || command==NULL || cddb_data==NULL ) return -1; - sprintf( request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d", cddb_data->freedb_server, command, cddb_data->cddb_hello.c_str(), cddb_data->freedb_proto_level ); - MSG_V("Request[%s]\n", request ); + sprintf( request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d", cddb_data->freedb_server, command, cddb_data->cddb_hello.c_str(), cddb_data->freedb_proto_level ); + MSG_V("Request[%s]\n", request ); - url = url_new(request); - if( url==NULL ) { - MSG_ERR("Not a valid URL\n"); - return -1; - } + url = url_new(request); + if( url==NULL ) { + MSG_ERR("Not a valid URL\n"); + return -1; + } - if(http_send_request(tcp,url,0)!=MPXP_Ok) { - MSG_ERR("failed to send the http request\n"); - return -1; - } + if(http_send_request(tcp,url,0)!=MPXP_Ok) { + MSG_ERR("failed to send the http request\n"); + return -1; + } - http_hdr = http_read_response( tcp ); - if( http_hdr==NULL ) { - MSG_ERR("Failed to read the http response\n"); - return -1; - } + HTTP_Header* http_hdr = http_read_response( tcp ); + if( http_hdr==NULL ) { + MSG_ERR("Failed to read the http response\n"); + return -1; + } - http_debug_hdr(http_hdr); - MSG_V("body=[%s]\n", http_hdr->body ); + http_hdr->debug_hdr(); - switch(http_hdr->status_code) { - case 200: - ret = reply_parser(http_hdr, cddb_data); - break; - case 400: - MSG_V("Not Found\n"); - break; - default: - MSG_V("Unknown Error code\n"); - } + switch(http_hdr->status_code) { + case 200: + ret = reply_parser(*http_hdr, cddb_data); + break; + case 400: + MSG_V("Not Found\n"); + break; + default: + MSG_V("Unknown Error code\n"); + } - http_free( http_hdr ); - url_free( url ); + delete http_hdr; + url_free( url ); - return ret; + return ret; } static int __FASTCALL__ cddb_read_cache(cddb_data_t *cddb_data) { @@ -265,60 +263,60 @@ return 0; } -static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { - unsigned long disc_id; - char category[100]; - char *ptr=NULL, *ptr2=NULL; - int ret, status; +static int cddb_read_parse(HTTP_Header& http_hdr, cddb_data_t *cddb_data) { + unsigned long disc_id; + char category[100]; + char *ptr=NULL, *ptr2=NULL; + int ret, status; - if( http_hdr==NULL || cddb_data==NULL ) return -1; + if( cddb_data==NULL ) return -1; - ret = sscanf((char*)http_hdr->body, "%d ", &status); - if( ret!=1 ) { + ret = sscanf((char*)http_hdr.body, "%d ", &status); + if( ret!=1 ) { + MSG_ERR("Parse error\n"); + return -1; + } + + switch(status) { + case 210: + ret = sscanf((char*)http_hdr.body, "%d %s %08lx", &status, category, &disc_id); + if( ret!=3 ) { MSG_ERR("Parse error\n"); return -1; - } - - switch(status) { - case 210: - ret = sscanf((char*)http_hdr->body, "%d %s %08lx", &status, category, &disc_id); - if( ret!=3 ) { - MSG_ERR("Parse error\n"); - return -1; - } - // Check if it's a xmcd database file - ptr = strstr((char*)http_hdr->body, "# xmcd"); - if( ptr==NULL ) { - MSG_ERR("Invalid xmcd database file returned\n"); - return -1; - } - // Ok found the beginning of the file - // look for the end - ptr2 = strstr(ptr, "\r\n.\r\n"); - if( ptr2==NULL ) { - ptr2 = strstr(ptr, "\n.\n"); - if( ptr2==NULL ) { - MSG_ERR("Unable to find '.'\n"); - return -1; - } - } - // Ok found the end - // do a sanity check - if( http_hdr->body_size<(unsigned long)(ptr2-ptr) ) { - MSG_ERR("Unexpected fix me\n"); - return -1; - } - cddb_data->xmcd_file = ptr; - cddb_data->xmcd_file_size = ptr2-ptr+2; - cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0'; - // Avoid the http_free function to mp_free the xmcd file...save a mempcy... - http_hdr->body = NULL; - http_hdr->body_size = 0; - return cddb_write_cache(cddb_data); - default: - MSG_ERR("Unhandled code\n"); - } - return 0; + } + // Check if it's a xmcd database file + ptr = strstr((char*)http_hdr.body, "# xmcd"); + if( ptr==NULL ) { + MSG_ERR("Invalid xmcd database file returned\n"); + return -1; + } + // Ok found the beginning of the file + // look for the end + ptr2 = strstr(ptr, "\r\n.\r\n"); + if( ptr2==NULL ) { + ptr2 = strstr(ptr, "\n.\n"); + if( ptr2==NULL ) { + MSG_ERR("Unable to find '.'\n"); + return -1; + } + } + // Ok found the end + // do a sanity check + if( http_hdr.body_size<(unsigned long)(ptr2-ptr) ) { + MSG_ERR("Unexpected fix me\n"); + return -1; + } + cddb_data->xmcd_file = ptr; + cddb_data->xmcd_file_size = ptr2-ptr+2; + cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0'; + // Avoid the http_free function to mp_free the xmcd file...save a mempcy... + http_hdr.body = NULL; + http_hdr.body_size = 0; + return cddb_write_cache(cddb_data); + default: + MSG_ERR("Unhandled code\n"); + } + return 0; } static int __FASTCALL__ cddb_request_titles(cddb_data_t *cddb_data) { @@ -327,12 +325,12 @@ return cddb_http_request(command, cddb_read_parse, cddb_data); } -static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { +static int cddb_query_parse(HTTP_Header& http_hdr, cddb_data_t *cddb_data) { char album_title[100]; char *ptr = NULL; int ret, status; - ret = sscanf((char*)http_hdr->body, "%d ", &status); + ret = sscanf((char*)http_hdr.body, "%d ", &status); if( ret!=1 ) { MSG_ERR("Parse error\n"); return -1; @@ -341,18 +339,18 @@ switch(status) { case 200: // Found exact match - ret = sscanf((char*)http_hdr->body, "%d %s %08lx %s", &status, cddb_data->category.c_str(), &(cddb_data->disc_id), album_title); + ret = sscanf((char*)http_hdr.body, "%d %s %08lx %s", &status, cddb_data->category.c_str(), &(cddb_data->disc_id), album_title); if( ret!=4 ) { MSG_ERR("Parse error\n"); return -1; } - ptr = strstr((char*)http_hdr->body, album_title); + ptr = strstr((char*)http_hdr.body, album_title); if( ptr!=NULL ) { char *ptr2; int len; ptr2 = strstr(ptr, "\n"); if( ptr2==NULL ) { - len = (http_hdr->body_size)-(ptr-(char*)(http_hdr->body)); + len = (http_hdr.body_size)-(ptr-(char*)(http_hdr.body)); } else { len = ptr2-ptr+1; } @@ -367,7 +365,7 @@ break; case 210: // Found exact matches, list follows - ptr = strstr((char*)http_hdr->body, "\n"); + ptr = strstr((char*)http_hdr.body, "\n"); if( ptr==NULL ) { MSG_ERR("Unable to find end of line\n"); return -1; @@ -380,13 +378,13 @@ MSG_ERR("Parse error\n"); return -1; } - ptr = strstr((char*)http_hdr->body, album_title); + ptr = strstr((char*)http_hdr.body, album_title); if( ptr!=NULL ) { char *ptr2; int len; ptr2 = strstr(ptr, "\n"); if( ptr2==NULL ) { - len = (http_hdr->body_size)-(ptr-(char*)(http_hdr->body)); + len = (http_hdr.body_size)-(ptr-(char*)(http_hdr.body)); } else { len = ptr2-ptr+1; } @@ -412,12 +410,12 @@ return -1; } -static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { +static int cddb_proto_level_parse(HTTP_Header& http_hdr, cddb_data_t *cddb_data) { int max; int ret, status; char *ptr; - ret = sscanf((char*)http_hdr->body, "%d ", &status); + ret = sscanf((char*)http_hdr.body, "%d ", &status); if( ret!=1 ) { MSG_ERR("Parse error\n"); return -1; @@ -425,7 +423,7 @@ switch(status) { case 210: - ptr = strstr((char*)http_hdr->body, "max proto:"); + ptr = strstr((char*)http_hdr.body, "max proto:"); if( ptr==NULL ) { MSG_ERR("Parse error\n"); return -1; @@ -447,10 +445,10 @@ return cddb_http_request("stat", cddb_proto_level_parse, cddb_data); } -static int cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) { +static int cddb_freedb_sites_parse(HTTP_Header& http_hdr, cddb_data_t *cddb_data) { int ret, status; UNUSED(cddb_data); - ret = sscanf((char*)http_hdr->body, "%d ", &status); + ret = sscanf((char*)http_hdr.body, "%d ", &status); if( ret!=1 ) { MSG_ERR("Parse error\n"); return -1; Modified: mplayerxp/libmpstream/cookies.cpp =================================================================== --- mplayerxp/libmpstream/cookies.cpp 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/cookies.cpp 2012-12-10 17:44:11 UTC (rev 535) @@ -26,6 +26,7 @@ #define MAX_COOKIES 20 +namespace mpxp { typedef struct cookie_list_type { char *name; char *value; @@ -220,8 +221,7 @@ } /* Take an HTTP_header_t, and insert the correct headers. The cookie files are read if necessary. */ -void -cookies_set(HTTP_header_t * http_hdr, const char *domain, const char *url) +void HTTP_Header::cookies_set(const char *domain, const char *url) { int found_cookies = 0; struct cookie_list_type *cookies[MAX_COOKIES]; @@ -281,7 +281,8 @@ } if (found_cookies) - http_set_field(http_hdr, buf); + set_field(buf); else delete buf; } +} // namespace mpxp Modified: mplayerxp/libmpstream/cookies.h =================================================================== --- mplayerxp/libmpstream/cookies.h 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/cookies.h 2012-12-10 17:44:11 UTC (rev 535) @@ -10,7 +10,4 @@ #include "http.h" -extern void cookies_set(HTTP_header_t * http_hdr, const char *hostname, - const char *url); - #endif Modified: mplayerxp/libmpstream/http.cpp =================================================================== --- mplayerxp/libmpstream/http.cpp 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/http.cpp 2012-12-10 17:44:11 UTC (rev 535) @@ -16,376 +16,323 @@ #include "url.h" #include "stream_msg.h" -HTTP_header_t * -http_new_header() { - HTTP_header_t *http_hdr; - - http_hdr = (HTTP_header_t*)mp_mallocz(sizeof(HTTP_header_t)); - if( http_hdr==NULL ) return NULL; - - return http_hdr; +namespace mpxp { +HTTP_Header::HTTP_Header() {} +HTTP_Header::~HTTP_Header() { + HTTP_field_t *field, *field2free; + if( protocol!=NULL ) delete protocol ; + if( uri!=NULL ) delete uri ; + if( reason_phrase!=NULL ) delete reason_phrase ; + if( field_search!=NULL ) delete field_search ; + if( method!=NULL ) delete method ; + if( buffer!=NULL ) delete buffer ; + field = first_field; + while( field!=NULL ) { + field2free = field; + if (field->field_name) delete field->field_name; + field = field->next; + delete field2free; + } } -void -http_free( HTTP_header_t *http_hdr ) { - HTTP_field_t *field, *field2free; - if( http_hdr==NULL ) return; - if( http_hdr->protocol!=NULL ) delete http_hdr->protocol ; - if( http_hdr->uri!=NULL ) delete http_hdr->uri ; - if( http_hdr->reason_phrase!=NULL ) delete http_hdr->reason_phrase ; - if( http_hdr->field_search!=NULL ) delete http_hdr->field_search ; - if( http_hdr->method!=NULL ) delete http_hdr->method ; - if( http_hdr->buffer!=NULL ) delete http_hdr->buffer ; - field = http_hdr->first_field; - while( field!=NULL ) { - field2free = field; - if (field->field_name) - delete field->field_name; - field = field->next; - delete field2free ; - } - delete http_hdr ; - http_hdr = NULL; -} +int HTTP_Header::response_append(const char *response, int length ) { + if( response==NULL || length<0 ) return -1; -int -http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { - if( http_hdr==NULL || response==NULL || length<0 ) return -1; - - if( (unsigned)length > std::numeric_limits<size_t>::max() - http_hdr->buffer_size - 1) { - MSG_FATAL("Bad size in memory (re)allocation\n"); - return -1; - } - http_hdr->buffer = (char*)mp_realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); - if(http_hdr->buffer ==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return -1; - } - memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); - http_hdr->buffer_size += length; - http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! - return http_hdr->buffer_size; + if( (unsigned)length > std::numeric_limits<size_t>::max() - buffer_size - 1) { + MSG_FATAL("Bad size in memory (re)allocation\n"); + return -1; + } + buffer = (char*)mp_realloc( buffer, buffer_size+length+1 ); + if(buffer ==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return -1; + } + memcpy( buffer+buffer_size, response, length ); + buffer_size += length; + buffer[buffer_size]=0; // close the string! + return buffer_size; } -int -http_is_header_entire( HTTP_header_t *http_hdr ) { - if( http_hdr==NULL ) return -1; - if( http_hdr->buffer==NULL ) return 0; // empty +int HTTP_Header::is_header_entire() const { + if( buffer==NULL ) return 0; // empty - if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && - strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; - return 1; + if( strstr(buffer, "\r\n\r\n")==NULL && + strstr(buffer, "\n\n")==NULL ) return 0; + return 1; } -int -http_response_parse( HTTP_header_t *http_hdr ) { - char *hdr_ptr, *ptr; - char *field=NULL; - int pos_hdr_sep, hdr_sep_len; - size_t len; - if( http_hdr==NULL ) return -1; - if( http_hdr->is_parsed ) return 0; +int HTTP_Header::response_parse( ) { + char *hdr_ptr, *ptr; + char *field=NULL; + int pos_hdr_sep, hdr_sep_len; + size_t len; + if( is_parsed ) return 0; - // Get the protocol - hdr_ptr = strstr( http_hdr->buffer, " " ); - if( hdr_ptr==NULL ) { - MSG_FATAL("Malformed answer. No space separator found.\n"); - return -1; + // Get the protocol + hdr_ptr = strstr( buffer, " " ); + if( hdr_ptr==NULL ) { + MSG_FATAL("Malformed answer. No space separator found.\n"); + return -1; + } + len = hdr_ptr-buffer; + protocol = new char [len+1]; + if( protocol==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return -1; + } + strncpy( protocol, buffer, len ); + protocol[len]='\0'; + if( !strncasecmp( protocol, "HTTP", 4) ) { + if( sscanf( protocol+5,"1.%d", &(http_minor_version) )!=1 ) { + MSG_FATAL("Malformed answer. Unable to get HTTP minor version.\n"); + return -1; } - len = hdr_ptr-http_hdr->buffer; - http_hdr->protocol = (char*)mp_malloc(len+1); - if( http_hdr->protocol==NULL ) { - MSG_FATAL("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 ) { - MSG_FATAL("Malformed answer. Unable to get HTTP minor version.\n"); - return -1; - } - } + } - // Get the status code - if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { - MSG_FATAL("Malformed answer. Unable to get status code.\n"); - return -1; - } - hdr_ptr += 4; + // Get the status code + if( sscanf( ++hdr_ptr, "%d", &status_code )!=1 ) { + MSG_FATAL("Malformed answer. Unable to get status code.\n"); + return -1; + } + hdr_ptr += 4; - // Get the reason phrase - ptr = strstr( hdr_ptr, "\n" ); - if( hdr_ptr==NULL ) { - MSG_FATAL("Malformed answer. Unable to get the reason phrase.\n"); - return -1; - } - len = ptr-hdr_ptr; - http_hdr->reason_phrase = (char*)mp_malloc(len+1); - if( http_hdr->reason_phrase==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return -1; - } - strncpy( http_hdr->reason_phrase, hdr_ptr, len ); - if( http_hdr->reason_phrase[len-1]=='\r' ) { - len--; - } - http_hdr->reason_phrase[len]='\0'; + // Get the reason phrase + ptr = strstr( hdr_ptr, "\n" ); + if( hdr_ptr==NULL ) { + MSG_FATAL("Malformed answer. Unable to get the reason phrase.\n"); + return -1; + } + len = ptr-hdr_ptr; + reason_phrase = new char[len+1]; + if( reason_phrase==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return -1; + } + strncpy( reason_phrase, hdr_ptr, len ); + if( reason_phrase[len-1]=='\r' ) { + len--; + } + reason_phrase[len]='\0'; - // Set the position of the header separator: \r\n\r\n - hdr_sep_len = 4; - ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); + // Set the position of the header separator: \r\n\r\n + hdr_sep_len = 4; + ptr = strstr( buffer, "\r\n\r\n" ); + if( ptr==NULL ) { + ptr = strstr( buffer, "\n\n" ); if( ptr==NULL ) { - ptr = strstr( http_hdr->buffer, "\n\n" ); - if( ptr==NULL ) { - MSG_ERR("Header may be incomplete. No CRLF CRLF found.\n"); - return -1; - } - hdr_sep_len = 2; + MSG_ERR("Header may be incomplete. No CRLF CRLF found.\n"); + return -1; } - pos_hdr_sep = ptr-http_hdr->buffer; + hdr_sep_len = 2; + } + pos_hdr_sep = ptr-buffer; - // Point to the first line after the method line. - hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; - do { - ptr = hdr_ptr; - while( *ptr!='\r' && *ptr!='\n' ) ptr++; - len = ptr-hdr_ptr; - if( len==0 ) break; - field = (char*)mp_realloc(field, len+1); - if( field==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return -1; - } - strncpy( field, hdr_ptr, len ); - field[len]='\0'; - http_set_field( http_hdr, field ); - hdr_ptr = ptr+((*ptr=='\r')?2:1); - } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); + // Point to the first line after the method line. + hdr_ptr = strstr( buffer, "\n" )+1; + do { + ptr = hdr_ptr; + while( *ptr!='\r' && *ptr!='\n' ) ptr++; + len = ptr-hdr_ptr; + if( len==0 ) break; + field = (char*)mp_realloc(field, len+1); + if( field==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return -1; + } + strncpy( field, hdr_ptr, len ); + field[len]='\0'; + set_field( field ); + hdr_ptr = ptr+((*ptr=='\r')?2:1); + } while( hdr_ptr<(buffer+pos_hdr_sep) ); - if( field!=NULL ) delete field ; + if( field!=NULL ) delete field ; - if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { - // Response has data! - http_hdr->body = (unsigned char*)http_hdr->buffer+pos_hdr_sep+hdr_sep_len; - http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); - } + if( pos_hdr_sep+hdr_sep_len<buffer_size ) { + // Response has data! + body = (unsigned char*)buffer+pos_hdr_sep+hdr_sep_len; + body_size = buffer_size-(pos_hdr_sep+hdr_sep_len); + } - http_hdr->is_parsed = 1; - return 0; + is_parsed = 1; + return 0; } -char * -http_build_request( HTTP_header_t *http_hdr ) { - char *ptr, *uri=NULL; - int len; - HTTP_field_t *field; - if( http_hdr==NULL ) return NULL; +char* HTTP_Header::build_request() { + char *ptr; + int len; + HTTP_field_t *field; - if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); - if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); - else { - uri = (char*)mp_malloc(strlen(http_hdr->uri) + 1); - if( uri==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return NULL; - } - strcpy(uri,http_hdr->uri); - } + if( method==NULL ) set_method( "GET"); + if( uri==NULL ) set_uri( "/"); - //**** Compute the request length - // Add the Method line - len = strlen(http_hdr->method)+strlen(uri)+12; - // Add the fields - field = http_hdr->first_field; - while( field!=NULL ) { - len += strlen(field->field_name)+2; - field = field->next; - } - // Add the CRLF - len += 2; - // Add the body - if( http_hdr->body!=NULL ) { - len += http_hdr->body_size; - } - // Free the buffer if it was previously used - if( http_hdr->buffer!=NULL ) { - delete http_hdr->buffer ; - http_hdr->buffer = NULL; - } - http_hdr->buffer = (char*)mp_malloc(len+1); - if( http_hdr->buffer==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return NULL; - } - http_hdr->buffer_size = len; + //**** Compute the request length + // Add the Method line + len = strlen(method)+strlen(uri)+12; + // Add the fields + field = first_field; + while( field!=NULL ) { + len += strlen(field->field_name)+2; + field = field->next; + } + // Add the CRLF + len += 2; + // Add the body + if( body!=NULL ) { + len += body_size; + } + // Free the buffer if it was previously used + if( buffer!=NULL ) { + delete buffer ; + buffer = NULL; + } + buffer = new char [len+1]; + if( buffer==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return NULL; + } + buffer_size = len; - //*** Building the request - ptr = http_hdr->buffer; - // Add the method line - ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); - field = http_hdr->first_field; - // Add the field - while( field!=NULL ) { - ptr += sprintf( ptr, "%s\r\n", field->field_name ); - field = field->next; - } - ptr += sprintf( ptr, "\r\n" ); - // Add the body - if( http_hdr->body!=NULL ) { - memcpy( ptr, http_hdr->body, http_hdr->body_size ); - } + //*** Building the request + ptr = buffer; + // Add the method line + ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", method,uri, http_minor_version ); + field = first_field; + // Add the field + while( field!=NULL ) { + ptr += sprintf( ptr, "%s\r\n", field->field_name ); + field = field->next; + } + ptr += sprintf( ptr, "\r\n" ); + // Add the body + if( body!=NULL ) { + memcpy( ptr, body, body_size ); + } - if( uri ) delete uri ; - return http_hdr->buffer; + return buffer; } -char * -http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { - if( http_hdr==NULL || field_name==NULL ) return NULL; - http_hdr->field_search_pos = http_hdr->first_field; - http_hdr->field_search = (char*)mp_realloc( http_hdr->field_search, strlen(field_name)+1 ); - if( http_hdr->field_search==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return NULL; - } - strcpy( http_hdr->field_search, field_name ); - return http_get_next_field( http_hdr ); +char* HTTP_Header::get_field(const char *field_name ) { + if( field_name==NULL ) return NULL; + field_search_pos = first_field; + field_search = (char*)mp_realloc( field_search, strlen(field_name)+1 ); + if( field_search==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return NULL; + } + strcpy( field_search, field_name ); + return get_next_field(); } -char * -http_get_next_field( HTTP_header_t *http_hdr ) { - char *ptr; - HTTP_field_t *field; - if( http_hdr==NULL ) return NULL; +char* HTTP_Header::get_next_field() { + char *ptr; + HTTP_field_t *field; - field = http_hdr->field_search_pos; - while( field!=NULL ) { - ptr = strstr( field->field_name, ":" ); - if( ptr==NULL ) return NULL; - 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->field_search_pos = field->next; - return ptr; // return the value without the field name - } - field = field->next; + field = field_search_pos; + while( field!=NULL ) { + ptr = strstr( field->field_name, ":" ); + if( ptr==NULL ) return NULL; + if( !strncasecmp( field->field_name, field_search, ptr-(field->field_name) ) ) { + ptr++; // Skip the column + while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some + field_search_pos = field->next; + return ptr; // return the value without the field name } - return NULL; + field = field->next; + } + return NULL; } -void -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; +void HTTP_Header::set_field(const char *field_name ) { + HTTP_field_t *new_field; + if( field_name==NULL ) return; - new_field = (HTTP_field_t*)mp_malloc(sizeof(HTTP_field_t)); - if( new_field==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return; - } - new_field->next = NULL; - new_field->field_name = (char*)mp_malloc(strlen(field_name)+1); - if( new_field->field_name==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - delete new_field; - return; - } - strcpy( new_field->field_name, field_name ); + new_field = new(zeromem) HTTP_field_t; + if( new_field==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + return; + } + new_field->next = NULL; + new_field->field_name = new char [strlen(field_name)+1]; + if( new_field->field_name==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + delete new_field; + 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++; + if( last_field==NULL ) { + first_field = new_field; + } else { + last_field->next = new_field; + } + last_field = new_field; + field_nb++; } -void -http_set_method( HTTP_header_t *http_hdr, const char *method ) { - if( http_hdr==NULL || method==NULL ) return; - - http_hdr->method = (char*)mp_malloc(strlen(method)+1); - if( http_hdr->method==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return; - } - strcpy( http_hdr->method, method ); +void HTTP_Header::set_method( const char *_method ) { + if( _method==NULL ) return; + method=mp_strdup(_method); } -void -http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { - if( http_hdr==NULL || uri==NULL ) return; - - http_hdr->uri = (char*)mp_malloc(strlen(uri)+1); - if( http_hdr->uri==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - return; - } - strcpy( http_hdr->uri, uri ); +void HTTP_Header::set_uri(const char *_uri ) { + if(_uri==NULL ) return; + uri=mp_strdup(_uri); } -int -http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { - char *auth=NULL, *usr_pass=NULL, *b64_usr_pass=NULL; - int encoded_len, pass_len=0, out_len; - int res = -1; - if( http_hdr==NULL || username==NULL ) return -1; +int HTTP_Header::add_basic_authentication( const char *username, const char *password ) { + char *auth=NULL, *usr_pass=NULL, *b64_usr_pass=NULL; + int encoded_len, pass_len=0, out_len; + int res = -1; + if( username==NULL ) return -1; - if( password!=NULL ) { - pass_len = strlen(password); - } + if( password!=NULL ) pass_len = strlen(password); - usr_pass = (char*)mp_malloc(strlen(username)+pass_len+2); - if( usr_pass==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - goto out; - } + usr_pass = new char [strlen(username)+pass_len+2]; + if( usr_pass==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + goto out; + } - sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); + sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); - // Base 64 encode with at least 33% more data than the original size - encoded_len = strlen(usr_pass)*2; - b64_usr_pass = (char*)mp_malloc(encoded_len); - if( b64_usr_pass==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - goto out; - } + // Base 64 encode with at least 33% more data than the original size + encoded_len = strlen(usr_pass)*2; + b64_usr_pass = new char [encoded_len]; + if( b64_usr_pass==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + goto out; + } - out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); - if( out_len<0 ) { - MSG_FATAL("Base64 out overflow\n"); - goto out; - } + out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); + if( out_len<0 ) { + MSG_FATAL("Base64 out overflow\n"); + goto out; + } - b64_usr_pass[out_len]='\0'; + b64_usr_pass[out_len]='\0'; - auth = (char*)mp_malloc(encoded_len+22); - if( auth==NULL ) { - MSG_FATAL("Memory allocation failed\n"); - goto out; - } + auth = new char [encoded_len+22]; + if( auth==NULL ) { + MSG_FATAL("Memory allocation failed\n"); + goto out; + } - sprintf( auth, "Authorization: Basic %s", b64_usr_pass); - http_set_field( http_hdr, auth ); - res = 0; - + sprintf( auth, "Authorization: Basic %s", b64_usr_pass); + set_field( auth ); + res = 0; out: - delete usr_pass ; - delete b64_usr_pass ; - delete auth ; + delete usr_pass ; + delete b64_usr_pass ; + delete auth ; - return res; + return res; } -void -http_debug_hdr( HTTP_header_t *http_hdr ) { - HTTP_field_t *field; - int i = 0; - if( http_hdr==NULL ) return; +void HTTP_Header::debug_hdr( ) { + HTTP_field_t *field; + int i = 0; - MSG_V( "--- HTTP DEBUG HEADER --- START ---\n" + MSG_V( "--- HTTP DEBUG HEADER --- START ---\n" "protocol: [%s]\n" "http minor version: [%d]\n" "uri: [%s]\n" @@ -393,21 +340,21 @@ "status code: [%d]\n" "reason phrase: [%s]\n" "body size: [%d]\n" - , http_hdr->protocol - , http_hdr->http_minor_version - , http_hdr->uri - , http_hdr->method - , http_hdr->status_code - , http_hdr->reason_phrase - , http_hdr->body_size ); + ,protocol + ,http_minor_version + ,uri + ,method + ,status_code + ,reason_phrase + ,body_size ); - MSG_V("Fields:\n"); - field = http_hdr->first_field; - while( field!=NULL ) { - MSG_V(" %d - %s\n", i++, field->field_name ); - field = field->next; - } - MSG_V("--- HTTP DEBUG HEADER --- END ---\n"); + MSG_V("Fields:\n"); + field = first_field; + while( field!=NULL ) { + MSG_V(" %d - %s\n", i++, field->field_name ); + field = field->next; + } + MSG_V("--- HTTP DEBUG HEADER --- END ---\n"); } int @@ -463,5 +410,4 @@ // Output overflow return -1; } - - +}// namespace mpxp Modified: mplayerxp/libmpstream/http.h =================================================================== --- mplayerxp/libmpstream/http.h 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/http.h 2012-12-10 17:44:11 UTC (rev 535) @@ -4,51 +4,59 @@ * (C) 2001, MPlayer team. */ -#ifndef __HTTP_H -#define __HTTP_H +#ifndef __HTTP_H_INCLUDED +#define __HTTP_H_INCLUDED 1 #include "mp_config.h" -typedef struct HTTP_field_type { +namespace mpxp { + struct HTTP_field_t { char *field_name; - struct HTTP_field_type *next; -} HTTP_field_t; + HTTP_field_t *next; + }; -typedef struct { - char *protocol; - char *method; - char *uri; - unsigned int status_code; - char *reason_phrase; - unsigned int http_minor_version; - // Field variables - HTTP_field_t *first_field; - HTTP_field_t *last_field; - unsigned int field_nb; - char *field_search; - HTTP_field_t *field_search_pos; - // Body variables - unsigned char *body; - size_t body_size; - char *buffer; - size_t buffer_size; - unsigned int is_parsed; -} HTTP_header_t; + class HTTP_Header : public Opaque { + public: + HTTP_Header(); + virtual ~HTTP_Header(); -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_entire( 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 ); -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 ); -int http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ); + virtual int response_append(const char *data, int length ); + virtual int response_parse(); + virtual int is_header_entire() const; + virtual char* build_request(); + virtual char* get_field(const char *field_name ); + virtual char* get_next_field(); + virtual void set_field(const char *field_name ); + virtual void set_method(const char *method ); + virtual void set_uri(const char *uri ); + virtual int add_basic_authentication(const char *username, const char *password ); -void http_debug_hdr( HTTP_header_t *http_hdr ); + virtual void debug_hdr(); + virtual void cookies_set(const char *hostname, const char *url); -int base64_encode(const any_t*enc, int encLen, char *out, int outMax); + char* buffer; + size_t buffer_size; + unsigned int status_code; + unsigned char* body; + size_t body_size; + const char* get_reason_phrase() const { return reason_phrase; } + const char* get_protocol() const { return protocol; } + private: + char* protocol; + char* method; + char* uri; + char* reason_phrase; + unsigned int http_minor_version; + // Field variables + HTTP_field_t* first_field; + HTTP_field_t* last_field; + unsigned int field_nb; + char* field_search; + HTTP_field_t* field_search_pos; + // Body variables + unsigned int is_parsed; + }; + + extern int base64_encode(const any_t*enc, int encLen, char *out, int outMax); +} // namespace mpxp #endif // __HTTP_H Modified: mplayerxp/libmpstream/network.cpp =================================================================== --- mplayerxp/libmpstream/network.cpp 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/network.cpp 2012-12-10 17:44:11 UTC (rev 535) @@ -172,48 +172,46 @@ } MPXP_Rc http_send_request(Tcp& tcp, URL_t *url, off_t pos ) { - HTTP_header_t *http_hdr; + HTTP_Header* http_hdr = new(zeromem) HTTP_Header; URL_t *server_url; char str[256]; int ret; int proxy = 0; // Boolean - http_hdr = http_new_header(); - if( !strcasecmp(url->protocol, "http_proxy") ) { proxy = 1; server_url = url_new( (url->file)+1 ); - http_set_uri( http_hdr, server_url->url ); + http_hdr->set_uri(server_url->url ); } else { server_url = url; - http_set_uri( http_hdr, server_url->file ); + http_hdr->set_uri( server_url->file ); } if (server_url->port && server_url->port != 80) snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port ); else snprintf(str, 256, "Host: %s", server_url->hostname ); - http_set_field( http_hdr, str); + http_hdr->set_field(str); if (net_conf.useragent) { snprintf(str, 256, "User-Agent: %s", net_conf.useragent); - http_set_field(http_hdr, str); + http_hdr->set_field(str); } else - http_set_field( http_hdr, "User-Agent: MPlayerXP/"VERSION); + http_hdr->set_field("User-Agent: MPlayerXP/"VERSION); - http_set_field(http_hdr, "Icy-MetaData: 1"); + http_hdr->set_field("Icy-MetaData: 1"); if(pos>0) { // Extend http_send_request with possibility to do partial content retrieval snprintf(str, 256, "Range: bytes=%d-", (int)pos); - http_set_field(http_hdr, str); + http_hdr->set_field(str); } - if (net_conf.cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url ); + if (net_conf.cookies_enabled) http_hdr->cookies_set( server_url->hostname, server_url->url ); - http_set_field( http_hdr, "Connection: closed"); - http_add_basic_authentication( http_hdr, url->username, url->password ); - if( http_build_request( http_hdr )==NULL ) { + http_hdr->set_field( "Connection: closed"); + http_hdr->add_basic_authentication( url->username, url->password ); + if( http_hdr->build_request( )==NULL ) { goto err_out; } @@ -237,46 +235,43 @@ goto err_out; } - http_free( http_hdr ); + delete http_hdr; return MPXP_Ok; err_out: - http_free(http_hdr); + delete http_hdr; if (proxy && server_url) url_free(server_url); return MPXP_False; } -HTTP_header_t* http_read_response( Tcp& tcp ) { - HTTP_header_t *http_hdr; +HTTP_Header* http_read_response( Tcp& tcp ) { + HTTP_Header* http_hdr = new(zeromem) HTTP_Header; char response[BUFFER_SIZE]; int i; - http_hdr = http_new_header(); - if( http_hdr==NULL ) { - return NULL; - } + if( http_hdr==NULL ) return NULL; do { i = tcp.read((uint8_t*)response, BUFFER_SIZE); if( i<0 ) { MSG_ERR("Read failed\n"); - http_free( http_hdr ); + delete http_hdr; return NULL; } if( i==0 ) { MSG_ERR("http_read_response read 0 -ie- EOF\n"); - http_free( http_hdr ); + delete http_hdr; return NULL; } - http_response_append( http_hdr, response, i ); - } while( !http_is_header_entire( http_hdr ) ); - http_response_parse( http_hdr ); + http_hdr->response_append(response, i ); + } while( !http_hdr->is_header_entire() ); + http_hdr->response_parse(); return http_hdr; } int -http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) { +http_authenticate(HTTP_Header& http_hdr, URL_t *url, int *auth_retry) { char *aut; if( *auth_retry==1 ) { @@ -294,7 +289,7 @@ } } - aut = http_get_field(http_hdr, "WWW-Authenticate"); + aut = http_hdr.get_field("WWW-Authenticate"); if( aut!=NULL ) { char *aut_space; aut_space = strstr(aut, "realm="); @@ -327,7 +322,7 @@ } off_t http_seek(Tcp& tcp, networking_t *networking, off_t pos ) { - HTTP_header_t *http_hdr = NULL; + HTTP_Header* http_hdr = NULL; tcp.close(); if(http_send_request(tcp,networking->url, pos)==MPXP_Ok) return 0; @@ -339,22 +334,22 @@ switch( http_hdr->status_code ) { case 200: case 206: // OK - MSG_V("Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); - MSG_V("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); + MSG_V("Content-Type: [%s]\n", http_hdr->get_field("Content-Type") ); + MSG_V("Content-Length: [%s]\n", http_hdr->get_field("Content-Length") ); if( http_hdr->body_size>0 ) { if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); + delete http_hdr; return 0; } } break; default: - MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); + MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->get_reason_phrase()); tcp.close(); } if( http_hdr ) { - http_free( http_hdr ); + delete http_hdr; networking->data = NULL; } @@ -364,7 +359,7 @@ // By using the protocol, the extension of the file or the content-type // we might be able to guess the networking type. static MPXP_Rc autodetectProtocol(networking_t *networking, Tcp& tcp) { - HTTP_header_t *http_hdr=NULL; + HTTP_Header *http_hdr=NULL; unsigned int i; int redirect; int auth_retry=0; @@ -404,34 +399,34 @@ http_hdr = http_read_response(tcp); if( http_hdr==NULL ) goto err_out; - if( mp_conf.verbose ) http_debug_hdr( http_hdr ); + if( mp_conf.verbose ) http_hdr->debug_hdr(); networking->data = (any_t*)http_hdr; // Check if we can make partial content requests and thus seek in http-streams - if( http_hdr!=NULL && http_hdr->status_code==200 ) { + if( http_hdr->status_code==200 ) { char *accept_ranges; - if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) + if( (accept_ranges = http_hdr->get_field("Accept-Ranges")) != NULL ) seekable = strncmp(accept_ranges,"bytes",5)==0?MPXP_Ok:MPXP_False; } // Check if the response is an ICY status_code reason_phrase - if( !strcasecmp(http_hdr->protocol, "ICY") ) { + if( !strcasecmp(http_hdr->get_protocol(), "ICY") ) { switch( http_hdr->status_code ) { case 200: { // OK char *field_data = NULL; // note: I skip icy-notice1 and 2, as they contain html <BR> // and are IMHO useless info ::atmos - if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) + if( (field_data = http_hdr->get_field("icy-name")) != NULL ) MSG_INFO("Name : %s\n", field_data); field_data = NULL; - if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) + if( (field_data = http_hdr->get_field("icy-genre")) != NULL ) MSG_INFO("Genre : %s\n", field_data); field_data = NULL; - if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) + if( (field_data = http_hdr->get_field("icy-url")) != NULL ) MSG_INFO("Website: %s\n", field_data); field_data = NULL; // XXX: does this really mean public server? ::atmos - if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) + if( (field_data = http_hdr->get_field("icy-pub")) != NULL ) MSG_INFO("Public : %s\n", atoi(field_data)?"yes":"no"); field_data = NULL; - if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) + if( (field_data = http_hdr->get_field("icy-br")) != NULL ) MSG_INFO("Bitrate: %skbit/s\n", field_data); field_data = NULL; - if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL ) + if ( (field_data = http_hdr->get_field("content-type")) != NULL ) networking->mime = field_data; return MPXP_Ok; } @@ -457,12 +452,12 @@ switch( http_hdr->status_code ) { case 200: // OK // Look if we can use the Content-Type - content_type = http_get_field( http_hdr, "Content-Type" ); + content_type = http_hdr->get_field("Content-Type" ); if( content_type!=NULL ) { const char *content_length = NULL; MSG_V("Content-Type: [%s]\n", content_type ); - if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) - MSG_V("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length")); + if( (content_length = http_hdr->get_field("Content-Length")) != NULL) + MSG_V("Content-Length: [%s]\n", http_hdr->get_field("Content-Length")); // Check in the mime type table for a demuxer type for( i=0 ; i<(sizeof(mime_type_table)/sizeof(mime_type_table[0])) ; i++ ) { if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { @@ -477,7 +472,7 @@ case 301: // Permanently case 302: // Temporarily // TODO: RFC 2616, recommand to detect infinite redirection loops - next_url = http_get_field( http_hdr, "Location" ); + next_url = http_hdr->get_field("Location" ); if( next_url!=NULL ) { networking->url = url = url_redirect( &url, next_url ); if (!strcasecmp(url->protocol, "mms")) goto err_out; @@ -489,11 +484,11 @@ } break; case 401: // Authentication required - if( http_authenticate(http_hdr, url, &auth_retry)<0 ) goto err_out; + if( http_authenticate(*http_hdr,url, &auth_retry)<0 ) goto err_out; redirect = 1; break; default: - MSG_ERR("Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); + MSG_ERR("Server returned %d: %s\n", http_hdr->status_code, http_hdr->get_reason_phrase()); goto err_out; } } else { @@ -502,8 +497,7 @@ } } while( redirect ); err_out: - http_free( http_hdr ); - http_hdr = NULL; + delete http_hdr; return MPXP_False; } @@ -562,7 +556,7 @@ } MPXP_Rc nop_networking_start(Tcp& tcp,networking_t* networking ) { - HTTP_header_t *http_hdr = NULL; + HTTP_Header *http_hdr = NULL; char *next_url=NULL; URL_t *rd_url=NULL; MPXP_Rc ret; @@ -575,11 +569,11 @@ switch( http_hdr->status_code ) { case 200: // OK - MSG_V("Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); - MSG_V("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); + MSG_V("Content-Type: [%s]\n", http_hdr->get_field("Content-Type") ); + MSG_V("Content-Length: [%s]\n", http_hdr->get_field("Content-Length") ); if( http_hdr->body_size>0 ) { if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); + delete http_hdr; return MPXP_False; } } @@ -588,7 +582,7 @@ case 301: // Permanently case 302: // Temporarily ret=MPXP_False; - next_url = http_get_field( http_hdr, "Location" ); + next_url = http_hdr->get_field("Location" ); if (next_url != NULL) rd_url=url_new(next_url); @@ -608,16 +602,16 @@ case 404: //Not found case 500: //Server Error default: - MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); + MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->get_reason_phrase()); tcp.close(); return MPXP_False; break; } } else { - http_hdr = (HTTP_header_t*)networking->data; + http_hdr = (HTTP_Header*)networking->data; if( http_hdr->body_size>0 ) { if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); + delete http_hdr; networking->data = NULL; return MPXP_False; } @@ -625,7 +619,7 @@ } if( http_hdr ) { - http_free( http_hdr ); + delete http_hdr; networking->data = NULL; } Modified: mplayerxp/libmpstream/network.h =================================================================== --- mplayerxp/libmpstream/network.h 2012-12-10 16:07:37 UTC (rev 534) +++ mplayerxp/libmpstream/network.h 2012-12-10 17:44:11 UTC (rev 535) @@ -3,9 +3,11 @@ * by Bertrand BAUDET <ber...@ya...> * (C) 2001, MPlayer team. */ - #ifndef __NETWORK_H #define __NETWORK_H +#include "mp_config.h" +#include "osdep/mplib.h" +using namespace mpxp; #include <string> @@ -81,9 +83,9 @@ int nop_networking_seek(Tcp& fd, off_t pos, networking_t *stream_ctrl ); MPXP_Rc http_send_request(Tcp& tcp,URL_t *url, off_t pos); -HTTP_header_t *http_read_response(Tcp& fd); +HTTP_Header* http_read_response(Tcp& fd); -int http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry); +int http_authenticate(HTTP_Header& http_hdr, URL_t *url, int *auth_retry); /* * Joey Parrish <jo...@yu...>: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |