📄 network.c
字号:
http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION); if(pos>0) { // Extend http_send_request with possibility to do partial content retrieval snprintf(str, 256, "Range: bytes=%d-", 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 ) { return -1; } 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 ); } 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 ) { return -1; } 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,"Error while sending HTTP request: didn't sent all the request\n"); return -1; } http_free( http_hdr ); return fd;}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,"Read failed\n"); http_free( http_hdr ); return NULL; } if( i==0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 -ie- EOF\n"); 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;}inthttp_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) { char *aut; if( *auth_retry==1 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,"Authentication failed\n"); mp_msg(MSGT_NETWORK,MSGL_ERR,"Please use the option -user and -passwd to provide your username/password for a list of URLs,\n"); mp_msg(MSGT_NETWORK,MSGL_ERR,"or form an URL like: http://username:password@hostname/file\n"); return -1; } if( *auth_retry>0 ) { if( url->username ) { free( url->username ); url->username = NULL; } if( url->password ) { free( url->password ); url->password = NULL; } } aut = http_get_field(http_hdr, "WWW-Authenticate"); if( aut!=NULL ) { char *aut_space; aut_space = strstr(aut, "realm="); if( aut_space!=NULL ) aut_space += 6; mp_msg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space); } else { mp_msg(MSGT_NETWORK,MSGL_INFO,"Authentication required\n"); } if( network_username ) { url->username = strdup(network_username); if( url->username==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return -1; } } else { mp_msg(MSGT_NETWORK,MSGL_ERR,"Unable to read the username\n"); mp_msg(MSGT_NETWORK,MSGL_ERR,"Please use the option -user and -passwd to provide your username/password for a list of URLs,\n"); mp_msg(MSGT_NETWORK,MSGL_ERR,"or form an URL like: http://username:password@hostname/file\n"); return -1; } if( network_password ) { url->password = strdup(network_password); if( url->password==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return -1; } } else { mp_msg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password\n"); } (*auth_retry)++; return 0;}inthttp_seek( stream_t *stream, off_t pos ) { HTTP_header_t *http_hdr = NULL; int fd; if( stream==NULL ) return 0; if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream fd = http_send_request( stream->streaming_ctrl->url, pos ); if( fd<0 ) return 0; http_hdr = http_read_response( fd ); if( http_hdr==NULL ) return 0; switch( http_hdr->status_code ) { case 200: case 206: // OK mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); if( http_hdr->body_size>0 ) { if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { http_free( http_hdr ); return -1; } } break; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); close( fd ); fd = -1; } stream->fd = fd; if( http_hdr ) { http_free( http_hdr ); stream->streaming_ctrl->data = NULL; } stream->pos=pos; return 1;}// By using the protocol, the extension of the file or the content-type// we might be able to guess the streaming type.intautodetectProtocol(streaming_ctrl_t *streaming_ctrl, int *fd_out, int *file_format) { HTTP_header_t *http_hdr; unsigned int i; int fd=-1; int redirect; int auth_retry=0; int seekable=0; char *extension; char *content_type; char *next_url; URL_t *url = streaming_ctrl->url; *file_format = DEMUXER_TYPE_UNKNOWN; do { *fd_out = -1; next_url = NULL; extension = NULL; content_type = NULL; redirect = 0; if( url==NULL ) { return -1; } // Checking for PNM:// if( !strcasecmp(url->protocol, "pnm") ) { *file_format = DEMUXER_TYPE_REAL; return 0; } /* * An autodetection based on the extension is not a good idea. * // Get the extension of the file if present if( url->file!=NULL ) { for( i=strlen(url->file) ; i>0 ; i-- ) { if( url->file[i]=='.' ) { extension=(url->file)+i+1; break; } } }extension=NULL; if( extension!=NULL ) { mp_msg(MSGT_NETWORK,MSGL_DBG2,"Extension: %s\n", extension ); // Look for the extension in the extensions table for( i=0 ; i<(sizeof(extensions_table)/sizeof(extensions_table[0])) ; i++ ) { if( !strcasecmp(extension, extensions_table[i].extension) ) { *file_format = extensions_table[i].demuxer_type; return 0; } } }*/ // Checking for RTSP if( !strcasecmp(url->protocol, "rtsp") ) { // Try Real rtsp:// first (it's always built in) // If it fails, try live.com (if compiled in) *file_format = DEMUXER_TYPE_REAL; return 0; // Checking for SIP } else if( !strcasecmp(url->protocol, "sip") ) {#ifdef STREAMING_LIVE_DOT_COM *file_format = DEMUXER_TYPE_RTP; return 0;#else mp_msg(MSGT_NETWORK,MSGL_ERR,"SIP support requires the \"LIVE.COM Streaming Media\" libraries!\n"); return -1;#endif } if(!strcasecmp(url->protocol, "udp") ) { *file_format = DEMUXER_TYPE_UNKNOWN; return 0; } // Old, hacked RTP support, which works for MPEG Streams // RTP streams only: // Checking for RTP if( !strcasecmp(url->protocol, "rtp") ) { if( url->port==0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,"You must enter a port number for RTP streams!\n"); return -1; } return 0; } // Checking for ASF if( !strncasecmp(url->protocol, "mms", 3) ) { *file_format = DEMUXER_TYPE_ASF; return 0; } // HTTP based protocol if( !strcasecmp(url->protocol, "http") || !strcasecmp(url->protocol, "http_proxy") ) { fd = http_send_request( url, 0 ); if( fd<0 ) { return -1; } http_hdr = http_read_response( fd ); if( http_hdr==NULL ) { closesocket( fd ); http_free( http_hdr ); return -1; } *fd_out=fd; if( verbose>0 ) { http_debug_hdr( http_hdr ); } streaming_ctrl->data = (void*)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 ) { char *accept_ranges; if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) seekable = strncmp(accept_ranges,"bytes",5)==0; } // Check if the response is an ICY status_code reason_phrase if( !strcasecmp(http_hdr->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 ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); field_data = NULL; if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); field_data = NULL; if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_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 ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); field_data = NULL; if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); field_data = NULL; // If content-type == video/nsv we most likely have a winamp video stream // otherwise it should be mp3. if there are more types consider adding mime type // handling like later if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) *file_format = DEMUXER_TYPE_NSV; else *file_format = DEMUXER_TYPE_AUDIO; return 0; } case 400: // Server Full mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); return -1; case 401: // Service Unavailable mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); return -1; case 403: // Service Forbidden mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); return -1; case 404: // Resource Not Found mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); return -1; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); return -1; } } // Assume standard http if not ICY 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" ); if( content_type!=NULL ) { char *content_length = NULL; mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "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 ) ) { *file_format = mime_type_table[i].demuxer_type; return seekable; // for streaming_start } } } // Not found in the mime type table, don't fail, // we should try raw HTTP return seekable; // for streaming_start // Redirect case 301: // Permanently case 302: // Temporarily // TODO: RFC 2616, recommand to detect infinite redirection loops next_url = http_get_field( http_hdr, "Location" ); if( next_url!=NULL ) { closesocket( fd ); url_free( url ); streaming_ctrl->url = url = url_new( next_url ); http_free( http_hdr ); redirect = 1; } break; case 401: // Authentication required if( http_authenticate(http_hdr, url, &auth_retry)<0 ) return -1; redirect = 1; break; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); return -1; } } else { mp_msg(MSGT_NETWORK,MSGL_V,"Unknown protocol '%s'\n", url->protocol ); return -1; } } while( redirect ); return -1;}intstreaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {//printf("streaming_bufferize\n"); streaming_ctrl->buffer = (char*)malloc(size); if( streaming_ctrl->buffer==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return -1; } memcpy( streaming_ctrl->buffer, buffer, size ); streaming_ctrl->buffer_size = size; return size;}intnop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { int len=0;//printf("nop_streaming_read\n"); if( stream_ctrl->buffer_size!=0 ) { int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;//printf("%d bytes in buffer\n", stream_ctrl->buffer_size); len = (size<buffer_len)?size:buffer_len; memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len ); stream_ctrl->buffer_pos += len;//printf("buffer_pos = %d\n", stream_ctrl->buffer_pos ); if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) { free( stream_ctrl->buffer ); stream_ctrl->buffer = NULL; stream_ctrl->buffer_size = 0; stream_ctrl->buffer_pos = 0;//printf("buffer cleaned\n"); }//printf("read %d bytes from buffer\n", len ); } if( len<size ) { int ret; ret = recv( fd, buffer+len, size-len, 0 ); if( ret<0 ) { mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno)); } len += ret;//printf("read %d bytes from network\n", len ); } return len;}intnop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) { return -1; // To shut up gcc warning fd++; pos++; stream_ctrl=NULL;}intnop_streaming_start( stream_t *stream ) { HTTP_header_t *http_hdr = NULL; char *next_url=NULL; URL_t *rd_url=NULL; int fd,ret; if( stream==NULL ) return -1; fd = stream->fd; if( fd<0 ) { fd = http_send_request( stream->streaming_ctrl->url, 0 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -