00001 /******************************************************************************* 00002 00003 @file HttpServer.d 00004 00005 Copyright (C) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, April 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.http.server.HttpServer; 00037 00038 private import mango.io.Exception, 00039 mango.io.ServerSocket; 00040 00041 private import mango.io.model.IConduit; 00042 00043 private import mango.log.model.ILogger; 00044 00045 private import mango.utils.ServerThread, 00046 mango.utils.AbstractServer; 00047 00048 private import mango.http.server.HttpThread, 00049 mango.http.server.HttpBridge; 00050 00051 public import mango.http.server.model.IProvider; 00052 00053 private import mango.http.server.model.IProviderBridge; 00054 00055 /****************************************************************************** 00056 00057 Extends the AbstractServer to glue all the Http support together. 00058 One should subclass this to provide a secure (https) server. 00059 00060 ******************************************************************************/ 00061 00062 class HttpServer : AbstractServer 00063 { 00064 private IProvider provider; 00065 00066 /********************************************************************** 00067 00068 Construct this server with the requisite attribites. The 00069 IProvider represents a service handler, the binding addr 00070 is the local address we'll be listening on, and 'threads' 00071 represents the number of thread to initiate. 00072 00073 **********************************************************************/ 00074 00075 this (IProvider provider, InternetAddress bind, int threads, ILogger logger = null) 00076 { 00077 this (provider, bind, threads, 10, logger); 00078 } 00079 00080 /********************************************************************** 00081 00082 Construct this server with the requisite attribites. The 00083 IProvider represents a service handler, the binding addr 00084 is the local address we'll be listening on, and 'threads' 00085 represents the number of thread to initiate. Backlog is 00086 the number of "simultaneous" connection requests that a 00087 socket layer will buffer on our behalf. 00088 00089 **********************************************************************/ 00090 00091 this (IProvider provider, InternetAddress bind, int threads, int backlog, ILogger logger = null) 00092 { 00093 super (bind, threads, backlog, logger); 00094 this.provider = provider; 00095 } 00096 00097 /********************************************************************** 00098 00099 Return the protocol in use. 00100 00101 **********************************************************************/ 00102 00103 char[] getProtocol() 00104 { 00105 return "http"; 00106 } 00107 00108 /********************************************************************** 00109 00110 Return a text string identifying this server 00111 00112 **********************************************************************/ 00113 00114 override char[] toString() 00115 { 00116 return getProtocol()~"::"~provider.toString(); 00117 } 00118 00119 /********************************************************************** 00120 00121 Create a ServerSocket instance. Secure implementations 00122 would provide a SecureSocketServer, or something along 00123 those lines. 00124 00125 **********************************************************************/ 00126 00127 override ServerSocket createSocket (InternetAddress bind, int backlog) 00128 { 00129 return new ServerSocket (bind, backlog); 00130 } 00131 00132 /********************************************************************** 00133 00134 Create a ServerThread instance. This can be overridden to 00135 create other thread-types, perhaps with additional thread- 00136 level data attached. 00137 00138 **********************************************************************/ 00139 00140 override ServerThread createThread (ServerSocket socket) 00141 { 00142 return new HttpThread (this, socket); 00143 } 00144 00145 /********************************************************************** 00146 00147 Factory method for servicing a request. We just toss the 00148 request across our bridge, for the IProvider to handle. 00149 00150 **********************************************************************/ 00151 00152 override void service (ServerThread st, IConduit conduit) 00153 { 00154 HttpThread thread; 00155 IProviderBridge bridge; 00156 00157 // we know what this is because we created it (above) 00158 thread = cast(HttpThread) st; 00159 00160 // extract thread-local HttpBridge 00161 if ((bridge = thread.getBridge()) is null) 00162 { 00163 // create a new instance if it's the first service 00164 // request on this particular thread 00165 bridge = new HttpBridge (this, provider, thread); 00166 thread.setBridge (bridge); 00167 } 00168 00169 // process request 00170 bridge.cross (conduit); 00171 } 00172 } 00173