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

Commit 48b489e

Browse files
committed
Bug fixes
1 parent 006801f commit 48b489e

File tree

4 files changed

+96
-67
lines changed

4 files changed

+96
-67
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ out
55
# Project ignore
66
.docs
77
src/LICENSE.txt
8-
/src/main/java/com/kttdevelopment/simplehttpserver/_ignore/
8+
/src/main/java/_ignore/
99
# Gradle
1010
.gradle
1111
# Maven

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

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.kttdevelopment.simplehttpserver;
22

3+
import com.sun.net.httpserver.Headers;
34
import com.sun.net.httpserver.HttpExchange;
45

56
import java.util.*;
7+
import java.util.function.Predicate;
68

79
/**
810
* This class assigns {@link HttpSession} to every client.
@@ -13,7 +15,7 @@
1315
*/
1416
public class HttpSessionHandler {
1517

16-
private final Map<String,HttpSession> sessions = new HashMap<>();
18+
private final Map<String,HttpSession> sessions = Collections.synchronizedMap(new HashMap<>());
1719

1820
private final String cookie;
1921

@@ -55,6 +57,25 @@ public synchronized String assignSessionID(final HttpExchange exchange){
5557
return id;
5658
}
5759

60+
private final Predicate<Headers> hasSetHeader = new Predicate<>() {
61+
@Override
62+
public boolean test(final Headers headers){
63+
if(headers.containsKey("Set-Cookie"))
64+
for(final String value : headers.get("Set-Cookie"))
65+
if(value.startsWith(cookie + "="))
66+
return true;
67+
return false;
68+
}
69+
};
70+
71+
private String getSetSession(final Headers headers){
72+
if(headers.containsKey("Set-Cookie"))
73+
for(final String value : headers.get("Set-Cookie"))
74+
if(value.startsWith(cookie + "=")){
75+
return value.substring(value.indexOf(cookie + '=') + 1,value.indexOf(";"));
76+
return null;
77+
}
78+
5879
/**
5980
* Returns the session of the client or assigns one if it does not yet have one Session will only be saved client side if the exchange headers are sent using {@link HttpExchange#sendResponseHeaders(int, long)}.
6081
*
@@ -79,68 +100,75 @@ public final HttpSession getSession(final HttpExchange exchange){
79100
}
80101
}
81102

82-
if((sessionId = cookies.get(cookie)) == null || !sessions.containsKey(sessionId)){
83-
session = new HttpSession() {
84-
85-
private final String sessionID;
86-
private final long creationTime;
87-
private long lastAccessTime;
88-
89-
{
90-
sessionID = assignSessionID(exchange);
91-
creationTime = System.currentTimeMillis();
92-
lastAccessTime = creationTime;
93-
sessions.put(sessionID,this);
94-
}
95-
96-
@Override
97-
public final String getSessionID(){
98-
return sessionID;
99-
}
100-
101-
//
102-
103-
@Override
104-
public final long getCreationTime(){
105-
return creationTime;
106-
}
107-
108-
@Override
109-
public final long getLastAccessTime(){
110-
return lastAccessTime;
111-
}
112-
113-
@Override
114-
public synchronized final void updateLastAccessTime(){
115-
lastAccessTime = System.currentTimeMillis();
116-
}
117-
118-
//
119-
120-
@SuppressWarnings("StringBufferReplaceableByString")
121-
@Override
122-
public final String toString(){
123-
final StringBuilder OUT = new StringBuilder();
124-
OUT.append("HttpSession") .append('{');
125-
OUT.append("sessionID") .append('=') .append(sessionID) .append(", ");
126-
OUT.append("creationTime") .append('=') .append(creationTime) .append(", ");
127-
OUT.append("lastAccessTime").append('=') .append(lastAccessTime);
128-
OUT.append('}');
129-
return OUT.toString();
130-
}
131-
132-
};
133-
134-
final SimpleHttpCookie out =
135-
new SimpleHttpCookie.Builder(cookie,session.getSessionID())
136-
.setPath("/")
137-
.setHttpOnly(true)
138-
.build();
139-
exchange.getResponseHeaders().add("Set-Cookie",out.toCookieHeaderString());
140-
sessions.put(sessionId,session);
141-
}else{
142-
session = sessions.get(sessionId);
103+
final String cookieSessionId, setCookieSessionId;
104+
105+
synchronized(this){
106+
if(
107+
((cookieSessionId = cookies.get(cookie)) == null || !sessions.containsKey(sessionId) ) && (setCookieSessionId = getSetSession(exchange.getResponseHeaders())) == null
108+
){
109+
session = new HttpSession() {
110+
111+
private final String sessionID;
112+
private final long creationTime;
113+
private long lastAccessTime;
114+
115+
{
116+
sessionID = assignSessionID(exchange);
117+
creationTime = System.currentTimeMillis();
118+
lastAccessTime = creationTime;
119+
sessions.put(sessionID, this);
120+
}
121+
122+
@Override
123+
public final String getSessionID(){
124+
return sessionID;
125+
}
126+
127+
//
128+
129+
@Override
130+
public final long getCreationTime(){
131+
return creationTime;
132+
}
133+
134+
@Override
135+
public final long getLastAccessTime(){
136+
return lastAccessTime;
137+
}
138+
139+
@Override
140+
public synchronized final void updateLastAccessTime(){
141+
lastAccessTime = System.currentTimeMillis();
142+
}
143+
144+
//
145+
146+
@SuppressWarnings("StringBufferReplaceableByString")
147+
@Override
148+
public final String toString(){
149+
final StringBuilder OUT = new StringBuilder();
150+
OUT.append("HttpSession").append('{');
151+
OUT.append("sessionID").append('=').append(sessionID).append(", ");
152+
OUT.append("creationTime").append('=').append(creationTime).append(", ");
153+
OUT.append("lastAccessTime").append('=').append(lastAccessTime);
154+
OUT.append('}');
155+
return OUT.toString();
156+
}
157+
158+
};
159+
160+
final SimpleHttpCookie out =
161+
new SimpleHttpCookie.Builder(cookie, session.getSessionID())
162+
.setPath("/")
163+
.setHttpOnly(true)
164+
.build();
165+
exchange.getResponseHeaders().add("Set-Cookie", out.toCookieHeaderString());
166+
sessions.put(session.getSessionID(), session);
167+
}else{
168+
session = sessions.get(sessionId);
169+
}
143170
}
171+
System.out.println(sessions);
144172
return session;
145173
}
146174

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,16 @@ public final Map<HttpContext, HttpHandler> getContexts(){
289289

290290
@Override
291291
public synchronized final String getRandomContext(){
292-
return getRandomContext("/");
292+
return getRandomContext("");
293293
}
294294

295295
@Override
296296
public synchronized final String getRandomContext(final String context){
297297
String targetContext;
298298

299-
final String head = getContext(context);
299+
final String head = context.isEmpty() ? "" : getContext(context);
300300

301-
do targetContext = head + '/' + UUID.randomUUID().toString();
301+
do targetContext = head + getContext(UUID.randomUUID().toString());
302302
while(getContextHandler(targetContext) != null);
303303

304304
return targetContext;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public TemporaryHandler(final HttpHandler handler, final long maxTime){
5757

5858
@Override
5959
public final void handle(final HttpExchange exchange) throws IOException{
60-
if(!hasExpiry || expiry < System.currentTimeMillis())
60+
if(!hasExpiry || System.currentTimeMillis() < expiry)
6161
handler.handle(exchange);
6262
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
63+
exchange.close();
6364
}
6465

6566
@Override

0 commit comments

Comments
 (0)