michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.cookie;
michael@0:
michael@0: import java.util.ArrayList;
michael@0: import java.util.List;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.HeaderElement;
michael@0: import ch.boye.httpclientandroidlib.NameValuePair;
michael@0: import ch.boye.httpclientandroidlib.ParseException;
michael@0: import ch.boye.httpclientandroidlib.message.BasicHeaderElement;
michael@0: import ch.boye.httpclientandroidlib.message.BasicNameValuePair;
michael@0: import ch.boye.httpclientandroidlib.message.ParserCursor;
michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP;
michael@0: import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
michael@0:
michael@0: /**
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @Immutable
michael@0: public class NetscapeDraftHeaderParser {
michael@0:
michael@0: public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser();
michael@0:
michael@0: public NetscapeDraftHeaderParser() {
michael@0: super();
michael@0: }
michael@0:
michael@0: public HeaderElement parseHeader(
michael@0: final CharArrayBuffer buffer,
michael@0: final ParserCursor cursor) throws ParseException {
michael@0: if (buffer == null) {
michael@0: throw new IllegalArgumentException("Char array buffer may not be null");
michael@0: }
michael@0: if (cursor == null) {
michael@0: throw new IllegalArgumentException("Parser cursor may not be null");
michael@0: }
michael@0: NameValuePair nvp = parseNameValuePair(buffer, cursor);
michael@0: List params = new ArrayList();
michael@0: while (!cursor.atEnd()) {
michael@0: NameValuePair param = parseNameValuePair(buffer, cursor);
michael@0: params.add(param);
michael@0: }
michael@0: return new BasicHeaderElement(
michael@0: nvp.getName(),
michael@0: nvp.getValue(), params.toArray(new NameValuePair[params.size()]));
michael@0: }
michael@0:
michael@0: private NameValuePair parseNameValuePair(
michael@0: final CharArrayBuffer buffer, final ParserCursor cursor) {
michael@0: boolean terminated = false;
michael@0:
michael@0: int pos = cursor.getPos();
michael@0: int indexFrom = cursor.getPos();
michael@0: int indexTo = cursor.getUpperBound();
michael@0:
michael@0: // Find name
michael@0: String name = null;
michael@0: while (pos < indexTo) {
michael@0: char ch = buffer.charAt(pos);
michael@0: if (ch == '=') {
michael@0: break;
michael@0: }
michael@0: if (ch == ';') {
michael@0: terminated = true;
michael@0: break;
michael@0: }
michael@0: pos++;
michael@0: }
michael@0:
michael@0: if (pos == indexTo) {
michael@0: terminated = true;
michael@0: name = buffer.substringTrimmed(indexFrom, indexTo);
michael@0: } else {
michael@0: name = buffer.substringTrimmed(indexFrom, pos);
michael@0: pos++;
michael@0: }
michael@0:
michael@0: if (terminated) {
michael@0: cursor.updatePos(pos);
michael@0: return new BasicNameValuePair(name, null);
michael@0: }
michael@0:
michael@0: // Find value
michael@0: String value = null;
michael@0: int i1 = pos;
michael@0:
michael@0: while (pos < indexTo) {
michael@0: char ch = buffer.charAt(pos);
michael@0: if (ch == ';') {
michael@0: terminated = true;
michael@0: break;
michael@0: }
michael@0: pos++;
michael@0: }
michael@0:
michael@0: int i2 = pos;
michael@0: // Trim leading white spaces
michael@0: while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) {
michael@0: i1++;
michael@0: }
michael@0: // Trim trailing white spaces
michael@0: while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) {
michael@0: i2--;
michael@0: }
michael@0: value = buffer.substring(i1, i2);
michael@0: if (terminated) {
michael@0: pos++;
michael@0: }
michael@0: cursor.updatePos(pos);
michael@0: return new BasicNameValuePair(name, value);
michael@0: }
michael@0:
michael@0: }