Skip to content
Permalink
Browse files

Add support for overriding creation of Http ListenerRequest

  • Loading branch information...
mythz committed Apr 30, 2014
1 parent 5a0d380 commit 0d1661eb971b48118ae292ef096e6c522a39b031
@@ -119,7 +119,7 @@ public virtual void ProcessRequest(HttpListenerContext context)

if (DefaultHandledRequest(context)) return;

var httpReq = new ListenerRequest(context, operationName);
var httpReq = ((HttpListenerBase)ServiceStackHost.Instance).CreateRequest(context, operationName);

ProcessRequest(httpReq, httpReq.Response, operationName);
}
@@ -66,6 +66,13 @@ public override ServiceStackHost Start(string urlBase)
return this;
}

public virtual ListenerRequest CreateRequest(HttpListenerContext httpContext, string operationName)
{
var req = new ListenerRequest(httpContext, operationName, RequestAttributes.None);
req.RequestAttributes = req.GetAttributes();
return req;
}

/// <summary>
/// Starts the Web Service
/// </summary>
@@ -19,12 +19,6 @@ public partial class ListenerRequest : IHttpRequest
private readonly HttpListenerRequest request;
private readonly IHttpResponse response;

public ListenerRequest(HttpListenerContext httpContext, string operationName = null)
: this(httpContext, operationName, RequestAttributes.None)
{
this.RequestAttributes = this.GetAttributes();
}

public ListenerRequest(HttpListenerContext httpContext, string operationName, RequestAttributes requestAttributes)
{
this.OperationName = operationName;
@@ -283,9 +277,11 @@ public string ContentType
get { return request.ContentType; }
}

public Encoding contentEncoding;
public Encoding ContentEncoding
{
get { return request.ContentEncoding; }
get { return contentEncoding ?? request.ContentEncoding; }
set { contentEncoding = value; }
}

public Uri UrlReferrer
@@ -841,7 +841,7 @@ public static IHttpRequest ToRequest(this HttpRequestBase httpCtx, string operat

public static IHttpRequest ToRequest(this HttpListenerContext httpCtxReq, string operationName = null)
{
return new ListenerRequest(httpCtxReq, operationName);
return ((HttpListenerBase)ServiceStackHost.Instance).CreateRequest(httpCtxReq, operationName);
}

public static IHttpResponse ToResponse(this HttpRequestBase httpCtx)
@@ -1,6 +1,10 @@
using Funq;
using System.Net;
using System.Text;
using System.Threading;
using Funq;
using NUnit.Framework;
using ServiceStack.Host.Handlers;
using ServiceStack.Host.HttpListener;
using ServiceStack.Text;

namespace ServiceStack.WebHost.Endpoints.Tests
@@ -11,9 +15,21 @@ public class AppHostListenerEchoRequestTests
public class AppHost : AppHostHttpListenerBase
{
public AppHost()
: base("Echo AppHost", typeof(AppHost).Assembly) { }
: base("Echo AppHost", typeof(AppHost).Assembly)
{
}

public override void Configure(Container container) {}
public override void Configure(Container container) { }

public override ListenerRequest CreateRequest(HttpListenerContext httpContext, string operationName)
{
var req = new ListenerRequest(httpContext, operationName, RequestAttributes.None)
{
ContentEncoding = Encoding.UTF8
};
req.RequestAttributes = req.GetAttributes();
return req;
}
}

[Route("/echo")]
@@ -24,6 +40,9 @@ public class Echo : IReturn<Echo>
public string PathInfoParam { get; set; }
}

[Route("/customhtml")]
public class CustomHtml {}

public class EchoService : Service
{
public Echo Any(Echo request)
@@ -36,6 +55,24 @@ public RequestInfoResponse Any(RequestInfo request)
var requestInfo = RequestInfoHandler.GetRequestInfo(base.Request);
return requestInfo;
}

public object Any(CustomHtml request)
{
return @"<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
</head>
<body>
<form action='/echo' method='POST'>
<input name='Force' value='English' />
<input name='Param' id='Param'/>
<input type='submit' value='Send'/>
</form>
</body>
</html>";
}
}

private AppHost appHost;
@@ -105,5 +142,19 @@ public void Does_url_transparently_decode_RequestBody()
Assert.That(response.Param, Is.EqualTo(request.Param));
}

[Test]
public void Can_force_default_UTF8_encoding()
{
const string param = "привіт";

var json = Config.AbsoluteBaseUri.CombineWith("/echo").PostStringToUrl(
requestBody: "Param=" + param.UrlEncode(),
contentType: MimeTypes.FormUrlEncoded, accept: MimeTypes.Json);

var value = JsonObject.Parse(json)["Param"];

Assert.That(value, Is.EqualTo(param));
}
}

}

0 comments on commit 0d1661e

Please sign in to comment.
You can’t perform that action at this time.