-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRequestImpl.java
More file actions
136 lines (105 loc) · 3.65 KB
/
RequestImpl.java
File metadata and controls
136 lines (105 loc) · 3.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package net.gescobar.httpserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* This is an internal implementation of the {@link Request} interface.
*
* @author German Escobar
*/
class RequestImpl implements Request {
/**
* The Host header.
*/
private String host;
/**
* The HTTP method
*/
private final String method;
/**
* The request path
*/
private final String path;
/**
* The request headers
*/
private final Map<String,String> headers;
/**
* Constructor.
*
* @param reader from which we are reading the headers.
*
* @throws IOException if an I/O error occurs in the underlying connection.
*/
public RequestImpl(BufferedReader reader) throws IOException {
String request = reader.readLine();
// get the method and the path
method = request.split(" ")[0];
path = request.split(" ")[1];
// get the headers
headers = retrieveHeaders(reader);
}
/**
* Helper method. Retrieves the headers of the request.
*
* @param reader the reader from which we are retrieving the request information.
*
* @return a Map<String,String> object with the headers of the request.
* @throws IOException if an I/O error occurs in the underlying communication.
*/
private Map<String,String> retrieveHeaders(BufferedReader reader) throws IOException {
Map<String,String> headers = new HashMap<String,String>();
// iterate through the headers
String headerLine = reader.readLine();
while( !headerLine.equals("") ) {
// headers come in the form "name: value"
String name = headerLine.split(":")[0].trim();
String value = headerLine.split(":")[1].trim();
// add to the headers only if there is no corresponding field (e.g. "Host" header is mapped to the
// *host* field of the request)
if ( !isKnownHeader(name, value) ) {
headers.put(name, value);
}
// read next line
headerLine = reader.readLine();
}
return headers;
}
/**
* Checks if it is a known header and sets the corresponding field.
*
* @param name the name of the header to check.
* @param value the value of the header to check.
*
* @return true if it is a known header, false otherwise
*/
private boolean isKnownHeader(String name, String value) {
boolean ret = false;
if (name.equalsIgnoreCase("host")) {
host = value;
return true;
}
return ret;
}
@Override
public String getMethod() {
return method;
}
@Override
public String getPath() {
return path;
}
@Override
public String getHost() {
return host;
}
@Override
public Map<String, String> getHeaders() {
return headers;
}
@Override
public String getHeader(String name) {
return headers.get(name);
}
}