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  * questions.
  24  */
  25 
  26 package java.net.http;
  27 
  28 import java.util.Iterator;
  29 import java.util.Locale;
  30 import java.util.NoSuchElementException;
  31 
  32 /* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers
  33  * sensibly:
  34  * From a String like: 'timeout=15, max=5'
  35  * create an array of Strings:
  36  * { {"timeout", "15"},
  37  *   {"max", "5"}
  38  * }
  39  * From one like: 'Basic Realm="FuzzFace" Foo="Biz Bar Baz"'
  40  * create one like (no quotes in literal):
  41  * { {"basic", null},
  42  *   {"realm", "FuzzFace"}
  43  *   {"foo", "Biz Bar Baz"}
  44  * }
  45  * keys are converted to lower case, vals are left as is....
  46  */
  47 class HeaderParser {
  48 
  49     /* table of key/val pairs */
  50     String raw;
  51     String[][] tab;
  52     int nkeys;
  53     int asize = 10; // initial size of array is 10
  54 
  55     public HeaderParser(String raw) {
  56         this.raw = raw;
  57         tab = new String[asize][2];
  58         parse();
  59     }
  60 
  61     private HeaderParser () { }
  62 
  63     /**
  64      * Creates a new HeaderParser from this, whose keys (and corresponding
  65      * values) range from "start" to "end-1"
  66      */
  67     public HeaderParser subsequence(int start, int end) {
  68         if (start == 0 && end == nkeys) {
  69             return this;
  70         }
  71         if (start < 0 || start >= end || end > nkeys)
  72             throw new IllegalArgumentException("invalid start or end");
  73         HeaderParser n = new HeaderParser();
  74         n.tab = new String [asize][2];
  75         n.asize = asize;
  76         System.arraycopy (tab, start, n.tab, 0, (end-start));
  77         n.nkeys= (end-start);
  78         return n;
  79     }
  80 
  81     private void parse() {
  82 
  83         if (raw != null) {
  84             raw = raw.trim();
  85             char[] ca = raw.toCharArray();
  86             int beg = 0, end = 0, i = 0;
  87             boolean inKey = true;
  88             boolean inQuote = false;
  89             int len = ca.length;
  90             while (end < len) {
  91                 char c = ca[end];
  92                 if ((c == '=') && !inQuote) { // end of a key
  93                     tab[i][0] = new String(ca, beg, end-beg).toLowerCase(Locale.US);
  94                     inKey = false;
  95                     end++;
  96                     beg = end;
  97                 } else if (c == '\"') {
  98                     if (inQuote) {
  99                         tab[i++][1]= new String(ca, beg, end-beg);
 100                         inQuote=false;
 101                         do {
 102                             end++;
 103                         } while (end < len && (ca[end] == ' ' || ca[end] == ','));
 104                         inKey=true;
 105                         beg=end;
 106                     } else {
 107                         inQuote=true;
 108                         end++;
 109                         beg=end;
 110                     }
 111                 } else if (c == ' ' || c == ',') { // end key/val, of whatever we're in
 112                     if (inQuote) {
 113                         end++;
 114                         continue;
 115                     } else if (inKey) {
 116                         tab[i++][0] = (new String(ca, beg, end-beg)).toLowerCase(Locale.US);
 117                     } else {
 118                         tab[i++][1] = (new String(ca, beg, end-beg));
 119                     }
 120                     while (end < len && (ca[end] == ' ' || ca[end] == ',')) {
 121                         end++;
 122                     }
 123                     inKey = true;
 124                     beg = end;
 125                 } else {
 126                     end++;
 127                 }
 128                 if (i == asize) {
 129                     asize = asize * 2;
 130                     String[][] ntab = new String[asize][2];
 131                     System.arraycopy (tab, 0, ntab, 0, tab.length);
 132                     tab = ntab;
 133                 }
 134             }
 135             // get last key/val, if any
 136             if (--end > beg) {
 137                 if (!inKey) {
 138                     if (ca[end] == '\"') {
 139                         tab[i++][1] = (new String(ca, beg, end-beg));
 140                     } else {
 141                         tab[i++][1] = (new String(ca, beg, end-beg+1));
 142                     }
 143                 } else {
 144                     tab[i++][0] = (new String(ca, beg, end-beg+1)).toLowerCase();
 145                 }
 146             } else if (end == beg) {
 147                 if (!inKey) {
 148                     if (ca[end] == '\"') {
 149                         tab[i++][1] = String.valueOf(ca[end-1]);
 150                     } else {
 151                         tab[i++][1] = String.valueOf(ca[end]);
 152                     }
 153                 } else {
 154                     tab[i++][0] = String.valueOf(ca[end]).toLowerCase();
 155                 }
 156             }
 157             nkeys=i;
 158         }
 159     }
 160 
 161     public String findKey(int i) {
 162         if (i < 0 || i > asize)
 163             return null;
 164         return tab[i][0];
 165     }
 166 
 167     public String findValue(int i) {
 168         if (i < 0 || i > asize)
 169             return null;
 170         return tab[i][1];
 171     }
 172 
 173     public String findValue(String key) {
 174         return findValue(key, null);
 175     }
 176 
 177     public String findValue(String k, String Default) {
 178         if (k == null)
 179             return Default;
 180         k = k.toLowerCase(Locale.US);
 181         for (int i = 0; i < asize; ++i) {
 182             if (tab[i][0] == null) {
 183                 return Default;
 184             } else if (k.equals(tab[i][0])) {
 185                 return tab[i][1];
 186             }
 187         }
 188         return Default;
 189     }
 190 
 191     class ParserIterator implements Iterator<String> {
 192         int index;
 193         boolean returnsValue; // or key
 194 
 195         ParserIterator (boolean returnValue) {
 196             returnsValue = returnValue;
 197         }
 198         @Override
 199         public boolean hasNext () {
 200             return index<nkeys;
 201         }
 202         @Override
 203         public String next () {
 204             if (index >= nkeys)
 205                 throw new NoSuchElementException();
 206             return tab[index++][returnsValue?1:0];
 207         }
 208     }
 209 
 210     public Iterator<String> keys () {
 211         return new ParserIterator (false);
 212     }
 213 
 214     public Iterator<String> values () {
 215         return new ParserIterator (true);
 216     }
 217 
 218     @Override
 219     public String toString () {
 220         Iterator<String> k = keys();
 221         StringBuilder sb = new StringBuilder();
 222         sb.append("{size=").append(asize).append(" nkeys=").append(nkeys)
 223                 .append(' ');
 224         for (int i=0; k.hasNext(); i++) {
 225             String key = k.next();
 226             String val = findValue (i);
 227             if (val != null && "".equals (val)) {
 228                 val = null;
 229             }
 230             sb.append(" {").append(key).append(val == null ? "" : "," + val)
 231                     .append('}');
 232             if (k.hasNext()) {
 233                 sb.append (',');
 234             }
 235         }
 236         sb.append (" }");
 237         return sb.toString();
 238     }
 239 
 240     public int findInt(String k, int Default) {
 241         try {
 242             return Integer.parseInt(findValue(k, String.valueOf(Default)));
 243         } catch (Throwable t) {
 244             return Default;
 245         }
 246     }
 247 }