Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/robaho/net/httpserver/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@

import java.io.*;

public class AuthFilter extends Filter {
public final class AuthFilter extends Filter {

private Authenticator authenticator;

public AuthFilter(Authenticator authenticator) {
this.authenticator = authenticator;
}

@Override
public String description() {
return "Authentication filter";
}
Expand All @@ -56,6 +57,7 @@ public void consumeInput(HttpExchange t) throws IOException {
/**
* The filter's implementation, which is invoked by the server
*/
@Override
public void doFilter(HttpExchange t, Filter.Chain chain) throws IOException {
if (authenticator != null) {
Authenticator.Result r = authenticator.authenticate(t);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/robaho/net/httpserver/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

package robaho.net.httpserver;

public class Code {
public final class Code {

public static final int HTTP_CONTINUE = 100;
public static final int HTTP_OK = 200;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/robaho/net/httpserver/ContextList.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

class ContextList {
final class ContextList {

private final List<HttpContextImpl> list = new CopyOnWriteArrayList<>();
private final Map<CacheKey,HttpContextImpl> cache = new ConcurrentHashMap<>();
private record CacheKey(String protocol,String path){}

public synchronized void add(HttpContextImpl ctx) {
synchronized void add(HttpContextImpl ctx) {
assert ctx.getPath() != null;
if (contains(ctx)) {
throw new IllegalArgumentException("cannot add context to list");
Expand All @@ -47,7 +47,7 @@ boolean contains(HttpContextImpl ctx) {
return findContext(ctx.getProtocol(), ctx.getPath(), true) != null;
}

public int size() {
int size() {
return list.size();
}

Expand Down Expand Up @@ -90,7 +90,7 @@ HttpContextImpl findContext(String protocol, String path, boolean exact) {
return lc;
}

public synchronized void remove(String protocol, String path)
synchronized void remove(String protocol, String path)
throws IllegalArgumentException {
HttpContextImpl ctx = findContext(protocol, path, true);
if (ctx == null) {
Expand All @@ -99,7 +99,7 @@ public synchronized void remove(String protocol, String path)
list.remove(ctx);
}

public synchronized void remove(HttpContextImpl context)
synchronized void remove(HttpContextImpl context)
throws IllegalArgumentException {
for (HttpContextImpl ctx : list) {
if (ctx.equals(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import com.sun.net.httpserver.*;
import com.sun.net.httpserver.spi.*;

public class DefaultHttpServerProvider extends HttpServerProvider {
public final class DefaultHttpServerProvider extends HttpServerProvider {

@Override
public HttpServer createHttpServer(InetSocketAddress addr, int backlog) throws IOException {
return new HttpServerImpl(addr, backlog);
}

@Override
public HttpsServer createHttpsServer(InetSocketAddress addr, int backlog) throws IOException {
return new HttpsServerImpl(addr, backlog);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/robaho/net/httpserver/DelegatingHttpExchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,64 +44,81 @@ public DelegatingHttpExchange(HttpExchange ex) {
this.exchange = ex;
}

@Override
public abstract Headers getRequestHeaders();

@Override
public abstract String getRequestMethod();

@Override
public abstract URI getRequestURI();

@Override
public Headers getResponseHeaders() {
return exchange.getResponseHeaders();
}

@Override
public HttpContext getHttpContext() {
return exchange.getHttpContext();
}

@Override
public void close() {
exchange.close();
}

@Override
public InputStream getRequestBody() {
return exchange.getRequestBody();
}

@Override
public int getResponseCode() {
return exchange.getResponseCode();
}

@Override
public OutputStream getResponseBody() {
return exchange.getResponseBody();
}

@Override
public void sendResponseHeaders(int rCode, long contentLen) throws IOException {
exchange.sendResponseHeaders(rCode, contentLen);
}

@Override
public InetSocketAddress getRemoteAddress() {
return exchange.getRemoteAddress();
}

@Override
public InetSocketAddress getLocalAddress() {
return exchange.getLocalAddress();
}

@Override
public String getProtocol() {
return exchange.getProtocol();
}

@Override
public Object getAttribute(String name) {
return exchange.getAttribute(name);
}

@Override
public void setAttribute(String name, Object value) {
exchange.setAttribute(name, value);
}

@Override
public void setStreams(InputStream i, OutputStream o) {
exchange.setStreams(i, o);
}

@Override
public HttpPrincipal getPrincipal() {
return exchange.getPrincipal();
}
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/robaho/net/httpserver/ExchangeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import robaho.net.httpserver.websockets.WebSocketHandler;

class ExchangeImpl {
final class ExchangeImpl {

Headers reqHdrs, rspHdrs;
Request req;
Expand Down Expand Up @@ -108,23 +108,23 @@ class ExchangeImpl {
this.ris = req.inputStream();
}

public Headers getRequestHeaders() {
Headers getRequestHeaders() {
return reqHdrs;
}

public Headers getResponseHeaders() {
Headers getResponseHeaders() {
return rspHdrs;
}

public URI getRequestURI() {
URI getRequestURI() {
return uri;
}

public String getRequestMethod() {
String getRequestMethod() {
return method;
}

public HttpContextImpl getHttpContext() {
HttpContextImpl getHttpContext() {
return connection.getHttpContext();
}

Expand All @@ -136,7 +136,7 @@ private boolean isConnectRequest() {
return CONNECT.equals(getRequestMethod());
}

public void close() {
void close() {
if (closed) {
return;
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public void close() {
}
}

public InputStream getRequestBody() {
InputStream getRequestBody() {
if (uis != null) {
return uis;
}
Expand All @@ -187,11 +187,11 @@ LeftOverInputStream getOriginalInputStream() {
return uis_orig;
}

public int getResponseCode() {
int getResponseCode() {
return rcode;
}

public OutputStream getResponseBody() {
OutputStream getResponseBody() {
/*
* TODO. Change spec to remove restriction below. Filters
* cannot work with this restriction
Expand Down Expand Up @@ -219,7 +219,7 @@ PlaceholderOutputStream getPlaceholderResponseBody() {

// hard-coded formatting for Date header, rather than using slower DateFormatter

public void sendResponseHeaders(int rCode, long contentLen)
void sendResponseHeaders(int rCode, long contentLen)
throws IOException {
final Logger logger = getServerImpl().getLogger();
if (sentHeaders) {
Expand Down Expand Up @@ -353,31 +353,31 @@ void writeHeaders(Headers map, OutputStream os) throws IOException {
outputAscii(CRNL,os);
}

public InetSocketAddress getRemoteAddress() {
InetSocketAddress getRemoteAddress() {
Socket s = connection.getSocket();
InetAddress ia = s.getInetAddress();
int port = s.getPort();
return new InetSocketAddress(ia, port);
}

public InetSocketAddress getLocalAddress() {
InetSocketAddress getLocalAddress() {
Socket s = connection.getSocket();
InetAddress ia = s.getLocalAddress();
int port = s.getLocalPort();
return new InetSocketAddress(ia, port);
}

public String getProtocol() {
String getProtocol() {
String reqline = req.requestLine();
int index = reqline.lastIndexOf(" ");
return reqline.substring(index + 1);
}

public SSLSession getSSLSession() {
SSLSession getSSLSession() {
return connection.getSSLSession();
}

public Object getAttribute(String name) {
Object getAttribute(String name) {
if (name == null) {
throw new NullPointerException("null name parameter");
}
Expand All @@ -387,7 +387,7 @@ public Object getAttribute(String name) {
return attributes.get(name);
}

public void setAttribute(String name, Object value) {
void setAttribute(String name, Object value) {
if (name == null) {
throw new NullPointerException("null name parameter");
}
Expand All @@ -401,7 +401,7 @@ public void setAttribute(String name, Object value) {
}
}

public void setStreams(InputStream i, OutputStream o) {
void setStreams(InputStream i, OutputStream o) {
assert uis != null;
if (i != null) {
uis = i;
Expand All @@ -422,7 +422,7 @@ ServerImpl getServerImpl() {
return getHttpContext().getServerImpl();
}

public HttpPrincipal getPrincipal() {
HttpPrincipal getPrincipal() {
return principal;
}

Expand All @@ -447,7 +447,7 @@ static ExchangeImpl get(HttpExchange t) {
* the wrapped stream has been provided, then an IOException will
* be thrown.
*/
class PlaceholderOutputStream extends java.io.OutputStream {
final class PlaceholderOutputStream extends java.io.OutputStream {

OutputStream wrapped;

Expand All @@ -469,26 +469,31 @@ private void checkWrap() throws IOException {
}
}

@Override
public void write(int b) throws IOException {
checkWrap();
wrapped.write(b);
}

@Override
public void write(byte b[]) throws IOException {
checkWrap();
wrapped.write(b);
}

@Override
public void write(byte b[], int off, int len) throws IOException {
checkWrap();
wrapped.write(b, off, len);
}

@Override
public void flush() throws IOException {
checkWrap();
wrapped.flush();
}

@Override
public void close() throws IOException {
checkWrap();
wrapped.close();
Expand Down
Loading