Skip to content

Commit 0de1e14

Browse files
committed
Added changes to Responsebuilder to make merging easier
1 parent 758f8c4 commit 0de1e14

1 file changed

Lines changed: 38 additions & 23 deletions

File tree

src/main/java/org/example/http/HttpResponseBuilder.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,30 @@ public class HttpResponseBuilder {
99
private static final String PROTOCOL = "HTTP/1.1";
1010
private int statusCode = 200;
1111
private String body = "";
12+
private byte[] bytebody;
1213
private Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
1314

1415
private static final String CRLF = "\r\n";
1516

17+
private static final Map<Integer, String> REASON_PHRASES = Map.ofEntries(
18+
Map.entry(200, "OK"),
19+
Map.entry(201, "Created"),
20+
Map.entry(204, "No Content"),
21+
Map.entry(301, "Moved Permanently"),
22+
Map.entry(302, "Found"),
23+
Map.entry(303, "See Other"),
24+
Map.entry(304, "Not Modified"),
25+
Map.entry(307, "Temporary Redirect"),
26+
Map.entry(308, "Permanent Redirect"),
27+
Map.entry(400, "Bad Request"),
28+
Map.entry(401, "Unauthorized"),
29+
Map.entry(403, "Forbidden"),
30+
Map.entry(404, "Not Found"),
31+
Map.entry(500, "Internal Server Error"),
32+
Map.entry(502, "Bad Gateway"),
33+
Map.entry(503, "Service Unavailable")
34+
);
35+
1636
public void setStatusCode(int statusCode) {
1737
this.statusCode = statusCode;
1838
}
@@ -21,6 +41,10 @@ public void setBody(String body) {
2141
this.body = body != null ? body : "";
2242
}
2343

44+
public void setBody(byte[] body) {
45+
this.bytebody = body;
46+
}
47+
2448
public void setHeaders(Map<String, String> headers) {
2549
this.headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
2650
this.headers.putAll(headers);
@@ -35,42 +59,33 @@ public void setContentTypeFromFilename(String filename) {
3559
setHeader("Content-Type", mimeType);
3660
}
3761

38-
private static final Map<Integer, String> REASON_PHRASES = Map.ofEntries(
39-
Map.entry(200, "OK"),
40-
Map.entry(201, "Created"),
41-
Map.entry(204, "No Content"),
42-
Map.entry(301, "Moved Permanently"),
43-
Map.entry(302, "Found"),
44-
Map.entry(303, "See Other"),
45-
Map.entry(304, "Not Modified"),
46-
Map.entry(307, "Temporary Redirect"),
47-
Map.entry(308, "Permanent Redirect"),
48-
Map.entry(400, "Bad Request"),
49-
Map.entry(401, "Unauthorized"),
50-
Map.entry(403, "Forbidden"),
51-
Map.entry(404, "Not Found"),
52-
Map.entry(500, "Internal Server Error"),
53-
Map.entry(502, "Bad Gateway"),
54-
Map.entry(503, "Service Unavailable")
55-
);
56-
57-
public String build(){
62+
public String build() {
5863
StringBuilder sb = new StringBuilder();
59-
String reason = REASON_PHRASES.getOrDefault(statusCode, "");
6064

65+
// Calculate content length (both String and byte[] body)
66+
int contentLength;
67+
if (body.isEmpty() && bytebody != null) {
68+
contentLength = bytebody.length;
69+
setBody(new String(bytebody, StandardCharsets.UTF_8));
70+
} else {
71+
contentLength = body.getBytes(StandardCharsets.UTF_8).length;
72+
}
73+
74+
// Status line
75+
String reason = REASON_PHRASES.getOrDefault(statusCode, "");
6176
sb.append(PROTOCOL).append(" ").append(statusCode);
6277
if (!reason.isEmpty()) {
6378
sb.append(" ").append(reason);
6479
}
6580
sb.append(CRLF);
6681

6782
// User-defined headers
68-
headers.forEach((k,v) -> sb.append(k).append(": ").append(v).append(CRLF));
83+
headers.forEach((k, v) -> sb.append(k).append(": ").append(v).append(CRLF));
6984

7085
// Auto-append Content-Length if not set
7186
if (!headers.containsKey("Content-Length")) {
7287
sb.append("Content-Length: ")
73-
.append(body.getBytes(StandardCharsets.UTF_8).length)
88+
.append(contentLength)
7489
.append(CRLF);
7590
}
7691

0 commit comments

Comments
 (0)