Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 6b1fc5b

Browse files
committed
code optimizations
1 parent dc2a42f commit 6b1fc5b

22 files changed

+255
-294
lines changed

src/main/java/dev/katsute/simplehttpserver/ContextUtility.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
abstract class ContextUtility {
2424

25+
private ContextUtility(){ }
26+
2527
// replace consecutive slashes and back slashes with a single forward slash
2628
@SuppressWarnings("RegExpRedundantEscape")
2729
private static final Pattern forwardSlash = Pattern.compile("\\/{2,}|\\\\+");

src/main/java/dev/katsute/simplehttpserver/FileRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final String getContentType(){
4343
}
4444

4545
public final byte[] getBytes(){
46-
return Arrays.copyOf(bytes, bytes.length);
46+
return Arrays.copyOf(bytes, bytes.length); // dereference
4747
}
4848

4949
}

src/main/java/dev/katsute/simplehttpserver/HttpSessionHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ public HttpSessionHandler(final String cookie){
4040

4141
public synchronized String assignSessionID(final HttpExchange exchange){
4242
String id;
43-
do id = UUID.randomUUID().toString();
43+
do id = UUID.randomUUID().toString(); // assign session ID
4444
while(sessions.containsKey(id));
4545
return id;
4646
}
4747

48-
private String getSetSession(final Headers headers){
48+
private String getSetSession(final Headers headers){ // get session that will be set by cookie
4949
if(headers.containsKey("Set-Cookie"))
5050
for(final String value : headers.get("Set-Cookie"))
5151
if(value.startsWith(cookie + "="))
@@ -54,7 +54,7 @@ private String getSetSession(final Headers headers){
5454
}
5555

5656
public final HttpSession getSession(final HttpExchange exchange){
57-
final String sessionId;
57+
final String sessionID;
5858
final HttpSession session;
5959

6060
@SuppressWarnings("SpellCheckingInspection")
@@ -70,10 +70,10 @@ public final HttpSession getSession(final HttpExchange exchange){
7070
}
7171

7272
final String setSession = getSetSession(exchange.getResponseHeaders());
73-
sessionId = setSession != null ? setSession : cookies.get(cookie);
73+
sessionID = setSession != null ? setSession : cookies.get(cookie); // use session that will be written or session from cookie
7474

7575
synchronized(this){
76-
if(!sessions.containsKey(sessionId)){
76+
if(!sessions.containsKey(sessionID)){
7777
session = new HttpSession() {
7878
private final String sessionID;
7979
private final long creationTime;
@@ -117,7 +117,7 @@ public synchronized final void update(){
117117
exchange.getResponseHeaders().add("Set-Cookie", OUT.toString());
118118
sessions.put(session.getSessionID(), session);
119119
}else{
120-
session = sessions.get(sessionId);
120+
session = sessions.get(sessionID);
121121
}
122122
}
123123
return session;

src/main/java/dev/katsute/simplehttpserver/SimpleHttpExchange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract class SimpleHttpExchange extends HttpExchange {
3131
SimpleHttpExchange(){ }
3232

3333
static SimpleHttpExchange create(final HttpExchange exchange){
34-
return null;
34+
return new SimpleHttpExchangeImpl(exchange);
3535
}
3636

3737
//

src/main/java/dev/katsute/simplehttpserver/SimpleHttpServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public abstract class SimpleHttpServer extends HttpServer implements HttpServerE
2727
SimpleHttpServer(){ }
2828

2929
public static SimpleHttpServer create() throws IOException {
30-
return SimpleHttpServerImpl.createHttpServer(null, null);
30+
return new SimpleHttpServerImpl(null, null);
3131
}
3232

3333
public static SimpleHttpServer create(final int port) throws IOException {
34-
return SimpleHttpServerImpl.createHttpServer(port, null);
34+
return new SimpleHttpServerImpl(port, null);
3535
}
3636

3737
public static SimpleHttpServer create(final int port, final int backlog) throws IOException {
38-
return SimpleHttpServerImpl.createHttpServer(port, backlog);
38+
return new SimpleHttpServerImpl(port, backlog);
3939
}
4040

4141
//

src/main/java/dev/katsute/simplehttpserver/SimpleHttpServerImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ final class SimpleHttpServerImpl extends SimpleHttpServer {
3434

3535
private final Map<HttpContext, HttpHandler> contexts = Collections.synchronizedMap(new HashMap<>());
3636

37-
static SimpleHttpServer createHttpServer(final Integer port, final Integer backlog) throws IOException{
38-
return new SimpleHttpServerImpl(port, backlog);
39-
}
40-
4137
SimpleHttpServerImpl(final Integer port, final Integer backlog) throws IOException{
4238
if(port != null)
4339
server.bind(new InetSocketAddress(port), backlog != null ? backlog : 0);

src/main/java/dev/katsute/simplehttpserver/SimpleHttpsServer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ public abstract class SimpleHttpsServer extends HttpsServer implements HttpServe
2727
SimpleHttpsServer(){ }
2828

2929
public static SimpleHttpsServer create() throws IOException {
30-
return SimpleHttpsServerImpl.createHttpsServer(null, null);
30+
return new SimpleHttpsServerImpl(null, null);
3131
}
3232

3333
public static SimpleHttpsServer create(final int port) throws IOException {
34-
return SimpleHttpsServerImpl.createHttpsServer(port, null);
34+
return new SimpleHttpsServerImpl(port, null);
3535
}
3636

3737
public static SimpleHttpsServer create(final int port, final int backlog) throws IOException {
38-
return SimpleHttpsServerImpl.createHttpsServer(port, backlog);
38+
return new SimpleHttpsServerImpl(port, backlog);
3939
}
4040

41+
//
42+
4143
public abstract HttpsServer getHttpsServer();
4244

4345
}

src/main/java/dev/katsute/simplehttpserver/SimpleHttpsServerImpl.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ final class SimpleHttpsServerImpl extends SimpleHttpsServer {
3232

3333
private HttpSessionHandler sessionHandler;
3434

35-
private final Map<HttpContext, HttpHandler> contexts = Collections.synchronizedMap(new HashMap<>());
36-
37-
static SimpleHttpsServer createHttpsServer(final Integer port, final Integer backlog) throws IOException{
38-
return new SimpleHttpsServerImpl(port, backlog);
39-
}
35+
private final Map<HttpContext,HttpHandler> contexts = Collections.synchronizedMap(new HashMap<>());
4036

4137
SimpleHttpsServerImpl(final Integer port, final Integer backlog) throws IOException{
4238
if(port != null)

src/main/java/dev/katsute/simplehttpserver/handler/RootHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222

2323
public class RootHandler extends PredicateHandler {
2424

25-
public RootHandler(final HttpHandler handlerIfTrue, final HttpHandler handlerIfFalse){
25+
public RootHandler(final HttpHandler index, final HttpHandler other){
2626
super(
2727
exchange -> exchange.getRequestURI().getPath().equals("/"),
28-
handlerIfTrue,
29-
handlerIfFalse
28+
index,
29+
other
3030
);
3131
}
3232

33-
3433
}

src/main/java/dev/katsute/simplehttpserver/handler/SSEHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
6666
}catch(final NumberFormatException | NullPointerException ignored){ }
6767

6868
exchange.send(HttpURLConnection.HTTP_OK);
69-
for(int index = latest; index < queue.size(); index++){
69+
for(int index = latest; index < queue.size(); index++){ // write latest events
7070
exchange.getResponseBody().write(queue.get(index).toString(eventID.get()).getBytes(StandardCharsets.UTF_8));
7171
exchange.getResponseBody().flush();
7272
}
7373

74-
listeners.add(exchange.getResponseBody());
74+
listeners.add(exchange.getResponseBody()); // track events
7575
}
7676

7777
public synchronized final void push(final String data){
@@ -82,11 +82,13 @@ public synchronized final void push(final String data, final int retry, final St
8282
eventID.addAndGet(1);
8383
final EventStreamRecord record = new EventStreamRecord(retry, event, data);
8484
queue.add(record);
85-
for(final OutputStream OUT : listeners){
85+
for(final OutputStream OUT : listeners){ // push events to all clients
8686
try{
8787
OUT.write(record.toString(eventID.get()).getBytes(StandardCharsets.UTF_8));
8888
OUT.flush();
89-
}catch(final IOException ignored){ }
89+
}catch(final IOException ignored){ // internal error or closed
90+
listeners.remove(OUT); // remove from tracking
91+
}
9092
}
9193
}
9294

0 commit comments

Comments
 (0)