001package org.json;
002
003/*
004Copyright (c) 2002 JSON.org
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016The Software shall be used for Good, not Evil.
017
018THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
021AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
023OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
024SOFTWARE.
025*/
026
027/**
028 * The HTTPTokener extends the JSONTokener to provide additional methods
029 * for the parsing of HTTP headers.
030 * @author JSON.org
031 * @version 2015-12-09
032 */
033public class HTTPTokener extends JSONTokener {
034
035    /**
036     * Construct an HTTPTokener from a string.
037     * @param string A source string.
038     */
039    public HTTPTokener(String string) {
040        super(string);
041    }
042
043
044    /**
045     * Get the next token or string. This is used in parsing HTTP headers.
046     * @throws JSONException
047     * @return A String.
048     */
049    public String nextToken() throws JSONException {
050        char c;
051        char q;
052        StringBuilder sb = new StringBuilder();
053        do {
054            c = next();
055        } while (Character.isWhitespace(c));
056        if (c == '"' || c == '\'') {
057            q = c;
058            for (;;) {
059                c = next();
060                if (c < ' ') {
061                    throw syntaxError("Unterminated string.");
062                }
063                if (c == q) {
064                    return sb.toString();
065                }
066                sb.append(c);
067            }
068        }
069        for (;;) {
070            if (c == 0 || Character.isWhitespace(c)) {
071                return sb.toString();
072            }
073            sb.append(c);
074            c = next();
075        }
076    }
077}