Path: blob/master/src/java.net.http/share/classes/jdk/internal/net/http/HeaderParser.java
41171 views
/*1* Copyright (c) 2015, 2018, 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 jdk.internal.net.http;2627import java.util.Iterator;28import java.util.Locale;29import java.util.NoSuchElementException;3031/* This is useful for the nightmare of parsing multi-part HTTP/RFC822 headers32* sensibly:33* From a String like: 'timeout=15, max=5'34* create an array of Strings:35* { {"timeout", "15"},36* {"max", "5"}37* }38* From one like: 'Basic Realm="FuzzFace" Foo="Biz Bar Baz"'39* create one like (no quotes in literal):40* { {"basic", null},41* {"realm", "FuzzFace"}42* {"foo", "Biz Bar Baz"}43* }44* keys are converted to lower case, vals are left as is....45*/46class HeaderParser {4748/* table of key/val pairs */49String raw;50String[][] tab;51int nkeys;52int asize = 10; // initial size of array is 105354public HeaderParser(String raw) {55this.raw = raw;56tab = new String[asize][2];57parse();58}5960// private HeaderParser () { }6162// /**63// * Creates a new HeaderParser from this, whose keys (and corresponding64// * values) range from "start" to "end-1"65// */66// public HeaderParser subsequence(int start, int end) {67// if (start == 0 && end == nkeys) {68// return this;69// }70// if (start < 0 || start >= end || end > nkeys) {71// throw new IllegalArgumentException("invalid start or end");72// }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// }8081private void parse() {8283if (raw != null) {84raw = raw.trim();85char[] ca = raw.toCharArray();86int beg = 0, end = 0, i = 0;87boolean inKey = true;88boolean inQuote = false;89int len = ca.length;90while (end < len) {91char c = ca[end];92if ((c == '=') && !inQuote) { // end of a key93tab[i][0] = new String(ca, beg, end-beg).toLowerCase(Locale.US);94inKey = false;95end++;96beg = end;97} else if (c == '\"') {98if (inQuote) {99tab[i++][1]= new String(ca, beg, end-beg);100inQuote=false;101do {102end++;103} while (end < len && (ca[end] == ' ' || ca[end] == ','));104inKey=true;105beg=end;106} else {107inQuote=true;108end++;109beg=end;110}111} else if (c == ' ' || c == ',') { // end key/val, of whatever we're in112if (inQuote) {113end++;114continue;115} else if (inKey) {116tab[i++][0] = (new String(ca, beg, end-beg)).toLowerCase(Locale.US);117} else {118tab[i++][1] = (new String(ca, beg, end-beg));119}120while (end < len && (ca[end] == ' ' || ca[end] == ',')) {121end++;122}123inKey = true;124beg = end;125} else {126end++;127}128if (i == asize) {129asize = asize * 2;130String[][] ntab = new String[asize][2];131System.arraycopy (tab, 0, ntab, 0, tab.length);132tab = ntab;133}134}135// get last key/val, if any136if (--end > beg) {137if (!inKey) {138if (ca[end] == '\"') {139tab[i++][1] = (new String(ca, beg, end-beg));140} else {141tab[i++][1] = (new String(ca, beg, end-beg+1));142}143} else {144tab[i++][0] = (new String(ca, beg, end-beg+1)).toLowerCase(Locale.US);145}146} else if (end == beg) {147if (!inKey) {148if (ca[end] == '\"') {149tab[i++][1] = String.valueOf(ca[end-1]);150} else {151tab[i++][1] = String.valueOf(ca[end]);152}153} else {154tab[i++][0] = String.valueOf(ca[end]).toLowerCase(Locale.US);155}156}157nkeys=i;158}159}160161public String findKey(int i) {162if (i < 0 || i > asize) {163return null;164}165return tab[i][0];166}167168public String findValue(int i) {169if (i < 0 || i > asize) {170return null;171}172return tab[i][1];173}174175public String findValue(String key) {176return findValue(key, null);177}178179public String findValue(String k, String Default) {180if (k == null) {181return Default;182}183k = k.toLowerCase(Locale.US);184for (int i = 0; i < asize; ++i) {185if (tab[i][0] == null) {186return Default;187} else if (k.equals(tab[i][0])) {188return tab[i][1];189}190}191return Default;192}193194class ParserIterator implements Iterator<String> {195int index;196boolean returnsValue; // or key197198ParserIterator (boolean returnValue) {199returnsValue = returnValue;200}201@Override202public boolean hasNext () {203return index<nkeys;204}205@Override206public String next () {207if (index >= nkeys) {208throw new NoSuchElementException();209}210return tab[index++][returnsValue?1:0];211}212}213214public Iterator<String> keys () {215return new ParserIterator (false);216}217218// public Iterator<String> values () {219// return new ParserIterator (true);220// }221222@Override223public String toString () {224Iterator<String> k = keys();225StringBuilder sb = new StringBuilder();226sb.append("{size=").append(asize).append(" nkeys=").append(nkeys)227.append(' ');228for (int i=0; k.hasNext(); i++) {229String key = k.next();230String val = findValue (i);231if (val != null && "".equals (val)) {232val = null;233}234sb.append(" {").append(key).append(val == null ? "" : "," + val)235.append('}');236if (k.hasNext()) {237sb.append (',');238}239}240sb.append (" }");241return sb.toString();242}243244// public int findInt(String k, int Default) {245// try {246// return Integer.parseInt(findValue(k, String.valueOf(Default)));247// } catch (Throwable t) {248// return Default;249// }250// }251}252253254