View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty5.handler.codec.http;
18  
19  import io.netty5.util.internal.StringUtil;
20  
21  import java.util.Map;
22  
23  /**
24   * Provides some utility methods for HTTP message implementations.
25   */
26  final class HttpMessageUtil {
27  
28      static StringBuilder appendRequest(StringBuilder buf, HttpRequest req) {
29          appendCommon(buf, req);
30          appendInitialLine(buf, req);
31          appendHeaders(buf, req.headers());
32          removeLastNewLine(buf);
33          return buf;
34      }
35  
36      static StringBuilder appendResponse(StringBuilder buf, HttpResponse res) {
37          appendCommon(buf, res);
38          appendInitialLine(buf, res);
39          appendHeaders(buf, res.headers());
40          removeLastNewLine(buf);
41          return buf;
42      }
43  
44      private static void appendCommon(StringBuilder buf, HttpMessage msg) {
45          buf.append(StringUtil.simpleClassName(msg));
46          buf.append("(decodeResult: ");
47          buf.append(msg.decoderResult());
48          buf.append(", version: ");
49          buf.append(msg.protocolVersion());
50          buf.append(')');
51          buf.append(StringUtil.NEWLINE);
52      }
53  
54      static StringBuilder appendFullRequest(StringBuilder buf, FullHttpRequest req) {
55          appendFullCommon(buf, req);
56          appendInitialLine(buf, req);
57          appendHeaders(buf, req.headers());
58          appendHeaders(buf, req.trailingHeaders());
59          removeLastNewLine(buf);
60          return buf;
61      }
62  
63      static StringBuilder appendFullResponse(StringBuilder buf, FullHttpResponse res) {
64          appendFullCommon(buf, res);
65          appendInitialLine(buf, res);
66          appendHeaders(buf, res.headers());
67          appendHeaders(buf, res.trailingHeaders());
68          removeLastNewLine(buf);
69          return buf;
70      }
71  
72      private static void appendFullCommon(StringBuilder buf, FullHttpMessage<?> msg) {
73          buf.append(StringUtil.simpleClassName(msg));
74          buf.append("(decodeResult: ");
75          buf.append(msg.decoderResult());
76          buf.append(", version: ");
77          buf.append(msg.protocolVersion());
78          buf.append(", content: ");
79          buf.append(msg.payload());
80          buf.append(')');
81          buf.append(StringUtil.NEWLINE);
82      }
83  
84      private static void appendInitialLine(StringBuilder buf, HttpRequest req) {
85          buf.append(req.method());
86          buf.append(' ');
87          buf.append(req.uri());
88          buf.append(' ');
89          buf.append(req.protocolVersion());
90          buf.append(StringUtil.NEWLINE);
91      }
92  
93      private static void appendInitialLine(StringBuilder buf, HttpResponse res) {
94          buf.append(res.protocolVersion());
95          buf.append(' ');
96          buf.append(res.status());
97          buf.append(StringUtil.NEWLINE);
98      }
99  
100     private static void appendHeaders(StringBuilder buf, HttpHeaders headers) {
101         for (Map.Entry<String, String> e: headers) {
102             buf.append(e.getKey());
103             buf.append(": ");
104             buf.append(e.getValue());
105             buf.append(StringUtil.NEWLINE);
106         }
107     }
108 
109     private static void removeLastNewLine(StringBuilder buf) {
110         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
111     }
112 
113     private HttpMessageUtil() { }
114 }