1 |
|
package net.technearts.rip; |
2 |
|
|
3 |
|
import java.io.BufferedReader; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.StringReader; |
6 |
|
import java.net.MalformedURLException; |
7 |
|
import java.net.URL; |
8 |
|
import java.util.HashMap; |
9 |
|
|
10 |
|
import org.eclipse.jetty.http.HttpMethod; |
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
|
|
| 0% |
Uncovered Elements: 55 (55) |
Complexity: 19 |
Complexity Density: 0.56 |
|
15 |
|
public class HttpRequestParser { |
16 |
|
private String reqLine; |
17 |
|
private final HashMap<String, String> reqHeaders; |
18 |
|
private final StringBuilder msgBody; |
19 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
20 |
0 |
public HttpRequestParser() {... |
21 |
0 |
reqHeaders = new HashMap<>(); |
22 |
0 |
msgBody = new StringBuilder(); |
23 |
|
} |
24 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
25 |
0 |
private void appendHeaderParameter(final String header)... |
26 |
|
throws HttpFormatException { |
27 |
0 |
final int idx = header.indexOf(':'); |
28 |
0 |
if (idx == -1) { |
29 |
0 |
throw new HttpFormatException("Invalid Header Parameter: " + header); |
30 |
|
} |
31 |
0 |
reqHeaders.put(header.substring(0, idx), |
32 |
|
header.substring(idx + 1, header.length())); |
33 |
|
} |
34 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
35 |
0 |
private void appendMessageBody(final String bodyLine) {... |
36 |
0 |
msgBody.append(bodyLine).append("\r\n"); |
37 |
|
} |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@param |
43 |
|
@return |
44 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
45 |
0 |
public String getHeaderParam(final String headerName) {... |
46 |
0 |
return reqHeaders.get(headerName); |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@return |
54 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
55 |
0 |
public String getMessageBody() {... |
56 |
0 |
return msgBody.toString(); |
57 |
|
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@return |
63 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
64 |
0 |
public HttpMethod getMethod() {... |
65 |
0 |
final HttpMethod method = HttpMethod |
66 |
|
.fromString(getRequestLine().split(" ")[0]); |
67 |
0 |
if (method == null) { |
68 |
0 |
throw new IllegalArgumentException(); |
69 |
|
} |
70 |
0 |
return method; |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@return |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
0 |
public String getRequestLine() {... |
80 |
0 |
return reqLine; |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@return |
87 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
88 |
0 |
public URL getUrl() {... |
89 |
0 |
final String protocol = "HTTP"; |
90 |
0 |
final String host = getHeaderParam("Host"); |
91 |
0 |
final int port = 80; |
92 |
0 |
final String file = getRequestLine().split(" ")[1]; |
93 |
0 |
try { |
94 |
0 |
return new URL(protocol, host, port, file); |
95 |
|
} catch (final MalformedURLException ex) { |
96 |
0 |
throw new IllegalArgumentException(); |
97 |
|
} |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
@throws |
105 |
|
@throws |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
107 |
0 |
public void parseRequest(final BufferedReader reader)... |
108 |
|
throws IOException, HttpFormatException { |
109 |
0 |
setRequestLine(reader.readLine()); |
110 |
0 |
String header = reader.readLine(); |
111 |
0 |
while (header != null && header.length() > 0) { |
112 |
0 |
appendHeaderParameter(header); |
113 |
0 |
header = reader.readLine(); |
114 |
|
} |
115 |
0 |
String bodyLine = reader.readLine(); |
116 |
0 |
while (bodyLine != null) { |
117 |
0 |
appendMessageBody(bodyLine); |
118 |
0 |
bodyLine = reader.readLine(); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@param |
126 |
|
@throws |
127 |
|
@throws |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
129 |
0 |
public void parseRequest(final String request)... |
130 |
|
throws IOException, HttpFormatException { |
131 |
0 |
parseRequest(new BufferedReader(new StringReader(request))); |
132 |
|
} |
133 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
134 |
0 |
private void setRequestLine(final String requestLine)... |
135 |
|
throws HttpFormatException { |
136 |
0 |
if (requestLine == null || requestLine.length() == 0) { |
137 |
0 |
throw new HttpFormatException("Invalid Request-Line: " + requestLine); |
138 |
|
} |
139 |
0 |
reqLine = requestLine; |
140 |
|
} |
141 |
|
} |