https: add support for HTTPS proxies
[vlc.git] / src / posix / netconf.c
blob4dd751b2e4e292c6358197f665989ca760431e41
1 /*****************************************************************************
2 * netconf.c : Network configuration
3 *****************************************************************************
4 * Copyright (C) 2013 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <stdlib.h>
26 #include <string.h>
27 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <spawn.h>
33 #include <unistd.h>
35 extern char **environ;
37 #include <vlc_common.h>
38 #include <vlc_fs.h>
39 #include <vlc_network.h>
41 /**
42 * Determines the network proxy server to use (if any).
43 * @param url absolute URL for which to get the proxy server
44 * @return proxy URL, NULL if no proxy or error
46 char *vlc_getProxyUrl(const char *url)
48 /* libproxy helper */
49 pid_t pid;
50 posix_spawn_file_actions_t actions;
51 posix_spawnattr_t attr;
52 char *argv[3] = { (char *)"proxy", (char *)url, NULL };
53 int fd[2];
55 if (vlc_pipe(fd))
56 return NULL;
58 posix_spawn_file_actions_init(&actions);
59 posix_spawn_file_actions_addopen(&actions, STDIN_FILENO, "/dev/null",
60 O_RDONLY, 0644);
61 posix_spawn_file_actions_adddup2(&actions, fd[1], STDOUT_FILENO);
63 posix_spawnattr_init(&attr);
65 sigset_t set;
67 sigemptyset(&set);
68 posix_spawnattr_setsigmask(&attr, &set);
69 sigaddset (&set, SIGPIPE);
70 posix_spawnattr_setsigdefault(&attr, &set);
71 posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF
72 | POSIX_SPAWN_SETSIGMASK);
75 if (posix_spawnp(&pid, "proxy", &actions, &attr, argv, environ))
76 pid = -1;
78 posix_spawnattr_destroy(&attr);
79 posix_spawn_file_actions_destroy(&actions);
80 close(fd[1]);
82 if (pid != -1)
84 char buf[1024];
85 size_t len = 0;
89 ssize_t val = read(fd[0], buf + len, sizeof (buf) - len);
90 if (val <= 0)
91 break;
92 len += val;
94 while (len < sizeof (buf));
96 close(fd[0]);
97 while (waitpid(pid, &(int){ 0 }, 0) == -1);
99 if (len >= 9 && !strncasecmp(buf, "direct://", 9))
100 return NULL;
102 char *end = memchr(buf, '\n', len);
103 if (end != NULL)
105 *end = '\0';
106 return strdup(buf);
108 /* Parse error: fallback (may be due to missing executable) */
110 else
111 close(fd[0]);
113 /* Fallback to environment variable */
114 char *var = getenv("http_proxy");
115 if (var != NULL)
116 var = strdup(var);
117 return var;