Example usage for org.apache.http.io SessionInputBuffer readLine

List of usage examples for org.apache.http.io SessionInputBuffer readLine

Introduction

In this page you can find the example usage for org.apache.http.io SessionInputBuffer readLine.

Prototype

int readLine(CharArrayBuffer charArrayBuffer) throws IOException;

Source Link

Usage

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

public static Header[] parseHeaders(final SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen,
        LineParser parser) throws HttpException, IOException {

    if (inbuffer == null) {
        throw new IllegalArgumentException("Session input buffer may not be null");
    }/*from   w ww. j av  a2 s . com*/

    if (parser == null)
        parser = BasicLineParser.DEFAULT;

    ArrayList<CharArrayBuffer> headerLines = new ArrayList<CharArrayBuffer>();

    CharArrayBuffer current = null;
    CharArrayBuffer previous = null;
    for (;;) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // Parse the header name and value
        // Check for folded headers first
        // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
        // discussion on folded headers
        if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int i = 0;
            while (i < current.length()) {
                char ch = current.charAt(i);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                i++;
            }
            if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, i, current.length() - i);
        } else {
            headerLines.add(current);
            previous = current;
            current = null;
        }

        if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }

    Header[] headers = new Header[headerLines.size()];
    for (int i = 0; i < headerLines.size(); i++) {
        CharArrayBuffer buffer = headerLines.get(i);
        try {
            headers[i] = parser.parseHeader(buffer);
        } catch (ParseException ex) {
            throw new ProtocolException(ex.getMessage());
        }
    }
    return headers;
}

From source file:net.swas.explorer.httpprofile.DefaultHttpResponseParser.java

@Override
protected HttpMessage parseHead(SessionInputBuffer sessionBuffer)
        throws IOException, HttpException, ParseException {

    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);

    if (i == -1) {

        throw new ConnectionClosedException("Client closed connection");

    }/*from   w w  w.  ja va  2 s.c  om*/

    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    StatusLine statusLine = this.lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusLine, null);

}

From source file:net.swas.explorer.httpprofile.DefaultHttpRequestParser.java

@Override
protected HttpRequest parseHead(final SessionInputBuffer sessionBuffer)
        throws IOException, HttpException, ParseException {

    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);
    if (i == -1) {

        throw new ConnectionClosedException("Client closed connection");

    }//  w w  w . j av  a2  s. c o  m

    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf, cursor);

    return this.requestFactory.newHttpRequest(requestline);

}

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  ww .ja  va2s .c  o 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: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;//from w  w  w .  ja va2s  .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:org.apache.http.impl.conn.DefaultHttpResponseParser.java

@Override
protected HttpResponse parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
    //read out the HTTP status string
    int count = 0;
    ParserCursor cursor = null;/*from ww w  . ja  va2  s.  c  o m*/
    do {
        // clear the buffer
        this.lineBuf.clear();
        final 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 || reject(this.lineBuf, count)) {
            // Giving up
            throw new ProtocolException("The server failed to respond with a " + "valid HTTP response");
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Garbage in response: " + this.lineBuf.toString());
        }
        count++;
    } while (true);
    //create the status line from the status string
    final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}

From source file:org.apache.http.impl.conn.DefaultResponseParser.java

@Override
protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException {
    //read out the HTTP status string
    int count = 0;
    ParserCursor cursor = null;/*from   www .jav a2  s .com*/
    do {
        // clear the buffer
        this.lineBuf.clear();
        final 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");
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Garbage in response: " + this.lineBuf.toString());
        }
        count++;
    } while (true);
    //create the status line from the status string
    final StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}