Content-LengthとTransfer-Encoding: chunked

Apache   圧縮の設定(enabledにする)

# a2enmod deflate
Module deflate already enabled

  KeepAliveの設定(Offにする)

vim /etc/apache2/apache2.conf
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive Off

  再起動する

# /etc/init.d/apache2 restart
 * Restarting web server apache2                                                                                                                            ... waiting

  ■ クライアント(Apache commons HTTPClient)  

public static void main( String[] args ) throws Exception
{
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");

    HttpClient httpClient = new HttpClient();
    HttpMethod method =
            new GetMethod("http://localhost/hoge.php");  ★ Transfer-Encoding: chunked
            // new GetMethod("http://localhost/hoge.html"); ★ Content-Length
            //method.addRequestHeader("Accept-Encoding", "gzip"); ★ gzipするかどうか

    int status = httpClient.executeMethod(method);
    System.out.println("status:" + status);

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(method.getResponseBodyAsStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        //System.out.println(line);
    }
    reader.close();
}

  ■ コンテンツ   hoge.php

<?php
phpinfo();
sleep(1);
phpinfo();
sleep(1);
phpinfo();
sleep(1);
phpinfo();
?>

  hoge.html

<html>
<head><title>hoge</title></head>
<body>
aaa
bbb
ccc
ddd
eee
</body>
</html>

  ■ 実行すると、、   hoge.php

2011/07/08 17:02:25:246 JST [DEBUG] header - << "Transfer-Encoding: chunked[r][n]"

  hoge.html

2011/07/08 17:03:54:458 JST [DEBUG] header - << "Content-Length: 83[r][n]"

  ■ Accept-Encodingでgzipする

hoge.html 

 
2011/07/08 17:05:39:360 JST [DEBUG] header - << "Content-Encoding: gzip[r][n]"
2011/07/08 17:05:39:360 JST [DEBUG] header - << "Content-Length: 83[r][n]"

  hoge.php 

 
2011/07/08 17:06:24:120 JST [DEBUG] header - << "Content-Encoding: gzip[r][n]"
2011/07/08 17:06:24:120 JST [DEBUG] header - << "Connection: close[r][n]"
2011/07/08 17:06:24:121 JST [DEBUG] header - << "Transfer-Encoding: chunked[r][n]"

  ■ Transfer-Encoding: chunked でコンテンツのサイズが大きい場合   hoge.phpを以下のようにどでかくしてみました。

6046 phpinfo();
6047 phpinfo();
6048 phpinfo();
6049 phpinfo();

↓普通に取得できました。

2011/07/08 17:16:33:781 JST [DEBUG] header - << "Vary: Accept-Encoding[r][n]"
2011/07/08 17:16:33:781 JST [DEBUG] header - << "Connection: close[r][n]"
2011/07/08 17:16:33:781 JST [DEBUG] header - << "Transfer-Encoding: chunked[r][n]"
2011/07/08 17:16:33:781 JST [DEBUG] header - << "Content-Type: text/html[r][n]"
2011/07/08 17:16:33:781 JST [DEBUG] header - << "[r][n]"
status:200
2011/07/08 17:16:52:785 JST [DEBUG] header - << "[r][n]"
2011/07/08 17:16:52:786 JST [DEBUG] HttpMethodBase - Should close connection in response to directive: close
2011/07/08 17:16:52:786 JST [DEBUG] HttpConnection - Releasing connection back to connection manager.

  addRequestHeader("Accept-Encoding", "gzip")すると

2011/07/08 17:43:23:691 JST [DEBUG] header - << "Vary: Accept-Encoding[r][n]"
2011/07/08 17:43:23:691 JST [DEBUG] header - << "Content-Encoding: gzip[r][n]"
2011/07/08 17:43:23:691 JST [DEBUG] header - << "Connection: close[r][n]"
2011/07/08 17:43:23:691 JST [DEBUG] header - << "Transfer-Encoding: chunked[r][n]"
2011/07/08 17:43:23:691 JST [DEBUG] header - << "Content-Type: text/html[r][n]"
2011/07/08 17:43:23:692 JST [DEBUG] header - << "[r][n]"
status:200
2011/07/08 17:43:58:939 JST [DEBUG] header - << "[r][n]"