Skip to content

Commit c1b9d11

Browse files
committed
Server: Bug fixed. Supports external connection.
1 parent 06d4e35 commit c1b9d11

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,18 @@ Data
5454

5555
```
5656
SYNOPSIS
57-
~ [-p <PORT>] [--keep-alive]
57+
~ [-a <ADDRESS>] [-p <PORT>] [--keep-alive]
5858
[-t <TIMEOUT>]
59-
59+
6060
OPTIONS
61-
-p <PORT> Set up the server with the specified port number.
61+
-a <ADDRESS> Bind the server to the specified IPv4 address.
62+
The default value is 127.0.0.1
63+
64+
-p <PORT> Bind the server to the specified port number.
6265
The default value is 8080
63-
66+
6467
--keep-alive Enable keep-alive.
65-
68+
6669
-t <TIMEOUT> Socket timeout.
6770
The default value is 10000
6871
```
@@ -86,6 +89,7 @@ SERVER: Preset mappings:
8689
/test, methods: [GET]
8790
/missing, methods: [GET,POST]
8891
/moved, methods: [GET]
92+
SERVER: Server bound to /127.0.0.1:8080
8993
SERVER: Reading static files from: [file:///.../Data/Server/Static]
9094
SERVER: The server is now running
9195
```

src/main/java/edu/nju/http/server/HttpServer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
import edu.nju.http.util.Log;
1313

1414
import java.io.IOException;
15+
import java.net.InetAddress;
1516
import java.net.InetSocketAddress;
17+
import java.net.InterfaceAddress;
18+
import java.net.NetworkInterface;
19+
import java.net.SocketException;
1620
import java.net.StandardSocketOptions;
1721
import java.nio.channels.AsynchronousServerSocketChannel;
1822
import java.nio.channels.AsynchronousSocketChannel;
1923
import java.nio.channels.CompletionHandler;
24+
import java.util.Arrays;
2025
import java.util.HashMap;
2126
import java.util.Map;
2227
import java.util.concurrent.ExecutionException;
@@ -45,6 +50,8 @@ public HttpServer(String hostName, int port) throws IOException {
4550
this.handler = TargetHandler.getInstance();
4651
this.aServerSocket = AsynchronousServerSocketChannel.open();
4752
this.aServerSocket.bind(new InetSocketAddress(hostName, port));
53+
Log.logServer("Server bound to " + this.aServerSocket.getLocalAddress().toString());
54+
4855
this.globalHeaders = new HashMap<>();
4956

5057
this.running = new Semaphore(0);
@@ -168,7 +175,6 @@ private void handleSocket(AsynchronousSocketChannel socket, boolean keepAlive, i
168175
keepAlive = false;
169176
}
170177

171-
172178
// -------------------- 2. Handle the request and generate response -------------------- //
173179
responseMessage = handler.handle(requestMessage);
174180

src/main/java/edu/nju/http/server/ServerDriver.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
public class ServerDriver {
88
public static final String HELP = """
99
SYNOPSIS
10-
~ [-p <PORT>] [--keep-alive]
10+
~ [-a <ADDRESS>] [-p <PORT>] [--keep-alive]
1111
[-t <TIMEOUT>]
1212
1313
OPTIONS
14-
-p <PORT> Set up the server with the specified port number.
14+
-a <ADDRESS> Bind the server to the specified IPv4 address.
15+
The default value is 127.0.0.1
16+
17+
-p <PORT> Bind the server to the specified port number.
1518
The default value is 8080
1619
1720
--keep-alive Enable keep-alive.
@@ -24,21 +27,23 @@ public static void main(String[] args) {
2427
try {
2528
ArgIterator ai = new ArgIterator(args, "-");
2629

30+
String hostname = "127.0.0.1";
2731
int port = 8080;
2832
boolean keepAlive = Config.DEFAULT_KEEP_ALIVE;
2933
int timeout = Config.DEFAULT_TIMEOUT;
3034

3135
while (ai.hasNext()) {
3236
String opt = ai.next();
3337
switch (opt) {
38+
case "-a" -> hostname = ai.next();
3439
case "-p" -> port = Integer.parseInt(ai.next());
3540
case "--keep-alive" -> keepAlive = true;
3641
case "-t" -> timeout = Integer.parseInt(ai.next());
3742
default -> throw new InvalidCommandException("Invalid token at \"%s\"".formatted(opt));
3843
}
3944
}
4045

41-
HttpServer server = new HttpServer("127.0.0.1", port);
46+
HttpServer server = new HttpServer(hostname, port);
4247
server.launch(keepAlive, timeout);
4348

4449
} catch (InvalidCommandException e) {

src/main/java/edu/nju/http/server/TargetHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public HttpResponseMessage handle(HttpRequestMessage msg) {
129129
// -------------------- 1. Static Resource -------------------- //
130130
Path path = getResourcePath(target);
131131

132-
Log.debug("Search resource in path: ", path);
132+
Log.debug("Searching resource in path: ", path);
133133

134134
if (WebMethods.GET.equals(msg.getMethod())
135-
&& Files.exists(path)
135+
&& Files.exists(path) && !Files.isDirectory(path)
136136
) {
137137
return loadStaticResource(path, msg);
138138
} else {

src/main/java/edu/nju/http/util/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Config {
4545

4646
public final static int DEFAULT_TIMEOUT = 10000;
4747
public final static boolean DEFAULT_KEEP_ALIVE = false;
48-
public final static int SOCKET_BUFFER_SIZE = 1 << 10;
48+
public final static int SOCKET_BUFFER_SIZE = 1 << 30;
4949

5050
private static Path getPath(String resource) {
5151
URL url = ClassLoader.getSystemClassLoader().getResource(resource);

0 commit comments

Comments
 (0)