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