/// <summary> /// End request is typically used for post processing. The response should already contain everything required. /// </summary> /// <param name="context">HTTP context</param> /// <remarks> /// <para>The last method that is executed in the pipeline.</para> /// Try to avoid throwing exceptions if you can. Let all modules have a chance to handle this method. You may break the processing in any other method than the Begin/EndRequest methods.</remarks> public void EndRequest(IHttpContext context) { _logger.Trace("Pipeline => ErrorModule.EndRequest"); if (context.LastException != null) { string errorPage = HtmlFactory.HttpResponse("utf-8", "Exeption Error", string.Format("Opps, fail with exception: {0}", context.LastException)); //string errorPage = string.Format("<!DOCTYPE html><html><body>Opps, fail with exception: {0}</html></body>", context.LastException); context.Response.StatusCode = HttpStatusCode.InternalServerError; context.Response.ReasonPhrase = "Exception Error"; context.Response.Content = new HttpStringContent(errorPage, Windows.Storage.Streams.UnicodeEncoding.Utf8, "text/html"); } if (context.Response.RequestMessage == null) { string errorPage = HtmlFactory.HttpResponse("utf-8", "Not Found", "Opps, page not found"); context.Response.StatusCode = HttpStatusCode.NotFound; context.Response.ReasonPhrase = "Not Found"; context.Response.Content = new HttpStringContent(errorPage, Windows.Storage.Streams.UnicodeEncoding.Utf8, "text/html"); } }
private async Task SendResponseAsync(StreamSocket socket, HttpResponseMessage response) { if (socket == null) { return; } _logger.Trace("Pipeline => HttpServer.SendResponseAsync"); try { if (response == null) { string errorPage = HtmlFactory.HttpResponse("utf-8", "Not Found", "Opps, page not found"); response.Content = new HttpStringContent(errorPage, UnicodeEncoding.Utf8, "text/html"); response.StatusCode = HttpStatusCode.NotFound; response.ReasonPhrase = "Not Found"; response.Headers.Add("Connection", "close"); } //if (response.Content == null) // response.Content = new HttpStringContent(string.Empty); //ulong contentLength = await response.Content.BufferAllAsync(); //if (contentLength == 0) // return; using (DataWriter outputWriter = new DataWriter(socket.OutputStream)) { string htmlVersion; switch (response.Version) { case HttpVersion.Http10: htmlVersion = "HTTP/1.0"; break; case HttpVersion.Http20: htmlVersion = "HTTP/2.0"; break; default: htmlVersion = "HTTP/1.1"; break; } outputWriter.UnicodeEncoding = UnicodeEncoding.Utf8; outputWriter.WriteString(string.Format("{0} {1} {2}\r\n", htmlVersion, (int)response.StatusCode, response.StatusCode)); foreach (var item in response.Headers) { outputWriter.WriteString(string.Format("{0}: {1}\r\n", item.Key, item.Value)); } foreach (var item in response.Content.Headers) { outputWriter.WriteString(string.Format("{0}: {1}\r\n", item.Key, item.Value)); } //string ret = "Set-Cookie: {0}={1}; Path={2}; Expires={3};\r\n"; outputWriter.WriteString("\r\n"); var bodyContent = await response.Content.ReadAsBufferAsync(); outputWriter.WriteBuffer(bodyContent); await outputWriter.StoreAsync(); //await outputWriter.FlushAsync(); } } catch (Exception ex) { _logger.Error("Http request failed with error: {0}", ex.Message, ex); } finally { if (socket != null) { socket.Dispose(); } _logger.Debug("Connection from {0}:{1} to {2}:{3} was closed", socket.Information.RemoteHostName.DisplayName, socket.Information.RemotePort, socket.Information.LocalAddress.DisplayName, socket.Information.LocalPort); } }