Example usage for org.apache.http.message ParserCursor ParserCursor

List of usage examples for org.apache.http.message ParserCursor ParserCursor

Introduction

In this page you can find the example usage for org.apache.http.message ParserCursor ParserCursor.

Prototype

public ParserCursor(int i, int i2) 

Source Link

Usage

From source file:com.subgraph.vega.internal.http.proxy.VegaHttpRequestParser.java

private RequestLine parseRequestLine(final SessionInputBuffer sessionBuffer)
        throws IOException, HttpException, ParseException {
    lineBuf.clear();/*from   w w w. jav  a 2  s  .  co  m*/
    int i = sessionBuffer.readLine(lineBuf);
    if (i == -1) {
        throw new ConnectionClosedException("Client closed connection");
    }
    ParserCursor cursor = new ParserCursor(0, lineBuf.length());
    return lineParser.parseRequestLine(lineBuf, cursor);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.HttpRequestParser.java

/**
 * Read and parse the request line.//  ww  w .  ja  v  a 2  s.co  m
 * 
 * @param parser HC LineParser.
 * @param builder HTTP request builder.
 * @param buf
 * @param bufCursor
 * @return
 * @throws URISyntaxException
 */
private int parseRequestLine(final LineParser parser, final IHttpRequestBuilder builder,
        final CharArrayBuffer buf, final ParserCursor bufCursor) throws URISyntaxException {
    final CharArrayBuffer lnBuf = new CharArrayBuffer(0);
    if (readLine(buf, bufCursor, lnBuf) < 1) {
        // no data! 
        return -1;
    }
    final ParserCursor lnCursor = new ParserCursor(0, lnBuf.length());

    String method, uri;
    ProtocolVersion version;
    if ((method = nextWord(lnBuf, lnCursor)) != null) {
        if ((uri = nextWord(lnBuf, lnCursor)) != null) {
            try {
                version = parser.parseProtocolVersion(lnBuf, lnCursor);
            } catch (ParseException e) {
                // treat the unparseable version as HTTP/1.1
                version = new ProtocolVersion("HTTP", 1, 1);
            }
            ;
        } else {
            uri = "";
            version = null;//new ProtocolVersion("HTTP", 0, 9);
        }
    } else {
        method = lnBuf.toString();
        uri = "";
        version = null;//new ProtocolVersion("HTTP", 0, 9);
    }

    builder.setMethod(method);
    builder.setFromUri(new URI(uri));
    builder.setProtocolVersion(version);

    return 0;
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

/**
 * Parses parameters with the given parser.
 *
 * @param value  the parameter list to parse
 * @param parser the parser to use, or <code>null</code> for default
 * @return array holding the parameters, never <code>null</code>
 *///from  ww  w.  jav  a2 s  . c  om
public static NameValuePair[] parseParameters(final String value, final HeaderValueParser parser)
        throws ParseException {
    AssertUtils.notNull(value, "Value");

    final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    final ParserCursor cursor = new ParserCursor(0, value.length());
    return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseParameters(buffer, cursor);
}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public List<InetAddressRange> parseAll(final String s) throws ParseException, UnknownHostException {
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);/* w ww  .ja va  2 s  . co m*/
    ParserCursor cursor = new ParserCursor(0, s.length());
    return parseAll(buffer, cursor, null);
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

/**
 * Parses a name-value-pair with the given parser.
 *
 * @param value  the NVP to parse// w ww  .jav a2s. c  om
 * @param parser the parser to use, or <code>null</code> for default
 * @return the parsed name-value pair
 */
public static NameValuePair parseNameValuePair(final String value, final HeaderValueParser parser)
        throws ParseException {
    AssertUtils.notNull(value, "Value");

    final CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    final ParserCursor cursor = new ParserCursor(0, value.length());
    return (parser != null ? parser : BasicHeaderValueParser.INSTANCE).parseNameValuePair(buffer, cursor);
}

From source file:org.vietspider.net.apache.DefaultResponseParser.java

protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
    // clear the buffer
    this.lineBuf.clear();
    //read out the HTTP status string
    int count = 0;
    ParserCursor cursor = null;/*www . j  av  a2s.c om*/

    //    long start = System.currentTimeMillis();
    //    HeaderReaderTimer timer =  null;
    //    if(timeoutSocket) timer = new HeaderReaderTimer(sessionBuffer);
    //    try {
    //      if(timer != null) new Thread(timer).start();
    do {
        int i = sessionBuffer.readLine(this.lineBuf);
        if (i == -1 && count == 0) {
            // The server just dropped connection on us
            throw new NoHttpResponseException("The target server failed to respond");
        }
        cursor = new ParserCursor(0, this.lineBuf.length());
        if (lineParser.hasProtocolVersion(this.lineBuf, cursor)) {
            // Got one
            break;
        } else if (i == -1 || count >= this.maxGarbageLines) {
            // Giving up
            throw new ProtocolException("The server failed to respond with a valid HTTP response");
        }
        //        else if(timer != null && timer.isDead()) {
        //          throw new NoHttpResponseException("The target server failed to respond");
        //        }
        count++;
    } while (true);
    //    } finally {
    //      if(timer != null)  timer.closeTimer();
    ////      long end = System.currentTimeMillis();
    ////      System.out.println(" ihihihi "+ sessionBuffer.hashCode()+ " / "+(end - start));
    //    }

    //create the status line from the status string
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}

From source file:com.android.idtt.http.client.util.URLEncodedUtils.java

/**
 * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding./*from  www .  j  a  v a  2  s. com*/
 *
 * @param s       text to parse.
 * @param charset Encoding to use when decoding the parameters.
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s, final Charset charset) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
        }
    }
    return list;
}

