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

Commit 156eceb

Browse files
committed
migrate docs
1 parent 3c48989 commit 156eceb

File tree

17 files changed

+268
-20
lines changed

17 files changed

+268
-20
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
<a href="https://docs.katsute.dev/simplehttpserver">Documentation</a>
66
77
<a href="https://github.com/KatsuteDev/simplehttpserver/blob/main/setup.md#readme">Setup</a>
8-
9-
<a href="https://github.com/KatsuteDev/simplehttpserver/blob/main/faq.md#readme">FAQ</a>
108
</div>
119
</div>
1210

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,49 @@
2828

2929
/**
3030
* A {@link HttpExchange} with additional extensions to simplify usage.
31+
* <h1>Requests</h1>
32+
* <h2><code>GET</code> Request</h2>
33+
* If a user sends a <code>GET</code> request to the server, a map of keys and values of that request can be retrieved by using the {@link #getGetMap()} method.
34+
* <h2><code>POST</code> Request</h2>
35+
* If a user sends a <code>POST</code> request to the server, a map of keys and values; or a {@link MultipartFormData} can be retrieved by using {@link #getPostMap()} and {@link #getMultipartFormData()}.
36+
*
37+
* <h3><code>multipart/form-data</code></h3>
38+
* For requests that have content type <code>multipart/form-data</code>, data must be retrieved using {@link #getMultipartFormData()}, which returns a {@link MultipartFormData} using {@link Record}s and {@link FileRecord}s. Files sent through here are sent as a {@link Byte} array and not a {@link File}.
39+
*
40+
* <h2>Cookies</h2>
41+
* A clients browser cookies for the site can be retrieved by using the {@link #getCookie(String)} or {@link #getCookies()} method.
42+
* <br>
43+
* Cookies can be set by using the {@link #setCookie(HttpCookie)} or {@link #setCookie(String, String)}.
44+
* <br>
45+
* An exchange must be sent in order to change on the client.
46+
*
47+
* <h2>Session</h2>
48+
* Normally the only "identifier" that we can retrieve from the user is their address and port provided from an exchange. This however, doesn't work across multiple tabs or when the user refreshes the page; instead we use a session cookie to track a user.
49+
* <br>
50+
* A server must have a {@link HttpSessionHandler} set using {@link SimpleHttpServer#setSessionHandler(HttpSessionHandler)} for this to work.
51+
* <br>
52+
* The {@link HttpSessionHandler} assigns session IDs to clients and allows the server to retrieve them using {@link SimpleHttpServer#getSession(HttpExchange)}.
53+
*
54+
* <h1>Response</h1>
55+
* To send response headers you must first retrieve then with {@link #getResponseHeaders()}, modify them, then send them using {@link #sendResponseHeaders(int, long)} or any other of the send methods.
56+
* <br>
57+
* Data can be sent as a {@link Byte} array, {@link String}, or as a {@link File}. Responses can optionally gziped to compress the data sent.
58+
* <ul>
59+
* <li>{@link #send(int)}</li>
60+
* <li>{@link #send(byte[])}</li>
61+
* <li>{@link #send(byte[], int)}</li>
62+
* <li>{@link #send(byte[], boolean)}</li>
63+
* <li>{@link #send(byte[], int, boolean)}</li>
64+
* <li>{@link #send(String)}</li>
65+
* <li>{@link #send(String, int)}</li>
66+
* <li>{@link #send(String, boolean)}</li>
67+
* <li>{@link #send(String, int, boolean)}</li>
68+
* <li>{@link #send(File)}</li>
69+
* <li>{@link #send(File, int)}</li>
70+
* <li>{@link #send(File, boolean)}</li>
71+
* <li>{@link #send(File, int, boolean)}</li>
72+
* </ul>
73+
* <b>Note:</b> An exchange must be sent or closed, otherwise the connection may resend the request until it gets response or times out.
3174
*
3275
* @see HttpExchange
3376
* @since 5.0.0

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@
2424
import java.io.IOException;
2525

2626
/**
27-
* A http handler that uses a {@link SimpleHttpExchange}
27+
* A http handler that uses a {@link SimpleHttpExchange}. All handlers in this library can be used with a standard {@link com.sun.net.httpserver.HttpServer}.
2828
* <br>
29-
* Http handlers will not throw an exception in the main thread, you must use a try-catch to expose them. All requests must be closed with {@link HttpExchange#close()}, otherwise the handler will rerun the request multiple times.
29+
* <b>Note:</b> Http handlers will not throw an exception in the main thread, you must use a try-catch to expose them.
3030
* <br>
31-
* This handler can be used with a standard {@link com.sun.net.httpserver.HttpServer}.
31+
* <b>Note:</b> An exchange must be sent or closed, otherwise the connection may resend the request until it gets response or times out.
3232
*
3333
* @see HttpHandler
3434
* @see SimpleHttpExchange
35+
* @see dev.katsute.simplehttpserver.handler.PredicateHandler
36+
* @see dev.katsute.simplehttpserver.handler.RedirectHandler
37+
* @see dev.katsute.simplehttpserver.handler.RootHandler
38+
* @see dev.katsute.simplehttpserver.handler.SSEHandler
39+
* @see dev.katsute.simplehttpserver.handler.TemporaryHandler
40+
* @see dev.katsute.simplehttpserver.handler.throttler.ThrottledHandler
41+
* @see dev.katsute.simplehttpserver.handler.file.FileHandler
3542
* @since 5.0.0
3643
* @version 5.0.0
3744
* @author Katsute

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,92 @@
1818

1919
package dev.katsute.simplehttpserver;
2020

21+
import com.sun.net.httpserver.HttpHandler;
2122
import com.sun.net.httpserver.HttpServer;
23+
import dev.katsute.simplehttpserver.handler.RootHandler;
2224

2325
import java.io.IOException;
2426
import java.net.InetSocketAddress;
27+
import java.util.concurrent.Executor;
28+
import java.util.concurrent.Executors;
2529

2630
/**
2731
* A {@link HttpServer} with additional extensions to simplify usage.
2832
*
33+
* <h1>Creating a SimpleHttpServer</h1>
34+
* A SimpleHttpServer is created in the same way was a standard HttpServer, by using one of the <code>create</code> methods.
35+
* <ul>
36+
* <li>{@link #create()}</li>
37+
* <li>{@link #create(int)}</li>
38+
* <li>{@link #create(int, int)}</li>
39+
* <li>{@link #create(InetSocketAddress, int)}</li>
40+
* </ul>
41+
*
42+
* <h2>Binding to a Port</h2>
43+
* A SimpleHttpServer requires a port to become accessible, the port can be defined in the <code>create</code> or <code>bind</code> methods.
44+
* <ul>
45+
* <li>{@link #create(int)}</li>
46+
* <li>{@link #create(int, int)}</li>
47+
* <li>{@link #create(InetSocketAddress, int)}</li>
48+
* <li>{@link #bind(int)}</li>
49+
* <li>{@link #bind(int, int)}</li>
50+
* <li>{@link #bind(InetSocketAddress, int)}</li>
51+
* </ul>
52+
* The backlog parameter in each of these determines the maximum amount of simultaneous connections at any given time.
53+
*
54+
* <h2>Starting the Server</h2>
55+
* The server can be started by using the {@link #start()} method.
56+
*
57+
* <h2>Stopping the Server</h2>
58+
* The server can be stopped by using {@link #stop()} or {@link #stop(int)} methods.
59+
* <br>
60+
* The delay parameter determines how long the server should wait before forcefully closing the connection, by default this is 0.
61+
*
62+
* <h1>Adding Pages</h1>
63+
* Pages (referred to as a context) are added by using any of the <code>createContext</code> methods.
64+
* <ul>
65+
* <li>{@link #createContext(String)}</li>
66+
* <li>{@link #createContext(String, HttpHandler)}</li>
67+
* </ul>
68+
* Content for each context is determined by using a {@link HttpHandler}. SimpleHttpServer offers some simplified handlers to handle some complex operations, documentation for those can be found in {@link SimpleHttpHandler}.
69+
* <br>
70+
* Contexts in a server are <b>case sensitive</b> and will resolve to the most specific context available.
71+
* <br><br>
72+
* <b>Example:</b>
73+
* <br>
74+
* If a user goes to <code>/this/web/page</code> and there are only handlers for <code>/this</code> and <code>/this/web</code>, then the handler for <code>/this/web</code> would be used because its the most specific context that exists on the server.
75+
* <br><br>
76+
* This behavior consequentially means that any handler added to the root <code>/</code> context would handle any requests that don't have a handler, since it's the most specific one available. A {@link RootHandler} can be used to mediate this issue.
77+
*
78+
* <h1>Accessing the Server</h1>
79+
* <h2>Accessing on Host Machine</h2>
80+
* When a server is started it is immediately available at whatever port was set.
81+
* <br>
82+
* <b>Example:</b> <code>localhost:8000</code>, <code>127.0.0.1:80</code>
83+
* <h2>Accessing on Local Network</h2>
84+
* If permitted, the server should also be accessible to all computers on your immediate internet network at your <a href="https://www.google.com/search?q=How+to+find+my+local+ip+address">local IP</a>.
85+
* <h2>Accessing Globally</h2>
86+
* For clients not connected to your network, they must use your <a href="https://www.whatismyip.com/what-is-my-public-ip-address/">public IP address</a>.
87+
* <br>
88+
* People outside your network can not connect unless you <b>port forward</b> the port the server is using. This process varies depending on your ISP and can often leave your network vulnerable to attackers. It is not suggested to this unless you know what you are doing.
89+
* <br>
90+
* You can learn to port forward <a href="https://www.google.com/search?q=How+to+port+forward">here</a> (at your own risk).
91+
*
92+
* <h1>Multi-threaded Server</h1>
93+
* By default the server runs on a single thread. This means that only one clients exchange can be processed at a time and can lead to long queues.
94+
* <br>
95+
* For a server to be multi-threaded the executor must be changed to one that process threads in parallel. The executor can be changed using the {@link #setExecutor(Executor)} method on the server.
96+
* <br>
97+
* To process a fixed amount of threads you can use {@link Executors#newFixedThreadPool(int)}.
98+
* <br>
99+
* To process an unlimited amount of threads you can use {@link Executors#newCachedThreadPool()}.
100+
* <h2>Requests are still not being processed in parallel</h2>
101+
* Requests to the same context may not run in parallel for a user that is accessing the same page more than once. This issue is caused by the browser, where it will not send duplicate requests to the server at the same time.
102+
* <br>
103+
* This issue is better explained <a href="https://stackoverflow.com/a/58676470">here</a>.
104+
* <br>
105+
* If you still need to test multithreading then you must use an older browser like Internet Explorer or Microsoft Edge.
106+
*
29107
* @see HttpServer
30108
* @since 5.0.0
31109
* @version 5.0.0

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@
1818

1919
package dev.katsute.simplehttpserver;
2020

21-
import com.sun.net.httpserver.*;
21+
import com.sun.net.httpserver.HttpsServer;
2222

2323
import java.io.IOException;
2424
import java.net.InetSocketAddress;
2525

2626
/**
27-
* A http handler that uses a {@link SimpleHttpExchange}
28-
* <br>
29-
* Http handlers will not throw an exception in the main thread, you must use a try-catch to expose them. All requests must be closed with {@link HttpExchange#close()}, otherwise the handler will rerun the request multiple times.
30-
* <br>
31-
* This handler can be used with a standard {@link com.sun.net.httpserver.HttpsServer}.
27+
* A {@link HttpsServer} with additional extensions to simplify usage. See {@link SimpleHttpServer} for setup documentation.
3228
*
33-
* @see HttpHandler
34-
* @see SimpleHttpExchange
29+
* @see SimpleHttpServer
30+
* @see HttpsServer
3531
* @since 5.0.0
3632
* @version 5.0.0
3733
* @author Katsute

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.function.Predicate;
2929

3030
/**
31-
* Handler that process requests based on a predicate.
31+
* Handler that process requests based on the result of a predicate.
3232
*
3333
* @see Predicate
3434
* @see SimpleHttpExchange

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import com.sun.net.httpserver.HttpHandler;
2222

2323
/**
24-
* Handler that process requests for the root <code>/</code> context.
24+
* By default, exchanges will look for the closest matching context for their handler, this consequently means that the root index <code>/</code> would catch any requests without a handler instead of returning a code 404.
25+
* <br>
26+
* The RootHandler resolves this issue by only accepting requests to the exact context <code>/</code> and sending the rest to an alternative handler, typically where a 404 page would reside.
2527
*
2628
* @since 5.0.0
2729
* @version 5.0.0

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.util.concurrent.atomic.AtomicInteger;
3333

3434
/**
35-
* Send events from the server to the client using an <code>text/event-stream</code>. Events are sent using {@link #push(String)} or {@link #push(String, int, String)}.
35+
* A <a href="https://www.w3schools.com/html/html5_serversentevents.asp">Server sent events (SSE)</a> handler sends events from the server to a client using an <code>text/event-stream</code>. Events are sent using {@link #push(String)} or {@link #push(String, int, String)}.
3636
*
3737
* @since 5.0.0
3838
* @version 5.0.0

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020

2121
import com.sun.net.httpserver.HttpExchange;
2222
import com.sun.net.httpserver.HttpHandler;
23+
import dev.katsute.simplehttpserver.SimpleHttpServer;
2324

2425
import java.io.IOException;
2526
import java.util.Objects;
2627

2728
/**
28-
* Handler that removes itself after a single request, or after a set time, whichever comes first.
29+
* Handler that removes itself after a single request, or after a set time, whichever comes first. This can be used for single use downloads or disposable links. A random unused context can be created by using {@link SimpleHttpServer#getRandomContext()} or {@link SimpleHttpServer#getRandomContext(String)}.
2930
*
31+
* @see SimpleHttpServer#getRandomContext()
32+
* @see SimpleHttpServer#getRandomContext(String)
3033
* @since 5.0.0
3134
* @version 5.0.0
3235
* @author Katsute

src/main/java/dev/katsute/simplehttpserver/handler/file/FileHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@
3131

3232
/**
3333
* A file handler can be used to serve single or multiple files on a server with optional pre/post processing using {@link FileAdapter}s.
34+
* <br>
35+
* <h1>{@link FileAdapter}</h1>
36+
* A {@link FileAdapter} determines where a file can be accessed and what content it will return. By default files would be accessible at the file name (including extension) with the file content.
37+
*
38+
* <h1>Adding Files</h1>
39+
* The name parameters in the add methods supersedes the {@link FileAdapter} and makes a file accessible at whatever name you set.
40+
*
41+
* <h1>{@link FileOptions}</h1>
42+
* File options can be added to influence the behavior of added files.
43+
* <h2>Context</h2>
44+
* The {@link FileOptions#context} property determines at where the file will be located with respect to the file handler. By default this is "" and any added files will be accessible directly after the file handler's context.
45+
* <br>
46+
* <b>Example:</b> <code>/fileHandlerContext/file.txt</code> by default and <code>/fileHandlerContext/optionsContext/file.txt</code> if a context was set.
47+
* <h2>Loading Options</h2>
48+
* The {@link FileOptions#loading} option determines how a file should be loaded when added.
49+
* <ul>
50+
* <li>{@link FileOptions.FileLoadingOption#PRELOAD} - files are read when added</li>
51+
* <li>{@link FileOptions.FileLoadingOption#MODIFY} - files are read when added and when modified</li>
52+
* <li>{@link FileOptions.FileLoadingOption#CACHE} - files are read when requested and cached for a set time</li>
53+
* <li>{@link FileOptions.FileLoadingOption#LIVE} - files are read when requested</li>
54+
* </ul>
55+
* <h2>Cache</h2>
56+
* If the loading option {@link FileOptions.FileLoadingOption#CACHE} is used, the {@link FileOptions#cache} determines how long to cache files for in milliseconds.
57+
* <h2>Walk</h2>
58+
* When directories are added, if true, will also include subdirectories; if false, will only include files in the immediate directory.
3459
*
3560
* @see FileAdapter
3661
* @see FileOptions
@@ -154,6 +179,8 @@ public final void addFile(final File file, final String fileName, final FileOpti
154179
Objects.requireNonNull(file);
155180
try{
156181
final FileOptions opts = options == null ? defaultOptions : new FileOptions(options); // dereference to prevent modification
182+
Objects.requireNonNull(opts.loading);
183+
Objects.requireNonNull(opts.context);
157184
files.put(
158185
ContextUtility.joinContexts(true, false, opts.context, fileName == null ? adapter.getName(file) : fileName),
159186
new FileEntry(file, adapter, opts)
@@ -254,6 +281,8 @@ public final void addDirectory(final File directory, final String directoryName,
254281
Objects.requireNonNull(directory);
255282
try{
256283
final FileOptions opts = options == null ? defaultOptions : new FileOptions(options); // dereference to prevent modification
284+
Objects.requireNonNull(opts.loading);
285+
Objects.requireNonNull(opts.context);
257286
final String target = ContextUtility.joinContexts(true, false, opts.context, directoryName);
258287
directories.put(
259288
target,

0 commit comments

Comments
 (0)