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