static int cddb_http_request(char *command, int (*reply_parser)(HTTP_header_t*, cddb_data_t*), cddb_data_t *cddb_data) { char request[4096]; int fd, ret = 0; URL_t *url; HTTP_header_t *http_hdr; 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, cddb_data->freedb_proto_level); mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request); url = url_new(request); if (url == NULL) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_NotAValidURL); return -1; } fd = http_send_request(url,0); if (fd < 0) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToSendHTTPRequest); return -1; } http_hdr = http_read_response(fd); if (http_hdr == NULL) { mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToReadHTTPResponse); return -1; } http_debug_hdr(http_hdr); mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body); switch (http_hdr->status_code) { case 200: ret = reply_parser(http_hdr, cddb_data); break; case 400: mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_HTTPErrorNOTFOUND); break; default: mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_HTTPErrorUnknown); } http_free(http_hdr); url_free(url); return ret; }
static int cddb_http_request(char *command, int (*reply_parser)(HTTP_header_t*, cddb_data_t*), cddb_data_t *cddb_data) { char request[4096]; int fd, ret = 0; URL_t *url; HTTP_header_t *http_hdr; 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, cddb_data->freedb_proto_level); mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request); url = url_new(request); if (url == NULL) { mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n"); return -1; } fd = http_send_request(url,0); if (fd < 0) { mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n"); return -1; } http_hdr = http_read_response(fd); if (http_hdr == NULL) { mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n"); return -1; } http_debug_hdr(http_hdr); mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body); switch (http_hdr->status_code) { case 200: ret = reply_parser(http_hdr, cddb_data); break; case 400: mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n"); break; default: mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n"); } http_free(http_hdr); url_free(url); return ret; }
int http_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; if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) http_debug_hdr( http_hdr ); 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,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase ); closesocket( fd ); fd = -1; } stream->fd = fd; if( http_hdr ) { http_free( http_hdr ); stream->streaming_ctrl->data = NULL; } stream->pos=pos; return 1; }
int http_seek(stream_t *stream, off_t pos) { HTTP_header_t * http_hdr = NULL; int fd = -1; if (stream == NULL) { OS_PRINTF("[%s][ERROR] stream == NULL!!!!\n",__func__); return 0; } //This if removed for network play issue fix, Gavin 2013-09-09 //if (stream->fd > 0) closesocket(stream->fd); // need to reconnect to seek in http-stream //yliu add :for fd = 0 #ifdef __LINUX__ if (stream->fd > 0) { closesocket(stream->fd); // need to reconnect to seek in http-stream } #else if (stream->fd >= 0) { closesocket(stream->fd); stream->fd = -1; } #endif if(is_file_seq_exit()) { return -1; } fd = http_send_request(stream->streaming_ctrl->url, pos); if (fd < 0) { OS_PRINTF("[%s][ERROR] fail to send request fd[%d]!!!!!!!!\n",__func__,fd); return 0; } http_hdr = http_read_response(fd); if (http_hdr == NULL) { //yliu add: closesocket(fd); fd = -1; OS_PRINTF("[%s][ERROR] fail to read response !!!!!!!!\n",__func__); return 0; } stream->streaming_ctrl->chunksize = http_hdr->chunksize; if (mp_msg_test(MSGT_NETWORK, MSGL_V)) { http_debug_hdr(http_hdr); } 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")); OS_PRINTF("[%s] Content-Type: [%s]\n",__func__,http_get_field(http_hdr, "Content-Type")); OS_PRINTF("[%s] Content-Length: [%s]\n",__func__,http_get_field(http_hdr, "Content-Length")); // yliu add :for reconnect //if(pos == 0) { if(http_get_field(http_hdr, "Content-Length")) stream->end_pos = atoll(http_get_field(http_hdr, "Content-Length"))+pos; else stream->end_pos = 0; } OS_PRINTF("[%s] stream->end_pos: [%ld]\n",__func__,stream->end_pos); if (http_hdr->body_size > 0) { if (streaming_bufferize(stream->streaming_ctrl, http_hdr->body, http_hdr->body_size) < 0) { http_free(http_hdr); closesocket(fd); fd = -1; return -1; } } break; default: mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase); closesocket(fd); fd = -1; } //add macro for consistency in Linux Version, yliu 2013-09-10 #if 0 //This if added for network play issue fix, Gavin 2013-09-09 if(stream->fd >= 0) { closesocket(stream->fd); // need to reconnect to seek in http-stream stream->fd = -1; } #endif stream->fd = fd; if (http_hdr) { http_free(http_hdr); stream->streaming_ctrl->data = NULL; } stream->pos = pos; return 1; }
//! If this function succeeds you must closesocket stream->fd static int http_streaming_start(stream_t *stream, int* file_format) { HTTP_header_t *http_hdr = NULL; int fd = stream->fd; int res = STREAM_UNSUPPORTED; int redirect = 0; int auth_retry=0; int seekable=0; char *content_type; const char *content_length; char *next_url; URL_t *url = stream->streaming_ctrl->url; do { redirect = 0; if (fd >= 0) closesocket(fd); fd = http_send_request( url, 0 ); if( fd<0 ) { goto err_out; } http_free(http_hdr); http_hdr = http_read_response( fd ); if( http_hdr==NULL ) { goto err_out; } if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { http_debug_hdr( 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 ) { const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges"); const char *server = http_get_field(http_hdr, "Server"); if (accept_ranges) seekable = strncmp(accept_ranges,"bytes",5)==0; else if (server && (strcmp(server, "gvs 1.0") == 0 || strncmp(server, "MakeMKV", 7) == 0)) { // HACK for youtube and MakeMKV incorrectly claiming not to support seeking mp_msg(MSGT_NETWORK, MSGL_WARN, "Broken webserver, incorrectly claims to not support Accept-Ranges\n"); seekable = 1; } } print_icy_metadata(http_hdr); // Check if the response is an ICY status_code reason_phrase if( !strcasecmp(http_hdr->protocol, "ICY") || http_get_field(http_hdr, "Icy-MetaInt") ) { switch( http_hdr->status_code ) { case 200: { // OK char *field_data; // 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 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) *file_format = DEMUXER_TYPE_AAC; else *file_format = DEMUXER_TYPE_LAVF; res = STREAM_ERROR; goto out; } case 400: // Server Full mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); goto err_out; case 401: // Service Unavailable mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); goto err_out; case 403: // Service Forbidden mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); goto err_out; case 404: // Resource Not Found mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); goto err_out; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); goto err_out; } } // Assume standard http if not ICY switch( http_hdr->status_code ) { case 200: // OK content_length = http_get_field(http_hdr, "Content-Length"); if (content_length) { mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length); stream->end_pos = atoll(content_length); } // Look if we can use the Content-Type content_type = http_get_field( http_hdr, "Content-Type" ); if( content_type!=NULL ) { unsigned int i; mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); // Check in the mime type table for a demuxer type for (i = 0; mime_type_table[i].mime_type != NULL; i++) { if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { *file_format = mime_type_table[i].demuxer_type; res = seekable; goto out; } } } // Not found in the mime type table, don't fail, // we should try raw HTTP res = seekable; goto out; // Redirect case 301: // Permanently case 302: // Temporarily case 303: // See Other case 307: // Temporarily (since HTTP/1.1) // TODO: RFC 2616, recommand to detect infinite redirection loops next_url = http_get_field( http_hdr, "Location" ); if( next_url!=NULL ) { int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; stream->streaming_ctrl->url = url_redirect( &url, next_url ); if (url_is_protocol(url, "mms")) { res = STREAM_REDIRECTED; goto err_out; } if (!url_is_protocol(url, "http")) { mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol); goto err_out; } if (is_ultravox) url_set_protocol(url, "unsv"); redirect = 1; } break; case 401: // Authentication required if( http_authenticate(http_hdr, url, &auth_retry)<0 ) goto err_out; redirect = 1; break; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); goto err_out; } } while( redirect ); err_out: if (fd >= 0) closesocket( fd ); fd = -1; http_free( http_hdr ); http_hdr = NULL; out: stream->streaming_ctrl->data = http_hdr; stream->fd = fd; return res; }