View Javadoc
1   /*
2    * Copyright 2012 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    *   http://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.buffer.ByteBuf;
19  import io.netty.handler.codec.AsciiString;
20  import io.netty.util.CharsetUtil;
21  
22  import static io.netty.handler.codec.http.HttpConstants.*;
23  
24  /**
25   * Encodes an {@link HttpRequest} or an {@link HttpContent} into
26   * a {@link ByteBuf}.
27   */
28  public class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
29      private static final char SLASH = '/';
30      private static final char QUESTION_MARK = '?';
31      private static final byte[] CRLF = { CR, LF };
32  
33      @Override
34      public boolean acceptOutboundMessage(Object msg) throws Exception {
35          return super.acceptOutboundMessage(msg) && !(msg instanceof HttpResponse);
36      }
37  
38      @Override
39      protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception {
40          AsciiString method = request.method().name();
41          buf.writeBytes(method.array(), method.arrayOffset(), method.length());
42          buf.writeByte(SP);
43  
44          // Add / as absolute path if no is present.
45          // See http://tools.ietf.org/html/rfc2616#section-5.1.2
46          String uri = request.uri();
47  
48          if (uri.length() == 0) {
49              uri += SLASH;
50          } else {
51              int start = uri.indexOf("://");
52              if (start != -1 && uri.charAt(0) != SLASH) {
53                  int startIndex = start + 3;
54                  // Correctly handle query params.
55                  // See https://github.com/netty/netty/issues/2732
56                  int index = uri.indexOf(QUESTION_MARK, startIndex);
57                  if (index == -1) {
58                      if (uri.lastIndexOf(SLASH) <= startIndex) {
59                          uri += SLASH;
60                      }
61                  } else {
62                      if (uri.lastIndexOf(SLASH, index) <= startIndex) {
63                          int len = uri.length();
64                          StringBuilder sb = new StringBuilder(len + 1);
65                          sb.append(uri, 0, index)
66                            .append(SLASH)
67                            .append(uri, index, len);
68                          uri = sb.toString();
69                      }
70                  }
71              }
72          }
73  
74          buf.writeBytes(uri.getBytes(CharsetUtil.UTF_8));
75          buf.writeByte(SP);
76  
77          AsciiString version = request.protocolVersion().text();
78          buf.writeBytes(version.array(), version.arrayOffset(), version.length());
79          buf.writeBytes(CRLF);
80      }
81  }