예제 #1
0
파일: http.c 프로젝트: cobr123/qtVlc
int  HttpCallback( httpd_file_sys_t *p_args,
                       httpd_file_t *p_file,
                       uint8_t *_p_request,
                       uint8_t **_pp_data, int *pi_data )
{
    VLC_UNUSED(p_file);
    char *p_request = (char *)_p_request;
    char **pp_data = (char **)_pp_data;
    FILE *f;

    if( ( f = vlc_fopen( p_args->file, "r" ) ) == NULL )
    {
        Callback404( p_args, pp_data, pi_data );
        return VLC_SUCCESS;
    }

    if( !p_args->b_html )
    {
        FileLoad( f, pp_data, pi_data );
    }
    else
    {
        int  i_buffer;
        char *p_buffer;

        /* first we load in a temporary buffer */
        FileLoad( f, &p_buffer, &i_buffer );

        ParseExecute( p_args, p_buffer, i_buffer, p_request, pp_data, pi_data );

        free( p_buffer );
    }

    fclose( f );

    return VLC_SUCCESS;
}
예제 #2
0
파일: http.c 프로젝트: cobr123/qtVlc
int  ArtCallback( httpd_handler_sys_t *p_args,
                          httpd_handler_t *p_handler, char *_p_url,
                          uint8_t *p_request, int i_type,
                          uint8_t *p_in, int i_in,
                          char *psz_remote_addr, char *psz_remote_host,
                          uint8_t **pp_data, int *pi_data )
{
    VLC_UNUSED(p_handler); VLC_UNUSED(_p_url); VLC_UNUSED(i_type); 
    VLC_UNUSED(p_in); VLC_UNUSED(i_in); VLC_UNUSED(psz_remote_addr); 
    VLC_UNUSED(psz_remote_host); 

    char *psz_art = NULL;
    intf_thread_t *p_intf = p_args->file.p_intf;
    intf_sys_t *p_sys = p_intf->p_sys;
    char psz_id[16];
    input_item_t *p_item = NULL;
    int i_id;

    psz_id[0] = '\0';
    if( p_request )
        ExtractURIValue( (char *)p_request, "id", psz_id, 15 );
    i_id = atoi( psz_id );
    if( i_id )
    {
        playlist_Lock( p_sys->p_playlist );
        playlist_item_t *p_pl_item = playlist_ItemGetById( p_sys->p_playlist,
                                                           i_id );
        if( p_pl_item )
            p_item = p_pl_item->p_input;
        playlist_Unlock( p_sys->p_playlist );
    }
    else
    {
        /* FIXME: Workarround a stupid assert in input_GetItem */
        if( p_sys->p_input && p_sys->p_input->p )
            p_item = input_GetItem( p_sys->p_input );
    }

    if( p_item )
    {
        psz_art = input_item_GetArtURL( p_item );
    }

    if( psz_art )
    {
        char *psz = make_path( psz_art );
        free( psz_art );
        psz_art = psz;
    }

    if( psz_art == NULL )
    {
        msg_Dbg( p_intf, "No album art found" );
        Callback404( &p_args->file, (char**)pp_data, pi_data );
        return VLC_SUCCESS;
    }

    FILE *f = vlc_fopen( psz_art, "r" );
    if( f == NULL )
    {
        msg_Dbg( p_intf, "Couldn't open album art file %s", psz_art );
        Callback404( &p_args->file, (char**)pp_data, pi_data );
        free( psz_art );
        return VLC_SUCCESS;
    }
    free( psz_art );

    char *p_data = NULL;
    int i_data;
    FileLoad( f, &p_data, &i_data );
    fclose( f );

    char *psz_ext = strrchr( psz_art, '.' );
    if( psz_ext ) psz_ext++;

#define HEADER  "Content-Type: image/%s\n" \
                "Content-Length: %d\n" \
                "\n"
    char *psz_header;
    int i_header_size = asprintf( &psz_header, HEADER, psz_ext, i_data );
#undef HEADER
    if( likely(i_header_size != -1) )
    {
        *pp_data = malloc( i_header_size + i_data );
        if( likely(*pp_data != NULL) )
        {
            *pi_data = i_header_size + i_data;
            memcpy( *pp_data, psz_header, i_header_size );
            memcpy( *pp_data+i_header_size, p_data, i_data );
        }
        free( psz_header );
    }
    free( p_data );

    return VLC_SUCCESS;
}
예제 #3
0
파일: http.c 프로젝트: cobr123/qtVlc
/****************************************************************************
 * HandlerCallback:
 ****************************************************************************
 * call the external handler and parse vlc macros if Content-Type is HTML
 ****************************************************************************/
