public void HandleResource(ParsedHttpRequest request, HttpResponse response)
        {
            Int32 dotIndex = request.url.LastIndexOf('.');

            if (dotIndex >= 0 && dotIndex + 1 < request.url.Length)
            {
                String extension = request.url.Substring(dotIndex + 1);

                IResourceHandler handler;
                if (handlerDictionary.TryGetValue(extension, out handler))
                {
                    handler.HandleResource(request, response);
                    return;
                }
            }

            defaultHandler.HandleResource(request, response);
        }
예제 #2
0
        public void Run()
        {
            try
            {
                //
                // 1. Receive the Request
                //
                MemoryStream requestBuffer = new MemoryStream(1024);

                ParsedHttpRequest request = new ParsedHttpRequest(stream, messageLogger, connectionDataLogger.AToBDataLogger);

                //
                // 2. Log the Request
                //
                Byte[] requestBytes = requestBuffer.ToArray();
                requestBuffer.Dispose();

                messageLogger.Log("Got Request {0} bytes", requestBytes.Length);

                //
                // 3. Create the Response
                //
                HttpResponse response = new HttpResponse();
                response.version = "HTTP/1.1";
                response.Headers = new Dictionary <String, String>();
                response.Headers.Add("Server", WebServer.ServerName);
                response.Headers.Add("Date", DateTime.Now.ToString("r"));
                response.Headers.Add("Connection", "close");

                if (!request.ParsedSuccessfully)
                {
                    response.status = Http.ResponseBadRequest;
                }
                else
                {
                    response.status = Http.ResponseOK;

                    try
                    {
                        resourceHandler.HandleResource(request, response);
                    }
                    catch (Exception e)
                    {
                        response.bodyStream.Dispose();
                        response.bodyStream = new MemoryStream();
                        Byte[] responseBytes = Encoding.Default.GetBytes(String.Format(
                                                                             "<html><head><title>Exception: {0}</title><body><h1>URL: {1}</h1>\n<h2>Exception: {2}</h2>\n{3}</body></html>",
                                                                             e.GetType().ToString(), request.url, e.Message, e.StackTrace));
                        response.bodyStream.Write(responseBytes, 0, responseBytes.Length);
                        response.Headers.Add("Content-Type", "text/html");
                    }
                }

                //
                // 4. Check Headers
                //
                if (!response.Headers.ContainsKey("Content-Type"))
                {
                    response.Headers.Add("Content-Type", "application/octet-stream");
                }
                Byte[] responseBody = response.bodyStream.ToArray();
                response.Headers.Add("Content-Length", responseBody.Length.ToString());


                //
                // 5. Send the Response
                //

                // Put together the response string
                StringBuilder headerStringBuilder = new StringBuilder(
                    String.Format("{0} {1}\n", response.version, Http.ResponseMessageMap[response.status]));
                foreach (KeyValuePair <String, String> headerPair in response.Headers)
                {
                    headerStringBuilder.Append(String.Format("{0}: {1}\n", headerPair.Key, headerPair.Value));
                }
                headerStringBuilder.Append('\n');

                String headerString = headerStringBuilder.ToString();
                messageLogger.Log("Sending Header {0} bytes", headerString.Length);

                // Send headers
                byte[] headerByteArray = Encoding.UTF8.GetBytes(headerString);
                connectionDataLogger.LogDataBToA(headerByteArray, 0, headerByteArray.Length);
                stream.Write(headerByteArray, 0, headerByteArray.Length);

                // Send body
                if (responseBody.Length > 0)
                {
                    messageLogger.Log("Sending Body {0} bytes", responseBody.Length);
                    connectionDataLogger.LogDataBToA(responseBody, 0, responseBody.Length);
                    stream.Write(responseBody, 0, responseBody.Length);
                }
            }
            catch (Exception e)
            {
                messageLogger.Log("Exception {0}", e.ToString());
            }
            finally
            {
                messageLogger.Log("Close");
                stream.Close();
            }
        }