private static void Redirect(IMutableWebResponse response, string redirectUri)
        {
            Trace.TraceInformation("Redirecting to {0}", redirectUri);

            response.StatusCode       = 302;
            response.RedirectLocation = redirectUri;
        }
        private void BuildResponse(IMutableWebResponse response, IWebResponse proxyResponse)
        {
            response.ProtocolVersion   = proxyResponse.ProtocolVersion;
            response.ContentType       = proxyResponse.ContentType;
            response.StatusCode        = (int)proxyResponse.StatusCode;
            response.StatusDescription = proxyResponse.StatusDescription;

            if (proxyResponse.ContentLength >= 0)
            {
                response.ContentLength = proxyResponse.ContentLength;
            }
            if (proxyResponse.Headers.ContainsKey("Content-Encoding") && !String.IsNullOrWhiteSpace(proxyResponse.ContentEncoding))
            {
                response.ContentEncoding = Encoding.GetEncoding(proxyResponse.ContentEncoding);
            }
            if (proxyResponse.Headers.ContainsKey("Location"))
            {
                response.RedirectLocation = proxyResponse.Headers["Location"];
            }
            if (proxyResponse.Headers.ContainsKey("Keep-Alive"))
            {
                bool keepAlive;
                if (Boolean.TryParse(proxyResponse.Headers["Keep-Alive"], out keepAlive))
                {
                    response.KeepAlive = keepAlive;
                }
            }

            foreach (var cookie in proxyResponse.Cookies)
            {
                response.Cookies.Add(cookie);
            }
            foreach (var header in proxyResponse.Headers)
            {
                if (header.Key.StartsWith("Proxy-"))
                {
                    continue; // Ignore proxy headers
                }
                if (PropertyHeaders.Contains(header.Key))
                {
                    continue;
                }

                response.Headers.Add(header.Key, header.Value);
            }

            var proxyStream   = proxyResponse.GetStream();
            var requestStream = response.GetStream();

            proxyStream.CopyTo(requestStream);
            requestStream.Close();
        }
        private static void CustomAction(IMutableWebResponse response, HttpStatusCode customResultStatus, Stream customResultStream)
        {
            if (customResultStatus == 0)
            {
                customResultStatus = HttpStatusCode.OK;
            }

            response.StatusCode = (int)customResultStatus;

            var responseStream = response.GetStream();

            customResultStream.CopyTo(responseStream);
            responseStream.Close();
        }
        public void ProcessRequest(IWebRequest request,
                                   IMutableWebResponse response, IPrincipal user,
                                   IEnumerable <IWebRequestFilter> requestFilters,
                                   IEnumerable <IWebResponseFilter> responseFilters)
        {
            Trace.TraceInformation("Responding to request: {0}", request.RawUrl);
            try
            {
                // Filter request
                var requestFilterResult = FilterRequest(request, requestFilters);
                if (requestFilterResult.Action == (requestFilterResult.Action | FilterResultAction.Block))
                {
                    Trace.TraceInformation("Blocking request.");
                    if (requestFilterResult.Action == (requestFilterResult.Action | FilterResultAction.Redirect))
                    {
                        Redirect(response, requestFilterResult.RedirectUrl);
                        return;
                    }
                    if (requestFilterResult.Action == (requestFilterResult.Action | FilterResultAction.CustomResult))
                    {
                        CustomAction(response, requestFilterResult.CustomResultStatus, requestFilterResult.CustomResultStream);
                        return;
                    }
                    var message       = String.Format("Request for {0} was blocked by a filter", request.RawUrl);
                    var data          = Encoding.Default.GetBytes(message);
                    var messageStream = new MemoryStream(data);

                    CustomAction(response, HttpStatusCode.OK, messageStream);
                    return;
                }
                Trace.TraceInformation("Proxying request");

                // Build proxy request
                var requestBuilder = new ProxyRequestBuilder(_webRequestFactory, request);
                var proxyRequest   = requestBuilder.Build();

                // Get proxy response
                Trace.TraceInformation("Getting proxy response");
                using (var proxyResponse = proxyRequest.GetResponse())
                {
                    var responseFilterResult = FilterResponse(proxyResponse, responseFilters);
                    if (responseFilterResult.Action == (responseFilterResult.Action | FilterResultAction.Block))
                    {
                        Trace.TraceInformation("Blocking request.");
                        if (responseFilterResult.Action == (responseFilterResult.Action | FilterResultAction.Redirect))
                        {
                            Redirect(response, responseFilterResult.RedirectUrl);
                            return;
                        }
                        if (responseFilterResult.Action == (responseFilterResult.Action | FilterResultAction.CustomResult))
                        {
                            CustomAction(response, responseFilterResult.CustomResultStatus, responseFilterResult.CustomResultStream);
                            return;
                        }
                        var message       = String.Format("Request for {0} was blocked by a filter", request.RawUrl);
                        var data          = Encoding.Default.GetBytes(message);
                        var messageStream = new MemoryStream(data);

                        CustomAction(response, HttpStatusCode.OK, messageStream);
                        return;
                    }
                    Trace.TraceInformation("Forwarding proxy response");
                    BuildResponse(response, proxyResponse);
                }
            }
            catch (HttpListenerException hEx)
            {
                if (hEx.ErrorCode == 64)
                {
                    Trace.TraceWarning("Stream aborted");
                }
                else
                {
                    Trace.TraceError("Error processing request: {0}", hEx);

                    var data          = Encoding.Default.GetBytes("SimpleProxy: Error Processing Request.");
                    var messageStream = new MemoryStream(data);

                    CustomAction(response, HttpStatusCode.InternalServerError, messageStream);
                }
            }
            catch (ProxyResponseException wEx)
            {
                if (wEx.Response != null)
                {
                    BuildResponse(response, wEx.Response);
                }
                if (wEx.Status == WebExceptionStatus.NameResolutionFailure)
                {
                    Trace.TraceWarning("Name Resolution Failure, redirecting to search: {0}", request.RawUrl);
                    Redirect(response, String.Format("http://www.google.com/search?q={0}", HttpUtility.UrlEncode(request.RawUrl)));
                }
                else
                {
                    Trace.TraceError("Error processing request: {0}", wEx);

                    var data          = Encoding.Default.GetBytes(String.Format("SimpleProxy: Error Processing Request: {0}", wEx.Message));
                    var messageStream = new MemoryStream(data);

                    CustomAction(response, HttpStatusCode.InternalServerError, messageStream);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error processing request: {0}", ex);

                var data          = Encoding.Default.GetBytes("SimpleProxy: Error Processing Request.");
                var messageStream = new MemoryStream(data);

                CustomAction(response, HttpStatusCode.InternalServerError, messageStream);
            }
        }
 /// <param name="user">Identity, authentication, and security information of the client making the request.</param>
 /// <param name="request">Client request</param>
 /// <param name="response">Response that will be sent back to the client</param>
 public ListenerRequestEventArgs(IPrincipal user, IWebRequest request, IMutableWebResponse response)
 {
     User     = user;
     Request  = request;
     Response = response;
 }