Permalink
Browse files

Adding support for jsonp

  • Loading branch information...
ayende committed Feb 16, 2011
1 parent d283540 commit a9d6f07a803da615e3281211d787cac95b9155c5
Showing with 32 additions and 10 deletions.
  1. +32 −10 Raven.Http/Extensions/HttpExtensions.cs
@@ -80,34 +80,56 @@ public static string ReadString(this IHttpContext context)
public static void WriteJson(this IHttpContext context, object obj)
{
- context.Response.AddHeader("Content-Type", "application/json; charset=utf-8");
- var streamWriter = new StreamWriter(context.Response.OutputStream, Encoding.UTF8);
- new JsonSerializer
+ WriteJson(context, JToken.FromObject(obj, new JsonSerializer
{
Converters = {new JsonToJsonConverter(), new JsonEnumConverter()},
- }.Serialize(new JsonTextWriter(streamWriter)
- {
- Formatting = Formatting.None
- }, obj);
- streamWriter.Flush();
+ }));
}
public static void WriteJson(this IHttpContext context, JToken obj)
{
- context.Response.AddHeader("Content-Type", "application/json; charset=utf-8");
var streamWriter = new StreamWriter(context.Response.OutputStream, Encoding.UTF8);
+ var jsonp = context.Request.QueryString["jsonp"];
+ if (string.IsNullOrEmpty(jsonp) == false)
+ {
+ context.Response.AddHeader("Content-Type", "application/javascript; charset=utf-8");
+ streamWriter.Write(jsonp);
+ streamWriter.Write("(");
+ }
+ else
+ {
+ context.Response.AddHeader("Content-Type", "application/json; charset=utf-8");
+
+ }
+
var jsonTextWriter = new JsonTextWriter(streamWriter)
{
Formatting = Formatting.None
};
obj.WriteTo(jsonTextWriter, new JsonEnumConverter());
jsonTextWriter.Flush();
+ if (string.IsNullOrEmpty(jsonp) == false)
+ {
+ streamWriter.Write(");");
+ }
+
streamWriter.Flush();
}
public static void WriteData(this IHttpContext context, JObject data, JObject headers, Guid etag)
{
- WriteData(context, Encoding.UTF8.GetBytes(data.ToString(Formatting.None)), headers, etag);
+ var str = data.ToString(Formatting.None);
+ var jsonp = context.Request.QueryString["jsonp"];
+ if (string.IsNullOrEmpty(jsonp) == false)
+ {
+ str = jsonp + "(" + str + ");";
+ context.Response.AddHeader("Content-Type", "application/javascript; charset=utf-8");
+ }
+ else
+ {
+ context.Response.AddHeader("Content-Type", "application/json; charset=utf-8");
+ }
+ WriteData(context, Encoding.UTF8.GetBytes(str), headers, etag);
}
public static void WriteData(this IHttpContext context, byte[] data, JObject headers, Guid etag)

0 comments on commit a9d6f07

Please sign in to comment.