001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.cookie;
029
030import java.util.ArrayList;
031import java.util.BitSet;
032import java.util.List;
033
034import org.apache.http.HeaderElement;
035import org.apache.http.NameValuePair;
036import org.apache.http.ParseException;
037import org.apache.http.annotation.Contract;
038import org.apache.http.annotation.ThreadingBehavior;
039import org.apache.http.message.BasicHeaderElement;
040import org.apache.http.message.BasicNameValuePair;
041import org.apache.http.message.ParserCursor;
042import org.apache.http.message.TokenParser;
043import org.apache.http.util.Args;
044import org.apache.http.util.CharArrayBuffer;
045
046/**
047 *
048 * @since 4.0
049 */
050@Contract(threading = ThreadingBehavior.IMMUTABLE)
051public class NetscapeDraftHeaderParser {
052
053    public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser();
054
055    private final static char PARAM_DELIMITER                = ';';
056
057    // IMPORTANT!
058    // These private static variables must be treated as immutable and never exposed outside this class
059    private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER);
060    private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER);
061
062    private final TokenParser tokenParser;
063
064    public NetscapeDraftHeaderParser() {
065        super();
066        this.tokenParser = TokenParser.INSTANCE;
067    }
068
069    public HeaderElement parseHeader(
070            final CharArrayBuffer buffer,
071            final ParserCursor cursor) throws ParseException {
072        Args.notNull(buffer, "Char array buffer");
073        Args.notNull(cursor, "Parser cursor");
074        final NameValuePair nvp = parseNameValuePair(buffer, cursor);
075        final List<NameValuePair> params = new ArrayList<NameValuePair>();
076        while (!cursor.atEnd()) {
077            final NameValuePair param = parseNameValuePair(buffer, cursor);
078            params.add(param);
079        }
080        return new BasicHeaderElement(
081                nvp.getName(),
082                nvp.getValue(), params.toArray(new NameValuePair[params.size()]));
083    }
084
085    private NameValuePair parseNameValuePair(
086            final CharArrayBuffer buffer, final ParserCursor cursor) {
087        final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
088        if (cursor.atEnd()) {
089            return new BasicNameValuePair(name, null);
090        }
091        final int delim = buffer.charAt(cursor.getPos());
092        cursor.updatePos(cursor.getPos() + 1);
093        if (delim != '=') {
094            return new BasicNameValuePair(name, null);
095        }
096        final String value = tokenParser.parseToken(buffer, cursor, VALUE_DELIMS);
097        if (!cursor.atEnd()) {
098            cursor.updatePos(cursor.getPos() + 1);
099        }
100        return new BasicNameValuePair(name, value);
101    }
102
103}