- /* Skip the hostname part */
- while( *psz_port && *psz_port != ':' && *psz_port != '/' )
- {
- psz_port++;
- }
-
- /* Found a port name */
- if( *psz_port )
- {
- if( *psz_port == ':' )
- {
- /* Replace ':' with '\0' */
- *psz_port = '\0';
- psz_port++;
- }
-
- psz_path = psz_port;
- while( *psz_path && *psz_path != '/' )
- {
- psz_path++;
- }
-
- if( *psz_path )
- {
- *psz_path = '\0';
- psz_path++;
- }
- else
- {
- psz_path = NULL;
- }
-
- if( *psz_port != '\0' )
- {
- i_port = atoi( psz_port );
- }
- }
- }
- else
- {
- psz_server = NULL;
- }
- }
-
- /* Check that we got a valid server */
- if( psz_server == NULL )
- {
- intf_ErrMsg( "input error: No server given" );
- p_input->b_error = 1;
- return;
- }
-
- /* Check that we got a valid port */
- if( i_port == 0 )
- {
- i_port = 80; /* FIXME */
- }
-
- intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
- i_port, psz_path );
-
- /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
- * * protocol */
- p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
- if( p_input->i_handle == -1 )
- {
- intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
- return;
- }
-
- /* We may want to reuse an already used socket */
- i_opt = 1;
- if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
- (void *) &i_opt, sizeof( i_opt ) ) == -1 )
- {
- intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
- strerror(errno));
- close( p_input->i_handle );
- p_input->b_error = 1;
- return;
- }
-
- /* Check proxy */
- if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
- {
- /* http://myproxy.mydomain:myport/ */
- int i_proxy_port = 0;
-
- /* Skip the protocol name */
- while( *psz_proxy && *psz_proxy != ':' )
- {
- psz_proxy++;
- }
-
- /* Skip the "://" part */
- while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
- {
- psz_proxy++;
- }
-
- /* Found a proxy name */
- if( *psz_proxy )
- {
- char *psz_port = psz_proxy;
-
- /* Skip the hostname part */
- while( *psz_port && *psz_port != ':' && *psz_port != '/' )
- {
- psz_port++;
- }
-
- /* Found a port name */
- if( *psz_port )
- {
- char * psz_junk;
-
- /* Replace ':' with '\0' */
- *psz_port = '\0';
- psz_port++;
-
- psz_junk = psz_port;
- while( *psz_junk && *psz_junk != '/' )
- {
- psz_junk++;
- }
-
- if( *psz_junk )
- {
- *psz_junk = '\0';
- }
-
- if( *psz_port != '\0' )
- {
- i_proxy_port = atoi( psz_port );
- }
- }
- }
- else
- {
- intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
- close( p_input->i_handle );
- p_input->b_error = 1;
- return;
- }
-
- /* Build socket for proxy connection */
- if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
- {
- intf_ErrMsg( "input error: can't build remote address" );
- close( p_input->i_handle );
- p_input->b_error = 1;
- return;
- }
- sock.sin_port = htons( i_proxy_port );
- }
- else
- {
- /* No proxy, direct connection */
- if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
- {
- intf_ErrMsg( "input error: can't build remote address" );
- close( p_input->i_handle );
- p_input->b_error = 1;
- return;
- }
- sock.sin_port = htons( i_port );
- }