<commit_before>/*****************************************************************************
 * http.c :  http remote control plugin for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
 * $Id: http.c,v 1.7 2003/05/22 15:34:02 hartman Exp $
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <errno.h>                                                 /* ENOMEM */
#include <stdio.h>
#include <ctype.h>
#include <signal.h>

#include <vlc/vlc.h>
#include <vlc/intf.h>

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
#include <sys/types.h>

#include "httpd.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Activate     ( vlc_object_t * );
static void Close        ( vlc_object_t * );
static void Run          ( intf_thread_t *p_intf );

static int  httpd_page_interface_get( httpd_file_callback_args_t *p_args,
                                      uint8_t *p_request, int i_request,
                                      uint8_t **pp_data, int *pi_data );

/*****************************************************************************
 * intf_sys_t: description and status of interface
 *****************************************************************************/
struct intf_sys_t
{
    httpd_t             *p_httpd;
    httpd_host_t        *p_httpd_host;

    input_thread_t *    p_input;
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define PORT_TEXT N_( "HTTP interface bind port" )
#define PORT_LONGTEXT N_( \
    "You can set the port on which the http interface will accept connections" )
#define ADDR_TEXT N_( "HTTP interface bind address" )
#define ADDR_LONGTEXT N_( \
    "You can set the address on which the http interface will bind" )

vlc_module_begin();
    add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
    add_string( "http-addr", NULL, NULL, ADDR_TEXT, ADDR_LONGTEXT, VLC_TRUE );
    add_integer( "http-port", 8080, NULL, PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
    set_description( _("HTTP remote control interface") );
    set_capability( "interface", 10 );
    set_callbacks( Activate, Close );
vlc_module_end();

/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;

    /* Allocate instance and initialize some members */
    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        return VLC_EGENERIC;
    };

    p_intf->p_sys->p_httpd = NULL;
    p_intf->p_sys->p_httpd_host = NULL;

    p_intf->pf_run = Run;

    CONSOLE_INTRO_MSG;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
void Close ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    if( p_intf->p_sys->p_httpd_host )
        p_intf->p_sys->p_httpd->pf_unregister_host( p_intf->p_sys->p_httpd,
                                                 p_intf->p_sys->p_httpd_host );

    if( p_intf->p_sys->p_httpd )
        httpd_Release( p_intf->p_sys->p_httpd );

    /* Destroy structure */
    free( p_intf->p_sys );
}

/*****************************************************************************
 * Run: http interface thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    input_thread_t *p_input = NULL;
    playlist_t     *p_playlist = NULL;
    httpd_file_t   *p_page_intf;

#if 0
    input_info_category_t * p_category;
    input_info_t * p_info;

    off_t      i_oldpos = 0;
    off_t      i_newpos;
    double     f_ratio = 1.0;
#endif

    /* Get bind address and port */
    char *psz_bind_addr = config_GetPsz( p_intf, "http-addr" );
    int i_bind_port = config_GetInt( p_intf, "http-port" );
    if( !psz_bind_addr ) psz_bind_addr = strdup( "" );

    p_intf->p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE );
    if( !p_intf->p_sys->p_httpd )
    {
        msg_Err( p_intf, "cannot start httpd daemon" );
        free( p_intf->p_sys );
        return;
    }

    p_intf->p_sys->p_httpd_host =
        p_intf->p_sys->p_httpd->pf_register_host( p_intf->p_sys->p_httpd,
                                                  psz_bind_addr, i_bind_port );

    if( !p_intf->p_sys->p_httpd_host )
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_bind_addr, i_bind_port);
        httpd_Release( p_intf->p_sys->p_httpd );
        free( p_intf->p_sys );
        return;
    }

    msg_Info( p_intf, "http interface started" );

    /*
     * Register our interface page with the httpd daemon
     */
    p_page_intf = p_intf->p_sys->p_httpd->pf_register_file(
                      p_intf->p_sys->p_httpd, "/", "text/html",
                      NULL, NULL, httpd_page_interface_get,
                      httpd_page_interface_get,
                      (httpd_file_callback_args_t*)p_intf );

    while( !p_intf->b_die )
    {

        /* Manage the input part */
        if( p_input == NULL )
        {
            if( p_playlist )
            {
                p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
                                                       FIND_CHILD );
            }
            else
            {<commit_msg>* Updated the informal message to report the ip and port the http intf was   started on. This will help ppl to actually use the thing ;)<commit_after>/*****************************************************************************
 * http.c :  http remote control plugin for vlc
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
 * $Id: http.c,v 1.8 2003/05/23 13:02:53 hartman Exp $
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <errno.h>                                                 /* ENOMEM */
#include <stdio.h>
#include <ctype.h>
#include <signal.h>

#include <vlc/vlc.h>
#include <vlc/intf.h>

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
#include <sys/types.h>

#include "httpd.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Activate     ( vlc_object_t * );
static void Close        ( vlc_object_t * );
static void Run          ( intf_thread_t *p_intf );

static int  httpd_page_interface_get( httpd_file_callback_args_t *p_args,
                                      uint8_t *p_request, int i_request,
                                      uint8_t **pp_data, int *pi_data );

/*****************************************************************************
 * intf_sys_t: description and status of interface
 *****************************************************************************/
struct intf_sys_t
{
    httpd_t             *p_httpd;
    httpd_host_t        *p_httpd_host;

    input_thread_t *    p_input;
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define PORT_TEXT N_( "HTTP interface bind port" )
#define PORT_LONGTEXT N_( \
    "You can set the port on which the http interface will accept connections" )
#define ADDR_TEXT N_( "HTTP interface bind address" )
#define ADDR_LONGTEXT N_( \
    "You can set the address on which the http interface will bind" )

