00001 /******************************************************************************* 00002 00003 @file HttpBridge.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 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, April 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.http.server.HttpBridge; 00040 00041 private import mango.io.Socket; 00042 00043 private import mango.io.model.IConduit; 00044 00045 private import mango.utils.model.IServer; 00046 00047 private import mango.http.server.HttpThread, 00048 mango.http.server.HttpServer, 00049 mango.http.server.HttpRequest, 00050 mango.http.server.HttpResponse; 00051 00052 private import mango.http.server.model.IProvider, 00053 mango.http.server.model.IProviderBridge; 00054 00055 /****************************************************************************** 00056 00057 Bridges between an IProvider and an IServer, and contains a set of 00058 data specific to each thread. There is only one instance of server 00059 and provider, but multiple live instances of HttpBridge (one per 00060 server-thread). 00061 00062 Any additional thread-specific data should probably be contained 00063 within this class, since it is reachable from almost everywhere. 00064 00065 ******************************************************************************/ 00066 00067 class HttpBridge : IProviderBridge 00068 { 00069 private IServer server; 00070 private IProvider provider; 00071 00072 private HttpThread thread; 00073 private HttpRequest request; 00074 private HttpResponse response; 00075 00076 /********************************************************************** 00077 00078 Construct a bridge with the requisite attributes. We create 00079 the per-thread request/response pair here, and maintain them 00080 for the lifetime of the server. 00081 00082 **********************************************************************/ 00083 00084 this (IServer server, IProvider provider, HttpThread thread) 00085 { 00086 this.thread = thread; 00087 this.server = server; 00088 this.provider = provider; 00089 00090 request = provider.createRequest(this); 00091 response = provider.createResponse(this); 00092 } 00093 00094 /********************************************************************** 00095 00096 Return the server from one side of this bridge 00097 00098 **********************************************************************/ 00099 00100 IServer getServer() 00101 { 00102 return server; 00103 } 00104 00105 /********************************************************************** 00106 00107 Return the provider from the other side of the bridge 00108 00109 **********************************************************************/ 00110 00111 IProvider getProvider() 00112 { 00113 return provider; 00114 } 00115 00116 /********************************************************************** 00117 00118 Bridge the divide between IServer and IProvider instances. 00119 Note that there is one instance of this class per thread. 00120 00121 Note also that this is probably the right place to implement 00122 keep-alive support if that were ever to happen, although the 00123 implementation should itself be in a subclass. 00124 00125 **********************************************************************/ 00126 00127 void cross (IConduit conduit) 00128 { 00129 // bind our input & output instance to this conduit 00130 request.setConduit (conduit); 00131 response.setConduit (conduit); 00132 00133 try { 00134 // reset the (probably overridden) input and output 00135 request.reset(); 00136 response.reset(); 00137 00138 // first, extract HTTP headers from input 00139 request.readHeaders (); 00140 00141 // pass request off to the provider. It is the provider's 00142 // responsibility to flush the output! 00143 provider.service (request, response); 00144 } finally { 00145 // close and destroy this conduit (socket) 00146 conduit.close(); 00147 } 00148 } 00149 }