26
26
package jdk .internal .net .http ;
27
27
28
28
import java .io .IOException ;
29
- import java .net .InetSocketAddress ;
30
29
import java .net .ProtocolException ;
31
- import java .net .ProxySelector ;
32
- import java .net .URI ;
33
- import java .net .URISyntaxException ;
34
- import java .net .URLPermission ;
35
- import java .security .AccessControlContext ;
36
30
import java .time .Duration ;
37
- import java .util .List ;
38
- import java .util .Map ;
39
31
import java .util .Optional ;
40
32
import java .util .concurrent .CompletableFuture ;
41
33
import java .util .concurrent .Executor ;
42
34
import java .util .concurrent .TimeUnit ;
43
- import java .util .concurrent .TimeoutException ;
44
35
import java .util .concurrent .atomic .AtomicInteger ;
45
36
import java .util .function .Function ;
46
37
import java .net .http .HttpClient ;
47
- import java .net .http .HttpHeaders ;
48
38
import java .net .http .HttpResponse ;
49
39
import java .net .http .HttpTimeoutException ;
50
40
53
43
import jdk .internal .net .http .common .Utils ;
54
44
import jdk .internal .net .http .common .Log ;
55
45
56
- import static jdk .internal .net .http .common .Utils .permissionForProxy ;
57
-
58
46
/**
59
47
* One request/response exchange (handles 100/101 intermediate response also).
60
48
* depth field used to track number of times a new request is being sent
61
49
* for a given API request. If limit exceeded exception is thrown.
62
50
*
63
- * Security check is performed here:
64
- * - uses AccessControlContext captured at API level
65
- * - checks for appropriate URLPermission for request
66
- * - if permission allowed, grants equivalent SocketPermission to call
67
- * - in case of direct HTTP proxy, checks additionally for access to proxy
68
- * (CONNECT proxying uses its own Exchange, so check done there)
69
- *
70
51
*/
71
52
final class Exchange <T > {
72
53
@@ -83,8 +64,6 @@ final class Exchange<T> {
83
64
// used to record possible cancellation raised before the exchImpl
84
65
// has been established.
85
66
private volatile IOException failed ;
86
- @ SuppressWarnings ("removal" )
87
- final AccessControlContext acc ;
88
67
final MultiExchange <T > multi ;
89
68
final Executor parentExecutor ;
90
69
volatile boolean upgrading ; // to HTTP/2
@@ -103,22 +82,6 @@ final class Exchange<T> {
103
82
this .upgrading = false ;
104
83
this .client = multi .client ();
105
84
this .multi = multi ;
106
- this .acc = multi .acc ;
107
- this .parentExecutor = multi .executor ;
108
- this .pushGroup = multi .pushGroup ;
109
- this .dbgTag = "Exchange" ;
110
- }
111
-
112
- /* If different AccessControlContext to be used */
113
- Exchange (HttpRequestImpl request ,
114
- MultiExchange <T > multi ,
115
- @ SuppressWarnings ("removal" ) AccessControlContext acc )
116
- {
117
- this .request = request ;
118
- this .acc = acc ;
119
- this .upgrading = false ;
120
- this .client = multi .client ();
121
- this .multi = multi ;
122
85
this .parentExecutor = multi .executor ;
123
86
this .pushGroup = multi .pushGroup ;
124
87
this .dbgTag = "Exchange" ;
@@ -338,7 +301,7 @@ private void checkCancelled() {
338
301
}
339
302
}
340
303
341
- <T > CompletableFuture <T > checkCancelled (CompletableFuture <T > cf , HttpConnection connection ) {
304
+ <U > CompletableFuture <U > checkCancelled (CompletableFuture <U > cf , HttpConnection connection ) {
342
305
return cf .handle ((r ,t ) -> {
343
306
if (t == null ) {
344
307
if (multi .requestCancelled ()) {
@@ -354,7 +317,7 @@ <T> CompletableFuture<T> checkCancelled(CompletableFuture<T> cf, HttpConnection
354
317
} catch (Throwable x ) {
355
318
if (debug .on ()) debug .log ("Failed to close connection" , x );
356
319
}
357
- return MinimalFuture .<T >failedFuture (t );
320
+ return MinimalFuture .<U >failedFuture (t );
358
321
}
359
322
}
360
323
}
@@ -422,15 +385,6 @@ public CompletableFuture<Response> responseAsync() {
422
385
return responseAsyncImpl (null );
423
386
}
424
387
425
- CompletableFuture <Response > responseAsyncImpl (HttpConnection connection ) {
426
- SecurityException e = checkPermissions ();
427
- if (e != null ) {
428
- return MinimalFuture .failedFuture (e );
429
- } else {
430
- return responseAsyncImpl0 (connection );
431
- }
432
- }
433
-
434
388
// check whether the headersSentCF was completed exceptionally with
435
389
// ProxyAuthorizationRequired. If so the Response embedded in the
436
390
// exception is returned. Otherwise we proceed.
@@ -584,7 +538,7 @@ private CompletableFuture<Response> ignore1xxResponse(final Response rsp) {
584
538
}
585
539
}
586
540
587
- CompletableFuture <Response > responseAsyncImpl0 (HttpConnection connection ) {
541
+ CompletableFuture <Response > responseAsyncImpl (HttpConnection connection ) {
588
542
Function <ExchangeImpl <T >, CompletableFuture <Response >> after407Check ;
589
543
bodyIgnored = null ;
590
544
if (request .expectContinue ()) {
@@ -735,109 +689,6 @@ HttpResponse.BodySubscriber<T> ignoreBody(HttpResponse.ResponseInfo hdrs) {
735
689
return MinimalFuture .completedFuture (resp );
736
690
}
737
691
738
- private URI getURIForSecurityCheck () {
739
- URI u ;
740
- String method = request .method ();
741
- InetSocketAddress authority = request .authority ();
742
- URI uri = request .uri ();
743
-
744
- // CONNECT should be restricted at API level
745
- if (method .equalsIgnoreCase ("CONNECT" )) {
746
- try {
747
- u = new URI ("socket" ,
748
- null ,
749
- authority .getHostString (),
750
- authority .getPort (),
751
- null ,
752
- null ,
753
- null );
754
- } catch (URISyntaxException e ) {
755
- throw new InternalError (e ); // shouldn't happen
756
- }
757
- } else {
758
- u = uri ;
759
- }
760
- return u ;
761
- }
762
-
763
- /**
764
- * Returns the security permission required for the given details.
765
- * If method is CONNECT, then uri must be of form "scheme://host:port"
766
- */
767
- private static URLPermission permissionForServer (URI uri ,
768
- String method ,
769
- Map <String , List <String >> headers ) {
770
- if (method .equals ("CONNECT" )) {
771
- return new URLPermission (uri .toString (), "CONNECT" );
772
- } else {
773
- return Utils .permissionForServer (uri , method , headers .keySet ().stream ());
774
- }
775
- }
776
-
777
- /**
778
- * Performs the necessary security permission checks required to retrieve
779
- * the response. Returns a security exception representing the denied
780
- * permission, or null if all checks pass or there is no security manager.
781
- */
782
- private SecurityException checkPermissions () {
783
- String method = request .method ();
784
- @ SuppressWarnings ("removal" )
785
- SecurityManager sm = System .getSecurityManager ();
786
- if (sm == null || method .equals ("CONNECT" )) {
787
- // tunneling will have a null acc, which is fine. The proxy
788
- // permission check will have already been preformed.
789
- return null ;
790
- }
791
-
792
- HttpHeaders userHeaders = request .getUserHeaders ();
793
- URI u = getURIForSecurityCheck ();
794
- URLPermission p = permissionForServer (u , method , userHeaders .map ());
795
-
796
- try {
797
- assert acc != null ;
798
- sm .checkPermission (p , acc );
799
- } catch (SecurityException e ) {
800
- return e ;
801
- }
802
- String hostHeader = userHeaders .firstValue ("Host" ).orElse (null );
803
- if (hostHeader != null && !hostHeader .equalsIgnoreCase (u .getHost ())) {
804
- // user has set a Host header different to request URI
805
- // must check that for URLPermission also
806
- URI u1 = replaceHostInURI (u , hostHeader );
807
- URLPermission p1 = permissionForServer (u1 , method , userHeaders .map ());
808
- try {
809
- assert acc != null ;
810
- sm .checkPermission (p1 , acc );
811
- } catch (SecurityException e ) {
812
- return e ;
813
- }
814
- }
815
- ProxySelector ps = client .proxySelector ();
816
- if (ps != null ) {
817
- if (!method .equals ("CONNECT" )) {
818
- // a non-tunneling HTTP proxy. Need to check access
819
- URLPermission proxyPerm = permissionForProxy (request .proxy ());
820
- if (proxyPerm != null ) {
821
- try {
822
- sm .checkPermission (proxyPerm , acc );
823
- } catch (SecurityException e ) {
824
- return e ;
825
- }
826
- }
827
- }
828
- }
829
- return null ;
830
- }
831
-
832
- private static URI replaceHostInURI (URI u , String hostPort ) {
833
- StringBuilder sb = new StringBuilder ();
834
- sb .append (u .getScheme ())
835
- .append ("://" )
836
- .append (hostPort )
837
- .append (u .getRawPath ());
838
- return URI .create (sb .toString ());
839
- }
840
-
841
692
HttpClient .Version version () {
842
693
return multi .version ();
843
694
}
0 commit comments