1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  */
  24 package java.net.http;
  25 
  26 import java.util.Iterator;
  27 import java.util.Locale;
  28 import java.util.NoSuchElementException;
  29 
  30 /* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers
  31  * sensibly:
  32  * From a String like: 'timeout=15, max=5'
  33  * create an array of Strings:
  34  * { {"timeout", "15"},
  35  *   {"max", "5"}
  36  * }
  37  * From one like: 'Basic Realm="FuzzFace" Foo="Biz Bar Baz"'
  38  * create one like (no quotes in literal):
  39  * { {"basic", null},
  40  *   {"realm", "FuzzFace"}
  41  *   {"foo", "Biz Bar Baz"}
  42  * }
  43  * keys are converted to lower case, vals are left as is....
  44  */
  45 class HeaderParser {
  46 
  47     /* table of key/val pairs */
  48     String raw;
  49     String[][] tab;
  50     int nkeys;
  51     int asize = 10; // initial size of array is 10
  52 
  53     public HeaderParser(String raw) {
  54         this.raw = raw;
  55         tab = new String[asize][2];
  56         parse();
  57     }
  58 
  59     private HeaderParser () { }
  60 
  61     /**
  62      * Creates a new HeaderParser from this, whose keys (and corresponding
  63      * values) range from "start" to "end-1"
  64      */
  65     public HeaderParser subsequence(int start, int end) {
  66         if (start == 0 && end == nkeys) {
  67             return this;
  68         }
  69         if (start < 0 || start >= end || end > nkeys)
  70             throw new IllegalArgumentException("invalid start or end");
  71         HeaderParser n = new HeaderParser();
  72         n.tab = new String [asize][2];
  73         n.asize = asize;
  74         System.arraycopy (tab, start, n.tab, 0, (end-start));
  75         n.nkeys= (end-start);
  76         return n;
  77     }
  78 
  79     private void parse() {
  80 
  81         if (raw != null) {
  82             raw = raw.trim();
  83             char[] ca = raw.toCharArray();
  84             int beg = 0, end = 0, i = 0;
  85             boolean inKey = true;
  86             boolean inQuote = false;
  87             int len = ca.length;
  88             while (end < len) {
  89                 char c = ca[end];
  90                 if ((c == '=') && !inQuote) { // end of a key
  91                     tab[i][0] = new String(ca, beg, end-beg).toLowerCase(Locale.US);
  92                     inKey = false;
  93                     end++;
  94                     beg = end;
  95                 } else if (c == '\"') {
  96                     if (inQuote) {
  97                         tab[i++][1]= new String(ca, beg, end-beg);
  98                         inQuote=false;
  99                         do {
 100                             end++;
 101                         } while (end < len && (ca[end] == ' ' || ca[end] == ','));
 102                         inKey=true;
 103                         beg=end;
 104                     } else {
 105                         inQuote=true;
 106                         end++;
 107                         beg=end;
 108                     }
 109                 } else if (c == ' ' || c == ',') { // end key/val, of whatever we're in
 110                     if (inQuote) {
 111                         end++;
 112                         continue;
 113                     } else if (inKey) {
 114                         tab[i++][0] = (new String(ca, beg, end-beg)).toLowerCase(Locale.US);
 115                     } else {
 116                         tab[i++][1] = (new String(ca, beg, end-beg));
 117                     }
 118                     while (end < len && (ca[end] == ' ' || ca[end] == ',')) {
 119                         end++;
 120                     }
 121                     inKey = true;
 122                     beg = end;
 123                 } else {
 124                     end++;
 125                 }
 126                 if (i == asize) {
 127                     asize = asize * 2;
 128                     String[][] ntab = new String[asize][2];
 129                     System.arraycopy (tab, 0, ntab, 0, tab.length);
 130                     tab = ntab;
 131                 }
 132             }
 133             // get last key/val, if any
 134             if (--end > beg) {
 135                 if (!inKey) {
 136                     if (ca[end] == '\"') {
 137                         tab[i++][1] = (new String(ca, beg, end-beg));
 138                     } else {
 139                         tab[i++][1] = (new String(ca, beg, end-beg+1));
 140                     }
 141                 } else {
 142                     tab[i++][0] = (new String(ca, beg, end-beg+1)).toLowerCase();
 143                 }
 144             } else if (end == beg) {
 145                 if (!inKey) {
 146                     if (ca[end] == '\"') {
 147                         tab[i++][1] = String.valueOf(ca[end-1]);
 148                     } else {
 149                         tab[i++][1] = String.valueOf(ca[end]);
 150                     }
 151                 } else {
 152                     tab[i++][0] = String.valueOf(ca[end]).toLowerCase();
 153                 }
 154             }
 155             nkeys=i;
 156         }
 157     }
 158 
 159     public String findKey(int i) {
 160         if (i < 0 || i > asize)
 161             return null;
 162         return tab[i][0];
 163     }
 164 
 165     public String findValue(int i) {
 166         if (i < 0 || i > asize)
 167             return null;
 168         return tab[i][1];
 169     }
 170 
 171     public String findValue(String key) {
 172         return findValue(key, null);
 173     }
 174 
 175     public String findValue(String k, String Default) {
 176         if (k == null)
 177             return Default;
 178         k = k.toLowerCase(Locale.US);
 179         for (int i = 0; i < asize; ++i) {
 180             if (tab[i][0] == null) {
 181                 return Default;
 182             } else if (k.equals(tab[i][0])) {
 183                 return tab[i][1];
 184             }
 185         }
 186         return Default;
 187     }
 188 
 189     class ParserIterator implements Iterator<String> {
 190         int index;
 191         boolean returnsValue; // or key
 192 
 193         ParserIterator (boolean returnValue) {
 194             returnsValue = returnValue;
 195         }
 196         @Override
 197         public boolean hasNext () {
 198             return index<nkeys;
 199         }
 200         @Override
 201         public String next () {
 202             if (index >= nkeys)
 203                 throw new NoSuchElementException();
 204             return tab[index++][returnsValue?1:0];
 205         }
 206     }
 207 
 208     public Iterator<String> keys () {
 209         return new ParserIterator (false);
 210     }
 211 
 212     public Iterator<String> values () {
 213         return new ParserIterator (true);
 214     }
 215 
 216     @Override
 217     public String toString () {
 218         Iterator<String> k = keys();
 219         StringBuilder sb = new StringBuilder();
 220         sb.append("{size=").append(asize).append(" nkeys=").append(nkeys)
 221                 .append(' ');
 222         for (int i=0; k.hasNext(); i++) {
 223             String key = k.next();
 224             String val = findValue (i);
 225             if (val != null && "".equals (val)) {
 226                 val = null;
 227             }
 228             sb.append(" {").append(key).append(val == null ? "" : "," + val)
 229                     .append('}');
 230             if (k.hasNext()) {
 231                 sb.append (',');
 232             }
 233         }
 234         sb.append (" }");
 235         return sb.toString();
 236     }
 237 
 238     public int findInt(String k, int Default) {
 239         try {
 240             return Integer.parseInt(findValue(k, String.valueOf(Default)));
 241         } catch (Throwable t) {
 242             return Default;
 243         }
 244     }
 245 }