| 1 | /* |
| 2 | * Copyright (C) 2017 Igalia S.L. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "WebKitNetworkProxySettings.h" |
| 22 | |
| 23 | #include "WebKitNetworkProxySettingsPrivate.h" |
| 24 | #include <WebCore/SoupNetworkProxySettings.h> |
| 25 | #include <wtf/glib/WTFGType.h> |
| 26 | |
| 27 | using namespace WebCore; |
| 28 | |
| 29 | /** |
| 30 | * SECTION: WebKitNetworkProxySettings |
| 31 | * @Short_description: Network Proxy Settings |
| 32 | * @Title: WebKitNetworkProxySettings |
| 33 | * @See_also: #WebKitWebContext |
| 34 | * |
| 35 | * WebKitNetworkProxySettings can be used to provide a custom proxy configuration |
| 36 | * to a #WebKitWebContext. You need to call webkit_web_context_set_network_proxy_settings() |
| 37 | * with %WEBKIT_NETWORK_PROXY_MODE_CUSTOM and a WebKitNetworkProxySettings. |
| 38 | * |
| 39 | * Since: 2.16 |
| 40 | */ |
| 41 | struct _WebKitNetworkProxySettings { |
| 42 | _WebKitNetworkProxySettings() |
| 43 | : settings(SoupNetworkProxySettings::Mode::Custom) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | explicit _WebKitNetworkProxySettings(const SoupNetworkProxySettings& otherSettings) |
| 48 | : settings(otherSettings) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | SoupNetworkProxySettings settings; |
| 53 | }; |
| 54 | |
| 55 | G_DEFINE_BOXED_TYPE(WebKitNetworkProxySettings, webkit_network_proxy_settings, webkit_network_proxy_settings_copy, webkit_network_proxy_settings_free) |
| 56 | |
| 57 | const SoupNetworkProxySettings& webkitNetworkProxySettingsGetNetworkProxySettings(WebKitNetworkProxySettings* proxySettings) |
| 58 | { |
| 59 | ASSERT(proxySettings); |
| 60 | return proxySettings->settings; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * webkit_network_proxy_settings_new: |
| 65 | * @default_proxy_uri: (allow-none): the default proxy URI to use, or %NULL. |
| 66 | * @ignore_hosts: (allow-none) (array zero-terminated=1): an optional list of hosts/IP addresses to not use a proxy for. |
| 67 | * |
| 68 | * Create a new #WebKitNetworkProxySettings with the given @default_proxy_uri and @ignore_hosts. |
| 69 | * |
| 70 | * The default proxy URI will be used for any URI that doesn't match @ignore_hosts, and doesn't match any |
| 71 | * of the schemes added with webkit_network_proxy_settings_add_proxy_for_scheme(). |
| 72 | * If @default_proxy_uri starts with "socks://", it will be treated as referring to all three of the |
| 73 | * socks5, socks4a, and socks4 proxy types. |
| 74 | * |
| 75 | * @ignore_hosts is a list of hostnames and IP addresses that the resolver should allow direct connections to. |
| 76 | * Entries can be in one of 4 formats: |
| 77 | * <itemizedlist> |
| 78 | * <listitem><para> |
| 79 | * A hostname, such as "example.com", ".example.com", or "*.example.com", any of which match "example.com" or |
| 80 | * any subdomain of it. |
| 81 | * </para></listitem> |
| 82 | * <listitem><para> |
| 83 | * An IPv4 or IPv6 address, such as "192.168.1.1", which matches only that address. |
| 84 | * </para></listitem> |
| 85 | * <listitem><para> |
| 86 | * A hostname or IP address followed by a port, such as "example.com:80", which matches whatever the hostname or IP |
| 87 | * address would match, but only for URLs with the (explicitly) indicated port. In the case of an IPv6 address, the address |
| 88 | * part must appear in brackets: "[::1]:443" |
| 89 | * </para></listitem> |
| 90 | * <listitem><para> |
| 91 | * An IP address range, given by a base address and prefix length, such as "fe80::/10", which matches any address in that range. |
| 92 | * </para></listitem> |
| 93 | * </itemizedlist> |
| 94 | * |
| 95 | * Note that when dealing with Unicode hostnames, the matching is done against the ASCII form of the name. |
| 96 | * Also note that hostname exclusions apply only to connections made to hosts identified by name, and IP address exclusions apply only |
| 97 | * to connections made to hosts identified by address. That is, if example.com has an address of 192.168.1.1, and @ignore_hosts |
| 98 | * contains only "192.168.1.1", then a connection to "example.com" will use the proxy, and a connection to 192.168.1.1" will not. |
| 99 | * |
| 100 | * Returns: (transfer full): A new #WebKitNetworkProxySettings. |
| 101 | * |
| 102 | * Since: 2.16 |
| 103 | */ |
| 104 | WebKitNetworkProxySettings* webkit_network_proxy_settings_new(const char* defaultProxyURI, const char* const* ignoreHosts) |
| 105 | { |
| 106 | WebKitNetworkProxySettings* proxySettings = static_cast<WebKitNetworkProxySettings*>(fastMalloc(sizeof(WebKitNetworkProxySettings))); |
| 107 | new (proxySettings) WebKitNetworkProxySettings; |
| 108 | if (defaultProxyURI) |
| 109 | proxySettings->settings.defaultProxyURL = defaultProxyURI; |
| 110 | if (ignoreHosts) |
| 111 | proxySettings->settings.ignoreHosts.reset(g_strdupv(const_cast<char**>(ignoreHosts))); |
| 112 | return proxySettings; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * webkit_network_proxy_settings_copy: |
| 117 | * @proxy_settings: a #WebKitNetworkProxySettings |
| 118 | * |
| 119 | * Make a copy of the #WebKitNetworkProxySettings. |
| 120 | * |
| 121 | * Returns: (transfer full): A copy of passed in #WebKitNetworkProxySettings |
| 122 | * |
| 123 | * Since: 2.16 |
| 124 | */ |
| 125 | WebKitNetworkProxySettings* webkit_network_proxy_settings_copy(WebKitNetworkProxySettings* proxySettings) |
| 126 | { |
| 127 | g_return_val_if_fail(proxySettings, nullptr); |
| 128 | |
| 129 | WebKitNetworkProxySettings* copy = static_cast<WebKitNetworkProxySettings*>(fastMalloc(sizeof(WebKitNetworkProxySettings))); |
| 130 | new (copy) WebKitNetworkProxySettings(webkitNetworkProxySettingsGetNetworkProxySettings(proxySettings)); |
| 131 | return copy; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * webkit_network_proxy_settings_free: |
| 136 | * @proxy_settings: A #WebKitNetworkProxySettings |
| 137 | * |
| 138 | * Free the #WebKitNetworkProxySettings. |
| 139 | * |
| 140 | * Since: 2.16 |
| 141 | */ |
| 142 | void webkit_network_proxy_settings_free(WebKitNetworkProxySettings* proxySettings) |
| 143 | { |
| 144 | g_return_if_fail(proxySettings); |
| 145 | |
| 146 | proxySettings->~WebKitNetworkProxySettings(); |
| 147 | fastFree(proxySettings); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * webkit_network_proxy_settings_add_proxy_for_scheme: |
| 152 | * @proxy_settings: a #WebKitNetworkProxySettings |
| 153 | * @scheme: the URI scheme to add a proxy for |
| 154 | * @proxy_uri: the proxy URI to use for @uri_scheme |
| 155 | * |
| 156 | * Adds a URI-scheme-specific proxy. URIs whose scheme matches @uri_scheme will be proxied via @proxy_uri. |
| 157 | * As with the default proxy URI, if @proxy_uri starts with "socks://", it will be treated as referring to |
| 158 | * all three of the socks5, socks4a, and socks4 proxy types. |
| 159 | * |
| 160 | * Since: 2.16 |
| 161 | */ |
| 162 | void webkit_network_proxy_settings_add_proxy_for_scheme(WebKitNetworkProxySettings* proxySettings, const char* scheme, const char* proxyURI) |
| 163 | { |
| 164 | g_return_if_fail(proxySettings); |
| 165 | g_return_if_fail(scheme); |
| 166 | g_return_if_fail(proxyURI); |
| 167 | |
| 168 | proxySettings->settings.proxyMap.add(scheme, proxyURI); |
| 169 | } |
| 170 | |