src/java.base/share/classes/sun/net/www/MessageHeader.java
author dfuchs
Fri, 30 Aug 2019 15:42:27 +0100
branchJDK-8229867-branch
changeset 57968 8595871a5446
parent 57713 0211b062843d
permissions -rw-r--r--
JDK-8229867: first prototype
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
     2
 * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *      news stream opener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.net.www;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Collections;
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    34
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/** An RFC 844 or MIME message header.  Includes methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    for parsing headers from incoming streams, fetching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    values, setting values, and printing headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    Key values of null are legal: they indicate lines in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    the header that don't have a valid key, but do have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    a value (this isn't legal according to the standard,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    but lines like this are everywhere). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class MessageHeader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private String keys[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private String values[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private int nkeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public MessageHeader () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public MessageHeader (InputStream is) throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        parseHeader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
17473
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    58
     * Returns list of header names in a comma separated list
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    59
     */
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    60
    public synchronized String getHeaderNamesInList() {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    61
        StringJoiner joiner = new StringJoiner(",");
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    62
        for (int i=0; i<nkeys; i++) {
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    63
            joiner.add(keys[i]);
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    64
        }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    65
        return joiner.toString();
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    66
    }
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    67
35cd9b3a98ff 8010464: Evolve java networking same origin policy
michaelm
parents: 14766
diff changeset
    68
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Reset a message header (all key/values removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public synchronized void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        keys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        values = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        nkeys = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Find the value that corresponds to this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * It finds only the first occurrence of the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param k the key to find.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @return null if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public synchronized String findValue(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                if (keys[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            for (int i = nkeys; --i >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (k.equalsIgnoreCase(keys[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // return the location of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public synchronized int getKey(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if ((keys[i] == k) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                (k != null && k.equalsIgnoreCase(keys[i])))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public synchronized String getKey(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (n < 0 || n >= nkeys) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return keys[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public synchronized String getValue(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (n < 0 || n >= nkeys) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return values[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /** Deprecated: Use multiValueIterator() instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *  Find the next value that corresponds to this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *  It finds the first value that follows v. To iterate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  over all the values of a key use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *          for(String v=h.findValue(k); v!=null; v=h.findNextValue(k, v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *              ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public synchronized String findNextValue(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        boolean foundV = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (keys[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (foundV)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    else if (values[i] == v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        foundV = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (k.equalsIgnoreCase(keys[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    if (foundV)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        return values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    else if (values[i] == v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        foundV = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
14766
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   146
    /**
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   147
     * Removes bare Negotiate and Kerberos headers when an "NTLM ..."
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   148
     * appears. All Performed on headers with key being k.
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   149
     * @return true if there is a change
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   150
     */
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   151
    public boolean filterNTLMResponses(String k) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   152
        boolean found = false;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   153
        for (int i=0; i<nkeys; i++) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   154
            if (k.equalsIgnoreCase(keys[i])
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   155
                    && values[i] != null && values[i].length() > 5
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   156
                    && values[i].substring(0, 5).equalsIgnoreCase("NTLM ")) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   157
                found = true;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   158
                break;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   159
            }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   160
        }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   161
        if (found) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   162
            int j = 0;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   163
            for (int i=0; i<nkeys; i++) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   164
                if (k.equalsIgnoreCase(keys[i]) && (
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   165
                        "Negotiate".equalsIgnoreCase(values[i]) ||
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   166
                        "Kerberos".equalsIgnoreCase(values[i]))) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   167
                    continue;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   168
                }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   169
                if (i != j) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   170
                    keys[j] = keys[i];
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   171
                    values[j] = values[i];
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   172
                }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   173
                j++;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   174
            }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   175
            if (j != nkeys) {
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   176
                nkeys = j;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   177
                return true;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   178
            }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   179
        }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   180
        return false;
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   181
    }
1f22dd6f2658 8003948: NTLM/Negotiate authentication problem
michaelm
parents: 10596
diff changeset
   182
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   183
    class HeaderIterator implements Iterator<String> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int next = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        boolean haveNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        public HeaderIterator (String k, Object lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            key = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            this.lock = lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        public boolean hasNext () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (haveNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                while (index < nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    if (key.equalsIgnoreCase (keys[index])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        haveNext = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        next = index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   210
        public String next() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (haveNext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    haveNext = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    return values [next];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    throw new NoSuchElementException ("No more elements");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public void remove () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new UnsupportedOperationException ("remove not allowed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * return an Iterator that returns all values of a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * key in sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   232
    public Iterator<String> multiValueIterator (String k) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return new HeaderIterator (k, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   236
    public synchronized Map<String, List<String>> getHeaders() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return getHeaders(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   240
    public synchronized Map<String, List<String>> getHeaders(String[] excludeList) {
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   241
        return filterAndAddHeaders(excludeList, null);
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   242
    }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   243
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   244
    public synchronized Map<String, List<String>> filterAndAddHeaders(
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   245
            String[] excludeList, Map<String, List<String>>  include) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean skipIt = false;
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   247
        Map<String, List<String>> m = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (int i = nkeys; --i >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (excludeList != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                // check if the key is in the excludeList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                // if so, don't include it in the Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                for (int j = 0; j < excludeList.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    if ((excludeList[j] != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        (excludeList[j].equalsIgnoreCase(keys[i]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        skipIt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (!skipIt) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   261
                List<String> l = m.get(keys[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (l == null) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   263
                    l = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    m.put(keys[i], l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                l.add(values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                // reset the flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                skipIt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   273
        if (include != null) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   274
                for (Map.Entry<String,List<String>> entry: include.entrySet()) {
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   275
                List<String> l = m.get(entry.getKey());
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   276
                if (l == null) {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   277
                    l = new ArrayList<>();
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   278
                    m.put(entry.getKey(), l);
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   279
                }
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   280
                l.addAll(entry.getValue());
6876
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   281
            }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   282
        }
13fdbd146659 6980004: limit HTTP request cookie headers in HttpURLConnection
michaelm
parents: 5506
diff changeset
   283
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   284
        for (String key : m.keySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   285
            m.put(key, Collections.unmodifiableList(m.get(key)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return Collections.unmodifiableMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
57713
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   291
    /** Check if a line of message header looks like a request line.
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   292
     * This method does not perform a full validation but simply
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   293
     * returns false if the line does not end with 'HTTP/[1-9].[0-9]'
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   294
     * @param line the line to check.
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   295
     * @return true if the line might be a request line.
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   296
     */
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   297
    private boolean isRequestline(String line) {
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   298
        String k = line.trim();
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   299
        int i = k.lastIndexOf(' ');
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   300
        if (i <= 0) return false;
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   301
        int len = k.length();
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   302
        if (len - i < 9) return false;
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   303
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   304
        char c1 = k.charAt(len-3);
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   305
        char c2 = k.charAt(len-2);
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   306
        char c3 = k.charAt(len-1);
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   307
        if (c1 < '1' || c1 > '9') return false;
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   308
        if (c2 != '.') return false;
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   309
        if (c3 < '0' || c3 > '9') return false;
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   310
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   311
        return (k.substring(i+1, len-3).equalsIgnoreCase("HTTP/"));
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   312
    }
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   313
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   314
    /** Prints the key-value pairs represented by this
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   315
        header. Also prints the RFC required blank line
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   316
        at the end. Omits pairs with a null key. Omits
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   317
        colon if key-value pair is the requestline. */
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   318
    public void print(PrintStream p) {
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   319
        // no synchronization: use cloned arrays instead.
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   320
        String[] k; String[] v; int n;
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   321
        synchronized (this) { n = nkeys; k = keys.clone(); v = values.clone(); }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   322
        print(n, k, v, p);
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   323
    }
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   324
57713
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   325
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /** Prints the key-value pairs represented by this
57713
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   327
        header. Also prints the RFC required blank line
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   328
        at the end. Omits pairs with a null key. Omits
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   329
        colon if key-value pair is the requestline. */
57968
8595871a5446 JDK-8229867: first prototype
dfuchs
parents: 57713
diff changeset
   330
    private  void print(int nkeys, String[] keys, String[] values, PrintStream p) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        for (int i = 0; i < nkeys; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (keys[i] != null) {
57713
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   333
                StringBuilder sb = new StringBuilder(keys[i]);
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   334
                if (values[i] != null) {
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   335
                    sb.append(": " + values[i]);
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   336
                } else if (i != 0 || !isRequestline(keys[i])) {
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   337
                    sb.append(":");
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   338
                }
0211b062843d 8185898: setRequestProperty(key, null) results in HTTP header without colon in request
michaelm
parents: 47216
diff changeset
   339
                p.print(sb.append("\r\n"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        p.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        p.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /** Adds a key value pair to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        header.  Duplicates are allowed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public synchronized void add(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        keys[nkeys] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        values[nkeys] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        nkeys++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /** Prepends a key value pair to the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        header.  Duplicates are allowed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public synchronized void prepend(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        for (int i = nkeys; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            keys[i] = keys[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            values[i] = values[i-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        keys[0] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        values[0] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        nkeys++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /** Overwrite the previous key/val pair at location 'i'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * with the new k/v.  If the index didn't exist before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * the key/val is simply tacked onto the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public synchronized void set(int i, String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (i < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else if (i >= nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            keys[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            values[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /** grow the key/value arrays as needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (keys == null || nkeys >= keys.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            String[] nk = new String[nkeys + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            String[] nv = new String[nkeys + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (keys != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                System.arraycopy(keys, 0, nk, 0, nkeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (values != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                System.arraycopy(values, 0, nv, 0, nkeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            keys = nk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            values = nv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Remove the key from the header. If there are multiple values under
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * the same key, they are all removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Nothing is done if the key doesn't exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * After a remove, the other pairs' order are not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param k the key to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public synchronized void remove(String k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if(k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            for (int i = 0; i < nkeys; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                while (keys[i] == null && i < nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    for(int j=i; j<nkeys-1; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        keys[j] = keys[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        values[j] = values[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    nkeys--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            for (int i = 0; i < nkeys; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                while (k.equalsIgnoreCase(keys[i]) && i < nkeys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    for(int j=i; j<nkeys-1; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        keys[j] = keys[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        values[j] = values[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    nkeys--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /** Sets the value of a key.  If the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        exists in the header, it's value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        changed.  Otherwise a new key/value pair will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        be added to the end of the header. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public synchronized void set(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        for (int i = nkeys; --i >= 0;)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (k.equalsIgnoreCase(keys[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                values[i] = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /** Set's the value of a key only if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *  key with that value already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public synchronized void setIfNotSet(String k, String v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (findValue(k) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /** Convert a message-id string to canonical form (strips off
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 29986
diff changeset
   455
        leading and trailing {@literal <>s}) */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public static String canonicalID(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (id == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        int st = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int len = id.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        boolean substr = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        while (st < len && ((c = id.charAt(st)) == '<' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            c <= ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            st++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            substr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        while (st < len && ((c = id.charAt(len - 1)) == '>' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            c <= ' ')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            substr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return substr ? id.substring(st, len) : id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /** Parse a MIME header from an input stream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public void parseHeader(InputStream is) throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            nkeys = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        mergeHeader(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /** Parse and merge a MIME header from an input stream. */
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   485
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public void mergeHeader(InputStream is) throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (is == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        char s[] = new char[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        int firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        while (firstc != '\n' && firstc != '\r' && firstc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            int keyend = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            boolean inKey = firstc > ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            s[len++] = (char) firstc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    parseloop:{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                while ((c = is.read()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                      case ':':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        if (inKey && len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            keyend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                      case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        c = ' ';
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   507
                      /*fall through*/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                      case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                      case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                      case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                        firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        if (c == '\r' && firstc == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                            firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                            if (firstc == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        if (firstc == '\n' || firstc == '\r' || firstc > ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            break parseloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        /* continuation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    if (len >= s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        char ns[] = new char[s.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        System.arraycopy(s, 0, ns, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        s = ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    s[len++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                firstc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            while (len > 0 && s[len - 1] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            String k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (keyend <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                keyend = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                k = String.copyValueOf(s, 0, keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (keyend < len && s[keyend] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                while (keyend < len && s[keyend] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            String v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (keyend >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                v = new String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                v = String.copyValueOf(s, keyend, len - keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            add(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        String result = super.toString() + nkeys + " pairs: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        for (int i = 0; i < keys.length && i < nkeys; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            result += "{"+keys[i]+": "+values[i]+"}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
}