Ticket #13158: proxy_sysconf.diff

File proxy_sysconf.diff, 11.7 KB (added by blb@…, 16 years ago)

Patch (from trunk/) to add better libcurl proxy support from SystemConfiguration

  • src/pextlib1.0/Makefile

    diff -uNr --exclude=.svn base.orig/src/pextlib1.0/Makefile base/src/pextlib1.0/Makefile
    old new  
    11OBJS=           Pextlib.o strsed.o fgetln.o md5cmd.o setmode.o xinstall.o \
    22                fs-traverse.o strcasecmp.o vercomp.o filemap.o \
    33                sha1cmd.o compat.o curl.o rmd160cmd.o readline.o uid.o\
    4                 tracelib.o
     4                tracelib.o proxyfromsystemconfiguration.o
    55SHLIB_NAME=     Pextlib${SHLIB_SUFFIX}
    66INSTALLDIR= ${DESTDIR}${datadir}/macports/Tcl/pextlib1.0
    77export MACOSX_DEPLOYMENT_TARGET=10.3
     
    1111
    1212CFLAGS+= ${CURL_CFLAGS} ${MD5_CFLAGS} ${READLINE_CFLAGS}
    1313LIBS+= ${CURL_LIBS} ${MD5_LIBS} ${READLINE_LIBS}
    14 
     14ifeq ($(OBJC_RUNTIME), APPLE_RUNTIME)
     15    LIBS+= -framework CoreFoundation -framework SystemConfiguration
     16endif
    1517.PHONY: test
    1618
    1719test:: ${SHLIB_NAME}
  • src/pextlib1.0/Pextlib.c

    diff -uNr --exclude=.svn base.orig/src/pextlib1.0/Pextlib.c base/src/pextlib1.0/Pextlib.c
    old new  
    114114#include "readline.h"
    115115#include "uid.h"
    116116#include "tracelib.h"
     117#include "proxyfromsystemconfiguration.h"
    117118
    118119#if HAVE_CRT_EXTERNS_H
    119120#include <crt_externs.h>
     
    11671168        Tcl_CreateObjCommand(interp, "gid_to_name", gid_to_nameCmd, NULL, NULL);
    11681169       
    11691170        Tcl_CreateObjCommand(interp, "tracelib", TracelibCmd, NULL, NULL);
     1171        Tcl_CreateObjCommand(interp, "proxyfromsystemconfiguration", getProxyFromSystemConfigurationCmd, NULL, NULL);
    11701172
    11711173        if (Tcl_PkgProvide(interp, "Pextlib", "1.0") != TCL_OK)
    11721174                return TCL_ERROR;
  • src/pextlib1.0/proxyfromsystemconfiguration.c

    diff -uNr --exclude=.svn base.orig/src/pextlib1.0/proxyfromsystemconfiguration.c base/src/pextlib1.0/proxyfromsystemconfiguration.c
    old new  
     1/*
     2 * proxyfromsystemconfiguration.c
     3 * $Id: $
     4 *
     5 * Copyright (c) 2008, The MacPorts Project.
     6 * All rights reserved.
     7 *
     8 * Redistribution and use in source and binary forms, with or without
     9 * modification, are permitted provided that the following conditions
     10 * are met:
     11 * 1. Redistributions of source code must retain the above copyright
     12 *    notice, this list of conditions and the following disclaimer.
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in the
     15 *    documentation and/or other materials provided with the distribution.
     16 * 3. Neither the name of MacPorts Team nor the names of its contributors
     17 *    may be used to endorse or promote products derived from this software
     18 *    without specific prior written permission.
     19 *
     20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30 * POSSIBILITY OF SUCH DAMAGE.
     31 */
     32
     33#ifdef HAVE_CONFIG_H
     34#include <config.h>
     35#endif
     36
     37#include "proxyfromsystemconfiguration.h"
     38
     39#ifdef HAVE_FRAMEWORK_SYSTEMCONFIGURATION
     40#include <CoreFoundation/CoreFoundation.h>
     41#include <SystemConfiguration/SystemConfiguration.h>
     42
     43int setProxyInfoForEnvWithKeys( CFDictionaryRef proxies, const char *envName, const void *enabledKey, const void *hostKey, const void *portKey );
     44char *cfStringToCStringASCII( CFStringRef cfString );
     45
     46#endif   /* HAVE_FRAMEWORK_SYSTEMCONFIGURATION */
     47
     48
     49/**
     50 *
     51 * Set various libcurl-specific environment variables based on settings
     52 * gleaned from the SystemConfiguration framework on Mac OS X.  If this
     53 * Is run on a non-Mac system, it does nothing
     54 *
     55 * Synopsis: getproxyfromsystemconfiguration
     56 */
     57int getProxyFromSystemConfigurationCmd( ClientData clientData UNUSED, Tcl_Interp *interp, int objc UNUSED, Tcl_Obj *CONST objv[] UNUSED )
     58{
     59#ifdef HAVE_FRAMEWORK_SYSTEMCONFIGURATION
     60    int result = 0;
     61    CFDictionaryRef proxies = SCDynamicStoreCopyProxies( NULL );
     62    if( proxies != NULL )
     63    {
     64        if( !setProxyInfoForEnvWithKeys( proxies, "http_proxy", kSCPropNetProxiesHTTPEnable, kSCPropNetProxiesHTTPProxy, kSCPropNetProxiesHTTPPort ) )
     65        {
     66            if( !setProxyInfoForEnvWithKeys( proxies, "HTTPS_PROXY", kSCPropNetProxiesHTTPSEnable, kSCPropNetProxiesHTTPSProxy, kSCPropNetProxiesHTTPSPort ) )
     67            {
     68                if( !setProxyInfoForEnvWithKeys( proxies, "FTP_PROXY", kSCPropNetProxiesFTPEnable, kSCPropNetProxiesFTPProxy, kSCPropNetProxiesFTPPort ) )
     69                {
     70                    CFArrayRef exceptionsList = CFDictionaryGetValue( proxies, kSCPropNetProxiesExceptionsList );
     71                    if( exceptionsList != NULL )
     72                    {
     73                        CFStringRef exceptionsCommaSeparated = CFStringCreateByCombiningStrings( kCFAllocatorDefault, exceptionsList, CFSTR( "," ) );
     74                        char *cStringList = cfStringToCStringASCII( exceptionsCommaSeparated );
     75                        if( cStringList != NULL )
     76                        {
     77                            setenv( "NO_PROXY", cStringList, 0 );
     78                            free( cStringList );
     79                        }
     80                        else
     81                            result = -1;
     82                        CFRelease( exceptionsCommaSeparated );
     83                    }
     84                }
     85                else
     86                    result = -1;
     87            }
     88            else
     89                result = -1;
     90        }
     91        else
     92            result = -1;
     93
     94        CFRelease( proxies );
     95    }
     96
     97    if( result == -1 )
     98    {
     99        Tcl_SetResult( interp, (char *) Tcl_PosixError( interp ), TCL_STATIC );
     100        return TCL_ERROR;
     101    }
     102    return TCL_OK;
     103#else    /* HAVE_FRAMEWORK_SYSTEMCONFIGURATION */
     104   return TCL_OK;
     105#endif   /* HAVE_FRAMEWORK_SYSTEMCONFIGURATION */
     106}
     107
     108
     109#ifdef HAVE_FRAMEWORK_SYSTEMCONFIGURATION
     110/**
     111 *
     112 * Set an environment variable based on the given SystemConfiguration
     113 * dictionary keys.
     114 *
     115 * Returns 0 on success; -1 on failure (to allocate memory)
     116 */
     117int setProxyInfoForEnvWithKeys( CFDictionaryRef proxies, const char *envName, const void *enabledKey, const void *hostKey, const void *portKey )
     118{
     119    int result = 0;
     120    CFNumberRef proxyEnabledNumber = CFDictionaryGetValue( proxies, enabledKey );
     121    if( proxyEnabledNumber != NULL )
     122    {
     123        int proxyEnabled;
     124        CFNumberGetValue( proxyEnabledNumber, kCFNumberIntType, &proxyEnabled );
     125        if( proxyEnabled )
     126        {
     127            CFStringRef proxyHost = CFDictionaryGetValue( proxies, hostKey );
     128            if( proxyHost != NULL )
     129            {
     130                char *hostname = cfStringToCStringASCII( proxyHost );
     131                if( hostname != NULL )
     132                {
     133                    CFNumberRef proxyPort = CFDictionaryGetValue( proxies, portKey );
     134                    int port = 0;
     135                    if( proxyPort != NULL )
     136                        CFNumberGetValue( proxyPort, kCFNumberIntType, &port );
     137
     138                    if( port > 0 )
     139                    {
     140                        /* hostname:12345\0 is adding up to 7 chars... */
     141                        int newLength = strlen( hostname ) + 7;
     142                        char *hostAndPort = calloc( 1, newLength );
     143                        if( hostAndPort != NULL )
     144                        {
     145                            snprintf( hostAndPort, newLength, "%s:%d", hostname, port );
     146                            setenv( envName, hostAndPort, 0 );
     147                            free( hostAndPort );
     148                        }
     149                        else
     150                            result = -1;
     151                    }
     152                    else
     153                       setenv( envName, hostname, 0 );
     154                    free( hostname );
     155                }
     156                else
     157                    result = -1;
     158            }
     159        }
     160    }
     161
     162    return result;
     163}
     164
     165
     166/**
     167 *
     168 * Convert a CFStringRef to an ASCII-encoded C string; be sure to free()
     169 * the returned string when done with it.
     170 */
     171char *cfStringToCStringASCII( CFStringRef cfString )
     172{
     173    int strLen = CFStringGetMaximumSizeForEncoding( CFStringGetLength( cfString ), kCFStringEncodingASCII ) + 1;
     174    char *cString = calloc( 1, strLen );
     175    if( cString != NULL )
     176        CFStringGetCString( cfString, cString, strLen, kCFStringEncodingASCII );
     177
     178   return cString;
     179}
     180
     181#endif
     182
  • src/pextlib1.0/proxyfromsystemconfiguration.h

    diff -uNr --exclude=.svn base.orig/src/pextlib1.0/proxyfromsystemconfiguration.h base/src/pextlib1.0/proxyfromsystemconfiguration.h
    old new  
     1/*
     2 * proxyfromsystemconfiguration.h
     3 * $Id: $
     4 *
     5 * Copyright (c) 2008, The MacPorts Project.
     6 * All rights reserved.
     7 *
     8 * Redistribution and use in source and binary forms, with or without
     9 * modification, are permitted provided that the following conditions
     10 * are met:
     11 * 1. Redistributions of source code must retain the above copyright
     12 *    notice, this list of conditions and the following disclaimer.
     13 * 2. Redistributions in binary form must reproduce the above copyright
     14 *    notice, this list of conditions and the following disclaimer in the
     15 *    documentation and/or other materials provided with the distribution.
     16 * 3. Neither the name of MacPorts Team nor the names of its contributors
     17 *    may be used to endorse or promote products derived from this software
     18 *    without specific prior written permission.
     19 *
     20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30 * POSSIBILITY OF SUCH DAMAGE.
     31 */
     32
     33#ifndef _PROXYFROMSYSTEMCONFIGURATION_H
     34#define _PROXYFROMSYSTEMCONFIGURATION_H
     35
     36#include <tcl.h>
     37
     38int getProxyFromSystemConfigurationCmd( ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[] );
     39
     40#endif   /* _PROXYFROMSYSTEMCONFIGURATION_H */
     41
  • src/port1.0/portfetch.tcl

    diff -uNr --exclude=.svn base.orig/src/port1.0/portfetch.tcl base/src/port1.0/portfetch.tcl
    old new  
    466466        if {$portverbose == "yes"} {
    467467                lappend fetch_options "-v"
    468468        }
    469        
     469
     470        proxyfromsystemconfiguration
     471
    470472        foreach {url_var distfile} $fetch_urls {
    471473                if {![file isfile $distpath/$distfile]} {
    472474                        ui_info "$UI_PREFIX [format [msgcat::mc "%s doesn't seem to exist in %s"] $distfile $distpath]"