我正在运行 Rails 3.2.7 和 Ruby 1.9.3p194,以从 SQLite 数据库输出 JSON 数据。
render :json => result.to_json
Android 应用程序使用通过以下方式加载的 JSON 文件HTTP-GET
:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urlPath);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, HTTP.UTF_8);
JSONObject jsonObject = new JSONObject(result);
平台支持:
我在 API 中读到Rails 提供 gzip 支持 http://api.rubyonrails.org/classes/ActiveSupport/Gzip.html如下:
ActiveSupport::Gzip.compress(result)
我也猜测来自HTTP/1.1,RFC2626,第 14.3 节 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3我可以配置 HTTP 请求的标头:
httpPost.setHeader("Accept-Encoding", "gzip");
我还发现其中包含的信息非常有趣第 3.5 节“内容编码” http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.5:
- 所有内容编码值都是不区分大小写.
- HTTP/1.1 使用内容编码值接受编码(第 14.3 节)和内容编码(第 14.11 节)标头字段。
- The Internet Assigned Numbers Authority (IANA) acts as a registry for content-coding value tokens. Initially, the registry contains the
following tokens:
-
gzip由文件压缩程序“gzip”(GNU zip)生成的编码格式,如 RFC 1952 [25] 中所述。
这篇文章进一步解释了如何使用 Android 处理 GZIP 编码内容 https://stackoverflow.com/a/1576513/356895.
服务器测试:
因此,我不知道如何知道数据是否已被服务器压缩。
为了测试 Rails 是否输出 gzip,我尝试使用curl按照建议here https://stackoverflow.com/a/251012/356895:
$ curl --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
然而,输出并没有告诉我是否支持 gzip:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "f6f6732c747466f75052f88b1eff393b"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 74ee0562c05adea679deb701f1b8fd88
X-Runtime: 0.004205
Content-Length: 0
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Date: Thu, 16 Aug 2012 23:33:25 GMT
Connection: Keep-Alive
我也尝试过--compressed
的参数curl http://curl.haxx.se/docs/manpage.html ...
$ curl --compressed --head -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
它输出与前一个命令相同的头信息。当我跑...
$ curl --compressed -H "Accept-Encoding: gzip" http://localhost:3000/posts.json
JSON 数据作为可读文本打印到控制台。我看不到压缩是否发生。或许,因为curl已经解压响应了?!
在线测试
我也尝试过在线测试HTTP压缩测试 http://www.whatsmyip.org/http-compression-test/提及here https://webmasters.stackexchange.com/a/543。它确认 JSON 内容“未经过 gzip 压缩”。第二个网站GIDZip测试 http://www.gidnetwork.com/tools/gzip-test.php linked here https://webmasters.stackexchange.com/a/4613承认检测结果呈阴性。
问题:
- 如何从 Rails 输出 gzip 压缩的 JSON?
- 如何配置 HTTP 客户端来请求 gzip 压缩数据?
- 当我在 Heroku (PostgreSQL) 上运行 Rails 服务器时,相同的配置是否仍然有效,...?
得到教训
据我所知,我必须为 REST 服务器配置的所有内容是use Rack::Deflater
。需要明确的是:我不使用ActiveSupport::Gzip.compress()
在我的代码中。如果有人感兴趣,这就是启用 GZIP 压缩时标头的样子。
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "8170a04be41673bf25824256740a9460"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 700b9536f6a20164d31b8528bde423af
X-Runtime: 0.369337
Vary: Accept-Encoding
Content-Encoding: gzip
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Date: Tue, 21 Aug 2012 12:10:48 GMT
Content-Length: 20
Connection: Keep-Alive
现在我知道了神奇的关键字,很容易搜索并查找有关的文章use Rack::Deflater http://bit.ly/PtAmzg.