Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit a4dde07

Browse files
authored
Merge branch 'master' into file-handler-update
2 parents 705237a + 670f94d commit a4dde07

19 files changed

+690
-197
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/HttpSessionHandler.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* This class assigns {@link HttpSession} to every client.
1010
*
1111
* @since 03.03.00
12-
* @version 03.03.00
12+
* @version 03.05.00
1313
* @author Ktt Development
1414
*/
1515
public class HttpSessionHandler {
@@ -130,16 +130,14 @@ public synchronized final void updateLastAccessTime(){
130130

131131
//
132132

133-
@SuppressWarnings("StringBufferReplaceableByString")
134133
@Override
135134
public String toString(){
136-
final StringBuilder OUT = new StringBuilder();
137-
OUT.append("HttpSession") .append('{');
138-
OUT.append("sessionID") .append('=') .append(sessionID) .append(", ");
139-
OUT.append("creationTime") .append('=') .append(creationTime) .append(", ");
140-
OUT.append("lastAccessTime") .append('=') .append(lastAccessTime);
141-
OUT.append('}');
142-
return OUT.toString();
135+
return
136+
"HttpSession" + '{' +
137+
"sessionID" + '=' + sessionID + ", " +
138+
"creationTime" + '=' + creationTime + ", " +
139+
"lastAccessTime" + '=' + lastAccessTime +
140+
'}';
143141
}
144142

145143
};
@@ -158,4 +156,13 @@ public String toString(){
158156
return session;
159157
}
160158

159+
@Override
160+
public String toString(){
161+
return
162+
"HttpSessionHandler" + '{' +
163+
"sessions" + '=' + sessions + ", " +
164+
"cookie" + '=' + cookie + ", " +
165+
'}';
166+
}
167+
161168
}

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @see SimpleHttpExchange
1010
* @see Builder
1111
* @since 02.00.00
12-
* @version 02.03.00
12+
* @version 03.05.00
1313
* @author Ktt Development
1414
*/
1515
public class SimpleHttpCookie {
@@ -93,23 +93,21 @@ public final String toCookieHeaderString(){
9393
return OUT.toString();
9494
}
9595

96-
@SuppressWarnings("StringBufferReplaceableByString")
97-
public String toString(){
98-
final StringBuilder OUT = new StringBuilder();
99-
100-
OUT.append("SimpleHttpCookie") .append('{');
101-
OUT.append("name") .append('=') .append(name) .append(", ");
102-
OUT.append("value") .append('=') .append(value) .append(", ");
103-
OUT.append("expires") .append('=') .append(expires) .append(", ");
104-
OUT.append("maxAge") .append('=') .append(maxAge) .append(", ");
105-
OUT.append("domain") .append('=') .append(domain) .append(", ");
106-
OUT.append("path") .append('=') .append(path) .append(", ");
107-
OUT.append("secure") .append('=') .append(secure) .append(", ");
108-
OUT.append("httpOnly") .append('=') .append(httpOnly) .append(", ");
109-
OUT.append("sameSite") .append('=') .append(sameSite);
110-
OUT.append('}');
96+
//
11197

112-
return OUT.toString();
98+
public String toString(){
99+
return
100+
"SimpleHttpCookie" + '{' +
101+
"name" + '=' + name + ", " +
102+
"value" + '=' + value + ", " +
103+
"expires" + '=' + expires + ", " +
104+
"maxAge" + '=' + maxAge + ", " +
105+
"domain" + '=' + domain + ", " +
106+
"path" + '=' + path + ", " +
107+
"secure" + '=' + secure + ", " +
108+
"httpOnly" + '=' + httpOnly + ", " +
109+
"sameSite" + '=' + sameSite +
110+
'}';
113111
}
114112

115113
/**

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpExchangeImpl.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @see SimpleHttpExchange
1919
* @since 02.00.00
20-
* @version 03.04.03
20+
* @version 03.05.00
2121
* @author Ktt Development
2222
*/
2323
@SuppressWarnings("SpellCheckingInspection")
@@ -415,31 +415,29 @@ public synchronized final void setAttribute(final String name, final Object valu
415415

416416
//
417417

418-
@SuppressWarnings("StringBufferReplaceableByString")
419418
@Override
420419
public String toString(){
421-
final StringBuilder OUT = new StringBuilder();
422-
OUT.append("SimpleHttpExchange").append('{');
423-
OUT.append("httpServer") .append('=') .append(httpServer) .append(", ");
424-
OUT.append("httpExchange") .append('=') .append(httpExchange) .append(", ");
425-
OUT.append("URI") .append('=') .append(URI) .append(", ");
426-
OUT.append("publicAddress") .append('=') .append(publicAddr) .append(", ");
427-
OUT.append("localAddress") .append('=') .append(localAddr) .append(", ");
428-
OUT.append("httpContext") .append('=') .append(httpContext) .append(", ");
429-
OUT.append("httpPrincipal") .append('=') .append(httpPrincipal) .append(", ");
430-
OUT.append("protocol") .append('=') .append(protocol) .append(", ");
431-
OUT.append("requestHeaders") .append('=') .append(requestHeaders) .append(", ");
432-
OUT.append("requestMethod") .append('=') .append(requestMethod) .append(", ");
433-
OUT.append("responseHeaders") .append('=') .append(getResponseHeaders()) .append(", ");
434-
OUT.append("responseCode") .append('=') .append(getResponseCode()) .append(", ");
435-
OUT.append("rawGet") .append('=') .append(rawGet) .append(", ");
436-
OUT.append("getMap") .append('=') .append(getMap) .append(", ");
437-
OUT.append("hasGet") .append('=') .append(hasGet) .append(", ");
438-
OUT.append("rawPost") .append('=') .append(rawPost) .append(", ");
439-
OUT.append("postMap") .append('=') .append(postMap) .append(", ");
440-
OUT.append("hasPost") .append('=') .append(hasPost) .append(", ");
441-
OUT.append("cookies") .append('=') .append(cookies);
442-
OUT.append('}');
443-
return OUT.toString();
420+
return
421+
"SimpleHttpExchange" + '{' +
422+
"httpServer" + '=' + httpServer + ", " +
423+
"httpExchange" + '=' + httpExchange + ", " +
424+
"URI" + '=' + URI + ", " +
425+
"publicAddress" + '=' + publicAddr + ", " +
426+
"localAddress" + '=' + localAddr + ", " +
427+
"httpContext" + '=' + httpContext + ", " +
428+
"httpPrincipal" + '=' + httpPrincipal + ", " +
429+
"protocol" + '=' + protocol + ", " +
430+
"requestHeaders" + '=' + requestHeaders + ", " +
431+
"requestMethod" + '=' + requestMethod + ", " +
432+
"responseHeaders" + '=' + getResponseHeaders() + ", " +
433+
"responseCode" + '=' + getResponseCode() + ", " +
434+
"rawGet" + '=' + rawGet + ", " +
435+
"getMap" + '=' + getMap + ", " +
436+
"hasGet" + '=' + hasGet + ", " +
437+
"rawPost" + '=' + rawPost + ", " +
438+
"postMap" + '=' + postMap + ", " +
439+
"hasPost" + '=' + hasPost + ", " +
440+
"cookies" + '=' + cookies +
441+
'}';
444442
}
445443
}

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @see SimpleHttpServer
1515
* @since 02.00.00
16-
* @version 03.04.03
16+
* @version 03.05.00
1717
* @author Ktt Development
1818
*/
1919
@SuppressWarnings("SpellCheckingInspection")
@@ -248,17 +248,15 @@ public synchronized final void stop(final int delay){
248248

249249
//
250250

251-
@SuppressWarnings("StringBufferReplaceableByString")
252251
@Override
253252
public String toString(){
254-
final StringBuilder OUT = new StringBuilder();
255-
OUT.append("SimpleHttpServer") .append('{');
256-
OUT.append("httpServer") .append('=') .append(server) .append(", ");
257-
OUT.append("contexts") .append('=') .append(contexts) .append(", ");
258-
OUT.append("address") .append('=') .append(getAddress()) .append(", ");
259-
OUT.append("executor") .append('=') .append(getExecutor());
260-
OUT.append('}');
261-
return OUT.toString();
253+
return
254+
"SimpleHttpServer" + '{' +
255+
"httpServer" + '=' + server + ", " +
256+
"contexts" + '=' + contexts + ", " +
257+
"address" + '=' + getAddress() + ", " +
258+
"executor" + '=' + getExecutor() +
259+
'}';
262260
}
263261

264262
// start slash; no end slash

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpsServerImpl.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @see SimpleHttpsServer
1515
* @since 03.04.00
16-
* @version 03.04.03
16+
* @version 03.05.00
1717
* @author Ktt Development
1818
*/
1919
final class SimpleHttpsServerImpl extends SimpleHttpsServer {
@@ -263,18 +263,16 @@ public synchronized final void stop(final int delay){
263263

264264
// endregion
265265

266-
@SuppressWarnings("StringBufferReplaceableByString")
267266
@Override
268267
public String toString(){
269-
final StringBuilder OUT = new StringBuilder();
270-
OUT.append("SimpleHttpsServer") .append('{');
271-
OUT.append("httpServer") .append('=') .append(server) .append(", ");
272-
OUT.append("httpsConfigurator") .append('=') .append(getHttpsConfigurator()) .append(", ");
273-
OUT.append("contexts") .append('=') .append(contexts) .append(", ");
274-
OUT.append("address") .append('=') .append(getAddress()) .append(", ");
275-
OUT.append("executor") .append('=') .append(getExecutor());
276-
OUT.append('}');
277-
return OUT.toString();
268+
return
269+
"SimpleHttpsServer" + '{' +
270+
"httpServer" + '=' + server + ", " +
271+
"httpsConfigurator" + '=' + getHttpsConfigurator() + ", " +
272+
"contexts" + '=' + contexts + ", " +
273+
"address" + '=' + getAddress() + ", " +
274+
"executor" + '=' + getExecutor() +
275+
'}';
278276
}
279277

280278
// start slash; no end slash

src/main/java/com/kttdevelopment/simplehttpserver/handler/ConnectionThrottler.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
/**
66
* Determines how connections are handled by the {@link ThrottledHandler}.
77
*
8-
* @see ServerThrottler
8+
* @see ThrottledHandler
9+
* @see ExchangeThrottler
10+
* @see ServerExchangeThrottler
911
* @see SessionThrottler
12+
* @see ServerSessionThrottler
1013
* @since 03.03.00
11-
* @version 03.03.00
14+
* @version 03.05.00
1215
* @author Ktt Development
1316
*/
1417
abstract class ConnectionThrottler {
@@ -19,7 +22,9 @@ abstract class ConnectionThrottler {
1922
* @param exchange exchange to process
2023
* @return if exchange was able to be added
2124
*
25+
* @see HttpExchange
2226
* @see #deleteConnection(HttpExchange)
27+
* @see #getMaxConnections(HttpExchange)
2328
* @since 03.03.00
2429
* @author Ktt Development
2530
*/
@@ -30,10 +35,26 @@ abstract class ConnectionThrottler {
3035
*
3136
* @param exchange exchange to process
3237
*
38+
* @see HttpExchange
3339
* @see #addConnection(HttpExchange)
40+
* @see #getMaxConnections(HttpExchange)
3441
* @since 03.03.00
3542
* @author Ktt Development
3643
*/
3744
abstract void deleteConnection(final HttpExchange exchange);
3845

46+
/**
47+
* Returns the maximum number of connections for an exchange. A value of <code>-1</code> means unlimited connections.
48+
*
49+
* @param exchange exchange to process
50+
* @return maximum number of connections allowed
51+
*
52+
* @see HttpExchange
53+
* @see #addConnection(HttpExchange)
54+
* @see #deleteConnection(HttpExchange)
55+
* @since 03.05.00
56+
* @author Ktt Development
57+
*/
58+
public abstract int getMaxConnections(final HttpExchange exchange);
59+
3960
}

src/main/java/com/kttdevelopment/simplehttpserver/handler/DirectoryEntry.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,16 @@ private static String getContext(final String path){
317317

318318
//
319319

320-
321320
@Override
322321
public String toString(){
323322
return
324-
"DirectoryEntry" + '{' +
325-
"directory" + '=' + directory + ", " +
326-
"adapter" + '=' + adapter + ", " +
327-
"loadingOption" + '=' + loadingOption + ", " +
328-
"isWalkthrough" + '=' + isWalkthrough + ", " +
329-
"files" + '=' + preloadedFiles +
330-
'}';
323+
"DirectoryEntry" + "{" +
324+
"isWalkthrough" + "=" + isWalkthrough + ", " +
325+
"isFilePreloaded" + "=" + isFilesPreloaded + ", " +
326+
"directory" + "=" + directory + ", " +
327+
"(preloaded) files" + "=" + files + ", " +
328+
"adapter" + "=" + adapter +
329+
"}";
331330
}
332331

333332
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.kttdevelopment.simplehttpserver.handler;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
5+
import java.net.InetAddress;
6+
import java.util.Map;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
import java.util.concurrent.atomic.AtomicInteger;
10+
11+
/**
12+
* Limits connections per address to the server.
13+
*
14+
* @see HttpExchange
15+
* @see ThrottledHandler
16+
* @see ServerExchangeThrottler
17+
* @see SessionThrottler
18+
* @see ServerSessionThrottler
19+
* @since 03.05.00
20+
* @version 03.05.00
21+
* @author Ktt Development
22+
*/
23+
public class ExchangeThrottler extends ConnectionThrottler {
24+
25+
private final Map<InetAddress,AtomicInteger> connections = new ConcurrentHashMap<>();
26+
27+
/**
28+
* Creates a throttler with limits on each exchange.
29+
*
30+
* @since 03.05.00
31+
* @author Ktt Development
32+
*/
33+
public ExchangeThrottler(){ }
34+
35+
@Override
36+
final boolean addConnection(final HttpExchange exchange){
37+
final InetAddress address = exchange.getRemoteAddress().getAddress();
38+
final int maxConn = getMaxConnections(exchange);
39+
40+
if(!connections.containsKey(address))
41+
connections.put(address,new AtomicInteger(0));
42+
43+
final AtomicInteger conn = connections.get(address);
44+
45+
if(maxConn < 0){
46+
conn.incrementAndGet();
47+
return true;
48+
}else{
49+
final AtomicBoolean added = new AtomicBoolean(false);
50+
conn.updateAndGet(operand -> {
51+
if(operand < maxConn) added.set(true);
52+
return operand < maxConn ? operand + 1 : operand;
53+
});
54+
return added.get();
55+
}
56+
}
57+
58+
@Override
59+
final void deleteConnection(final HttpExchange exchange){
60+
final InetAddress address = exchange.getRemoteAddress().getAddress();
61+
if(connections.containsKey(address))
62+
connections.get(address).decrementAndGet();
63+
}
64+
65+
@Override
66+
public int getMaxConnections(final HttpExchange exchange){
67+
return -1;
68+
}
69+
70+
@Override
71+
public String toString(){
72+
return
73+
"Exchange Throttler" + '{' +
74+
"connections" + '=' + connections.toString() +
75+
'}';
76+
}
77+
78+
}

src/main/java/com/kttdevelopment/simplehttpserver/handler/FileEntry.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ public final ByteLoadingOption getLoadingOption(){
158158

159159
//
160160

161-
162161
@Override
163162
public String toString(){
164163
return

0 commit comments

Comments
 (0)