public static RackRequest ToRackRequest(this IHttpRequest source)
        {
            //Workaround for HttpServer position being at EOF
            if (source.Body.CanSeek)
                source.Body.Position = 0;

            var target = new RackRequest();
            target.RequestMethod = source.Method;
            target.ApplicationPath = String.Empty; //Currently not supporting multiple applications per server instance
            target.ResourcePath = source.Uri.LocalPath;
            target.QueryString = source.QueryString.ToString(true);
            target.ServerName = source.Uri.Host;
            target.ServerPort = source.Uri.Port;
            target.UrlScheme = source.Uri.Scheme.ToLower();
            target.Body = new StreamReader(source.Body).ReadToEnd();
            target.RemoteAddress = source.Uri.Host;
            target.ServerProtocol = source.Headers["SERVER_PROTOCOL"];
            target.RequestPath = source.UriPath;
            target.RemoteAddress = source.RemoteEndPoint.Address.ToString();
            target.HttpVersion = source.HttpVersion;
            target.RequestUri = source.Uri.ToString();
            target.GatewayInterface = source.Headers["GATEWAY_INTERFACE"];
            target.ContentType = source.Headers["CONTENT_TYPE"];
            target.ContentLength = source.ContentLength;
            target.AddHeaders(source.Headers);
            return target;
        }
        private void ProcessRequest(object sender, HttpRequestEventArgs e)
        {
            try
            {
                var request = new RackRequest();
                var response = new RackResponse(e.RequestContext.Response.OutputStream);

                PrepareRackRequest(e.RequestContext.Request, request);
                _rack.HandleRequest(request, response);

                response.Status = response.Status;
                foreach (var header in response.Headers)
                    if (header.Key.ToLower() != "content-length") //Cannot set directly
                        e.RequestContext.Response.Headers.Add(header.Key, header.Value);

                
            }
            catch (HttpListenerException)
            {
                return;
            }
            catch (Exception ex)
            {
                RequestProcessingError(this, new HttpRequestErrorEventArgs(e.RequestContext,ex));
                var writer = new StreamWriter(e.RequestContext.Response.OutputStream);
                writer.Write(ex.ToString());
                writer.Flush();
                
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            var request = new RackRequest();
            var response = new RackResponse(context.Response.OutputStream);

            PrepareRackRequest(request, context.Request);

            _rack.HandleRequest(request, response);

            context.Response.StatusCode = response.Status;
            foreach (var header in response.Headers)
                context.Response.AppendHeader(header.Key, header.Value);
        }
        private static void PrepareRackRequest(HttpListenerRequest source,RackRequest target)
        {
            target.RequestMethod = source.HttpMethod;
            target.ApplicationPath = String.Empty;
            //Currently not supporting virtual directories / applications
            target.ResourcePath = source.Url.LocalPath;
            target.QueryString = source.Url.Query;
            target.ServerName = source.Url.Host;
            target.ServerPort = source.Url.Port;
            target.UrlScheme = source.Url.Scheme.ToLower();
            target.Body = new StreamReader(source.InputStream).ReadToEnd();
            target.RemoteAddress = source.Url.Host;
            target.ServerProtocol = source.Headers["SERVER_PROTOCOL"];
            target.RequestPath = source.Url.AbsolutePath;
            target.RemoteAddress = source.RemoteEndPoint.Address.ToString();
            target.HttpVersion = source.ProtocolVersion.ToString();
            target.RequestUri = source.Url.ToString();
            target.GatewayInterface = source.Headers["GATEWAY_INTERFACE"];
            target.ContentType = source.Headers["CONTENT_TYPE"];
            target.ContentLength = Convert.ToInt32(source.ContentLength64);
            target.AddHeaders(source.Headers);

        }
 private static void PrepareRackRequest(RackRequest target,HttpRequest source)
 {
     target.RequestMethod = source.HttpMethod;
     target.ApplicationPath = source.ApplicationPath;
     target.ResourcePath = source.Path.Substring(
         source.Path.IndexOf(source.ApplicationPath) +
         source.ApplicationPath.Length);
     target.QueryString = source.Url.Query;
     target.ServerName = source.Url.Host;
     target.ServerPort = source.Url.Port;
     target.UrlScheme = source.Url.Scheme.ToLower();
     target.Body = new StreamReader(source.InputStream).ReadToEnd();
     target.RemoteAddress = source.Url.Host;
     target.ServerProtocol = source.Headers["SERVER_PROTOCOL"];
     target.RequestPath = source.Url.AbsolutePath;
     //TODO:This is carried over from the original IronRuby version - I'm pretty sure it isn't correct
     target.RemoteAddress = "127.0.0.1";
     target.HttpVersion = source.Headers["SERVER_PROTOCOL"];
     target.RequestUri = source.Url.ToString();
     target.GatewayInterface = source.Headers["GATEWAY_INTERFACE"];
     target.ContentType = source.Headers["CONTENT_TYPE"];
     target.ContentLength = Convert.ToInt32(source.ContentLength);
     target.AddHeaders(source.Headers);
 }