vlc_module_begin();
    add_category_hint( N_("HTTP remote control"), NULL, VLC_TRUE );
    add_string( "http-addr", NULL, NULL, ADDR_TEXT, ADDR_LONGTEXT, VLC_TRUE );
    add_integer( "http-port", 8080, NULL, PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
    set_description( _("HTTP remote control interface") );
    set_capability( "interface", 10 );
    set_callbacks( Activate, Close );
vlc_module_end();

/*****************************************************************************
 * Activate: initialize and create stuff
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t*)p_this;

    /* Allocate instance and initialize some members */
    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        return VLC_EGENERIC;
    };

    p_intf->p_sys->p_httpd = NULL;
    p_intf->p_sys->p_httpd_host = NULL;

    p_intf->pf_run = Run;

    CONSOLE_INTRO_MSG;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
void Close ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    if( p_intf->p_sys->p_httpd_host )
        p_intf->p_sys->p_httpd->pf_unregister_host( p_intf->p_sys->p_httpd,
                                                 p_intf->p_sys->p_httpd_host );

    if( p_intf->p_sys->p_httpd )
        httpd_Release( p_intf->p_sys->p_httpd );

    /* Destroy structure */
    free( p_intf->p_sys );
}

/*****************************************************************************
 * Run: http interface thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
    input_thread_t *p_input = NULL;
    playlist_t     *p_playlist = NULL;
    httpd_file_t   *p_page_intf;

#if 0
    input_info_category_t * p_category;
    input_info_t * p_info;

    off_t      i_oldpos = 0;
    off_t      i_newpos;
    double     f_ratio = 1.0;
#endif

    /* Get bind address and port */
    char *psz_bind_addr = config_GetPsz( p_intf, "http-addr" );
    int i_bind_port = config_GetInt( p_intf, "http-port" );
    if( !psz_bind_addr ) psz_bind_addr = strdup( "" );

    p_intf->p_sys->p_httpd = httpd_Find( VLC_OBJECT(p_intf), VLC_TRUE );
    if( !p_intf->p_sys->p_httpd )
    {
        msg_Err( p_intf, "cannot start httpd daemon" );
        free( p_intf->p_sys );
        return;
    }

    p_intf->p_sys->p_httpd_host =
        p_intf->p_sys->p_httpd->pf_register_host( p_intf->p_sys->p_httpd,
                                                  psz_bind_addr, i_bind_port );
    
    if( !p_intf->p_sys->p_httpd_host )
    {
        msg_Err( p_intf, "cannot listen on %s:%d", psz_bind_addr, i_bind_port);
        httpd_Release( p_intf->p_sys->p_httpd );
        free( p_intf->p_sys );
        return;
    }

    msg_Info( p_intf, "http interface started on %s:%d", 
        *psz_bind_addr ? psz_bind_addr : "localhost" , i_bind_port );

    /*
     * Register our interface page with the httpd daemon
     */
    p_page_intf = p_intf->p_sys->p_httpd->pf_register_file(
                      p_intf->p_sys->p_httpd, "/", "text/html",
                      NULL, NULL, httpd_page_interface_get,
                      httpd_page_interface_get,
                      (httpd_file_callback_args_t*)p_intf );

    while( !p_intf->b_die )
    {

        /* Manage the input part */
        if( p_input == NULL )
        {
            if( p_playlist )
            {
                p_input = vlc_object_find( p_playlist, VLC_OBJECT_INPUT,
                                                       FIND_CHILD );
            }
            else
            {
{'fraction_non_alphanumeric': 0.2708793951929249, 'fraction_numerical': 0.0061336566578703376, 'mean_word_length': 3.0785340314136125, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7282964', 'n_tokens_mistral': 4291, 'n_tokens_neox': 3957, 'n_words': 990}