-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabReport2
49 lines (41 loc) · 1.32 KB
/
LabReport2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Part 1:
Code for Chat Server:
```
import java.io.IOException;
import java.net.URI;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
class ChatHandler implements URLHandler {
String chatHistory = "";
public String handleRequest(URI url) {
// expect /chat?user=<name>&message=<string>
if (url.getPath().equals("/chat")) {
String[] params = url.getQuery().split("&");
String[] shouldBeUser = params[0].split("=");
String[] shouldBeMessage = params[1].split("=");
if (shouldBeUser[0].equals("user") && shouldBeMessage[0].equals("message")) {
String user = shouldBeUser[1];
String message = shouldBeMessage[1];
this.chatHistory += user + ": " + message + "\n\n";
return this.chatHistory;
} else {
return "Invalid parameters: " + String.join("&", params);
}
}
return "404 Not Found";`
}
}
class ChatServer {
public static void main(String[] args) throws IOException {`
int port = Integer.parseInt(args[0]);`
Server.start(port, new ChatHandler());`
}
}
```
Part 2:
![Image](keygen.png)
Part 3:
I learned that a web browser isn’t the only way to access web URLs - the curl command
can be used for this as well. We can do this by running the `curl` command with the
URL as its argument from the terminal.