1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.util.internal.StringUtil;
19
20 import java.util.Map;
21
22
23
24
25 final class HttpMessageUtil {
26
27 static StringBuilder appendRequest(StringBuilder buf, HttpRequest req) {
28 appendCommon(buf, req);
29 appendInitialLine(buf, req);
30 appendHeaders(buf, req.headers());
31 removeLastNewLine(buf);
32 return buf;
33 }
34
35 static StringBuilder appendResponse(StringBuilder buf, HttpResponse res) {
36 appendCommon(buf, res);
37 appendInitialLine(buf, res);
38 appendHeaders(buf, res.headers());
39 removeLastNewLine(buf);
40 return buf;
41 }
42
43 private static void appendCommon(StringBuilder buf, HttpMessage msg) {
44 buf.append(StringUtil.simpleClassName(msg));
45 buf.append("(decodeResult: ");
46 buf.append(msg.decoderResult());
47 buf.append(", version: ");
48 buf.append(msg.protocolVersion());
49 buf.append(')');
50 buf.append(StringUtil.NEWLINE);
51 }
52
53 static StringBuilder appendFullRequest(StringBuilder buf, FullHttpRequest req) {
54 appendFullCommon(buf, req);
55 appendInitialLine(buf, req);
56 appendHeaders(buf, req.headers());
57 appendHeaders(buf, req.trailingHeaders());
58 removeLastNewLine(buf);
59 return buf;
60 }
61
62 static StringBuilder appendFullResponse(StringBuilder buf, FullHttpResponse res) {
63 appendFullCommon(buf, res);
64 appendInitialLine(buf, res);
65 appendHeaders(buf, res.headers());
66 appendHeaders(buf, res.trailingHeaders());
67 removeLastNewLine(buf);
68 return buf;
69 }
70
71 private static void appendFullCommon(StringBuilder buf, FullHttpMessage msg) {
72 buf.append(StringUtil.simpleClassName(msg));
73 buf.append("(decodeResult: ");
74 buf.append(msg.decoderResult());
75 buf.append(", version: ");
76 buf.append(msg.protocolVersion());
77 buf.append(", content: ");
78 buf.append(msg.content());
79 buf.append(')');
80 buf.append(StringUtil.NEWLINE);
81 }
82
83 private static void appendInitialLine(StringBuilder buf, HttpRequest req) {
84 buf.append(req.method());
85 buf.append(' ');
86 buf.append(req.uri());
87 buf.append(' ');
88 buf.append(req.protocolVersion());
89 buf.append(StringUtil.NEWLINE);
90 }
91
92 private static void appendInitialLine(StringBuilder buf, HttpResponse res) {
93 buf.append(res.protocolVersion());
94 buf.append(' ');
95 buf.append(res.status());
96 buf.append(StringUtil.NEWLINE);
97 }
98
99 private static void appendHeaders(StringBuilder buf, HttpHeaders headers) {
100 for (Map.Entry<String, String> e: headers) {
101 buf.append(e.getKey());
102 buf.append(": ");
103 buf.append(e.getValue());
104 buf.append(StringUtil.NEWLINE);
105 }
106 }
107
108 private static void removeLastNewLine(StringBuilder buf) {
109 buf.setLength(buf.length() - StringUtil.NEWLINE.length());
110 }
111
112 private HttpMessageUtil() { }
113 }