comparison stream/http.c @ 21540:147c1c305f21

Fix lots and lots of potential memory/fd leaks in http_streaming_start
author reimar
date Sat, 09 Dec 2006 19:33:28 +0000
parents 720206eef78b
children 3b4ed8857b38
comparison
equal deleted inserted replaced
21539:ad7616b01560 21540:147c1c305f21
716 // Output overflow 716 // Output overflow
717 return -1; 717 return -1;
718 } 718 }
719 719
720 static int http_streaming_start(stream_t *stream, int* file_format) { 720 static int http_streaming_start(stream_t *stream, int* file_format) {
721 HTTP_header_t *http_hdr; 721 HTTP_header_t *http_hdr = NULL;
722 unsigned int i; 722 unsigned int i;
723 int fd=-1; 723 int fd = stream->fd;
724 int res = 0;
724 int redirect = 0; 725 int redirect = 0;
725 int auth_retry=0; 726 int auth_retry=0;
726 int seekable=0; 727 int seekable=0;
727 char *content_type; 728 char *content_type;
728 char *next_url; 729 char *next_url;
729 URL_t *url = stream->streaming_ctrl->url; 730 URL_t *url = stream->streaming_ctrl->url;
730 731
731 do 732 do
732 { 733 {
734 if (fd > 0) closesocket(fd);
733 fd = http_send_request( url, 0 ); 735 fd = http_send_request( url, 0 );
734 if( fd<0 ) { 736 if( fd<0 ) {
735 return -1; 737 goto err_out;
736 } 738 }
737 739
740 http_free(http_hdr);
738 http_hdr = http_read_response( fd ); 741 http_hdr = http_read_response( fd );
739 if( http_hdr==NULL ) { 742 if( http_hdr==NULL ) {
740 closesocket( fd ); 743 goto err_out;
741 http_free( http_hdr );
742 return -1;
743 } 744 }
744 745
745 stream->fd=fd; 746 stream->fd=fd;
746 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { 747 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
747 http_debug_hdr( http_hdr ); 748 http_debug_hdr( http_hdr );
782 *file_format = DEMUXER_TYPE_NSV; 783 *file_format = DEMUXER_TYPE_NSV;
783 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) 784 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
784 *file_format = DEMUXER_TYPE_AAC; 785 *file_format = DEMUXER_TYPE_AAC;
785 else 786 else
786 *file_format = DEMUXER_TYPE_AUDIO; 787 *file_format = DEMUXER_TYPE_AUDIO;
787 return 0; 788 goto out;
788 } 789 }
789 case 400: // Server Full 790 case 400: // Server Full
790 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); 791 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
791 return -1; 792 goto err_out;
792 case 401: // Service Unavailable 793 case 401: // Service Unavailable
793 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); 794 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
794 return -1; 795 goto err_out;
795 case 403: // Service Forbidden 796 case 403: // Service Forbidden
796 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); 797 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
797 return -1; 798 goto err_out;
798 case 404: // Resource Not Found 799 case 404: // Resource Not Found
799 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); 800 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
800 return -1; 801 goto err_out;
801 default: 802 default:
802 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); 803 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
803 return -1; 804 goto err_out;
804 } 805 }
805 } 806 }
806 807
807 // Assume standard http if not ICY 808 // Assume standard http if not ICY
808 switch( http_hdr->status_code ) { 809 switch( http_hdr->status_code ) {
817 // Check in the mime type table for a demuxer type 818 // Check in the mime type table for a demuxer type
818 i = 0; 819 i = 0;
819 while(mime_type_table[i].mime_type != NULL) { 820 while(mime_type_table[i].mime_type != NULL) {
820 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { 821 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
821 *file_format = mime_type_table[i].demuxer_type; 822 *file_format = mime_type_table[i].demuxer_type;
822 return seekable; 823 res = seekable;
824 goto out;
823 } 825 }
824 i++; 826 i++;
825 } 827 }
826 } 828 }
827 // Not found in the mime type table, don't fail, 829 // Not found in the mime type table, don't fail,
828 // we should try raw HTTP 830 // we should try raw HTTP
829 return seekable; 831 res = seekable;
832 goto out;
830 // Redirect 833 // Redirect
831 case 301: // Permanently 834 case 301: // Permanently
832 case 302: // Temporarily 835 case 302: // Temporarily
833 case 303: // See Other 836 case 303: // See Other
834 // TODO: RFC 2616, recommand to detect infinite redirection loops 837 // TODO: RFC 2616, recommand to detect infinite redirection loops
835 next_url = http_get_field( http_hdr, "Location" ); 838 next_url = http_get_field( http_hdr, "Location" );
836 if( next_url!=NULL ) { 839 if( next_url!=NULL ) {
837 closesocket( fd );
838 stream->streaming_ctrl->url = url_redirect( &url, next_url ); 840 stream->streaming_ctrl->url = url_redirect( &url, next_url );
839 http_free( http_hdr );
840 redirect = 1; 841 redirect = 1;
841 } 842 }
842 break; 843 break;
843 case 401: // Authentication required 844 case 401: // Authentication required
844 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) return STREAM_UNSUPORTED; 845 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) {
846 res = STREAM_UNSUPORTED;
847 goto err_out;
848 }
845 redirect = 1; 849 redirect = 1;
846 break; 850 break;
847 default: 851 default:
848 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); 852 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
849 return -1; 853 goto err_out;
850 } 854 }
851 } while( redirect ); 855 } while( redirect );
852 856
853 return -1; 857 err_out:
858 if (fd > 0) closesocket( fd );
859 res = -1;
860 out:
861 http_free( http_hdr );
862 return res;
854 } 863 }
855 864
856 static int fixup_open(stream_t *stream,int seekable) { 865 static int fixup_open(stream_t *stream,int seekable) {
857 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; 866 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
858 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt"); 867 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");