-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathHttpRequest.java
More file actions
65 lines (51 loc) · 1.96 KB
/
HttpRequest.java
File metadata and controls
65 lines (51 loc) · 1.96 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.johan.http;
import java.util.HashMap;
import java.util.Set;
public class HttpRequest {
private HttpMethod method;
private String requestTarget;
private String originalHttpVersion;
private HttpVersion getBestCompatibleVersion;
private HashMap<String, String> headers = new HashMap<>();
HttpRequest() {
}
public HttpMethod getMethod() {
return method;
}
public String getRequestTarget() {
return requestTarget;
}
public HttpVersion getBestCompatibleVersion() { return getBestCompatibleVersion; }
public String getOriginalHttpVersion() { return originalHttpVersion; }
public Set<String> getHeaderNames() {
return headers.keySet();
}
public String getHeader(String headerName) {
return headers.get(headerName.toLowerCase());
}
void setMethod(String methodName) throws HttpParsingException {
for (HttpMethod method: HttpMethod.values()) {
if(methodName.equals(method.name())){
this.method = method;
return;
}
}
throw new HttpParsingException(HttpStatusCode.SERVER_ERROR_501_NOT_IMPLEMENTED);
}
public void setRequestTarget(String requestTarget) throws HttpParsingException {
if (requestTarget == null || requestTarget.isEmpty()){
throw new HttpParsingException(HttpStatusCode.CLIENT_ERROR_400_BAD_REQ);
}
this.requestTarget = requestTarget;
}
public void setHttpVersion(String originalHttpVersion) throws BadHttpVersionException, HttpParsingException {
this.originalHttpVersion = originalHttpVersion;
this.getBestCompatibleVersion = HttpVersion.getBestCompatibleVersion(originalHttpVersion);
if(this.getBestCompatibleVersion == null){
throw new BadHttpVersionException();
}
}
void addHeader(String headerName, String HeaderField){
headers.put(headerName.toLowerCase(), HeaderField);
}
}