1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.tomcat.util.http.parser;
18
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.io.StringReader;
22
23 import org.apache.tomcat.util.buf.ByteChunk;
24 import org.apache.tomcat.util.buf.MessageBytes;
25
26 public class Host {
27
28 private Host() {
29 // Utility class. Hide default constructor.
30 }
31
32
33 /**
34 * Parse the given input as an HTTP Host header value.
35 *
36 * @param mb The host header value
37 *
38 * @return The position of ':' that separates the host from the port or -1
39 * if it is not present
40 *
41 * @throws IllegalArgumentException If the host header value is not
42 * specification compliant
43 */
44 public static int parse(MessageBytes mb) {
45 return parse(new MessageBytesReader(mb));
46 }
47
48
49 /**
50 * Parse the given input as an HTTP Host header value.
51 *
52 * @param string The host header value
53 *
54 * @return The position of ':' that separates the host from the port or -1
55 * if it is not present
56 *
57 * @throws IllegalArgumentException If the host header value is not
58 * specification compliant
59 */
60 public static int parse(String string) {
61 return parse(new StringReader(string));
62 }
63
64
65 private static int parse(Reader reader) {
66 try {
67 reader.mark(1);
68 int first = reader.read();
69 reader.reset();
70 if (HttpParser.isAlpha(first)) {
71 return HttpParser.readHostDomainName(reader);
72 } else if (HttpParser.isNumeric(first)) {
73 return HttpParser.readHostIPv4(reader, false);
74 } else if ('[' == first) {
75 return HttpParser.readHostIPv6(reader);
76 } else {
77 // Invalid
78 throw new IllegalArgumentException();
79 }
80 } catch (IOException ioe) {
81 // Should never happen
82 throw new IllegalArgumentException(ioe);
83 }
84 }
85
86
87 private static class MessageBytesReader extends Reader {
88
89 private final byte[] bytes;
90 private final int end;
91 private int pos;
92 private int mark;
93
94 public MessageBytesReader(MessageBytes mb) {
95 ByteChunk bc = mb.getByteChunk();
96 bytes = bc.getBytes();
97 pos = bc.getOffset();
98 end = bc.getEnd();
99 }
100
101 @Override
102 public int read(char[] cbuf, int off, int len) throws IOException {
103 for (int i = off; i < off + len; i++) {
104 // Want output in range 0 to 255, not -128 to 127
105 cbuf[i] = (char) (bytes[pos++] & 0xFF);
106 }
107 return len;
108 }
109
110 @Override
111 public void close() throws IOException {
112 // NO-OP
113 }
114
115 // Over-ridden methods to improve performance
116
117 @Override
118 public int read() throws IOException {
119 if (pos < end) {
120 // Want output in range 0 to 255, not -128 to 127
121 return bytes[pos++] & 0xFF;
122 } else {
123 return -1;
124 }
125 }
126
127 // Methods to support mark/reset
128
129 @Override
130 public boolean markSupported() {
131 return true;
132 }
133
134 @Override
135 public void mark(int readAheadLimit) throws IOException {
136 mark = pos;
137 }
138
139 @Override
140 public void reset() throws IOException {
141 pos = mark;
142 }
143 }
144 }
145