-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP_Request.java
44 lines (34 loc) · 1.28 KB
/
HTTP_Request.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
package javachr.examples.webserver;
import java.net.Socket;
import java.util.Locale;
public class HTTP_Request {
private static final String RESOURCE_PREFIX = "./html/";
private final Socket sender;
private final String method;
private String resource;
public HTTP_Request(Socket sender, String method, String resource, String protocol){
this.sender = sender;
this.method = method.trim().toLowerCase(Locale.ROOT);
this.resource = resource.trim().equals("/") ? "index.html" : resource.trim().toLowerCase(Locale.ROOT); // map "/" to "index.html"
if(this.resource.startsWith("/"))
this.resource = resource.substring(1, this.resource.length()); // Remove first "/"
this.resource = RESOURCE_PREFIX + this.resource;
}
public Socket getSender(){
return sender;
}
public boolean isValid() {
return method.equals("get") || method.equals("head");
}
public HTTP_Response getResponse() {
return new HTTP_Response(sender, method, resource);
}
@Override
public String toString() {
return "HTTP_Request{" +
"sender=" + sender +
", method='" + method + '\'' +
", resource='" + resource + '\'' +
'}';
}
}