1 /* 2 * Copyright (c) 2002-2025 Gargoyle Software Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * https://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package org.htmlunit; 16 17 import java.io.Serializable; 18 import java.util.HashMap; 19 import java.util.Map; 20 import java.util.regex.Pattern; 21 22 /** 23 * Class which centralizes proxy configuration, in an effort to reduce clutter in the {@link WebClient} 24 * class. One instance of this class exists for each <code>WebClient</code> instance. 25 * 26 * @author Daniel Gredler 27 * @author Ronald Brill 28 * @see WebClientOptions#getProxyConfig() 29 */ 30 public class ProxyConfig implements Serializable { 31 32 private String proxyHost_; 33 private int proxyPort_; 34 private String proxyScheme_; 35 private boolean isSocksProxy_; 36 private final Map<String, Pattern> proxyBypassHosts_ = new HashMap<>(); 37 private String proxyAutoConfigUrl_; 38 private String proxyAutoConfigContent_; 39 40 /** 41 * Creates a new instance. 42 */ 43 public ProxyConfig() { 44 this(null, 0, null, false); 45 } 46 47 /** 48 * Creates a new instance. 49 * @param proxyHost the proxy host 50 * @param proxyPort the proxy port 51 * @param proxyScheme the scheme http/https; null defaults to http 52 */ 53 public ProxyConfig(final String proxyHost, final int proxyPort, final String proxyScheme) { 54 this(proxyHost, proxyPort, proxyScheme, false); 55 } 56 57 /** 58 * Creates a new instance. 59 * @param proxyHost the proxy host 60 * @param proxyPort the proxy port 61 * @param proxyScheme the scheme http/https 62 * @param isSocks whether SOCKS proxy or not 63 */ 64 public ProxyConfig(final String proxyHost, final int proxyPort, final String proxyScheme, final boolean isSocks) { 65 proxyHost_ = proxyHost; 66 proxyPort_ = proxyPort; 67 proxyScheme_ = proxyScheme; 68 isSocksProxy_ = isSocks; 69 } 70 71 /** 72 * Returns the proxy host used to perform HTTP requests. 73 * @return the proxy host used to perform HTTP requests 74 */ 75 public String getProxyHost() { 76 return proxyHost_; 77 } 78 79 /** 80 * Sets the proxy host used to perform HTTP requests. 81 * @param proxyHost the proxy host used to perform HTTP requests 82 */ 83 public void setProxyHost(final String proxyHost) { 84 proxyHost_ = proxyHost; 85 } 86 87 /** 88 * Returns the proxy port used to perform HTTP requests. 89 * @return the proxy port used to perform HTTP requests 90 */ 91 public int getProxyPort() { 92 return proxyPort_; 93 } 94 95 /** 96 * Sets the proxy port used to perform HTTP requests. 97 * @param proxyPort the proxy port used to perform HTTP requests 98 */ 99 public void setProxyPort(final int proxyPort) { 100 proxyPort_ = proxyPort; 101 } 102 103 /** 104 * Returns the proxy scheme used to perform HTTP requests. 105 * @return the proxy scheme used to perform HTTP requests 106 */ 107 public String getProxyScheme() { 108 return proxyScheme_; 109 } 110 111 /** 112 * Sets the proxy scheme used to perform HTTP requests. 113 * @param proxyScheme the proxy scheme used to perform HTTP requests 114 */ 115 public void setProxyPort(final String proxyScheme) { 116 proxyScheme_ = proxyScheme; 117 } 118 119 /** 120 * Returns whether SOCKS proxy or not. 121 * @return whether SOCKS proxy or not 122 */ 123 public boolean isSocksProxy() { 124 return isSocksProxy_; 125 } 126 127 /** 128 * Sets whether SOCKS proxy or not. 129 * @param isSocksProxy whether SOCKS proxy or not 130 */ 131 public void setSocksProxy(final boolean isSocksProxy) { 132 isSocksProxy_ = isSocksProxy; 133 } 134 135 /** 136 * Any hosts matched by the specified regular expression pattern will bypass the configured proxy. 137 * @param pattern a regular expression pattern that matches the hostnames of the hosts which should 138 * bypass the configured proxy. 139 * @see Pattern 140 */ 141 public void addHostsToProxyBypass(final String pattern) { 142 proxyBypassHosts_.put(pattern, Pattern.compile(pattern)); 143 } 144 145 /** 146 * Any hosts matched by the specified regular expression pattern will no longer bypass the configured proxy. 147 * @param pattern the previously added regular expression pattern 148 * @see Pattern 149 */ 150 public void removeHostsFromProxyBypass(final String pattern) { 151 proxyBypassHosts_.remove(pattern); 152 } 153 154 /** 155 * Returns {@code true} if the host with the specified hostname should be accessed bypassing the 156 * configured proxy. 157 * @param hostname the name of the host to check 158 * @return {@code true} if the host with the specified hostname should be accessed bypassing the 159 * configured proxy, {@code false} otherwise. 160 */ 161 protected boolean shouldBypassProxy(final String hostname) { 162 boolean bypass = false; 163 for (final Pattern p : proxyBypassHosts_.values()) { 164 if (p.matcher(hostname).find()) { 165 bypass = true; 166 break; 167 } 168 } 169 return bypass; 170 } 171 172 /** 173 * Returns the proxy auto-config URL. 174 * @return the proxy auto-config URL 175 */ 176 public String getProxyAutoConfigUrl() { 177 return proxyAutoConfigUrl_; 178 } 179 180 /** 181 * Sets the proxy auto-config URL. 182 * @param proxyAutoConfigUrl the proxy auto-config URL 183 */ 184 public void setProxyAutoConfigUrl(final String proxyAutoConfigUrl) { 185 proxyAutoConfigUrl_ = proxyAutoConfigUrl; 186 setProxyAutoConfigContent(null); 187 } 188 189 /** 190 * Returns the proxy auto-config content. 191 * @return the proxy auto-config content 192 */ 193 protected String getProxyAutoConfigContent() { 194 return proxyAutoConfigContent_; 195 } 196 197 /** 198 * Sets the proxy auto-config content. 199 * @param proxyAutoConfigContent the proxy auto-config content 200 */ 201 protected void setProxyAutoConfigContent(final String proxyAutoConfigContent) { 202 proxyAutoConfigContent_ = proxyAutoConfigContent; 203 } 204 }