Esempio n. 1
0
 public async Task WriteResponseAsync(MiddlemanResponse response)
 {
     await WriteResponseAsync(response, CancellationToken.None)
         .ConfigureAwait(false);
 }
Esempio n. 2
0
        public async Task WriteResponseAsync(MiddlemanResponse response, CancellationToken ct)
        {
            var ms = new MemoryStream();
            var sw = new StreamWriter(ms, HeaderEncoding) { NewLine = "\r\n" };

            sw.WriteLine("HTTP/{0} {1} {2}", RequestVersion, response.StatusCode, response.StatusDescription);

            for (var i = 0; i < response.Headers.Count; i++)
            {
                var key = response.Headers.GetKey(i);
                var val = response.Headers.Get(i);

                if (!key.Equals("X-AspNet-Version", StringComparison.InvariantCultureIgnoreCase) &&
                    !key.Equals("X-SourceFiles", StringComparison.InvariantCultureIgnoreCase) &&
                    !key.Equals("X-Powered-By", StringComparison.InvariantCultureIgnoreCase))
                {
                    sw.WriteLine("{0}: {1}", key, val);
                }
            }

            sw.WriteLine();
            sw.Flush();

            var writeStream = GetWriteStream();

            ms.Position = 0;

            var responseHeaderBytes = ms.ToArray();
            var responseHeaders = HeaderEncoding.GetString(responseHeaderBytes.Where(x => x != 0).ToArray());

            await writeStream.WriteAsync(responseHeaderBytes, 0, responseHeaderBytes.Length, ct).ConfigureAwait(false);
            Log.Debug("{0}: Wrote headers ({1}b)", RemoteEndPoint, ms.Length);

            var responseBody = "";

            if (response.ResponseBody != null && response.ResponseBody.CanRead)
            {
                var buffer = new byte[8192];
                int read;
                long written = 0;

                while (
                    (read = await response.ResponseBody.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) >
                    0)
                {
                    responseBody += HeaderEncoding.GetString(buffer.Where(x => x != 0).ToArray(), 0, read);

                    written += read;
                    Log.Debug("{0}: Read {1:N0} bytes from response body", RemoteEndPoint, read);

                    try
                    {
                        await writeStream.WriteAsync(buffer, 0, read, ct).ConfigureAwait(false);
                        Log.Debug("{0}: Wrote {1:N0} bytes to client", RemoteEndPoint, read);
                    }
                    catch (Exception ex)
                    {
                        Log.ErrorException("Error writing to response stream", ex);
                    }

                    if (written >= response.ContentLength)
                    {
                        break;
                    }
                }

                Log.Debug("{0}: Wrote response body ({1:N0} bytes) to client", RemoteEndPoint, written);
            }

            Log.Info("RESPONSE TO CLIENT: " + Environment.NewLine + (responseHeaders.Trim() + Environment.NewLine + Environment.NewLine + responseBody.Trim()).Trim() + Environment.NewLine);

            await writeStream.FlushAsync(ct).ConfigureAwait(false);
        }