Skip to content

Commit eb7a72d

Browse files
committed
add Compression support to JsonRpcHandler
IHttpModule Filter seems not appliable to IHttpAsyncHandler.
1 parent ecb3aee commit eb7a72d

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

AustinHarris.JsonRpc.AspNet/JsonRpcHandler.cs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
using System;
22
using System.IO;
3+
using System.IO.Compression;
4+
using System.Text;
35
using System.Web;
46

57
namespace AustinHarris.JsonRpc.Handlers.AspNet
68
{
79
public class JsonRpcHandler : IHttpAsyncHandler
8-
{
10+
{
11+
#region Fields
12+
/// <summary>
13+
/// UTF8 Encoding without BOM.
14+
/// </summary>
15+
static Encoding UTF8Encoding = new UTF8Encoding(false);
16+
#endregion
17+
918
#region IHttpHandler Members
1019

1120
public bool IsReusable
@@ -59,6 +68,7 @@ private static string GetJsonRpcString(System.Web.HttpRequest request)
5968
}
6069
return json;
6170
}
71+
6272
/// <summary>
6373
/// Provides an asynchronous process End method when the process ends.
6474
/// </summary>
@@ -74,10 +84,51 @@ public void EndProcessRequest(IAsyncResult result)
7484
{
7585
r = string.Format("{0}({1})", callback, r);
7686
}
77-
((HttpContext)state.AsyncState).Response.Write(r);
78-
((HttpContext)state.AsyncState).Response.End();
87+
88+
// try to compress the response data.
89+
// fix me: compression filters in IHttpModule always failed for IHttpAsyncHandler
90+
CompressResponseIfPossible(((HttpContext)state.AsyncState).Request, ((HttpContext)state.AsyncState).Response, r, UTF8Encoding);
91+
}
92+
}
93+
94+
#endregion
95+
96+
#region Utility methods
97+
98+
/// <summary>
99+
/// Transfer the result data compressed when the client accepts gzip.
100+
/// </summary>
101+
/// <param name="request">A HttpRequest object that represents the HTTP request.</param>
102+
/// <param name="response">A HttpResponse object that represents the HTTP response to be sent to the client.</param>
103+
/// <param name="result">The string data to be sent to the client.</param>
104+
/// <param name="encoding">The Encoding to be used to encode as the result.</param>
105+
static void CompressResponseIfPossible(HttpRequest request, HttpResponse response, String result, Encoding encoding)
106+
{
107+
string AcceptEncoding = request.Headers["Accept-Encoding"];
108+
if (AcceptEncoding != null && AcceptEncoding.Contains("gzip"))
109+
{
110+
//response.Headers.Remove("Content-Encoding");
111+
response.AddHeader("Content-Encoding", "gzip");
112+
113+
using (var gstream = new GZipStream(response.OutputStream, CompressionMode.Compress))
114+
using (var writer = new StreamWriter(gstream, encoding))
115+
{
116+
writer.Write(result);
117+
writer.Flush();
118+
}
119+
}
120+
else
121+
{
122+
using (StreamWriter writer = new StreamWriter(response.OutputStream, encoding))
123+
{
124+
writer.Write(result);
125+
writer.Flush();
126+
}
79127
}
128+
129+
response.End();
80130
}
131+
81132

82133
#endregion
83134
}

0 commit comments

Comments
 (0)