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'>
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'>
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'>
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 * <h5 class='section'>See Also:</h5><ul>
068 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
069 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
070 * </ul>
071 *
072 * @serial exclude
073 */
074@Header("Host")
075public class Host extends BasicStringHeader {
076
077   //-----------------------------------------------------------------------------------------------------------------
078   // Static
079   //-----------------------------------------------------------------------------------------------------------------
080
081   private static final long serialVersionUID = 1L;
082   private static final String NAME = "Host";
083
084   /**
085    * Static creator.
086    *
087    * @param value
088    *    The header value.
089    *    <br>Can be <jk>null</jk>.
090    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
091    */
092   public static Host of(String value) {
093      return value == null ? null : new Host(value);
094   }
095
096   /**
097    * Static creator with delayed value.
098    *
099    * <p>
100    * Header value is re-evaluated on each call to {@link #getValue()}.
101    *
102    * @param value
103    *    The supplier of the header value.
104    *    <br>Can be <jk>null</jk>.
105    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
106    */
107   public static Host of(Supplier<String> value) {
108      return value == null ? null : new Host(value);
109   }
110
111   //-----------------------------------------------------------------------------------------------------------------
112   // Instance
113   //-----------------------------------------------------------------------------------------------------------------
114
115   /**
116    * Constructor.
117    *
118    * @param value
119    *    The header value.
120    *    <br>Can be <jk>null</jk>.
121    */
122   public Host(String value) {
123      super(NAME, value);
124   }
125
126   /**
127    * Constructor with delayed value.
128    *
129    * <p>
130    * Header value is re-evaluated on each call to {@link #getValue()}.
131    *
132    * @param value
133    *    The supplier of the header value.
134    *    <br>Can be <jk>null</jk>.
135    */
136   public Host(Supplier<String> value) {
137      super(NAME, value);
138   }
139}