int  HandlerCallback( httpd_handler_sys_t *p_args,
                          httpd_handler_t *p_handler, char *_p_url,
                          uint8_t *_p_request, int i_type,
                          uint8_t *_p_in, int i_in,
                          char *psz_remote_addr, char *psz_remote_host,
                          uint8_t **_pp_data, int *pi_data )
{
    VLC_UNUSED(p_handler); VLC_UNUSED(_p_in);
    char *p_url = (char *)_p_url;
    char *p_request = (char *)_p_request;
    char **pp_data = (char **)_pp_data;
    char *p_in = (char *)_p_in;
    int i_request = p_request != NULL ? strlen( p_request ) : 0;
    char *p;
    int i_env = 0;
    char **ppsz_env = NULL;
    char *psz_tmp;
    size_t i_buffer;
    char *p_buffer;
    char *psz_cwd, *psz_file = NULL;
    int i_ret;

    /* Create environment for the CGI */
    TAB_APPEND( i_env, ppsz_env, strdup("GATEWAY_INTERFACE=CGI/1.1") );
    TAB_APPEND( i_env, ppsz_env, strdup("SERVER_PROTOCOL=HTTP/1.1") );
    TAB_APPEND( i_env, ppsz_env, strdup("SERVER_SOFTWARE=VLC "VERSION) );

    switch( i_type )
    {
    case HTTPD_MSG_GET:
        TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=GET") );
        break;
    case HTTPD_MSG_POST:
        TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=POST") );
        break;
    case HTTPD_MSG_HEAD:
        TAB_APPEND( i_env, ppsz_env, strdup("REQUEST_METHOD=HEAD") );
        break;
    default:
        break;
    }

    if( i_request )
    {
        if( -1==asprintf( &psz_tmp, "QUERY_STRING=%s", p_request ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );

        if( -1==asprintf( &psz_tmp, "REQUEST_URI=%s?%s", p_url, p_request ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }
    else
    {
        if( -1==asprintf( &psz_tmp, "REQUEST_URI=%s", p_url ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }

    if( -1==asprintf( &psz_tmp, "SCRIPT_NAME=%s", p_url ) )
        psz_tmp = NULL;
    TAB_APPEND( i_env, ppsz_env, psz_tmp );

#define p_sys p_args->file.p_intf->p_sys
    if( -1==asprintf( &psz_tmp, "SERVER_NAME=%s", p_sys->psz_address ) )
        psz_tmp = NULL;
    TAB_APPEND( i_env, ppsz_env, psz_tmp );

    if( -1==asprintf( &psz_tmp, "SERVER_PORT=%u", p_sys->i_port ) )
        psz_tmp = NULL;
    TAB_APPEND( i_env, ppsz_env, psz_tmp );
#undef p_sys

    p = getenv( "PATH" );
    if( p != NULL )
    {
        if( -1==asprintf( &psz_tmp, "PATH=%s", p ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }

#ifdef WIN32
    p = getenv( "windir" );
    if( p != NULL )
    {
        if( -1==asprintf( &psz_tmp, "SYSTEMROOT=%s", p ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }
#endif

    if( psz_remote_addr != NULL && *psz_remote_addr )
    {
        if( -1==asprintf( &psz_tmp, "REMOTE_ADDR=%s", psz_remote_addr ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }

    if( psz_remote_host != NULL && *psz_remote_host )
    {
        if( -1==asprintf( &psz_tmp, "REMOTE_HOST=%s", psz_remote_host ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );
    }

    if( i_in )
    {
        p = p_in;
        for ( ; ; )
        {
            if( !strncasecmp( p, "Content-Type: ", strlen("Content-Type: ") ) )
            {
                char *end = strchr( p, '\r' );
                if( end == NULL )
                    break;
                *end = '\0';
                if( -1==asprintf( &psz_tmp, "CONTENT_TYPE=%s", p ) )
                    psz_tmp = NULL;
                TAB_APPEND( i_env, ppsz_env, psz_tmp );
                *end = '\r';
            }
            if( !strncasecmp( p, "Content-Length: ",
                              strlen("Content-Length: ") ) )
            {
                char *end = strchr( p, '\r' );
                if( end == NULL )
                    break;
                *end = '\0';
                if( -1==asprintf( &psz_tmp, "CONTENT_LENGTH=%s", p ) )
                    psz_tmp = NULL;
                TAB_APPEND( i_env, ppsz_env, psz_tmp );
                *end = '\r';
            }

            p = strchr( p, '\n' );
            if( p == NULL || p[1] == '\r' )
            {
                p = NULL;
                break;
            }
            p++;
        }
    }

    psz_file = strrchr( p_args->file.file, DIR_SEP_CHAR );
    if( psz_file != NULL )
    {
        psz_file++;
        if( -1==asprintf( &psz_tmp, "SCRIPT_FILENAME=%s", psz_file ) )
            psz_tmp = NULL;
        TAB_APPEND( i_env, ppsz_env, psz_tmp );

        TAB_APPEND( p_args->p_association->i_argc,
                    p_args->p_association->ppsz_argv, psz_file );
    }

    TAB_APPEND( i_env, ppsz_env, NULL );

    TAB_APPEND( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
                NULL );

    psz_tmp = strdup( p_args->file.file );
    p = strrchr( psz_tmp, DIR_SEP_CHAR );
    if( p != NULL )
    {
        *p = '\0';
        psz_cwd = psz_tmp;
    }
    else
    {
        free( psz_tmp );
        psz_cwd = NULL;
    }

    i_ret = vlc_execve( p_args->file.p_intf, p_args->p_association->i_argc,
                        p_args->p_association->ppsz_argv, ppsz_env, psz_cwd,
                        (char *)p_in, i_in, &p_buffer, &i_buffer );
    TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
                NULL );
    TAB_REMOVE( p_args->p_association->i_argc, p_args->p_association->ppsz_argv,
                psz_file );
    free( psz_cwd );
    while( i_env )
        TAB_REMOVE( i_env, ppsz_env, ppsz_env[0] );

    if( i_ret == -1 )
    {
        Callback404( (httpd_file_sys_t *)p_args, pp_data, pi_data );
        return VLC_SUCCESS;
    }
    p = p_buffer;
    while( strncasecmp( p, "Content-Type: text/html",
                        strlen("Content-Type: text/html") ) )
    {
        p = strchr( p, '\n' );
        if( p == NULL || p[1] == '\r' )
        {
            p = NULL;
            break;
        }
        p++;
    }

    if( p == NULL )
    {
        *pp_data = p_buffer;
        *pi_data = i_buffer;
    }
    else
    {
        ParseExecute( (httpd_file_sys_t *)p_args, p_buffer, i_buffer,
                      p_request, pp_data, pi_data );

        free( p_buffer );
    }

    return VLC_SUCCESS;
}
예제 #4
0
파일: http.c 프로젝트: paa/vlc
int  ArtCallback( httpd_handler_sys_t *p_args,
                          httpd_handler_t *p_handler, char *_p_url,
                          uint8_t *p_request, int i_type,
                          uint8_t *p_in, int i_in,
                          char *psz_remote_addr, char *psz_remote_host,
                          uint8_t **pp_data, int *pi_data )
{
    VLC_UNUSED(p_handler); VLC_UNUSED(_p_url); VLC_UNUSED(i_type); 
    VLC_UNUSED(p_in); VLC_UNUSED(i_in); VLC_UNUSED(psz_remote_addr); 
    VLC_UNUSED(psz_remote_host); 
    VLC_UNUSED(p_request);

    char *psz_art = NULL;
    intf_thread_t *p_intf = p_args->file.p_intf;
    intf_sys_t *p_sys = p_intf->p_sys;
    input_item_t *p_item = NULL;
    p_sys->p_input = playlist_CurrentInput( p_sys->p_playlist );
        /* Workaround a stupid assert in input_GetItem */
        if( p_sys->p_input && p_sys->p_input->p )
            p_item = input_GetItem( p_sys->p_input );
    if( p_item )
    {
        psz_art = input_item_GetArtURL( p_item );
    }

    if( psz_art )
    {
        char *psz = make_path( psz_art );
        free( psz_art );
        psz_art = psz;
    }

    if( psz_art == NULL )
    {
        msg_Dbg( p_intf, "didn't find any art, so use default" );
        char *psz_src = var_InheritString( p_intf, "http-src" );
        if( psz_src == NULL )
        {
            char *data_path = config_GetDataDir( p_intf );
            if( asprintf( &psz_src, "%s" DIR_SEP "http", data_path ) == -1 )
                psz_src = NULL;
            free( data_path );
        }
        if( asprintf( &psz_art, "%s" DIR_SEP "images" DIR_SEP "default_album_art.png", psz_src ) == -1 )
                psz_art = NULL;
        free( psz_src );
    }

    FILE *f = vlc_fopen( psz_art, "r" );
    if( f == NULL )
    {
        Callback404( &p_args->file, (char**)pp_data, pi_data );
        free( psz_art );
        return VLC_SUCCESS;
    }
    free( psz_art );

    char *p_data = NULL;
    int i_data;
    FileLoad( f, &p_data, &i_data );
    fclose( f );

    char *psz_ext = strrchr( psz_art, '.' );
    if( psz_ext ) psz_ext++;

#define HEADER  "Content-Type: image/%s\n" \
                "Content-Length: %d\n" \
                "\n"
    char *psz_header;
    int i_header_size = asprintf( &psz_header, HEADER, psz_ext, i_data );
#undef HEADER
    if( likely(i_header_size != -1) )
    {
        *pp_data = malloc( i_header_size + i_data );
        if( likely(*pp_data != NULL) )
        {
            *pi_data = i_header_size + i_data;
            memcpy( *pp_data, psz_header, i_header_size );
            memcpy( *pp_data+i_header_size, p_data, i_data );
        }
        free( psz_header );
    }
    free( p_data );

    return VLC_SUCCESS;
}