HTTP_header_t * http_read_response( int fd ) { HTTP_header_t *http_hdr; char response[BUFFER_SIZE]; int i; http_hdr = http_new_header(); if( http_hdr==NULL ) { return NULL; } do { i = recv( fd, response, BUFFER_SIZE, 0 ); if( i<0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed); http_free( http_hdr ); return NULL; } if( i==0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF); http_free( http_hdr ); return NULL; } http_response_append( http_hdr, response, i ); } while( !http_is_header_entire( http_hdr ) ); http_response_parse( http_hdr ); return http_hdr; }
HTTP_header_t * http_read_response_with_content(int fd) { HTTP_header_t *http_hdr; char response[BUFFER_SIZE]; int i; http_hdr = http_new_header(); if (http_hdr == NULL) { return NULL; } do { i = recv(fd, response, BUFFER_SIZE, 0); if (i < 0) { mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ReadFailed); break; } if (i == 0) { mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_Read0CouldBeEOF); break; } http_response_append(http_hdr, response, i); } while (1); if ((http_hdr->buffer_size == 0) || http_response_parse(http_hdr) < 0) { http_free(http_hdr); return NULL; } return http_hdr; }
HTTP_header_t * http_read_response( int fd ) { HTTP_header_t *http_hdr; char response[BUFFER_SIZE]; int i; http_hdr = http_new_header(); if( http_hdr==NULL ) { return NULL; } do { i = recv( fd, response, BUFFER_SIZE, 0 ); if( i<0 ) { mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n"); http_free( http_hdr ); return NULL; } if( i==0 ) { mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n"); http_free( http_hdr ); return NULL; } http_response_append( http_hdr, response, i ); } while( !http_is_header_entire( http_hdr ) ); if (http_response_parse( http_hdr ) < 0) { http_free( http_hdr ); return NULL; } return http_hdr; }
HTTP_header_t * http_read_response(int fd) { HTTP_header_t *http_hdr = NULL; char response[BUFFER_SIZE]; int i; http_hdr = http_new_header(); if (http_hdr == NULL) { return NULL; } memset(response,0,BUFFER_SIZE); do { if(is_file_seq_exit()) { //yliu add:for exit http_free( http_hdr ); OS_PRINTF("\n%s %d stream check :%d\n",__func__,__LINE__,i); return NULL; } i = recv( fd, response, BUFFER_SIZE, 0 ); if( i<0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed); http_free( http_hdr ); return NULL; } if( i==0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF); http_free( http_hdr ); return NULL; } http_response_append( http_hdr, response, i ); } while( !http_is_header_entire( http_hdr ) ); if (http_response_parse( http_hdr ) < 0) { http_free( http_hdr ); return NULL; } return http_hdr; }
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 int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) { HTTP_header_t *http_hdr=NULL; URL_t *url = stream->streaming_ctrl->url; asf_http_streaming_ctrl_t *asf_http_ctrl; char buffer[BUFFER_SIZE]; int i, ret; int fd = stream->fd; int done; int auth_retry = 0; asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t)); if( asf_http_ctrl==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); return -1; } asf_http_ctrl->streaming_type = ASF_Unknown_e; asf_http_ctrl->request = 1; asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL; asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0; stream->streaming_ctrl->data = (void*)asf_http_ctrl; do { done = 1; if( fd>0 ) closesocket( fd ); if( !strcasecmp( url->protocol, "http_proxy" ) ) { if( url->port==0 ) url->port = 8080; } else { if( url->port==0 ) url->port = 80; } fd = connect2Server( url->hostname, url->port, 1); if( fd<0 ) return fd; http_hdr = asf_http_request( stream->streaming_ctrl ); mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer ); for(i=0; i < (int)http_hdr->buffer_size ; ) { int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, DEFAULT_SEND_FLAGS ); if(r <0) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno)); goto err_out; } i += r; } http_free( http_hdr ); http_hdr = http_new_header(); do { i = recv( fd, buffer, BUFFER_SIZE, 0 ); //printf("read: %d\n", i ); if( i<=0 ) { perror("read"); goto err_out; } http_response_append( http_hdr, buffer, i ); } while( !http_is_header_entire( http_hdr ) ); if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { http_hdr->buffer[http_hdr->buffer_size]='\0'; mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer ); } ret = asf_http_parse_response(asf_http_ctrl, http_hdr); if( ret<0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed); goto err_out; } switch( asf_http_ctrl->streaming_type ) { case ASF_Live_e: case ASF_Prerecorded_e: case ASF_PlainText_e: if( http_hdr->body_size>0 ) { if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { goto err_out; } } if( asf_http_ctrl->request==1 ) { if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) { // First request, we only got the ASF header. ret = asf_streaming_parse_header(fd,stream->streaming_ctrl); if(ret < 0) goto err_out; if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) { mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound); goto err_out; } asf_http_ctrl->request++; done = 0; } else { done = 1; } } break; case ASF_Redirector_e: if( http_hdr->body_size>0 ) { if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { goto err_out; } } *demuxer_type = DEMUXER_TYPE_PLAYLIST; done = 1; break; case ASF_Authenticate_e: if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1; asf_http_ctrl->streaming_type = ASF_Unknown_e; done = 0; break; case ASF_Unknown_e: default: mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType); goto err_out; } // Check if we got a redirect. } while(!done); stream->fd = fd; if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) { stream->streaming_ctrl->streaming_read = nop_streaming_read; stream->streaming_ctrl->streaming_seek = nop_streaming_seek; } else { stream->streaming_ctrl->streaming_read = asf_http_streaming_read; stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek; stream->streaming_ctrl->buffering = 1; } stream->streaming_ctrl->status = streaming_playing_e; stream->close = close_s; http_free( http_hdr ); return 0; err_out: if (fd > 0) closesocket(fd); stream->fd = -1; http_free(http_hdr); 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; }