char * http_build_request( HTTP_header_t *http_hdr ) { char *ptr; int len; HTTP_field_t *field; if( http_hdr==NULL ) return NULL; if( http_hdr->uri==NULL ) return NULL; if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); if( !http_hdr->uri || !http_hdr->method) return NULL; //**** Compute the request length // Add the Method line len = strlen(http_hdr->method)+strlen(http_hdr->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 ) { free( http_hdr->buffer ); http_hdr->buffer = NULL; } http_hdr->buffer = malloc(len+1); if( http_hdr->buffer==NULL ) { mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); return NULL; } http_hdr->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, http_hdr->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 ); } return http_hdr->buffer; }
int http_send_request( URL_t *url, off_t pos ) { HTTP_header_t *http_hdr; URL_t *server_url; char str[256]; int fd = -1; 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 ); } else { server_url = url; http_set_uri( http_hdr, 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); if (network_useragent) { snprintf(str, 256, "User-Agent: %s", network_useragent); http_set_field(http_hdr, str); } else http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION); if( strcasecmp(url->protocol, "noicyx") ) http_set_field(http_hdr, "Icy-MetaData: 1"); if(pos>0) { // Extend http_send_request with possibility to do partial content retrieval snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos); http_set_field(http_hdr, str); } if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url ); http_set_field( http_hdr, "Connection: close"); http_add_basic_authentication( http_hdr, url->username, url->password ); if( http_build_request( http_hdr )==NULL ) { goto err_out; } if( proxy ) { if( url->port==0 ) url->port = 8080; // Default port for the proxy server fd = connect2Server( url->hostname, url->port,1 ); url_free( server_url ); server_url = NULL; } else { if( server_url->port==0 ) server_url->port = 80; // Default port for the web server fd = connect2Server( server_url->hostname, server_url->port,1 ); } if( fd<0 ) { goto err_out; } mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer ); ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 ); if( ret!=(int)http_hdr->buffer_size ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest); goto err_out; } http_free( http_hdr ); return fd; err_out: if (fd > 0) closesocket(fd); http_free(http_hdr); if (proxy && server_url) url_free(server_url); return -1; }
int http_send_request( URL_t *url, int64_t pos ) { HTTP_header_t *http_hdr; URL_t *server_url; char str[256]; int fd = -1; 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 ); if (!server_url) { mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1); goto err_out; } http_set_uri( http_hdr, server_url->noauth_url ); } else { server_url = url; http_set_uri( http_hdr, server_url->file ); } if (server_url->port && server_url->port != 80) snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port ); else snprintf(str, sizeof(str), "Host: %s", server_url->hostname ); http_set_field( http_hdr, str); if (network_useragent) snprintf(str, sizeof(str), "User-Agent: %s", network_useragent); else snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version); http_set_field(http_hdr, str); if (network_referrer) { char *referrer = NULL; size_t len = strlen(network_referrer) + 10; // Check len to ensure we don't do something really bad in case of an overflow if (len > 10) referrer = malloc(len); if (referrer == NULL) { mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed); } else { snprintf(referrer, len, "Referer: %s", network_referrer); http_set_field(http_hdr, referrer); free(referrer); } } if( strcasecmp(url->protocol, "noicyx") ) http_set_field(http_hdr, "Icy-MetaData: 1"); if(pos>0) { // Extend http_send_request with possibility to do partial content retrieval snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos); http_set_field(http_hdr, str); } if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url ); if (network_http_header_fields) { int i=0; while (network_http_header_fields[i]) http_set_field(http_hdr, network_http_header_fields[i++]); } http_set_field( http_hdr, "Connection: close"); if (proxy) http_add_basic_proxy_authentication(http_hdr, url->username, url->password); http_add_basic_authentication(http_hdr, server_url->username, server_url->password); if( http_build_request( http_hdr )==NULL ) { goto err_out; } if( proxy ) { if( url->port==0 ) url->port = 8080; // Default port for the proxy server fd = connect2Server( url->hostname, url->port,1 ); url_free( server_url ); server_url = NULL; } else { if( server_url->port==0 ) server_url->port = 80; // Default port for the web server fd = connect2Server( server_url->hostname, server_url->port,1 ); } if( fd<0 ) { goto err_out; } mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer ); ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS ); if( ret!=(int)http_hdr->buffer_size ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest); goto err_out; } http_free( http_hdr ); return fd; err_out: if (fd > 0) closesocket(fd); http_free(http_hdr); if (proxy && server_url) url_free(server_url); return -1; }
static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) { HTTP_header_t *http_hdr; URL_t *url = NULL; URL_t *server_url = NULL; asf_http_streaming_ctrl_t *asf_http_ctrl; char str[250]; char *ptr; int i, enable; int offset_hi=0, offset_lo=0, length=0; int asf_nb_stream=0, stream_id; // Sanity check if( streaming_ctrl==NULL ) return NULL; url = streaming_ctrl->url; asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data; 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 ); // Check if we are using a proxy if( !strcasecmp( url->protocol, "http_proxy" ) ) { server_url = url_new( (url->file)+1 ); if( server_url==NULL ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL); http_free( http_hdr ); return NULL; } http_set_uri( http_hdr, 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 ); 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}" ); 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 ); switch( asf_http_ctrl->streaming_type ) { case ASF_Live_e: case ASF_Prerecorded_e: http_set_field( http_hdr, "Pragma: xPlayStrm=1" ); ptr = str; ptr += sprintf( ptr, "Pragma: stream-switch-entry="); if(asf_http_ctrl->n_audio > 0) { for( i=0; i<asf_http_ctrl->n_audio ; i++ ) { stream_id = asf_http_ctrl->audio_streams[i]; if(stream_id == asf_http_ctrl->audio_id) { enable = 0; } else { enable = 2; continue; } asf_nb_stream++; ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable); } } if(asf_http_ctrl->n_video > 0) { for( i=0; i<asf_http_ctrl->n_video ; i++ ) { stream_id = asf_http_ctrl->video_streams[i]; if(stream_id == asf_http_ctrl->video_id) { enable = 0; } else { enable = 2; continue; } asf_nb_stream++; ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable); } } http_set_field( http_hdr, str ); sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream ); http_set_field( http_hdr, str ); break; case ASF_Redirector_e: break; case ASF_Unknown_e: // First request goes here. break; default: mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType); } http_set_field( http_hdr, "Connection: Close" ); http_build_request( http_hdr ); return http_hdr; }