hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From steve labar <steve.labarbera....@gmail.com>
Subject Trying to implement a HttpsServer with Components
Date Thu, 10 Nov 2011 23:21:08 GMT
I’m trying to use HTTPClient and HTTPCORE to create a client/server so I
can create my own proxy to see traffic coming from browser to the server
and back. I have setup everything and have http traffic working fairly
well. My problem is that when in the browser I switch to https the browser
sends a CONNECT request. This request gets sent to my HTTPServer class
where I have a HTTPService.handleRequest() which sends it to my
DefaultHttpRequestHandler.
Once here it gets sent to the handle(HttpRequest request, HttpResponse
response,HttpContext context) method where I have HttpClient set and ready
to make the requests. Problem is do I need to look for the Connect in the
server class and handle myself. I thought maybe there was a helper class or
something to handle this tunneling process?


Code Examples:


===================== Server Code Chunks ========================

{ This is the server listener which listens for an http request from a
specified port}

RequestListenerThread extends Thread{

...

    public void run() {
            System.out.println("Listening on port "
                    + serversocket.getLocalPort());
            while (!Thread.interrupted()) {
                try {
                    // Set up HTTP connection
                    Socket socket = serversocket.accept();

                    DefaultHttpServerConnection conn = new
DefaultHttpServerConnection();
                    System.out.println("Incoming connection from "
                            + socket.getInetAddress());
                    conn.bind(socket, this.params);

                    // Start worker thread
                    Thread t = new WorkerThread(this.httpService, conn);
                    t.setDaemon(true);
                    t.start();
                } catch (InterruptedIOException ex) {
                    break;
                } catch (IOException ex) {
                    System.err
                            .println("I/O error initialising connection
thread: "
                                    + ex.getMessage());
                    ex.printStackTrace();
                    break;
                }
            }


...

}


(This is where http request gets sent to the request handler

static class WorkerThread extends Thread {

        private final HttpService httpservice;
        private final HttpServerConnection conn;

        public WorkerThread(final HttpService httpservice,
                final HttpServerConnection conn) {
            super();
            this.httpservice = httpservice;
            this.conn = conn;
        }

        public void run() {
            System.out.println("New connection thread");
            HttpContext context = new BasicHttpContext(null);
            try {
                while (!Thread.interrupted() && this.conn.isOpen()) {
                    this.httpservice.handleRequest(this.conn, context);

                }
            } catch (ConnectionClosedException ex) {
                System.err.println("Client closed connection");
            } catch (IOException ex) {
                System.err.println("I/O error: " + ex.getMessage());
                ex.printStackTrace();
            } catch (HttpException ex) {
                System.err.println("Unrecoverable HTTP protocol violation: "
                        + ex.getMessage());
            } finally {
                try {
                    this.conn.shutdown();
                } catch (IOException ignore) {
                }
            }
        }

    }



=======================================


============= HTTP REQUEST Handler =================

......

public void handle(HttpRequest request, HttpResponse response,
            HttpContext context) throws HttpException, IOException {

        System.out.println(request);
        RequestLine reqLine = request.getRequestLine();
        if(reqLine.getMethod().equalsIgnoreCase("CONNECT"))
        {
            response.setEntity(new BufferedHttpEntity(new
StringEntity("HTTP/1.0 200 Connection established\r\nProxy-agent: proxy
agentt\r\n\r\n")));
        }
        else
        {
            try {

                HttpResponse clientResponse = null;

                HttpEntity entity = null;

                clientResponse = httpClient.execute(new
RequestWrapper(request));

                entity = clientResponse.getEntity();

                if (entity != null) {
                    response.setEntity(new BufferedHttpEntity(entity));
                }
            } catch (Exception e) {
                System.err.println(e.getMessage());
                e.printStackTrace();
            }

        }
        // finally {
//            // When HttpClient instance is no longer needed,
//            // shut down the connection manager to ensure
//            // immediate deallocation of all system resources
//            httpClient.getConnectionManager().shutdown();
//
//        }

    }


........

Regards,


Steve

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message