From source file:com.soundcloud.playerapi.OAuth2Scheme.java

@Override
public void processChallenge(Header header) throws MalformedChallengeException {
    if (header == null) {
        throw new IllegalArgumentException("Header may not be null");
    }//from w  ww . ja  v a2  s  . c  om
    String authHeader = header.getName();
    if (!authHeader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
        throw new MalformedChallengeException("Unexpected header name: " + authHeader);
    }

    CharArrayBuffer buffer;
    int pos;
    if (header instanceof FormattedHeader) {
        buffer = ((FormattedHeader) header).getBuffer();
        pos = ((FormattedHeader) header).getValuePos();
    } else {
        String s = header.getValue();
        if (s == null) {
            throw new MalformedChallengeException("Header value is null");
        }
        buffer = new CharArrayBuffer(s.length());
        buffer.append(s);
        pos = 0;
    }
    while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
        pos++;
    }
    int beginIndex = pos;
    while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
        pos++;
    }
    int endIndex = pos;
    String s = buffer.substring(beginIndex, endIndex);
    if (!s.equalsIgnoreCase(getSchemeName())) {
        throw new MalformedChallengeException("Invalid scheme identifier: " + s);
    }
    HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    ParserCursor cursor = new ParserCursor(pos, buffer.length());
    HeaderElement[] elements = parser.parseElements(buffer, cursor);
    if (elements.length == 0) {
        throw new MalformedChallengeException("Authentication challenge is empty");
    }
    for (HeaderElement element : elements) {
        this.mParams.put(element.getName(), element.getValue());
    }
}

From source file:com.nominanuda.web.http.HttpCoreHelper.java

private HttpRequest deserializeRequest(InputStream is) throws IOException, HttpException {
    CharArrayBuffer lineBuf = readLine(is);
    final ParserCursor cursor = new ParserCursor(0, lineBuf.length());
    RequestLine requestline = lineParser.parseRequestLine(lineBuf, cursor);
    HttpRequest req = createRequest(requestline.getMethod(), requestline.getUri());
    fillMessageHeadersAndContent(req, is);
    return req;/* w w  w.  j a va2s  .c  om*/
}

From source file:com.sina.cloudstorage.util.URLEncodedUtils.java

/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding./*  w  w  w. j av a  2s.  c  om*/
 *
 * @param s
 *            text to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 *
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s, final Charset charset) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(decode(nvp.getName(), charset), decode(nvp.getValue(), charset)));
        }
    }
    return list;
}