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>Via</l> HTTP response header.
021 *
022 * <p>
023 * Informs the client of proxies through which the response was sent.
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Via: 1.0 fred, 1.1 example.com (Apache/1.1)
028 * </p>
029 *
030 * <p>
031 * Informs the client of proxies through which the response was sent.
032 *
033 * <p>
034 * <h5 class='figure'>Example</h5>
035 * <p class='bcode'>
036 *    Via: 1.0 fred, 1.1 example.com (Apache/1.1)
037 * </p>
038 *
039 * <p>
040 * <h5 class='topic'>RFC2616 Specification</h5>
041 *
042 * The Via general-header field MUST be used by gateways and proxies to indicate the intermediate protocols and
043 * recipients between the user agent and the server on requests, and between the origin server and the client on
044 * responses.
045 * It is analogous to the "Received" field of RFC 822 and is intended to be used for tracking message forwards,
046 * avoiding request loops, and identifying the protocol capabilities of all senders along the request/response chain.
047 *
048 * <p class='bcode'>
049 *    Via =  "Via" ":" 1#( received-protocol received-by [ comment ] )
050 *    received-protocol = [ protocol-name "/" ] protocol-version
051 *    protocol-name     = token
052 *    protocol-version  = token
053 *    received-by       = ( host [ ":" port ] ) | pseudonym
054 *    pseudonym         = token
055 * </p>
056 *
057 * <p>
058 * The received-protocol indicates the protocol version of the message received by the server or client along each
059 * segment of the request/response chain.
060 * The received-protocol version is appended to the Via field value when the message is forwarded so that information
061 * about the protocol capabilities of upstream applications remains visible to all recipients.
062 *
063 * <p>
064 * The protocol-name is optional if and only if it would be "HTTP".
065 * The received-by field is normally the host and optional port number of a recipient server or client that subsequently
066 * forwarded the message.
067 * However, if the real host is considered to be sensitive information, it MAY be replaced by a pseudonym.
068 * If the port is not given, it MAY be assumed to be the default port of the received-protocol.
069 *
070 * <p>
071 * Multiple Via field values represents each proxy or gateway that has forwarded the message.
072 * Each recipient MUST append its information such that the end result is ordered according to the sequence of
073 * forwarding applications.
074 *
075 * <p>
076 * Comments MAY be used in the Via header field to identify the software of the recipient proxy or gateway, analogous
077 * to the User-Agent and Server header fields.
078 * However, all comments in the Via field are optional and MAY be removed by any recipient prior to forwarding the
079 * message.
080 *
081 * <p>
082 * For example, a request message could be sent from an HTTP/1.0 user agent to an internal proxy code-named "fred",
083 * which uses HTTP/1.1 to forward the request to a public proxy at nowhere.com, which completes the request by
084 * forwarding it to the origin server at www.ics.uci.edu.
085 * The request received by www.ics.uci.edu would then have the following Via header field:
086 * <p class='bcode'>
087 *    Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
088 * </p>
089 *
090 * <p>
091 * Proxies and gateways used as a portal through a network firewall SHOULD NOT, by default, forward the names and ports
092 * of hosts within the firewall region.
093 * This information SHOULD only be propagated if explicitly enabled.
094 * If not enabled, the received-by host of any host behind the firewall SHOULD be replaced by an appropriate pseudonym
095 * for that host.
096 *
097 * <p>
098 * For organizations that have strong privacy requirements for hiding internal structures, a proxy MAY combine an
099 * ordered subsequence of Via header field entries with identical received-protocol values into a single such entry.
100 * For example...
101 * <p class='bcode'>
102 *    Via: 1.0 ricky, 1.1 ethel, 1.1 fred, 1.0 lucy
103 * </p>
104 *
105 * <p>
106 * ...could be collapsed to...
107 * <p class='bcode'>
108 *    Via: 1.0 ricky, 1.1 mertz, 1.0 lucy
109 * </p>
110 *
111 * <p>
112 * Applications SHOULD NOT combine multiple entries unless they are all under the same organizational control and the
113 * hosts have already been replaced by pseudonyms.
114 * Applications MUST NOT combine entries which have different received-protocol values.
115 *
116 * <h5 class='section'>See Also:</h5><ul>
117 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
118 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
119 * </ul>
120 *
121 * @serial exclude
122 */
123@Header("Via")
124public class Via extends BasicCsvHeader {
125
126   //-----------------------------------------------------------------------------------------------------------------
127   // Static
128   //-----------------------------------------------------------------------------------------------------------------
129
130   private static final long serialVersionUID = 1L;
131   private static final String NAME = "Via";
132
133   /**
134    * Static creator.
135    *
136    * @param value
137    *    The header value.
138    *    <br>Can be <jk>null</jk>.
139    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
140    */
141   public static Via of(String value) {
142      return value == null ? null : new Via(value);
143   }
144
145   /**
146    * Static creator.
147    *
148    * @param value
149    *    The header value.
150    *    <br>Can be <jk>null</jk>.
151    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
152    */
153   public static Via of(String...value) {
154      return value == null ? null : new Via(value);
155   }
156
157   /**
158    * Static creator with delayed value.
159    *
160    * <p>
161    * Header value is re-evaluated on each call to {@link #getValue()}.
162    *
163    * @param value
164    *    The supplier of the header value.
165    *    <br>Can be <jk>null</jk>.
166    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
167    */
168   public static Via of(Supplier<String[]> value) {
169      return value == null ? null : new Via(value);
170   }
171
172   //-----------------------------------------------------------------------------------------------------------------
173   // Instance
174   //-----------------------------------------------------------------------------------------------------------------
175
176   /**
177    * Constructor.
178    *
179    * @param value
180    *    The header value.
181    *    <br>Can be <jk>null</jk>.
182    */
183   public Via(String value) {
184      super(NAME, value);
185   }
186
187   /**
188    * Constructor.
189    *
190    * @param value
191    *    The header value.
192    *    <br>Can be <jk>null</jk>.
193    */
194   public Via(String...value) {
195      super(NAME, value);
196   }
197
198   /**
199    * Constructor with delayed value.
200    *
201    * <p>
202    * Header value is re-evaluated on each call to {@link #getValue()}.
203    *
204    * @param value
205    *    The supplier of the header value.
206    *    <br>Can be <jk>null</jk>.
207    */
208   public Via(Supplier<String[]> value) {
209      super(NAME, value);
210   }
211}