Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Made 'Transfer-Encoding: Chunked' optional in self-hosting, so optionall... #741

Closed
wants to merge 1 commit into from

2 participants

@dot-i

...y using responses with Content-Length instead.

@dot-i dot-i Made 'Transfer-Encoding: Chunked' optional in self-hosting, so option…
…ally using responses with Content-Length instead.
1056f33
@thecodejunkie thecodejunkie commented on the diff
src/Nancy.Hosting.Self/NancyHost.cs
@@ -134,9 +156,29 @@ private static void ConvertNancyResponseToResponse(Response nancyResponse, HttpL
response.ContentType = nancyResponse.ContentType;
response.StatusCode = (int)nancyResponse.StatusCode;
- using (var output = response.OutputStream)
+ if (sendChunked)
@thecodejunkie Owner

sendChunked should be prefixed with this. Also I think the entire if/else section should be refactored to it's own method with a descriptive name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@thecodejunkie

Part of me are thinking we should add a configuration object instead, and make it the last parameter. So you can do something like

var host = new NancyHost(..., ..., with => with.SendChunked())

which would let us add more in the future without making the constructors even messier

@thecodejunkie

@dot-i had a quick chat with @grumpydev and we agree that we need the configuration ability. If you are unsure on how to use the nested closure then let me know. Thank you

@dot-i

Why not simply make it a property then? So when you new the host you can do something like:

var host = new NancyHost(....)
{
    SendChunked = false
};

KISS principle ;-)

@dot-i

I'm closing this pull-request since I sent in a newer one here (now using HostConfiguration): #1103

@dot-i dot-i closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 11, 2012
  1. @dot-i

    Made 'Transfer-Encoding: Chunked' optional in self-hosting, so option…

    dot-i authored
    …ally using responses with Content-Length instead.
This page is out of date. Refresh to see the latest.
Showing with 50 additions and 8 deletions.
  1. +50 −8 src/Nancy.Hosting.Self/NancyHost.cs
View
58 src/Nancy.Hosting.Self/NancyHost.cs
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
+ using System.IO;
using System.Net;
using System.Linq;
using IO;
@@ -23,13 +24,22 @@ public class NancyHost
private readonly IList<Uri> baseUriList;
private readonly HttpListener listener;
private readonly INancyEngine engine;
+ private readonly bool sendChunked;
/// <summary>
/// Initializes a new instance of the <see cref="NancyHost"/> class for the specfied <paramref name="baseUris"/>.
/// </summary>
/// <param name="baseUris">The <see cref="Uri"/>s that the host will listen to.</param>
public NancyHost(params Uri[] baseUris)
- : this(NancyBootstrapperLocator.Bootstrapper, baseUris){}
+ : this(NancyBootstrapperLocator.Bootstrapper, true, baseUris) { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="NancyHost"/> class for the specfied <paramref name="baseUris"/>.
+ /// </summary>
+ /// <param name="sendChunked">Response uses Transfer-Encoding: Chunked instead of Content-Length.</param>
+ /// <param name="baseUris">The <see cref="Uri"/>s that the host will listen to.</param>
+ public NancyHost(bool sendChunked, params Uri[] baseUris)
+ : this(NancyBootstrapperLocator.Bootstrapper, sendChunked, baseUris) { }
/// <summary>
/// Initializes a new instance of the <see cref="NancyHost"/> class for the specfied <paramref name="baseUris"/>, using
@@ -38,9 +48,20 @@ public NancyHost(params Uri[] baseUris)
/// <param name="bootstrapper">The boostrapper that should be used to handle the request.</param>
/// <param name="baseUris">The <see cref="Uri"/>s that the host will listen to.</param>
public NancyHost(INancyBootstrapper bootstrapper, params Uri[] baseUris)
+ : this(bootstrapper, true, baseUris) { }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="NancyHost"/> class for the specfied <paramref name="baseUris"/>, using
+ /// the provided <paramref name="bootstrapper"/>.
+ /// </summary>
+ /// <param name="bootstrapper">The boostrapper that should be used to handle the request.</param>
+ /// <param name="sendChunked">Response uses Transfer-Encoding: Chunked instead of Content-Length.</param>
+ /// <param name="baseUris">The <see cref="Uri"/>s that the host will listen to.</param>
+ public NancyHost(INancyBootstrapper bootstrapper, bool sendChunked, params Uri[] baseUris)
{
baseUriList = baseUris;
listener = new HttpListener();
+ this.sendChunked = sendChunked;
foreach (var baseUri in baseUriList)
{
@@ -56,12 +77,13 @@ public NancyHost(INancyBootstrapper bootstrapper, params Uri[] baseUris)
/// the provided <paramref name="bootstrapper"/>.
/// </summary>
/// <param name="baseUri">The <see cref="Uri"/> that the host will listen to.</param>
- /// <param name="bootstrapper">The boostrapper that should be used to handle the request.</param>
- public NancyHost(Uri baseUri, INancyBootstrapper bootstrapper)
- : this (bootstrapper, baseUri)
+ /// <param name="bootstrapper">The bootstrapper that should be used to handle the request.</param>
+ /// <param name="sendChunked">Response uses Transfer-Encoding: Chunked instead of Content-Length.</param>
+ public NancyHost(Uri baseUri, INancyBootstrapper bootstrapper, bool sendChunked = true)
+ : this(bootstrapper, sendChunked, baseUri)
{
}
-
+
/// <summary>
/// Start listening for incoming requests.
/// </summary>
@@ -119,7 +141,7 @@ private Request ConvertRequestToNancyRequest(HttpListenerRequest request)
(request.RemoteEndPoint != null) ? request.RemoteEndPoint.Address.ToString() : null);
}
- private static void ConvertNancyResponseToResponse(Response nancyResponse, HttpListenerResponse response)
+ private void ConvertNancyResponseToResponse(Response nancyResponse, HttpListenerResponse response)
{
foreach (var header in nancyResponse.Headers)
{
@@ -134,9 +156,29 @@ private static void ConvertNancyResponseToResponse(Response nancyResponse, HttpL
response.ContentType = nancyResponse.ContentType;
response.StatusCode = (int)nancyResponse.StatusCode;
- using (var output = response.OutputStream)
+ if (sendChunked)
@thecodejunkie Owner

sendChunked should be prefixed with this. Also I think the entire if/else section should be refactored to it's own method with a descriptive name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ using (var output = response.OutputStream)
+ {
+ nancyResponse.Contents(output);
+ }
+ }
+ else
{
- nancyResponse.Contents.Invoke(output);
+ using (MemoryStream memoryStream = new MemoryStream())
+ {
+ nancyResponse.Contents(memoryStream);
+ memoryStream.Position = 0;
+
+ response.SendChunked = false;
+ response.ContentLength64 = memoryStream.Length;
+
+ using (var output = response.OutputStream)
+ {
+ memoryStream.CopyTo(output);
+ output.Flush();
+ }
+ }
}
}
Something went wrong with that request. Please try again.