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  package io.netty.handler.codec.http;
17  
18  import io.netty.util.internal.StringUtil;
19  
20  import java.util.Map;
21  
22  /**
23   * Provides some utility methods for HTTP message implementations.
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 }