List of usage examples for org.apache.http.impl DefaultBHttpServerConnection DefaultBHttpServerConnection
public DefaultBHttpServerConnection(int i)
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 w w. j a v a2s . com*/ 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:us.pserver.revok.server.RevokServer.java
/** * Not invoke directly. Executes server routines. *//* ww w. ja v a2s.co m*/ @Override public void run() { // Stablish a listen server connection try (ServerSocket server = con.connectServerSocket();) { server.setSoTimeout(SOCK_SO_TIMEOUT); // Log the server start log.info("Listening on: " + con.toString()); log.info("RevokServer started!\n"); // Loop while the server should be running while (isRunning()) { try { // Accept a client TCP connection Socket sock = server.accept(); // Create and bind an HTTP connection // over the TCP connection received. DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(HTTP_CONN_BUFFER_SIZE); conn.bind(sock); // Submit for Thread worker execution // a HttpConnectionHandler for handling // the HTTP connection log.info("------------------------------").info("Handling socket: " + conn.toString()); exec.submit(new RunnableConnectionHandler(factory.createChannel(conn, serial), container)); // Catch socket timeout exceptions and continue // accepting other connections } catch (SocketTimeoutException se) { } } //while } catch (IOException e) { // Catch and log other error occurred accepting connections. // Errors over server listening connections are fatal // and irrecoverable. log.error(new IOException("Error running RevokServer", e), true); if (log.outputs().isEmpty()) throw new RuntimeException("Error running RevokServer", e); } // Shutdown the server and log when it not should be running anymore log.info("Stopping ExecutorService..."); exec.shutdown(); log.info("RevokServer Shutdown!"); }
From source file:jscover.server.PersistentStaticHttpServer.java
public void run() { DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(8 * 1024); try {/*from ww w . ja v a2 s . c o 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); } }