Esempio n. 1
0
        private string BuildResponseBody(int httpStatusCode)
        {
            var reasonPhrase = HttpReasonPhrase.Get(httpStatusCode);
            var separator    = string.IsNullOrEmpty(reasonPhrase) ? "" : "; ";

            return(string.Format($"Status Code: {httpStatusCode}{separator}{reasonPhrase}"));
        }
Esempio n. 2
0
        public static void Encode(HttpContext context)
        {
            var outputWriter = new StreamWriter(context.Channel.OutputStream);

            var response = context.Response;

            // set status line
            var protocol     = context.Request.Protocol;
            var statusCode   = context.Response.StatusCode;
            var reasonPhrase = HttpReasonPhrase.Get(statusCode);

            outputWriter.Write($"{protocol} {statusCode} {reasonPhrase}\r\n");

            // process headers
            if (response.Headers != null && response.Headers.Count > 0)
            {
                foreach (HeaderValue header in response.Headers)
                {
                    outputWriter.Write($"{header.Key}: {header.Value}\r\n");
                }
            }

            if (response.Cookies != null && response.Cookies.Count > 0)
            {
                //Set-Cookie: <name>=<value>[; <name>=<value>][; expires=<date>][; domain=<domain_name>][; path=<some_path>][; secure][; httponly]
                foreach (CookieValue item in response.Cookies as CookieCollection)
                {
                    var cookie = item.Value;
                    outputWriter.Write($"Set-Cookie: {cookie}\r\n");
                }
            }

            outputWriter.Write("\r\n");
            outputWriter.Flush();

            // copy message body to input stream
            if (response.Body.Length > 0)
            {
                context.Response.Body.Position = 0;
                context.Response.Body.CopyTo(context.Channel.OutputStream);
                context.Response.Body.Dispose();
            }
        }