1 /* 2 * Copyright (c) 2011-2017 Contributors to the Eclipse Foundation 3 * 4 * This program and the accompanying materials are made available under the 5 * terms of the Eclipse Public License 2.0 which is available at 6 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 7 * which is available at https://www.apache.org/licenses/LICENSE-2.0. 8 * 9 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 10 */ 11 12 module hunt.net.ProxyOptions; 13 14 import hunt.net.ProxyType; 15 import hunt.Exceptions; 16 17 import std.array; 18 import std.conv; 19 20 /** 21 * Proxy options for a net client or a net client. 22 * 23 * @author <a href="http://oss.lehmann.cx/">Alexander Lehmann</a> 24 */ 25 class ProxyOptions { 26 27 /** 28 * The default proxy type (HTTP) 29 */ 30 enum ProxyType DEFAULT_TYPE = ProxyType.HTTP; 31 32 /** 33 * The default port for proxy connect = 3128 34 * 35 * 3128 is the default port for e.g. Squid 36 */ 37 enum int DEFAULT_PORT = 3128; 38 39 /** 40 * The default hostname for proxy connect = "localhost" 41 */ 42 enum string DEFAULT_HOST = "localhost"; 43 44 private string host; 45 private int port; 46 private string username; 47 private string password; 48 private ProxyType type; 49 50 /** 51 * Default constructor. 52 */ 53 this() { 54 host = DEFAULT_HOST; 55 port = DEFAULT_PORT; 56 type = DEFAULT_TYPE; 57 } 58 59 /** 60 * Copy constructor. 61 * 62 * @param other the options to copy 63 */ 64 this(ProxyOptions other) { 65 host = other.getHost(); 66 port = other.getPort(); 67 username = other.getUsername(); 68 password = other.getPassword(); 69 type = other.getType(); 70 } 71 72 /** 73 * Get proxy host. 74 * 75 * @return proxy hosts 76 */ 77 string getHost() { 78 return host; 79 } 80 81 /** 82 * Set proxy host. 83 * 84 * @param host the proxy host to connect to 85 * @return a reference to this, so the API can be used fluently 86 */ 87 ProxyOptions setHost(string host) { 88 assert(!host.empty(), "Proxy host may not be null"); 89 this.host = host; 90 return this; 91 } 92 93 /** 94 * Get proxy port. 95 * 96 * @return proxy port 97 */ 98 int getPort() { 99 return port; 100 } 101 102 /** 103 * Set proxy port. 104 * 105 * @param port the proxy port to connect to 106 * @return a reference to this, so the API can be used fluently 107 */ 108 ProxyOptions setPort(int port) { 109 if (port < 0 || port > 65535) { 110 throw new IllegalArgumentException("Invalid proxy port " ~ port.to!string); 111 } 112 this.port = port; 113 return this; 114 } 115 116 /** 117 * Get proxy username. 118 * 119 * @return proxy username 120 */ 121 string getUsername() { 122 return username; 123 } 124 125 /** 126 * Set proxy username. 127 * 128 * @param username the proxy username 129 * @return a reference to this, so the API can be used fluently 130 */ 131 ProxyOptions setUsername(string username) { 132 this.username = username; 133 return this; 134 } 135 136 /** 137 * Get proxy password. 138 * 139 * @return proxy password 140 */ 141 string getPassword() { 142 return password; 143 } 144 145 /** 146 * Set proxy password. 147 * 148 * @param password the proxy password 149 * @return a reference to this, so the API can be used fluently 150 */ 151 ProxyOptions setPassword(string password) { 152 this.password = password; 153 return this; 154 } 155 156 /** 157 * Get proxy type. 158 * 159 *<p>ProxyType can be HTTP, SOCKS4 and SOCKS5 160 * 161 * @return proxy type 162 */ 163 ProxyType getType() { 164 return type; 165 } 166 167 /** 168 * Set proxy type. 169 * 170 * <p>ProxyType can be HTTP, SOCKS4 and SOCKS5 171 * 172 * @param type the proxy type to connect to 173 * @return a reference to this, so the API can be used fluently 174 */ 175 ProxyOptions setType(ProxyType type) { 176 this.type = type; 177 return this; 178 } 179 180 override 181 bool opEquals(Object o) { 182 if (this is o) return true; 183 if (!super.opEquals(o)) return false; 184 185 ProxyOptions that = cast(ProxyOptions) o; 186 if(that is null) 187 return false; 188 189 if (type != that.type) return false; 190 if (host != that.host) return false; 191 if (port != that.port) return false; 192 if (password != that.password) return false; 193 if (username != that.username) return false; 194 195 return true; 196 } 197 198 override 199 size_t toHash() @trusted nothrow { 200 size_t result = super.toHash(); 201 result = 31 * result + type.hashOf(); 202 result = 31 * result + host.hashOf(); 203 result = 31 * result + port; 204 result = 31 * result + (password !is null ? password.hashOf() : 0); 205 result = 31 * result + (username !is null ? username.hashOf() : 0); 206 return result; 207 } 208 }