Example usage for org.apache.http.impl DefaultBHttpServerConnection receiveRequestHeader

List of usage examples for org.apache.http.impl DefaultBHttpServerConnection receiveRequestHeader

Introduction

In this page you can find the example usage for org.apache.http.impl DefaultBHttpServerConnection receiveRequestHeader.

Prototype

public HttpRequest receiveRequestHeader() throws HttpException, IOException 

Source Link

Usage

From source file:com.lion328.xenonlauncher.proxy.HttpDataHandler.java

@Override
public boolean process(Socket client, Socket server) throws Exception {
    InputStream clientIn = client.getInputStream();
    clientIn.mark(65536);//from   w  ww  . ja va  2s .c om

    try {
        DefaultBHttpServerConnection httpClient = new DefaultBHttpServerConnection(8192);
        httpClient.bind(client);
        httpClient.setSocketTimeout(timeout);

        DefaultBHttpClientConnection httpServer = new DefaultBHttpClientConnection(8192);
        httpServer.bind(server);

        HttpCoreContext context = HttpCoreContext.create();
        context.setAttribute("client.socket", client);
        context.setAttribute("server.socket", server);

        HttpEntityEnclosingRequest request;

        do {
            HttpRequest rawRequest = httpClient.receiveRequestHeader();

            if (rawRequest instanceof HttpEntityEnclosingRequest) {
                request = (HttpEntityEnclosingRequest) rawRequest;
            } else {
                request = new BasicHttpEntityEnclosingRequest(rawRequest.getRequestLine());
                request.setHeaders(rawRequest.getAllHeaders());
            }

            httpClient.receiveRequestEntity(request);

            HttpResponse response = new BasicHttpResponse(
                    new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));

            boolean sent = false;

            for (Map.Entry<Integer, HttpRequestHandler> entry : handlers.entrySet()) {
                entry.getValue().handle(request, response, context);

                if (context.getAttribute("response.set") instanceof HttpResponse) {
                    response = (HttpResponse) context.getAttribute("response.set");
                }

                if (context.getAttribute("pipeline.end") == Boolean.TRUE) {
                    break;
                }

                if (context.getAttribute("response.need-original") == Boolean.TRUE && !sent) {
                    httpServer.sendRequestHeader(request);
                    httpServer.sendRequestEntity(request);
                    response = httpServer.receiveResponseHeader();
                    httpServer.receiveResponseEntity(response);

                    entry.getValue().handle(request, response, context);

                    context.removeAttribute("response.need-original");
                    context.setAttribute("request.sent", true);

                    sent = true;
                }
            }

            if (context.getAttribute("response.sent") != Boolean.TRUE) {
                httpClient.sendResponseHeader(response);

                if (response.getEntity() != null) {
                    httpClient.sendResponseEntity(response);
                }
            }
        } while (request.getFirstHeader("Connection").getValue().equals("keep-alive"));

        return true;
    } catch (ProtocolException e) {
        clientIn.reset();
        return false;
    } catch (ConnectionClosedException e) {
        return true;
    }
}

From source file:jscover.server.PersistentStaticHttpServer.java

public void run() {
    DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024);
    try {/*from  w w w  . j av  a2s. co m*/
        conn.bind(socket);
        try {
            boolean keepAlive = true;
            while (keepAlive && !socket.isClosed()) {
                // fully read the request, whatever it is
                HttpRequest request = conn.receiveRequestHeader();
                logger.log(FINE, "Received request: {0}", request);
                keepAlive = isKeepAlive(request);

                if (request instanceof HttpEntityEnclosingRequest) {
                    conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                    if (entity != null) {
                        // consume all content to allow reuse
                        EntityUtils.consume(entity);
                    }
                }

                // send static content or reject the method
                String method = request.getRequestLine().getMethod();
                if (method.matches("(?i)get|post|put"))
                    sendOkContent(conn);
                else
                    rejectMethod(conn);
            }
        } finally {
            IOUtils.closeQuietly(conn);
            IOUtils.closeQuietly(socket);
        }
    } catch (HttpException e) {
        e.printStackTrace();
        IOUtils.closeQuietly(socket);
    } catch (IOException e) {
        e.printStackTrace();
        IOUtils.closeQuietly(socket);
    }
}