Debian Codegraph @

src/tools/proxy.c

raw
1 /* proxy.c
2 *
3 * Copyright 2022-2023 The Libproxy Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26
27 #include "proxy.h"
28
29 void print_proxies (char **proxies);
30
31 /**
32 * Prints an array of proxies. Proxies are space separated.
33 * @proxies an array containing the proxies returned by libproxy.
34 */
35 void
36 print_proxies (char **proxies)
37 {
38 int j;
39
40 if (!proxies) {
41 printf ("\n");
42 return;
43 }
44
45 for (j = 0; proxies[j]; j++)
46 printf ("%s%s", proxies[j], proxies[j + 1] ? " " : "\n");
47 }
48
49 int
50 main (int argc,
51 char **argv)
52 {
53 int i;
54 char url[102400]; /* Should be plently long for most URLs */
55 char **proxies;
56
57 /* Create the proxy factory object */
58 pxProxyFactory *pf = px_proxy_factory_new ();
59 if (!pf) {
60 fprintf (stderr, "An unknown error occurred!\n");
61 return 1;
62 }
63
64 /* User entered some arguments on startup. skip interactive */
65 if (argc > 1) {
66 for (i = 1; i < argc; i++) {
67 /*
68 * Get an array of proxies to use. These should be used
69 * in the order returned. Only move on to the next proxy
70 * if the first one fails (etc).
71 */
72 proxies = px_proxy_factory_get_proxies (pf, argv[i]);
73 print_proxies (proxies);
74 px_proxy_factory_free_proxies (proxies);
75 }
76 }
77 /* Interactive mode */
78 else {
79 /* For each URL we read on STDIN, get the proxies to use */
80 for (url[0] = '\0'; fgets (url, 102400, stdin) != NULL;) {
81 if (url[strlen (url) - 1] == '\n') url[strlen (url) - 1] = '\0';
82
83 /*
84 * Get an array of proxies to use. These should be used
85 * in the order returned. Only move on to the next proxy
86 * if the first one fails (etc).
87 */
88 proxies = px_proxy_factory_get_proxies (pf, url);
89 print_proxies (proxies);
90 px_proxy_factory_free_proxies (proxies);
91 }
92 }
93 /* Destroy the proxy factory object */
94 px_proxy_factory_free (pf);
95 return 0;
96 }