You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/11/12 17:05:47 UTC

svn commit: r473978 - in /jakarta/httpcomponents/httpcore/trunk/module-main/src: examples/org/apache/http/examples/ElementalHttpServer.java main/java/org/apache/http/protocol/HttpService.java

Author: olegk
Date: Sun Nov 12 08:05:46 2006
New Revision: 473978

URL: http://svn.apache.org/viewvc?view=rev&rev=473978
Log:
Some minor tweaks to the ElementalHttpServer sample app

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java
    jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java?view=diff&rev=473978&r1=473977&r2=473978
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/examples/org/apache/http/examples/ElementalHttpServer.java Sun Nov 12 08:05:46 2006
@@ -65,7 +65,6 @@
 import org.apache.http.protocol.ResponseContent;
 import org.apache.http.protocol.ResponseDate;
 import org.apache.http.protocol.ResponseServer;
-import org.apache.http.protocol.SyncHttpExecutionContext;
 
 /**
  * Basic, yet fully functional and spec compliant, HTTP/1.1 file server.
@@ -88,6 +87,13 @@
     
     static class HttpFileHandler implements HttpRequestHandler  {
         
+        private final String docRoot;
+        
+        public HttpFileHandler(final String docRoot) {
+            super();
+            this.docRoot = docRoot;
+        }
+        
         public void handle(
                 final HttpRequest request, 
                 final HttpResponse response,
@@ -97,11 +103,9 @@
             if (!method.equalsIgnoreCase("GET") && !method.equalsIgnoreCase("HEAD")) {
                 throw new MethodNotSupportedException(method + " method not supported"); 
             }
-            String docroot = (String) context.getAttribute("server.docroot");
-            
             String target = request.getRequestLine().getUri();
             
-            final File file = new File(docroot, URLDecoder.decode(target));
+            final File file = new File(this.docRoot, URLDecoder.decode(target));
             if (!file.exists()) {
 
                 response.setStatusCode(HttpStatus.SC_NOT_FOUND);
@@ -156,7 +160,7 @@
 
         private final ServerSocket serversocket;
         private final HttpParams params; 
-        private final HttpContext globalContext;
+        private final String docRoot;
         
         public RequestListenerThread(int port, final String docroot) throws IOException {
             this.serversocket = new ServerSocket(port);
@@ -167,8 +171,7 @@
                 .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
                 .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
                 .setParameter(HttpProtocolParams.ORIGIN_SERVER, "Jakarta-HttpComponents/1.1");
-            this.globalContext = new SyncHttpExecutionContext(null);
-            this.globalContext.setAttribute("server.docroot", docroot);
+            this.docRoot = docroot;
         }
         
         public void run() {
@@ -190,7 +193,7 @@
                     
                     // Set up request handlers
                     HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
-                    reqistry.register("*", new HttpFileHandler());
+                    reqistry.register("*", new HttpFileHandler(this.docRoot));
                     
                     // Set up the HTTP service
                     HttpService httpService = new HttpService(
@@ -201,7 +204,7 @@
                     httpService.setHandlerResolver(reqistry);
                     
                     // Start worker thread
-                    Thread t = new WorkerThread(httpService, conn, this.globalContext);
+                    Thread t = new WorkerThread(httpService, conn);
                     t.setDaemon(true);
                     t.start();
                 } catch (InterruptedIOException ex) {
@@ -219,23 +222,21 @@
 
         private final HttpService httpservice;
         private final HttpServerConnection conn;
-        private final HttpContext context;
         
         public WorkerThread(
                 final HttpService httpservice, 
-                final HttpServerConnection conn, 
-                final HttpContext parentContext) {
+                final HttpServerConnection conn) {
             super();
             this.httpservice = httpservice;
             this.conn = conn;
-            this.context = new HttpExecutionContext(parentContext);
         }
         
         public void run() {
             System.out.println("New connection thread");
+            HttpContext context = new HttpExecutionContext(null);
             try {
                 while (!Thread.interrupted() && this.conn.isOpen()) {
-                    this.httpservice.handleRequest(this.conn, this.context);
+                    this.httpservice.handleRequest(this.conn, context);
                 }
             } catch (ConnectionClosedException ex) {
                 System.err.println("Client closed connection");

Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java?view=diff&rev=473978&r1=473977&r2=473978
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/protocol/HttpService.java Sun Nov 12 08:05:46 2006
@@ -57,7 +57,7 @@
 
     private HttpParams params = null;
     private HttpProcessor processor = null;
-    private HttpRequestHandlerResolver handlerResolver;
+    private HttpRequestHandlerResolver handlerResolver = null;
     private ConnectionReuseStrategy connStrategy = null;
     private HttpResponseFactory responseFactory = null;