1 /*
2 * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
364 /**
365 * Constructor for the HttpURLConnection.
366 * @param u the URL
367 */
368 protected HttpURLConnection (URL u) {
369 super(u);
370 }
371
372 /**
373 * Sets whether HTTP redirects (requests with response code 3xx) should
374 * be automatically followed by this class. True by default. Applets
375 * cannot change this variable.
376 * <p>
377 * If there is a security manager, this method first calls
378 * the security manager's {@code checkSetFactory} method
379 * to ensure the operation is allowed.
380 * This could result in a SecurityException.
381 *
382 * @param set a {@code boolean} indicating whether or not
383 * to follow HTTP redirects.
384 * @exception SecurityException if a security manager exists and its
385 * {@code checkSetFactory} method doesn't
386 * allow the operation.
387 * @see SecurityManager#checkSetFactory
388 * @see #getFollowRedirects()
389 */
390 public static void setFollowRedirects(boolean set) {
391 SecurityManager sec = System.getSecurityManager();
392 if (sec != null) {
393 // seems to be the best check here...
394 sec.checkSetFactory();
395 }
396 followRedirects = set;
397 }
398
399 /**
400 * Returns a {@code boolean} indicating
401 * whether or not HTTP redirects (3xx) should
402 * be automatically followed.
403 *
404 * @return {@code true} if HTTP redirects should
439 * @since 1.3
440 */
441 public boolean getInstanceFollowRedirects() {
442 return instanceFollowRedirects;
443 }
444
445 /**
446 * Set the method for the URL request, one of:
447 * <UL>
448 * <LI>GET
449 * <LI>POST
450 * <LI>HEAD
451 * <LI>OPTIONS
452 * <LI>PUT
453 * <LI>DELETE
454 * <LI>TRACE
455 * </UL> are legal, subject to protocol restrictions. The default
456 * method is GET.
457 *
458 * @param method the HTTP method
459 * @exception ProtocolException if the method cannot be reset or if
460 * the requested method isn't valid for HTTP.
461 * @exception SecurityException if a security manager is set and the
462 * method is "TRACE", but the "allowHttpTrace"
463 * NetPermission is not granted.
464 * @see #getRequestMethod()
465 */
466 public void setRequestMethod(String method) throws ProtocolException {
467 if (connected) {
468 throw new ProtocolException("Can't reset method: already connected");
469 }
470 // This restriction will prevent people from using this class to
471 // experiment w/ new HTTP methods using java. But it should
472 // be placed for security - the request String could be
473 // arbitrarily long.
474
475 for (int i = 0; i < methods.length; i++) {
476 if (methods[i].equals(method)) {
477 if (method.equals("TRACE")) {
478 SecurityManager s = System.getSecurityManager();
479 if (s != null) {
480 s.checkPermission(new NetPermission("allowHttpTrace"));
481 }
610
611 /**
612 * Indicates that other requests to the server
613 * are unlikely in the near future. Calling disconnect()
614 * should not imply that this HttpURLConnection
615 * instance can be reused for other requests.
616 */
617 public abstract void disconnect();
618
619 /**
620 * Indicates if the connection is going through a proxy.
621 * @return a boolean indicating if the connection is
622 * using a proxy.
623 */
624 public abstract boolean usingProxy();
625
626 /**
627 * Returns a {@link SocketPermission} object representing the
628 * permission necessary to connect to the destination host and port.
629 *
630 * @exception IOException if an error occurs while computing
631 * the permission.
632 *
633 * @return a {@code SocketPermission} object representing the
634 * permission necessary to connect to the destination
635 * host and port.
636 */
637 public Permission getPermission() throws IOException {
638 int port = url.getPort();
639 port = port < 0 ? 80 : port;
640 String host = url.getHost() + ":" + port;
641 Permission permission = new SocketPermission(host, "connect");
642 return permission;
643 }
644
645 /**
646 * Returns the error stream if the connection failed
647 * but the server sent useful data nonetheless. The
648 * typical example is when an HTTP server responds
649 * with a 404, which will cause a FileNotFoundException
650 * to be thrown in connect, but the server sent an HTML
|
1 /*
2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
364 /**
365 * Constructor for the HttpURLConnection.
366 * @param u the URL
367 */
368 protected HttpURLConnection (URL u) {
369 super(u);
370 }
371
372 /**
373 * Sets whether HTTP redirects (requests with response code 3xx) should
374 * be automatically followed by this class. True by default. Applets
375 * cannot change this variable.
376 * <p>
377 * If there is a security manager, this method first calls
378 * the security manager's {@code checkSetFactory} method
379 * to ensure the operation is allowed.
380 * This could result in a SecurityException.
381 *
382 * @param set a {@code boolean} indicating whether or not
383 * to follow HTTP redirects.
384 * @throws SecurityException if a security manager exists and its
385 * {@code checkSetFactory} method doesn't
386 * allow the operation.
387 * @see SecurityManager#checkSetFactory
388 * @see #getFollowRedirects()
389 */
390 public static void setFollowRedirects(boolean set) {
391 SecurityManager sec = System.getSecurityManager();
392 if (sec != null) {
393 // seems to be the best check here...
394 sec.checkSetFactory();
395 }
396 followRedirects = set;
397 }
398
399 /**
400 * Returns a {@code boolean} indicating
401 * whether or not HTTP redirects (3xx) should
402 * be automatically followed.
403 *
404 * @return {@code true} if HTTP redirects should
439 * @since 1.3
440 */
441 public boolean getInstanceFollowRedirects() {
442 return instanceFollowRedirects;
443 }
444
445 /**
446 * Set the method for the URL request, one of:
447 * <UL>
448 * <LI>GET
449 * <LI>POST
450 * <LI>HEAD
451 * <LI>OPTIONS
452 * <LI>PUT
453 * <LI>DELETE
454 * <LI>TRACE
455 * </UL> are legal, subject to protocol restrictions. The default
456 * method is GET.
457 *
458 * @param method the HTTP method
459 * @throws ProtocolException if the method cannot be reset or if
460 * the requested method isn't valid for HTTP.
461 * @throws SecurityException if a security manager is set and the
462 * method is "TRACE", but the "allowHttpTrace"
463 * NetPermission is not granted.
464 * @see #getRequestMethod()
465 */
466 public void setRequestMethod(String method) throws ProtocolException {
467 if (connected) {
468 throw new ProtocolException("Can't reset method: already connected");
469 }
470 // This restriction will prevent people from using this class to
471 // experiment w/ new HTTP methods using java. But it should
472 // be placed for security - the request String could be
473 // arbitrarily long.
474
475 for (int i = 0; i < methods.length; i++) {
476 if (methods[i].equals(method)) {
477 if (method.equals("TRACE")) {
478 SecurityManager s = System.getSecurityManager();
479 if (s != null) {
480 s.checkPermission(new NetPermission("allowHttpTrace"));
481 }
610
611 /**
612 * Indicates that other requests to the server
613 * are unlikely in the near future. Calling disconnect()
614 * should not imply that this HttpURLConnection
615 * instance can be reused for other requests.
616 */
617 public abstract void disconnect();
618
619 /**
620 * Indicates if the connection is going through a proxy.
621 * @return a boolean indicating if the connection is
622 * using a proxy.
623 */
624 public abstract boolean usingProxy();
625
626 /**
627 * Returns a {@link SocketPermission} object representing the
628 * permission necessary to connect to the destination host and port.
629 *
630 * @throws IOException if an error occurs while computing
631 * the permission.
632 *
633 * @return a {@code SocketPermission} object representing the
634 * permission necessary to connect to the destination
635 * host and port.
636 */
637 public Permission getPermission() throws IOException {
638 int port = url.getPort();
639 port = port < 0 ? 80 : port;
640 String host = url.getHost() + ":" + port;
641 Permission permission = new SocketPermission(host, "connect");
642 return permission;
643 }
644
645 /**
646 * Returns the error stream if the connection failed
647 * but the server sent useful data nonetheless. The
648 * typical example is when an HTTP server responds
649 * with a 404, which will cause a FileNotFoundException
650 * to be thrown in connect, but the server sent an HTML
|