forked from ucsd-cse15l-f22/wavelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringServer.java
50 lines (44 loc) · 1.57 KB
/
StringServer.java
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
49
50
import java.io.IOException;
import java.net.URI;
class Handler implements URLHandler {
// The one bit of state on the server: a number that will be manipulated by
// various requests.
String strHistory = "";
/**
* This method takes in a URI/URL to handle requests and counts how many times you've visited
* the page.
*/
public String handleRequest(URI url) {
/**
* Checks path
* If in the default (/) path, display the total count (num)
*/
if (url.getPath().equals("/")) {
return strHistory;
// if add-message, concatenate message to the end of string History
} else {
System.out.println("Path: " + url.getPath());
if (url.getPath().contains("/add-message")) {
// splits at = to create an array
String[] parameters = url.getQuery().split("=");
// looks for object "s" at the first idx of the parameters[]
if (parameters[0].equals("s")) {
// adds str to strHistory
strHistory += parameters[1] + "\n";
return strHistory;
}
}
return "404 Not Found!";
}
}
}
public class StringServer {
public static void main(String[] args) throws IOException {
if(args.length == 0){
System.out.println("Missing port number! Try any number between 1024 to 49151");
return;
}
int port = Integer.parseInt(args[0]);
Server.start(port, new Handler());
}
}