]> git.sesse.net Git - vlc/blob - src/darwin/netconf.c
src/darwin: fix crash while accessing http content while no proxy is set
[vlc] / src / darwin / netconf.c
1 /*****************************************************************************
2  * netconf.c : Network configuration
3  *****************************************************************************
4  * Copyright (C) 2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne # videolan org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_network.h>
30
31 #include <CoreFoundation/CoreFoundation.h>
32 #include <SystemConfiguration/SystemConfiguration.h>
33
34 /**
35  * Determines the network proxy server to use (if any).
36  * @param url absolute URL for which to get the proxy server (not used)
37  * @return proxy URL, NULL if no proxy or error
38  */
39 char *vlc_getProxyUrl(const char *url)
40 {
41     VLC_UNUSED(url);
42     CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);
43     char *proxy_url = NULL;
44
45     if (proxies) {
46         CFNumberRef cfn_httpProxyOn =
47             (CFNumberRef)CFDictionaryGetValue(proxies,
48                                               kSCPropNetProxiesHTTPEnable);
49         if (cfn_httpProxyOn) {
50             int i_httpProxyOn;
51             CFNumberGetValue(cfn_httpProxyOn, kCFNumberIntType, &i_httpProxyOn);
52             CFRelease(cfn_httpProxyOn);
53
54             if (i_httpProxyOn == 1) // http proxy is on
55             {
56                 CFStringRef httpProxy =
57                     (CFStringRef)CFDictionaryGetValue(proxies,
58                                                       kSCPropNetProxiesHTTPProxy);
59
60                 if (httpProxy) {
61                     CFNumberRef cfn_httpProxyPort =
62                         (CFNumberRef)CFDictionaryGetValue(proxies,
63                                                         kSCPropNetProxiesHTTPPort);
64                     int i_httpProxyPort;
65                     CFNumberGetValue(cfn_httpProxyPort,
66                                      kCFNumberIntType,
67                                      &i_httpProxyPort);
68                     CFRelease(cfn_httpProxyPort);
69
70                     CFMutableStringRef outputURL =
71                         CFStringCreateMutableCopy(kCFAllocatorDefault,
72                                                   0,
73                                                   httpProxy);
74                     if (i_httpProxyPort > 0)
75                         CFStringAppendFormat(outputURL,
76                                              NULL,
77                                              CFSTR(":%i"),
78                                              i_httpProxyPort);
79
80                     CFStringGetCString(outputURL,
81                                        proxy_url,
82                                        sizeof(proxy_url),
83                                        kCFStringEncodingASCII);
84                     CFRelease(outputURL);
85                 }
86                 CFRelease(httpProxy);
87             }
88         }
89         CFRelease(proxies);
90     }
91
92     return proxy_url;
93 }