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

Commit 7cec061

Browse files
committed
Merge branch 'http-handler/temporary-handler' of https://github.com/Ktt-Development/simplehttpserver into http-handler/temporary-handler
2 parents 1426e12 + 063ecce commit 7cec061

File tree

7 files changed

+22
-136
lines changed

7 files changed

+22
-136
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,11 @@ public abstract class HttpSession {
1616
/**
1717
* Creates an empty {@link HttpSession}. Applications don't use this method.
1818
*
19-
* @see HttpSessionImpl#createHttpSession()
2019
* @since 02.00.00
2120
* @author Ktt Development
2221
*/
2322
HttpSession(){ }
2423

25-
//
26-
27-
/**
28-
* Creates a {@link HttpSession}.
29-
*
30-
* @return a {@link HttpSession}
31-
*
32-
* @since 02.00.00
33-
* @author Ktt Development
34-
*/
35-
synchronized static HttpSession create(){
36-
return HttpSessionImpl.createHttpSession();
37-
}
38-
3924
//
4025

4126
/**

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public synchronized String assignSessionID(final HttpExchange exchange){
5656
}
5757

5858
/**
59-
* Assigns a session to the client. Session will only be saved client side if the exchange headers are sent using {@link HttpExchange#sendResponseHeaders(int, long)}.
59+
* 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)}.
6060
*
6161
* @param exchange http exchange
6262
*
6363
* @since 03.03.00
6464
* @author Ktt Development
6565
*/
66-
public synchronized final void assignSession(final HttpExchange exchange){
66+
public final HttpSession getSession(final HttpExchange exchange){
6767
final String sessionId;
6868
final HttpSession session;
6969

@@ -136,7 +136,10 @@ public final String toString(){
136136
.setHttpOnly(true)
137137
.build();
138138
exchange.getResponseHeaders().add("Set-Cookie",out.toCookieHeaderString());
139+
}else{
140+
session = sessions.get(sessionId);
139141
}
142+
return session;
140143
}
141144

142145
}

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,6 @@ static SimpleHttpExchange create(final HttpExchange exchange){
346346
*/
347347
public abstract void setCookie(final SimpleHttpCookie cookie);
348348

349-
//
350-
351-
/**
352-
* Returns the http session of the exchange if one exists; if one does not exist it will create one. <b>A new session will only persist if the exchange is sent.</b>
353-
*
354-
* @return http session
355-
*
356-
* @see HttpSession
357-
* @since 02.00.00
358-
* @author Ktt Development
359-
*/
360-
public abstract HttpSession getHttpSession();
361-
362349
//
363350

364351
/**

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -317,32 +317,11 @@ public synchronized final void setCookie(final String key, final String value){
317317
@Override
318318
public synchronized final void setCookie(final SimpleHttpCookie cookie){
319319
final String cstring = cookie.toCookieHeaderString();
320-
if(cstring.startsWith("__session-id="))
321-
throw new IllegalArgumentException("The cookie '__session-id' can not be set because it is reserved by the server");
322320
getResponseHeaders().add("Set-Cookie",cstring);
323321
}
324322

325323
//
326-
327-
@Override
328-
public synchronized final HttpSession getHttpSession(){
329-
final String sessionId;
330-
final HttpSession session;
331-
332-
if((sessionId = cookies.get("__session-id")) == null || !HttpSession.sessions.containsKey(sessionId)){
333-
session = HttpSession.create();
334-
final SimpleHttpCookie cookie =
335-
new SimpleHttpCookie.Builder("__session-id",session.getSessionID())
336-
.setPath("/")
337-
.setHttpOnly(true)
338-
.build();
339-
getResponseHeaders().add("Set-Cookie",cookie.toCookieHeaderString()); // bypass implementation
340-
}else{
341-
session = HttpSession.sessions.get(sessionId);
342-
}
343-
return session;
344-
}
345-
324+
346325
@Override
347326
public final OutputStream getOutputStream(){
348327
return outputStream;

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@
1414
* @version 03.03.00
1515
* @author Ktt Development
1616
*/
17-
public interface SimpleHttpHandler {
18-
/*
17+
18+
public interface SimpleHttpHandler extends HttpHandler {
19+
1920
/**
20-
* Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. Applications do not use this.
21+
* Encapsulates the {@link #handle(SimpleHttpExchange)} for the authenticator. This method is reserved by the server; <b>do not override this</b>, it will break the {@link #handle(SimpleHttpExchange)} method.
2122
*
2223
* @param exchange client information
2324
* @throws IOException internal failure
2425
*
2526
* @since 02.00.00
2627
* @author Ktt Development
27-
/
28+
*/
2829
@Override
29-
default final void handle(final HttpExchange exchange) throws IOException{
30-
final SimpleHttpExchange sxe = SimpleHttpExchange.create(exchange);
31-
if(authenticate(sxe))
32-
handle(sxe);
33-
else
34-
sxe.close();
30+
default void handle(final HttpExchange exchange) throws IOException{
31+
handle(SimpleHttpExchange.create(exchange));
3532
}
36-
*/
33+
3734
/**
3835
* Handlers the given request and generates a response <b>if no exceptions occur</b>.
3936
*

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

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
215215

216216
public abstract HttpSessionHandler getHttpSessionHandler();
217217

218+
public abstract HttpSession getHttpSession(final HttpExchange exchange);
219+
218220
//
219221

220222
/**
@@ -227,7 +229,6 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
227229
*
228230
* @see HttpContext
229231
* @see #createContext(String, HttpHandler)
230-
* @see #createContext(String, SimpleHttpHandler)
231232
* @see #removeContext(String)
232233
* @see #removeContext(HttpContext)
233234
* @since 02.00.00
@@ -247,34 +248,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
247248
* @see HttpContext
248249
* @see HttpHandler
249250
* @see #createContext(String)
250-
* @see #createContext(String, SimpleHttpHandler)
251251
* @see #removeContext(String)
252252
* @see #removeContext(HttpContext)
253253
* @since 02.00.00
254254
* @author Ktt Development
255255
*/
256256
public abstract HttpContext createContext(final String context, final HttpHandler handler);
257257

258-
/**
259-
* Creates a context mapped to a specific {@link HttpHandler}.
260-
*
261-
* @param context the context
262-
* @param handler the handler
263-
* @return the http context associated with the context
264-
* @throws IllegalArgumentException if the context is invalid or taken
265-
* @throws NullPointerException if the context is null
266-
*
267-
* @see HttpContext
268-
* @see SimpleHttpHandler
269-
* @see #createContext(String)
270-
* @see #createContext(String, HttpHandler)
271-
* @see #removeContext(String)
272-
* @see #removeContext(HttpContext)
273-
* @since 03.03.00
274-
* @author Ktt Development
275-
*/
276-
public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler);
277-
278258
//
279259

280260
/**
@@ -289,7 +269,6 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
289269
* @see HttpContext
290270
* @see Authenticator
291271
* @see #createContext(String, HttpHandler, Authenticator)
292-
* @see #createContext(String, SimpleHttpHandler, Authenticator)
293272
* @see #removeContext(String)
294273
* @see #removeContext(HttpContext)
295274
* @since 03.03.00
@@ -311,36 +290,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
311290
* @see HttpHandler
312291
* @see Authenticator
313292
* @see #createContext(String, Authenticator)
314-
* @see #createContext(String, SimpleHttpHandler, Authenticator)
315293
* @see #removeContext(String)
316294
* @see #removeContext(HttpContext)
317295
* @since 03.03.00
318296
* @author Ktt Development
319297
*/
320298
public abstract HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator);
321299

322-
/**
323-
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
324-
*
325-
* @param context the context
326-
* @param handler the handler
327-
* @param authenticator authenticator
328-
* @return the http context associated with the context
329-
* @throws IllegalArgumentException if the context is invalid or taken
330-
* @throws NullPointerException if the context is null
331-
*
332-
* @see HttpContext
333-
* @see SimpleHttpHandler
334-
* @see Authenticator
335-
* @see #createContext(String, Authenticator)
336-
* @see #createContext(String, HttpHandler, Authenticator)
337-
* @see #removeContext(String)
338-
* @see #removeContext(HttpContext)
339-
* @since 03.03.00
340-
* @author Ktt Development
341-
*/
342-
public abstract HttpContext createContext(final String context, final SimpleHttpHandler handler, final Authenticator authenticator);
343-
344300
//
345301

346302
/**

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static SimpleHttpServer createSimpleHttpServer(final Integer port, final Integer
5050

5151
private void handle(final HttpExchange exchange){
5252
if(sessionHandler != null)
53-
sessionHandler.assignSession(exchange);
53+
sessionHandler.getSession(exchange).updateLastAccessTime();
5454
}
5555

5656
//
@@ -115,9 +115,12 @@ public final HttpSessionHandler getHttpSessionHandler(){
115115
return sessionHandler;
116116
}
117117

118-
//
118+
@Override
119+
public HttpSession getHttpSession(final HttpExchange exchange){
120+
return sessionHandler.getSession(exchange);
121+
}
119122

120-
//
123+
//
121124

122125
@Override
123126
public synchronized final HttpContext createContext(final String path){
@@ -140,22 +143,6 @@ public synchronized final HttpContext createContext(final String path, final Htt
140143
return context;
141144
}
142145

143-
@Override
144-
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler){
145-
if(!getContext(path).equals("/") && handler instanceof RootHandler)
146-
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
147-
148-
final HttpHandler wrapper = exchange -> {
149-
handle(exchange);
150-
handler.handle(SimpleHttpExchange.create(exchange));
151-
};
152-
final HttpContext context = server.createContext(getContext(path),wrapper);
153-
154-
contexts.put(context,context.getHandler());
155-
156-
return context;
157-
}
158-
159146
//
160147

161148
@Override
@@ -172,14 +159,6 @@ public synchronized final HttpContext createContext(final String path, final Htt
172159
return context;
173160
}
174161

175-
@Override
176-
public synchronized final HttpContext createContext(final String path, final SimpleHttpHandler handler, final Authenticator authenticator){
177-
final HttpContext context = createContext(path,handler);
178-
context.setAuthenticator(authenticator);
179-
return context;
180-
}
181-
182-
183162
//
184163

185164
private String generateRandomContext(){

0 commit comments

Comments
 (0)