1 | // HttpURLConnection.java - Subclass of communications links using
|
---|
2 | // Hypertext Transfer Protocol.
|
---|
3 |
|
---|
4 | /* Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation
|
---|
5 |
|
---|
6 | This file is part of GNU Classpath.
|
---|
7 |
|
---|
8 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 2, or (at your option)
|
---|
11 | any later version.
|
---|
12 |
|
---|
13 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
20 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
21 | 02111-1307 USA.
|
---|
22 |
|
---|
23 | Linking this library statically or dynamically with other modules is
|
---|
24 | making a combined work based on this library. Thus, the terms and
|
---|
25 | conditions of the GNU General Public License cover the whole
|
---|
26 | combination.
|
---|
27 |
|
---|
28 | As a special exception, the copyright holders of this library give you
|
---|
29 | permission to link this library with independent modules to produce an
|
---|
30 | executable, regardless of the license terms of these independent
|
---|
31 | modules, and to copy and distribute the resulting executable under
|
---|
32 | terms of your choice, provided that you also meet, for each linked
|
---|
33 | independent module, the terms and conditions of the license of that
|
---|
34 | module. An independent module is a module which is not derived from
|
---|
35 | or based on this library. If you modify this library, you may extend
|
---|
36 | this exception to your version of the library, but you are not
|
---|
37 | obligated to do so. If you do not wish to do so, delete this
|
---|
38 | exception statement from your version. */
|
---|
39 |
|
---|
40 | package java.net;
|
---|
41 |
|
---|
42 | import java.io.*;
|
---|
43 | import java.security.Permission;
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * Written using on-line Java Platform 1.2 API Specification, as well
|
---|
47 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
---|
48 | * Status: Believed complete and correct.
|
---|
49 | */
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * This class provides a common abstract implementation for those
|
---|
53 | * URL connection classes that will connect using the HTTP protocol.
|
---|
54 | * In addition to the functionality provided by the URLConnection
|
---|
55 | * class, it defines constants for HTTP return code values and
|
---|
56 | * methods for setting the HTTP request method and determining whether
|
---|
57 | * or not to follow redirects.
|
---|
58 | *
|
---|
59 | * @since 1.1
|
---|
60 | *
|
---|
61 | * @author Warren Levy (warrenl@cygnus.com)
|
---|
62 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
63 | */
|
---|
64 | public abstract class HttpURLConnection extends URLConnection
|
---|
65 | {
|
---|
66 | /* HTTP Success Response Codes */
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Indicates that the client may continue with its request. This value
|
---|
70 | * is specified as part of RFC 2068 but was not included in Sun's JDK, so
|
---|
71 | * beware of using this value
|
---|
72 | */
|
---|
73 | static final int HTTP_CONTINUE = 100;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Indicates the request succeeded.
|
---|
77 | */
|
---|
78 | public static final int HTTP_OK = 200;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * The requested resource has been created.
|
---|
82 | */
|
---|
83 | public static final int HTTP_CREATED = 201;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * The request has been accepted for processing but has not completed.
|
---|
87 | * There is no guarantee that the requested action will actually ever
|
---|
88 | * be completed succesfully, but everything is ok so far.
|
---|
89 | */
|
---|
90 | public static final int HTTP_ACCEPTED = 202;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * The meta-information returned in the header is not the actual data
|
---|
94 | * from the original server, but may be from a local or other copy.
|
---|
95 | * Normally this still indicates a successful completion.
|
---|
96 | */
|
---|
97 | public static final int HTTP_NOT_AUTHORITATIVE = 203;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * The server performed the request, but there is no data to send
|
---|
101 | * back. This indicates that the user's display should not be changed.
|
---|
102 | */
|
---|
103 | public static final int HTTP_NO_CONTENT = 204;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The server performed the request, but there is no data to sent back,
|
---|
107 | * however, the user's display should be "reset" to clear out any form
|
---|
108 | * fields entered.
|
---|
109 | */
|
---|
110 | public static final int HTTP_RESET = 205;
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * The server completed the partial GET request for the resource.
|
---|
114 | */
|
---|
115 | public static final int HTTP_PARTIAL = 206;
|
---|
116 |
|
---|
117 |
|
---|
118 | /* HTTP Redirection Response Codes */
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * There is a list of choices available for the requested resource.
|
---|
122 | */
|
---|
123 | public static final int HTTP_MULT_CHOICE = 300;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * The resource has been permanently moved to a new location.
|
---|
127 | */
|
---|
128 | public static final int HTTP_MOVED_PERM = 301;
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * The resource requested has been temporarily moved to a new location.
|
---|
132 | */
|
---|
133 | public static final int HTTP_MOVED_TEMP = 302;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * The response to the request issued is available at another location.
|
---|
137 | */
|
---|
138 | public static final int HTTP_SEE_OTHER = 303;
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * The document has not been modified since the criteria specified in
|
---|
142 | * a conditional GET.
|
---|
143 | */
|
---|
144 | public static final int HTTP_NOT_MODIFIED = 304;
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * The requested resource needs to be accessed through a proxy.
|
---|
148 | */
|
---|
149 | public static final int HTTP_USE_PROXY = 305;
|
---|
150 |
|
---|
151 |
|
---|
152 | /* HTTP Client Error Response Codes */
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * The request was misformed or could not be understood.
|
---|
156 | */
|
---|
157 | public static final int HTTP_BAD_REQUEST = 400;
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * The request made requires user authorization. Try again with
|
---|
161 | * a correct authentication header.
|
---|
162 | */
|
---|
163 | public static final int HTTP_UNAUTHORIZED = 401;
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Code reserved for future use - I hope way in the future.
|
---|
167 | */
|
---|
168 | public static final int HTTP_PAYMENT_REQUIRED = 402;
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * There is no permission to access the requested resource.
|
---|
172 | */
|
---|
173 | public static final int HTTP_FORBIDDEN = 403;
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * The requested resource was not found.
|
---|
177 | */
|
---|
178 | public static final int HTTP_NOT_FOUND = 404;
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * The specified request method is not allowed for this resource.
|
---|
182 | */
|
---|
183 | public static final int HTTP_BAD_METHOD = 405;
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Based on the input headers sent, the resource returned in response
|
---|
187 | * to the request would not be acceptable to the client.
|
---|
188 | */
|
---|
189 | public static final int HTTP_NOT_ACCEPTABLE = 406;
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * The client must authenticate with a proxy prior to attempting this
|
---|
193 | * request.
|
---|
194 | */
|
---|
195 | public static final int HTTP_PROXY_AUTH = 407;
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * The request timed out.
|
---|
199 | */
|
---|
200 | public static final int HTTP_CLIENT_TIMEOUT = 408;
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * There is a conflict between the current state of the resource and the
|
---|
204 | * requested action.
|
---|
205 | */
|
---|
206 | public static final int HTTP_CONFLICT = 409;
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * The requested resource is no longer available. This ususally indicates
|
---|
210 | * a permanent condition.
|
---|
211 | */
|
---|
212 | public static final int HTTP_GONE = 410;
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * A Content-Length header is required for this request, but was not
|
---|
216 | * supplied.
|
---|
217 | */
|
---|
218 | public static final int HTTP_LENGTH_REQUIRED = 411;
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * A client specified pre-condition was not met on the server.
|
---|
222 | */
|
---|
223 | public static final int HTTP_PRECON_FAILED = 412;
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * The request sent was too large for the server to handle.
|
---|
227 | */
|
---|
228 | public static final int HTTP_ENTITY_TOO_LARGE = 413;
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * The name of the resource specified was too long.
|
---|
232 | */
|
---|
233 | public static final int HTTP_REQ_TOO_LONG = 414;
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * The request is in a format not supported by the requested resource.
|
---|
237 | */
|
---|
238 | public static final int HTTP_UNSUPPORTED_TYPE = 415;
|
---|
239 |
|
---|
240 |
|
---|
241 | /* HTTP Server Error Response Codes */
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * This error code indicates that some sort of server error occurred.
|
---|
245 | */
|
---|
246 | public static final int HTTP_SERVER_ERROR = 500;
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * The server encountered an unexpected error (such as a CGI script crash)
|
---|
250 | * that prevents the request from being fulfilled.
|
---|
251 | */
|
---|
252 | public static final int HTTP_INTERNAL_ERROR = 500;
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * The server does not support the requested functionality.
|
---|
256 | * @since 1.3
|
---|
257 | */
|
---|
258 | public static final int HTTP_NOT_IMPLEMENTED = 501;
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * The proxy encountered a bad response from the server it was proxy-ing for
|
---|
262 | */
|
---|
263 | public static final int HTTP_BAD_GATEWAY = 502;
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * The HTTP service is not availalble, such as because it is overloaded
|
---|
267 | * and does not want additional requests.
|
---|
268 | */
|
---|
269 | public static final int HTTP_UNAVAILABLE = 503;
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * The proxy timed out getting a reply from the remote server it was
|
---|
273 | * proxy-ing for.
|
---|
274 | */
|
---|
275 | public static final int HTTP_GATEWAY_TIMEOUT = 504;
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * This server does not support the protocol version requested.
|
---|
279 | */
|
---|
280 | public static final int HTTP_VERSION = 505;
|
---|
281 |
|
---|
282 | // Non-HTTP response static variables
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Flag to indicate whether or not redirects should be automatically
|
---|
286 | * followed by default.
|
---|
287 | */
|
---|
288 | private static boolean followRedirects = true;
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * This is a list of valid request methods, separated by "|" characters.
|
---|
292 | */
|
---|
293 | private static String valid_methods
|
---|
294 | = "|GET|POST|HEAD|OPTIONS|PUT|DELETE|TRACE|";
|
---|
295 |
|
---|
296 | // Instance Variables
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * The requested method in use for this connection. Default is GET.
|
---|
300 | */
|
---|
301 | protected String method = "GET";
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * The response code received from the server
|
---|
305 | */
|
---|
306 | protected int responseCode = -1;
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * The response message string received from the server.
|
---|
310 | */
|
---|
311 | protected String responseMessage = null;
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * If this instance should follow redirect requests.
|
---|
315 | */
|
---|
316 | protected boolean instanceFollowRedirects = followRedirects;
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Whether we alreadt got a valid response code for this connection.
|
---|
320 | * Used by <code>getResponceCode()</code> and
|
---|
321 | * <code>getResponseMessage()</code>.
|
---|
322 | */
|
---|
323 | private boolean gotResponseVals = false;
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Create an HttpURLConnection for the specified URL
|
---|
327 | *
|
---|
328 | * @param url The URL to create this connection for.
|
---|
329 | */
|
---|
330 | protected HttpURLConnection(URL url)
|
---|
331 | {
|
---|
332 | super(url);
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Closes the connection to the server.
|
---|
337 | */
|
---|
338 | public abstract void disconnect();
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Returns a boolean indicating whether or not this connection is going
|
---|
342 | * through a proxy
|
---|
343 | *
|
---|
344 | * @return true if through a proxy, false otherwise
|
---|
345 | */
|
---|
346 | public abstract boolean usingProxy();
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Sets whether HTTP redirects (requests with response code 3xx) should be
|
---|
350 | * automatically followed by this class. True by default
|
---|
351 | *
|
---|
352 | * @param set true if redirects should be followed, false otherwis.
|
---|
353 | *
|
---|
354 | * @exception SecurityException If a security manager exists and its
|
---|
355 | * checkSetFactory method doesn't allow the operation
|
---|
356 | */
|
---|
357 | public static void setFollowRedirects(boolean set)
|
---|
358 | {
|
---|
359 | // Throw an exception if an extant security mgr precludes
|
---|
360 | // setting the factory.
|
---|
361 | SecurityManager s = System.getSecurityManager();
|
---|
362 | if (s != null)
|
---|
363 | s.checkSetFactory();
|
---|
364 |
|
---|
365 | followRedirects = set;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Returns a boolean indicating whether or not HTTP redirects will
|
---|
370 | * automatically be followed or not.
|
---|
371 | *
|
---|
372 | * @return true if redirects will be followed, false otherwise
|
---|
373 | */
|
---|
374 | public static boolean getFollowRedirects()
|
---|
375 | {
|
---|
376 | return followRedirects;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Returns the value of this HttpURLConnection's instanceFollowRedirects
|
---|
381 | * field
|
---|
382 | */
|
---|
383 | public boolean getInstanceFollowRedirects ()
|
---|
384 | {
|
---|
385 | return instanceFollowRedirects;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Sets the value of this HttpURLConnection's instanceFollowRedirects field
|
---|
390 | */
|
---|
391 | public void setInstanceFollowRedirects (boolean follow)
|
---|
392 | {
|
---|
393 | instanceFollowRedirects = follow;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Set the method for the URL request, one of:
|
---|
398 | * GET POST HEAD OPTIONS PUT DELETE TRACE are legal
|
---|
399 | *
|
---|
400 | * @exception ProtocolException If the method cannot be reset or if the
|
---|
401 | * requested method isn't valid for HTTP
|
---|
402 | */
|
---|
403 | public void setRequestMethod(String method) throws ProtocolException
|
---|
404 | {
|
---|
405 | if (connected)
|
---|
406 | throw new ProtocolException("Already connected");
|
---|
407 |
|
---|
408 | method = method.toUpperCase();
|
---|
409 | if (valid_methods.indexOf("|" + method + "|") != -1)
|
---|
410 | this.method = method;
|
---|
411 | else
|
---|
412 | throw new ProtocolException("Invalid HTTP request method: " + method);
|
---|
413 |
|
---|
414 | }
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * The request method currently in use for this connection.
|
---|
418 | *
|
---|
419 | * @return The request method
|
---|
420 | */
|
---|
421 | public String getRequestMethod()
|
---|
422 | {
|
---|
423 | return method;
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Gets the status code from an HTTP response message, or -1 if
|
---|
428 | * the response code could not be determined.
|
---|
429 | * Note that all valid response codes have class variables
|
---|
430 | * defined for them in this class.
|
---|
431 | *
|
---|
432 | * @return The response code
|
---|
433 | *
|
---|
434 | * @exception IOException If an error occurs
|
---|
435 | */
|
---|
436 | public int getResponseCode() throws IOException
|
---|
437 | {
|
---|
438 | if (!gotResponseVals)
|
---|
439 | getResponseVals();
|
---|
440 | return responseCode;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Gets the HTTP response message, if any, returned along with the
|
---|
445 | * response code from a server. Null if no response message was set
|
---|
446 | * or an error occured while connecting.
|
---|
447 | *
|
---|
448 | * @return The response message
|
---|
449 | *
|
---|
450 | * @exception IOException If an error occurs
|
---|
451 | */
|
---|
452 | public String getResponseMessage() throws IOException
|
---|
453 | {
|
---|
454 | if (!gotResponseVals)
|
---|
455 | getResponseVals();
|
---|
456 | return responseMessage;
|
---|
457 | }
|
---|
458 |
|
---|
459 | private void getResponseVals() throws IOException
|
---|
460 | {
|
---|
461 | // getHeaderField() will connect for us, but do it here first in
|
---|
462 | // order to pick up IOExceptions.
|
---|
463 | if (!connected)
|
---|
464 | connect();
|
---|
465 |
|
---|
466 | gotResponseVals = true;
|
---|
467 |
|
---|
468 | // If responseCode not yet explicitly set by subclass
|
---|
469 | if (responseCode == -1)
|
---|
470 | {
|
---|
471 | // Response is the first header received from the connection.
|
---|
472 | String respField = getHeaderField(0);
|
---|
473 |
|
---|
474 | if (respField == null || ! respField.startsWith("HTTP/"))
|
---|
475 | {
|
---|
476 | // Set to default values on failure.
|
---|
477 | responseCode = -1;
|
---|
478 | responseMessage = null;
|
---|
479 | return;
|
---|
480 | }
|
---|
481 |
|
---|
482 | int firstSpc, nextSpc;
|
---|
483 | firstSpc = respField.indexOf(' ');
|
---|
484 | nextSpc = respField.indexOf(' ', firstSpc + 1);
|
---|
485 | responseMessage = respField.substring(nextSpc + 1);
|
---|
486 | String codeStr = respField.substring(firstSpc + 1, nextSpc);
|
---|
487 | try
|
---|
488 | {
|
---|
489 | responseCode = Integer.parseInt(codeStr);
|
---|
490 | }
|
---|
491 | catch (NumberFormatException e)
|
---|
492 | {
|
---|
493 | // Set to default values on failure.
|
---|
494 | responseCode = -1;
|
---|
495 | responseMessage = null;
|
---|
496 | }
|
---|
497 | }
|
---|
498 | }
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Returns a permission object representing the permission necessary to make
|
---|
502 | * the connection represented by this object
|
---|
503 | *
|
---|
504 | * @exception IOException If an error occurs
|
---|
505 | */
|
---|
506 | public Permission getPermission() throws IOException
|
---|
507 | {
|
---|
508 | URL url = getURL();
|
---|
509 | String host = url.getHost();
|
---|
510 | int port = url.getPort();
|
---|
511 | if (port == -1)
|
---|
512 | port = 80;
|
---|
513 |
|
---|
514 | host = host + ":" + port;
|
---|
515 |
|
---|
516 | return new SocketPermission(host, "connect");
|
---|
517 | }
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * This method allows the caller to retrieve any data that might have
|
---|
521 | * been sent despite the fact that an error occurred. For example, the
|
---|
522 | * HTML page sent along with a 404 File Not Found error. If the socket
|
---|
523 | * is not connected, or if no error occurred or no data was returned,
|
---|
524 | * this method returns <code>null</code>.
|
---|
525 | *
|
---|
526 | * @return An <code>InputStream</code> for reading error data.
|
---|
527 | */
|
---|
528 | public InputStream getErrorStream ()
|
---|
529 | {
|
---|
530 | if (!connected)
|
---|
531 | return(null);
|
---|
532 |
|
---|
533 | int code;
|
---|
534 | try
|
---|
535 | {
|
---|
536 | code = getResponseCode();
|
---|
537 | }
|
---|
538 | catch(IOException e)
|
---|
539 | {
|
---|
540 | code = -1;
|
---|
541 | }
|
---|
542 |
|
---|
543 | if (code == -1)
|
---|
544 | return(null);
|
---|
545 |
|
---|
546 | if (((code/100) != 4) || ((code/100) != 5))
|
---|
547 | return(null);
|
---|
548 |
|
---|
549 | try
|
---|
550 | {
|
---|
551 | PushbackInputStream pbis = new PushbackInputStream(getInputStream());
|
---|
552 |
|
---|
553 | int i = pbis.read();
|
---|
554 | if (i == -1)
|
---|
555 | return(null);
|
---|
556 |
|
---|
557 | pbis.unread(i);
|
---|
558 | return(pbis);
|
---|
559 | }
|
---|
560 | catch(IOException e)
|
---|
561 | {
|
---|
562 | return(null);
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | /**
|
---|
567 | * Returns the value of the named field parsed as date
|
---|
568 | */
|
---|
569 | public long getHeaderFieldDate (String key, long value)
|
---|
570 | {
|
---|
571 | // FIXME: implement this correctly
|
---|
572 | // http://www.w3.org/Protocols/HTTP-NG/ng-notes.txt
|
---|
573 |
|
---|
574 | return super.getHeaderFieldDate (key, value);
|
---|
575 | }
|
---|
576 | }
|
---|