Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8f6e54

Browse files
committed
support Principal auth on http2 streams
1 parent b841d5b commit e8f6e54

File tree

9 files changed

+42
-57
lines changed

9 files changed

+42
-57
lines changed

src/main/java/robaho/net/httpserver/AuthFilter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.sun.net.httpserver.*;
2929

3030
import java.io.*;
31+
import java.security.Principal;
3132

3233
public class AuthFilter extends Filter {
3334

@@ -61,7 +62,7 @@ public void doFilter(HttpExchange t, Filter.Chain chain) throws IOException {
6162
Authenticator.Result r = authenticator.authenticate(t);
6263
if (r instanceof Authenticator.Success) {
6364
Authenticator.Success s = (Authenticator.Success) r;
64-
ExchangeImpl e = ExchangeImpl.get(t);
65+
PrincipalExchange e = (PrincipalExchange)t;
6566
e.setPrincipal(s.getPrincipal());
6667
chain.doFilter(t);
6768
} else if (r instanceof Authenticator.Retry) {
@@ -77,4 +78,9 @@ public void doFilter(HttpExchange t, Filter.Chain chain) throws IOException {
7778
chain.doFilter(t);
7879
}
7980
}
81+
82+
static interface PrincipalExchange {
83+
public void setPrincipal(HttpPrincipal p);
84+
public Principal getPrincipal();
85+
}
8086
}

src/main/java/robaho/net/httpserver/ExchangeImpl.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,6 @@ public HttpPrincipal getPrincipal() {
433433
void setPrincipal(HttpPrincipal principal) {
434434
this.principal = principal;
435435
}
436-
437-
static ExchangeImpl get(HttpExchange t) {
438-
if (t instanceof HttpExchangeImpl) {
439-
return ((HttpExchangeImpl) t).getExchangeImpl();
440-
} else {
441-
assert t instanceof HttpsExchangeImpl;
442-
return ((HttpsExchangeImpl) t).getExchangeImpl();
443-
}
444-
}
445436
}
446437

447438
/**

src/main/java/robaho/net/httpserver/Http2ExchangeImpl.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
import java.net.InetSocketAddress;
77
import java.net.URI;
88

9+
import javax.net.ssl.SSLSession;
10+
911
import com.sun.net.httpserver.Headers;
1012
import com.sun.net.httpserver.HttpContext;
11-
import com.sun.net.httpserver.HttpExchange;
1213
import com.sun.net.httpserver.HttpPrincipal;
14+
import com.sun.net.httpserver.HttpsExchange;
1315

1416
import robaho.net.httpserver.http2.HTTP2Stream;
1517

16-
public class Http2ExchangeImpl extends HttpExchange {
18+
public class Http2ExchangeImpl extends HttpsExchange implements AuthFilter.PrincipalExchange {
1719
private final Headers request;
1820
private final Headers response;
1921
private final InputStream in;
2022
private final OutputStream out;
2123
private final URI uri;
2224
private final String method;
2325
private final HttpContext ctx;
24-
private final HTTP2Stream stream;
26+
protected final HTTP2Stream stream;
2527
private HttpPrincipal principal;
2628
private int responseCode;
2729

@@ -130,4 +132,14 @@ public void setStreams(InputStream i, OutputStream o) {
130132
public HttpPrincipal getPrincipal() {
131133
return principal;
132134
}
135+
136+
@Override
137+
public SSLSession getSSLSession() {
138+
return stream.getSSLSession();
139+
}
140+
141+
@Override
142+
public void setPrincipal(HttpPrincipal p) {
143+
principal = p;
144+
}
133145
}

src/main/java/robaho/net/httpserver/HttpConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void debug() {
7878
logger.log(Level.INFO,toString()+", inRequest "+inRequest+", request count "+requestCount.get());
7979
}
8080

81-
SSLSession getSSLSession() {
81+
public SSLSession getSSLSession() {
8282
return (socket instanceof SSLSocket ssl) ? ssl.getHandshakeSession() : null;
8383
}
8484

src/main/java/robaho/net/httpserver/HttpExchangeImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727

2828
import java.io.*;
2929
import java.net.*;
30+
3031
import com.sun.net.httpserver.*;
3132

32-
class HttpExchangeImpl extends HttpExchange {
33+
class HttpExchangeImpl extends HttpExchange implements AuthFilter.PrincipalExchange {
3334

3435
ExchangeImpl impl;
3536

@@ -105,8 +106,8 @@ public void setStreams(InputStream i, OutputStream o) {
105106
public HttpPrincipal getPrincipal() {
106107
return impl.getPrincipal();
107108
}
108-
109-
ExchangeImpl getExchangeImpl() {
110-
return impl;
109+
@Override
110+
public void setPrincipal(HttpPrincipal p) {
111+
impl.setPrincipal(p);
111112
}
112113
}

src/main/java/robaho/net/httpserver/HttpsExchangeImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import com.sun.net.httpserver.HttpPrincipal;
3838
import com.sun.net.httpserver.HttpsExchange;
3939

40-
class HttpsExchangeImpl extends HttpsExchange {
40+
class HttpsExchangeImpl extends HttpsExchange implements AuthFilter.PrincipalExchange {
4141

4242
ExchangeImpl impl;
4343

@@ -118,7 +118,8 @@ public HttpPrincipal getPrincipal() {
118118
return impl.getPrincipal();
119119
}
120120

121-
ExchangeImpl getExchangeImpl() {
122-
return impl;
121+
@Override
122+
public void setPrincipal(HttpPrincipal p) {
123+
impl.setPrincipal(p);
123124
}
124125
}

src/main/java/robaho/net/httpserver/http2/HTTP2Stream.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.concurrent.atomic.AtomicLong;
1414
import java.util.concurrent.locks.LockSupport;
1515

16+
import javax.net.ssl.SSLSession;
17+
1618
import com.sun.net.httpserver.Headers;
1719

1820
import robaho.net.httpserver.NoSyncBufferedOutputStream;
@@ -106,10 +108,15 @@ public void debug() {
106108
public boolean isOpen() {
107109
return streamOpen;
108110
}
111+
109112
public boolean isHalfClosed() {
110113
return halfClosed;
111114
}
112115

116+
public SSLSession getSSLSession() {
117+
return connection.httpConnection.getSSLSession();
118+
}
119+
113120
private long expectedSize() {
114121
if(requestHeaders.containsKey("Content-length")) {
115122
return Long.parseLong(requestHeaders.getFirst("Content-length"));

src/test/java_default/HttpExchange/AutoCloseableHttpExchange.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.concurrent.atomic.AtomicBoolean;
3333

3434
import jdk.test.lib.net.URIBuilder;
35-
import robaho.net.httpserver.HttpExchangeAccess;
3635

3736
/**
3837
* @test
@@ -67,8 +66,10 @@ public void handle(HttpExchange t) throws IOException {
6766
;
6867
t.sendResponseHeaders(200, -1);
6968
}
70-
if (!HttpExchangeAccess.isClosed(t)) {
69+
try {
70+
t.getResponseBody().write("somedata".getBytes());
7171
exchangeCloseFail.set(true);
72+
} catch (IOException expected) {
7273
}
7374
latch.countDown();
7475
}

src/test/java_default/HttpExchange/robaho/net/httpserver/HttpExchangeAccess.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)