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