001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.header;
014
015import java.util.function.*;
016
017import org.apache.juneau.http.annotation.*;
018
019/**
020 * Represents a parsed <l>Host</l> HTTP request header.
021 *
022 * <p>
023 * The domain name of the server (for virtual hosting), and the TCP port number on which the server is listening.
024 * The port number may be omitted if the port is the standard port for the service requested.
025 * Mandatory since HTTP/1.1.
026 *
027 * <h5 class='figure'>Example</h5>
028 * <p class='bcode w800'>
029 *    Host: en.wikipedia.org:8080
030 *    Host: en.wikipedia.org
031 * </p>
032 *
033 * <h5 class='topic'>RFC2616 Specification</h5>
034 *
035 * The Host request-header field specifies the Internet host and port number of the resource being requested, as
036 * obtained from the original URI given by the user or referring resource (generally an HTTP URL, as described in
037 * section 3.2.2).
038 * The Host field value MUST represent the naming authority of the origin server or gateway given by the original URL.
039 * This allows the origin server or gateway to differentiate between internally-ambiguous URLs, such as the root "/" URL
040 * of a server for multiple host names on a single IP address.
041 *
042 * <p class='bcode w800'>
043 *    Host = "Host" ":" host [ ":" port ] ; Section 3.2.2
044 * </p>
045 *
046 * <p>
047 * A "host" without any trailing port information implies the default port for the service requested (e.g., "80" for an
048 * HTTP URL).
049 * For example, a request on the origin server for &lt;http://www.w3.org/pub/WWW/&gt; would properly include:
050 * <p class='bcode w800'>
051 *    GET /pub/WWW/ HTTP/1.1
052 *    Host: www.w3.org
053 * </p>
054 *
055 * <p>
056 * A client MUST include a Host header field in all HTTP/1.1 request messages.
057 * If the requested URI does not include an Internet host name for the service being requested, then the Host header
058 * field MUST be given with an empty value.
059 * An HTTP/1.1 proxy MUST ensure that any request message it forwards does contain an appropriate Host header field that
060 * identifies the service being requested by the proxy.
061 * All Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request) status code to any HTTP/1.1 request
062 * message which lacks a Host header field.
063 *
064 * <p>
065 * See sections 5.2 and 19.6.1.1 for other requirements relating to Host.
066 *
067 * <ul class='seealso'>
068 *    <li class='extlink'>{@doc ExtRFC2616}
069 * </ul>
070 */
071@Header("Host")
072public class Host extends BasicStringHeader {
073
074   private static final long serialVersionUID = 1L;
075
076   /**
077    * Convenience creator.
078    *
079    * @param value
080    *    The header value.
081    *    <br>Can be any of the following:
082    *    <ul>
083    *       <li>{@link String}
084    *       <li>Anything else - Converted to <c>String</c> then parsed.
085    *    </ul>
086    * @return A new {@link Host} object.
087    */
088   public static Host of(Object value) {
089      if (value == null)
090         return null;
091      return new Host(value);
092   }
093
094   /**
095    * Convenience creator using supplier.
096    *
097    * <p>
098    * Header value is re-evaluated on each call to {@link #getValue()}.
099    *
100    * @param value
101    *    The header value supplier.
102    *    <br>Can be any of the following:
103    *    <ul>
104    *       <li>{@link String}
105    *       <li>Anything else - Converted to <c>String</c> then parsed.
106    *    </ul>
107    * @return A new {@link Host} object.
108    */
109   public static Host of(Supplier<?> value) {
110      if (value == null)
111         return null;
112      return new Host(value);
113   }
114
115   /**
116    * Constructor.
117    *
118    * @param value
119    *    The header value.
120    *    <br>Can be any of the following:
121    *    <ul>
122    *       <li>{@link String}
123    *       <li>Anything else - Converted to <c>String</c> then parsed.
124    *       <li>A {@link Supplier} of anything on this list.
125    *    </ul>
126    */
127   public Host(Object value) {
128      super("Host", value);
129   }
130
131   /**
132    * Constructor
133    *
134    * @param value
135    *    The header value.
136    */
137   public Host(String value) {
138      this((Object)value);
139   }
140}