1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*
* $Id: HTTPURLConnection.java,v 1.4 2004/07/30 20:56:02 dog Exp $
* Copyright (C) 2004 The Free Software Foundation
*
* This file is part of GNU inetlib, a library.
*
* GNU inetlib is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNU inetlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* As a special exception, if you link this library with other files to
* produce an executable, this library does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* This exception does not however invalidate any other reasons why the
* executable file might be covered by the GNU General Public License.
*/
package gnu.inet.http;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Iterator;
import java.util.Map;
/**
* A URLConnection that uses the HTTPConnection class.
*
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
*/
public class HTTPURLConnection
extends HttpURLConnection
{
/*
* The underlying connection.
*/
private HTTPConnection connection;
private String proxyHostname;
private int proxyPort;
private Request request;
private Headers requestHeaders;
private ByteArrayOutputStream requestSink;
private Response response;
private ByteArrayInputStream responseSink;
/**
* Constructor.
* @param url the URL
*/
public HTTPURLConnection (URL url)
{
super (url);
requestHeaders = new Headers ();
AccessController.doPrivileged(this.new GetProxyAction());
}
class GetProxyAction implements PrivilegedAction
{
public Object run()
{
proxyHostname = System.getProperty("http.proxyHost");
if (proxyHostname != null)
{
String port = System.getProperty("http.proxyPort");
proxyPort = (port != null) ? Integer.parseInt (port) : -1;
}
return null;
}
}
public void connect ()
throws IOException
{
if (connected)
{
return;
}
String protocol = url.getProtocol ();
boolean secure = "https".equals (protocol);
String host = url.getHost ();
int port = url.getPort ();
if (port < 0)
{
port = secure ? HTTPConnection.HTTPS_PORT :
HTTPConnection.HTTP_PORT;
}
String file = url.getFile ();
String username = url.getUserInfo ();
String password = null;
if (username != null)
{
int ci = username.indexOf (':');
if (ci != -1)
{
password = username.substring (ci + 1);
username = username.substring (0, ci);
}
}
final Credentials creds = (username == null) ? null :
new Credentials (username, password);
boolean retry;
do
{
retry = false;
if (connection == null)
{
connection = new HTTPConnection (host, port, secure);
}
if (proxyHostname != null)
{
if (proxyPort < 0)
{
proxyPort = secure ? HTTPConnection.HTTPS_PORT :
HTTPConnection.HTTP_PORT;
}
connection.setProxy (proxyHostname, proxyPort);
}
request = connection.newRequest (method, file);
request.getHeaders ().putAll (requestHeaders);
if (requestSink != null)
{
byte[] content = requestSink.toByteArray ();
RequestBodyWriter writer = new ByteArrayRequestBodyWriter (content);
request.setRequestBodyWriter (writer);
}
ByteArrayResponseBodyReader reader = new ByteArrayResponseBodyReader ();
request.setResponseBodyReader (reader);
if (creds != null)
{
request.setAuthenticator(new Authenticator() {
public Credentials getCredentials(String realm, int attempts)
{
return (attempts < 2) ? creds : null;
}
});
}
response = request.dispatch ();
if (response.getCodeClass () == 3 && getInstanceFollowRedirects ())
{
// Follow redirect
String location = response.getHeader ("Location");
String connectionUri = connection.getURI ();
int start = connectionUri.length ();
if (location.startsWith (connectionUri) &&
location.charAt (start) == '/')
{
file = location.substring (start);
retry = true;
}
else if (location.startsWith ("http:"))
{
connection.close ();
connection = null;
secure = false;
start = 7;
int end = location.indexOf ('/', start);
host = location.substring(start, end);
int ci = host.lastIndexOf (':');
if (ci != -1)
{
port = Integer.parseInt (host.substring (ci + 1));
host = host.substring (0, ci);
}
else
{
port = HTTPConnection.HTTP_PORT;
}
file = location.substring (end);
retry = true;
}
else if (location.startsWith ("https:"))
{
connection.close ();
connection = null;
secure = true;
start = 8;
int end = location.indexOf ('/', start);
host = location.substring(start, end);
int ci = host.lastIndexOf (':');
if (ci != -1)
{
port = Integer.parseInt (host.substring (ci + 1));
host = host.substring (0, ci);
}
else
{
port = HTTPConnection.HTTPS_PORT;
}
file = location.substring (end);
retry = true;
}
// Otherwise this is not an HTTP redirect, can't follow
}
else
{
responseSink = new ByteArrayInputStream (reader.toByteArray ());
}
}
while (retry);
connected = true;
}
public void disconnect ()
{
if (connection != null)
{
try
{
connection.close ();
}
catch (IOException e)
{
}
}
}
public boolean usingProxy ()
{
return (proxyHostname != null);
}
/**
* Overrides the corresponding method in HttpURLConnection to permit
* arbitrary methods, as long as they're valid ASCII alphabetic
* characters. This is to permit WebDAV and other HTTP extensions to
* function.
* @param method the method
*/
public void setRequestMethod (String method)
throws ProtocolException
{
if (connected)
{
throw new ProtocolException ("Already connected");
}
// Validate
method = method.toUpperCase ();
int len = method.length ();
if (len == 0)
{
throw new ProtocolException ("Empty method name");
}
for (int i = 0; i < len; i++)
{
char c = method.charAt (i);
if (c < 0x41 || c > 0x5a)
{
throw new ProtocolException ("Illegal character '" + c +
"' at index " + i);
}
}
// OK
this.method = method;
}
public String getRequestProperty (String key)
{
return requestHeaders.getValue (key);
}
public Map getRequestProperties ()
{
return requestHeaders;
}
public void setRequestProperty (String key, String value)
{
requestHeaders.put (key, value);
}
public void addRequestProperty (String key, String value)
{
String old = requestHeaders.getValue (key);
if (old == null)
{
requestHeaders.put (key, value);
}
else
{
requestHeaders.put (key, old + "," + value);
}
}
public OutputStream getOutputStream ()
throws IOException
{
if (connected)
{
throw new ProtocolException ("Already connected");
}
if (!doOutput)
{
throw new ProtocolException ("doOutput is false");
}
if (requestSink == null)
{
requestSink = new ByteArrayOutputStream ();
}
return requestSink;
}
// -- Response --
public InputStream getInputStream ()
throws IOException
{
if (!connected)
{
connect ();
}
if (!doInput)
{
throw new ProtocolException ("doInput is false");
}
return responseSink;
}
public Map getHeaderFields ()
{
if (!connected)
{
return null;
}
return response.getHeaders ();
}
public String getHeaderField (int index)
{
if (!connected)
{
return null;
}
Iterator i = response.getHeaders ().entrySet ().iterator ();
Map.Entry entry;
int count = 0;
do
{
entry = (Map.Entry) i.next ();
count++;
}
while (count < index);
return (String) entry.getValue ();
}
public String getHeaderFieldKey (int index)
{
if (!connected)
{
return null;
}
Iterator i = response.getHeaders ().entrySet ().iterator ();
Map.Entry entry;
int count = 0;
do
{
entry = (Map.Entry) i.next ();
count++;
}
while (count < index);
return (String) entry.getKey ();
}
}
|