Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support JSONP even for HttpResults

  • Loading branch information...
commit 8cc4a74bd4ac6d1f46fac49e481f12ac26e1ceba 1 parent 486def1
@mythz mythz authored
View
2  NuGet/ServiceStack.Client.Silverlight/servicestack.client.silverlight.nuspec
@@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack.Client.Silverlight</id>
- <version>3.4.2</version>
+ <version>3.4.3</version>
<authors>Demis Bellot</authors>
<owners>Demis Bellot</owners>
<summary>Silverlight JSON, XML, JSV ServiceClients for ServiceStack web services</summary>
View
2  NuGet/ServiceStack.Mvc/servicestack.mvc.nuspec
@@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack.Mvc</id>
- <version>3.3.6</version>
+ <version>3.4.3</version>
<authors>Demis Bellot</authors>
<owners>Demis Bellot</owners>
<summary>.NET 4.0 project Integration of ServiceStack with MVC3</summary>
View
2  src/ServiceStack.Common/Web/ContentType.cs
@@ -29,6 +29,8 @@ public static class ContentType
public const string JsonText = "text/json";
+ public const string JavaScript = "application/javascript";
+
public const string Jsv = "application/jsv";
public const string JsvText = "text/jsv";
View
23 src/ServiceStack/WebHost.EndPoints/Extensions/IHttpResponseExtensions.cs
@@ -15,29 +15,36 @@ public static class HttpResponseExtensions
{
private static readonly ILog Log = LogManager.GetLogger(typeof(HttpResponseExtensions));
- public static bool WriteToOutputStream(IHttpResponse response, object result)
+ public static bool WriteToOutputStream(IHttpResponse response, object result, byte[] bodyPrefix, byte[] bodySuffix)
{
//var responseStream = response.OutputStream;
+
var streamWriter = result as IStreamWriter;
if (streamWriter != null)
{
- streamWriter.WriteTo(response.OutputStream);
+ if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
+ streamWriter.WriteTo(response.OutputStream);
+ if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
return true;
}
var stream = result as Stream;
if (stream != null)
{
- stream.WriteTo(response.OutputStream);
+ if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
+ stream.WriteTo(response.OutputStream);
+ if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
return true;
}
var bytes = result as byte[];
if (bytes != null)
{
- response.ContentType = ContentType.Binary;
- response.OutputStream.Write(bytes, 0, bytes.Length);
+ response.ContentType = ContentType.Binary;
+ if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
+ response.OutputStream.Write(bytes, 0, bytes.Length);
+ if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
return true;
}
@@ -135,7 +142,7 @@ public static bool WriteToResponse(this IHttpResponse response, object result, R
}
}
- if (WriteToOutputStream(response, result))
+ if (WriteToOutputStream(response, result, bodyPrefix, bodySuffix))
{
response.Flush(); //required for Compression
return true;
@@ -152,6 +159,10 @@ public static bool WriteToResponse(this IHttpResponse response, object result, R
{
response.ContentType = defaultContentType;
}
+ if (bodyPrefix != null && response.ContentType.IndexOf(ContentType.Json) >= 0)
+ {
+ response.ContentType = ContentType.JavaScript;
+ }
if (EndpointHost.Config.AppendUtf8CharsetOnContentTypes.Contains(response.ContentType))
{
View
22 tests/ServiceStack.WebHost.Endpoints.Tests/AppHostListenerBaseTests.cs
@@ -146,7 +146,27 @@ public void Calling_not_implemented_method_returns_405()
{
Assert.That(ex.StatusCode, Is.EqualTo(405));
}
- }
+ }
+
+ [Test]
+ public void Can_GET_single_gethttpresult_using_RestClient_with_JSONP_from_service_returning_HttpResult()
+ {
+ var url = ListeningOn + "gethttpresult?callback=cb";
+ string response;
+
+ var webReq = (HttpWebRequest)WebRequest.Create(url);
+ webReq.Accept = "*/*";
+ using (var webRes = webReq.GetResponse())
+ {
+ Assert.That(webRes.ContentType, Is.StringStarting(ContentType.Json));
+ response = webRes.DownloadText();
+ }
+
+ Assert.That(response, Is.Not.Null, "No response received");
+ Console.WriteLine(response);
+ Assert.That(response, Is.StringStarting("cb("));
+ Assert.That(response, Is.StringEnding(")"));
+ }
[Test, Ignore]
public void DebugHost()
View
25 tests/ServiceStack.WebHost.Endpoints.Tests/Support/Host/ExampleAppHostHttpListener.cs
@@ -325,8 +325,26 @@ public override object OnPost(ResetMovies request)
return new ResetMoviesResponse();
}
- }
-
+ }
+
+ [DataContract]
+ public class GetHttpResult {}
+
+ [DataContract]
+ public class GetHttpResultResponse
+ {
+ [DataMember]
+ public string Result { get; set; }
+ }
+
+ public class HttpResultService : IService<GetHttpResult>
+ {
+ public object Execute(GetHttpResult request)
+ {
+ var getHttpResultResponse = new GetHttpResultResponse { Result = "result" };
+ return new HttpResult(getHttpResultResponse);
+ }
+ }
public class ExampleAppHostHttpListener
: AppHostHttpListenerBase
@@ -363,7 +381,8 @@ public override void Configure(Container container)
.Add<Movie>("/custom-movies", "POST,PUT")
.Add<Movie>("/custom-movies/{Id}")
.Add<GetFactorial>("/fact/{ForNumber}")
- .Add<MoviesZip>("/movies.zip")
+ .Add<MoviesZip>("/movies.zip")
+ .Add<GetHttpResult>("/gethttpresult")
;
container.Register<IResourceManager>(new ConfigurationResourceManager());
Please sign in to comment.
Something went wrong with that request. Please try again.