private IEnumerable <Task> _makeSerializableWebRequest(HttpRequest request, TaskCompletionSource <SerialilzableWebRequest> result)
        {
            string path   = ExtractPath(request.Url.AbsolutePath);
            var    remote = new SerialilzableWebRequest
            {
                Path        = path,
                Query       = request.Url.Query.TrimStart('?'),
                Method      = request.HttpMethod,
                ContentType = request.ContentType,
                Headers     = request.Headers
            };

            remote.Content = new byte[request.InputStream.Length];
            using (var stream = new MemoryStream(remote.Content, 0, remote.Content.Length, true, true))
            {
                var task = Streams.CopyStreamAsync(request.InputStream, stream);
                yield return(task);

                task.Wait();
            }
            result.SetResult(remote);
        }
        private IEnumerable <Task> _processResponse(HttpResponse clientResponse, Task <HttpWebResponse> remoteCall, string path)
        {
            HttpWebResponse remoteResponse;

            if (remoteCall.Status == TaskStatus.RanToCompletion)
            {
                remoteResponse = remoteCall.Result;
            }
            else
            {
                var we = (WebException)Validation.EnumerateInner(remoteCall.Exception).Where(e => e is WebException).FirstOrDefault();
                if (we != null && we.Response != null)
                {
                    remoteResponse = (HttpWebResponse)we.Response;
                }
                else
                {
                    clientResponse.StatusCode = 500;
                    clientResponse.End();
                    yield break;
                }
            }

            clientResponse.TrySkipIisCustomErrors = true;
            clientResponse.StatusCode             = (int)remoteResponse.StatusCode;
            clientResponse.ContentType            = remoteResponse.ContentType;
            clientResponse.Headers.AddUnrestricted(remoteResponse.Headers);

            if (ShouldRewrite(remoteResponse.ContentType))
            {
                Encoding responseEncoding;
                string   responseContent;
                using (var ms = new MemoryStream())
                {
                    var st = Streams.CopyStreamAsync(remoteResponse.GetResponseStream(), ms);
                    yield return(st);

                    st.Wait();

                    if (remoteResponse.CharacterSet == "")
                    {
                        responseEncoding = Encoding.UTF8;
                    }
                    else
                    {
                        try
                        {
                            // CharacterSet may be wrong case etc.  May need to clean up.
                            responseEncoding = Encoding.GetEncoding(remoteResponse.CharacterSet);
                        }
                        catch
                        {
                            responseEncoding = Encoding.UTF8;
                        }
                    }

                    responseContent = responseEncoding.GetString(ms.GetBuffer(), 0, (int)ms.Length);
                }

                responseContent = RewriteResponse(responseContent, path);

                var rb   = responseEncoding.GetBytes(responseContent);
                var task = System.IO.StreamExtensions.WriteAsync(clientResponse.OutputStream, rb, 0, rb.Length);
                yield return(task);

                task.Wait();
            }
            else
            {
                var task = Streams.CopyStreamAsync(remoteResponse.GetResponseStream(), clientResponse.OutputStream);
                yield return(task);

                task.Wait();
            }
        }