Path: blob/master/src/jdk.httpserver/share/classes/com/sun/net/httpserver/Headers.java
41159 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.net.httpserver;2627import java.util.Collection;28import java.util.HashMap;29import java.util.LinkedList;30import java.util.List;31import java.util.Map;32import java.util.Set;3334/**35* HTTP request and response headers are represented by this class which36* implements the interface37* {@link java.util.Map}{@literal <}{@link java.lang.String},38* {@link java.util.List} {@literal <}{@link java.lang.String}{@literal >>}.39* The keys are case-insensitive Strings representing the header names and40* the value associated with each key is41* a {@link List}{@literal <}{@link String}{@literal >} with one42* element for each occurrence of the header name in the request or response.43*44* <p> For example, if a response header instance contains45* one key "HeaderName" with two values "value1 and value2"46* then this object is output as two header lines:47*48* <blockquote><pre>49* HeaderName: value150* HeaderName: value251* </pre></blockquote>52*53* All the normal {@link java.util.Map} methods are provided, but the54* following additional convenience methods are most likely to be used:55*56* <ul>57* <li>{@link #getFirst(String)} returns a single valued header or the first58* value of a multi-valued header.59* <li>{@link #add(String,String)} adds the given header value to the list60* for the given key.61* <li>{@link #set(String,String)} sets the given header field to the single62* value given overwriting any existing values in the value list.63* </ul>64*65* <p> All methods in this class accept {@code null} values for keys and values.66* However, {@code null} keys will never will be present in HTTP request67* headers, and will not be output/sent in response headers. Null values can be68* represented as either a {@code null} entry for the key (i.e. the list is69* {@code null}) or where the key has a list, but one (or more) of the list's70* values is {@code null}. Null values are output as a header line containing71* the key but no associated value.72*73* @since 1.674*/75public class Headers implements Map<String,List<String>> {7677HashMap<String,List<String>> map;7879/**80* Creates an empty instance of {@code Headers}.81*/82public Headers () {map = new HashMap<String,List<String>>(32);}8384/**85* Normalize the key by converting to following form.86* First {@code char} upper case, rest lower case.87* key is presumed to be {@code ASCII}.88*/89private String normalize (String key) {90if (key == null) {91return null;92}93int len = key.length();94if (len == 0) {95return key;96}97char[] b = key.toCharArray();98if (b[0] >= 'a' && b[0] <= 'z') {99b[0] = (char)(b[0] - ('a' - 'A'));100} else if (b[0] == '\r' || b[0] == '\n')101throw new IllegalArgumentException("illegal character in key");102103for (int i=1; i<len; i++) {104if (b[i] >= 'A' && b[i] <= 'Z') {105b[i] = (char) (b[i] + ('a' - 'A'));106} else if (b[i] == '\r' || b[i] == '\n')107throw new IllegalArgumentException("illegal character in key");108}109return new String(b);110}111112public int size() {return map.size();}113114public boolean isEmpty() {return map.isEmpty();}115116public boolean containsKey(Object key) {117if (key == null) {118return false;119}120if (!(key instanceof String)) {121return false;122}123return map.containsKey (normalize((String)key));124}125126public boolean containsValue(Object value) {127return map.containsValue(value);128}129130public List<String> get(Object key) {131return map.get(normalize((String)key));132}133134/**135* Returns the first value from the {@link List} of {@code String}136* values for the given key (if at least one exists).137*138* @param key the key to search for139* @return the first {@code String} value associated with the key140*/141public String getFirst (String key) {142List<String> l = map.get(normalize(key));143if (l == null) {144return null;145}146return l.get(0);147}148149public List<String> put(String key, List<String> value) {150for (String v : value)151checkValue(v);152return map.put (normalize(key), value);153}154155/**156* Adds the given value to the list of headers for the given key. If157* the mapping does not already exist, then it is created.158*159* @param key the header name160* @param value the value to add to the header161*/162public void add (String key, String value) {163checkValue(value);164String k = normalize(key);165List<String> l = map.get(k);166if (l == null) {167l = new LinkedList<String>();168map.put(k,l);169}170l.add (value);171}172173private static void checkValue(String value) {174int len = value.length();175for (int i=0; i<len; i++) {176char c = value.charAt(i);177if (c == '\r') {178// is allowed if it is followed by \n and a whitespace char179if (i >= len - 2) {180throw new IllegalArgumentException("Illegal CR found in header");181}182char c1 = value.charAt(i+1);183char c2 = value.charAt(i+2);184if (c1 != '\n') {185throw new IllegalArgumentException("Illegal char found after CR in header");186}187if (c2 != ' ' && c2 != '\t') {188throw new IllegalArgumentException("No whitespace found after CRLF in header");189}190i+=2;191} else if (c == '\n') {192throw new IllegalArgumentException("Illegal LF found in header");193}194}195}196197/**198* Sets the given value as the sole header value for the given199* key. If the mapping does not already exist, then it is created.200*201* @param key the header name202* @param value the header value to set203*/204public void set (String key, String value) {205LinkedList<String> l = new LinkedList<String>();206l.add (value);207put (key, l);208}209210211public List<String> remove(Object key) {212return map.remove(normalize((String)key));213}214215public void putAll(Map<? extends String,? extends List<String>> t) {216map.putAll (t);217}218219public void clear() {map.clear();}220221public Set<String> keySet() {return map.keySet();}222223public Collection<List<String>> values() {return map.values();}224225public Set<Map.Entry<String, List<String>>> entrySet() {226return map.entrySet();227}228229public boolean equals(Object o) {return map.equals(o);}230231public int hashCode() {return map.hashCode();}232}233234235