Ticket #678: fd_leak_asf_http.patch

File fd_leak_asf_http.patch, 6.7 KB (added by nbk@…, 20 years ago)

Fix fd leak in asf/http streaming

  • stream/network.c

     
    238238        ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
    239239        if( ret!=(int)http_hdr->buffer_size ) {
    240240                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
     241                closesocket( fd );
    241242                goto err_out;
    242243        }
    243244       
  • stream/asf_mmst_streaming.c

     
    626626
    627627  asf_header_len = get_header (s, asf_header, stream->streaming_ctrl);
    628628//  mp_msg(MSGT_NETWORK,MSGL_INFO,"---------------------------------- asf_header %d\n",asf_header);
    629   if (asf_header_len==0) return -1; //error reading header
     629  if (asf_header_len==0) {
     630    //error reading header
     631    closesocket (s);
     632    return -1;
     633  }
    630634  packet_length = interp_header (asf_header, asf_header_len);
    631635
    632636
  • stream/asf_streaming.c

     
    746746                        if(r <0) {
    747747                                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
    748748                                http_free( http_hdr );
     749                                closesocket( fd );
    749750                                return -1;
    750751                        }
    751752                        i += r;
     
    758759                        if( i<=0 ) {
    759760                                perror("read");
    760761                                http_free( http_hdr );
     762                                closesocket( fd );
    761763                                return -1;
    762764                        }
    763765                        http_response_append( http_hdr, buffer, i );
     
    770772                if( ret<0 ) {
    771773                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
    772774                        http_free( http_hdr );
     775                        closesocket( fd );
    773776                        return -1;
    774777                }
    775778                switch( asf_http_ctrl->streaming_type ) {
     
    779782                                if( http_hdr->body_size>0 ) {
    780783                                        if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
    781784                                                http_free( http_hdr );
     785                                                closesocket( fd );
    782786                                                return -1;
    783787                                        }
    784788                                }
     
    786790                                        if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
    787791                                                // First request, we only got the ASF header.
    788792                                                ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
    789                                                 if(ret < 0) return -1;
     793                                                if( ret < 0 ) {
     794                                                        http_free( http_hdr );
     795                                                        closesocket( fd );
     796                                                        return -1;
     797                                                }
    790798                                                if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
    791799                                                        mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
     800                                                        http_free( http_hdr );
     801                                                        closesocket( fd );
    792802                                                        return -1;
    793803                                                }
    794804                                                asf_http_ctrl->request++;
     
    802812                                if( http_hdr->body_size>0 ) {
    803813                                        if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
    804814                                                http_free( http_hdr );
     815                                                closesocket( fd );
    805816                                                return -1;
    806817                                        }
    807818                                }
     
    816827                        case ASF_Unknown_e:
    817828                        default:
    818829                                mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
    819                                 closesocket(fd);
    820830                                http_free( http_hdr );
     831                                closesocket( fd );
    821832                                return -1;
    822833                }
    823834        // Check if we got a redirect. 
  • stream/http.c

     
    742742                        return -1;
    743743                }
    744744
    745                 stream->fd=fd;
    746745                if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
    747746                        http_debug_hdr( http_hdr );
    748747                }
    749                
    750                 stream->streaming_ctrl->data = (void*)http_hdr;
    751748
    752749                // Check if we can make partial content requests and thus seek in http-streams
    753750                if( http_hdr!=NULL && http_hdr->status_code==200 ) {
     
    761758                        switch( http_hdr->status_code ) {
    762759                                case 200: { // OK
    763760                                        char *field_data = NULL;
     761                                        stream->fd = fd;
     762                                        stream->streaming_ctrl->data = (void*)http_hdr;
    764763                                        // note: I skip icy-notice1 and 2, as they contain html <BR>
    765764                                        // and are IMHO useless info ::atmos
    766765                                        if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
     
    788787                                }
    789788                                case 400: // Server Full
    790789                                        mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
     790                                        http_free(http_hdr);
     791                                        closesocket(fd);
    791792                                        return -1;
    792793                                case 401: // Service Unavailable
    793794                                        mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
     795                                        http_free(http_hdr);
     796                                        closesocket(fd);
    794797                                        return -1;
    795798                                case 403: // Service Forbidden
    796799                                        mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
     800                                        http_free(http_hdr);
     801                                        closesocket(fd);
    797802                                        return -1;
    798803                                case 404: // Resource Not Found
    799804                                        mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
     805                                        http_free(http_hdr);
     806                                        closesocket(fd);
    800807                                        return -1;
    801808                                default:
    802809                                        mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
     810                                        http_free(http_hdr);
     811                                        closesocket(fd);
    803812                                        return -1;
    804813                        }
    805814                }
     
    807816                // Assume standard http if not ICY                     
    808817                switch( http_hdr->status_code ) {
    809818                        case 200: // OK
     819                                stream->fd = fd;
     820                                stream->streaming_ctrl->data = (void*)http_hdr;
    810821                                // Look if we can use the Content-Type
    811822                                content_type = http_get_field( http_hdr, "Content-Type" );
    812823                                if( content_type!=NULL ) {
     
    834845                                // TODO: RFC 2616, recommand to detect infinite redirection loops
    835846                                next_url = http_get_field( http_hdr, "Location" );
    836847                                if( next_url!=NULL ) {
    837                                         closesocket( fd );
    838848                                        stream->streaming_ctrl->url = url_redirect( &url, next_url );
    839                                         http_free( http_hdr );
    840849                                        redirect = 1;   
    841850                                }
    842851                                break;
    843852                        case 401: // Authentication required
    844                                 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) return STREAM_UNSUPORTED;
     853                                if( http_authenticate(http_hdr, url, &auth_retry)<0 ) {
     854                                        http_free(http_hdr);
     855                                        closesocket(fd);
     856                                        return STREAM_UNSUPORTED;
     857                                }
    845858                                redirect = 1;
    846859                                break;
    847860                        default:
    848861                                mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
     862                                http_free(http_hdr);
     863                                closesocket(fd);
    849864                                return -1;
    850865                }
     866                http_free(http_hdr);
     867                closesocket(fd);
    851868        } while( redirect );
    852869
    853870        return -1;
     
    870887                mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
    871888                streaming_ctrl_free(stream->streaming_ctrl);
    872889                stream->streaming_ctrl = NULL;
     890                closesocket(stream->fd);
     891                stream->fd = -1;
    873892                return STREAM_UNSUPORTED;
    874893        }
    875894
     
    895914        if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
    896915                streaming_ctrl_free(stream->streaming_ctrl);
    897916                stream->streaming_ctrl = NULL;
     917                if (stream->fd >= 0) {
     918                        closesocket(stream->fd);
     919                        stream->fd = -1;
     920                }
    898921                return STREAM_UNSUPORTED;
    899922        }
    900923