Welcome Guest, Not a member yet? Register   Sign In
[Solved] Automatic generation of content-length header for CI views?
#1

[eluser]ch5i[/eluser]
Hello,

I've been researching a caching issue lately (SSL, no-cache & IE) when I came across a nice caching tutorial by mark nottingham.

There is one section about writing cache-aware scripts. In the last paragraph he recommends to generate Content-Length response headers:
Quote:Generate Content-Length response headers. It’s easy to do, and it will allow the response of your script to be used in a persistent connection. This allows clients to request multiple representations on one TCP/IP connection, instead of setting up a connection for every request. It makes your site seem much faster.
(Source)

Sounds logical enough, but I'm not very familiar with the HTTP and TCP/IP nor with the internal workings of CI, so I can't really judge if this is worth implementing...

Any opinions?


Wikipedia says
Quote:HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using the same TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair.
(Source)

and in Sun's Java Doc
Quote:What makes a connection reusable?

Since TCP by its nature is a stream based protocol, in order to reuse an existing connection, the HTTP protocol has to have a way to indicate the end of the previous response and the beginning of the next one. Thus, it is required that all messages on the connection MUST have a self-defined message length (i.e., one not defined by closure of the connection). Self demarcation is achieved by either setting the Content-Length header, or in the case of chunked transfer encoded entity body, each chunk starts with a size, and the response body ends with a special last chunk.
(Source)

I guess in CI it could be done along these lines:
Code:
$length = ob_get_length();
header("Content-Length: $length");

I'm not very familiar with the internal workings of CI, but I guess the right place for something like this would be in the Loader library (Loader.php) somewhere around line 700?
Code:
/*
         * Flush the buffer... or buff the flusher?
         *
         * In order to permit views to be nested within
         * other views, we need to flush the content back out whenever
         * we are beyond the first level of output buffering so that
         * it can be seen and included properly by the first included
         * template and any subsequent ones. Oy!
         *
         */    
        if (ob_get_level() > $this->_ci_ob_level + 1)
        {
            ob_end_flush();
        }
        else
        {
            // PHP 4 requires that we use a global
            global $OUT;
            $OUT->append_output(ob_get_contents());
            @ob_end_clean();
        }

br,
Thomas
#2

[eluser]gtech[/eluser]
CI will create the headers for you, and the content length is already in there for you. If you download firefox, and then download firebug its great for debugging javascript.. but it also shows you what headers are being generated in the net tab.


here is a typical header from a controller.

Code:
Date  Wed, 10 Jun 2009 20:29:30 GMT
Server  Apache/2.2.9 (Win32) DAV/2 mod_ssl/2.2.9 OpenSSL/0.9.8h mod_autoindex_color PHP/5.2.6
X-Powered-By PHP/5.2.6
Set-Cookie ci_session=************ removed ********
Content-Length    267
Keep-Alive  timeout=5, max=100
Connection  Keep-Alive
Content-Type  text/html

so if my understanding is correct it looks like you do not have to do anything.
#3

[eluser]ch5i[/eluser]
Thanks for your reply, gtech.

I use firebug and was looking at the response headers, that's why I started wondering about the content-length header.

Here's the response headers from two live sites running CI, both do not send Content-Length (and both have Transfer-Encoding: chunked...?)

Code:
Date: Thu, 11 Jun 2009 08:18:54 GMT
Server: Apache
Cache-Control: must-revalidate, post-check=0, pre-check=0
Expires: 0
Pragma: public
X-Powered-By: PHP/5.2.6
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

Code:
Date: Thu, 11 Jun 2009 08:11:28 GMT
Server: Apache
Cache-Control: no-cache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
X-Powered-By: PHP/4.4.9
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

From my first post:
Quote:Self demarcation is achieved by either setting the Content-Length header, or in the case of chunked transfer encoded entity body, each chunk starts with a size, and the response body ends with a special last chunk.

=> Does this mean that Content-Length is not needed when transfer-encoding is chunked?

I don't think the header is being set in CI, at least I was not able to find it in the core code (other than in download helper, trackback, xmlrpc/s).

A configuration issue maybe?

br,
Thomas
#4

[eluser]gtech[/eluser]
interesting maybee php does it, I will have a look when I get some time.
#5

[eluser]gtech[/eluser]
ah ha this might help

[url="http://www.httpwatch.com/httpgallery/chunked/"]http://www.httpwatch.com/httpgallery/chunked/[/url]

looks like you do not use content-length when using chunked encoding, as the size of the body is already determined in pre-defined chunk sizes.

Infact http1.1 specs suggests you should not use content-length when sending chunked data, and this was a bug in old versions of apache.
#6

[eluser]gtech[/eluser]
it looks like apache determines which headers should be sent,

[url="http://bytes.com/groups/php/10395-chunked-encoding-php-apache2"]http://bytes.com/groups/php/10395-chunked-encoding-php-apache2[/url] see Chung Leong's response.

As you notice I am using apache2 and it looks like you are using apache, so that might explain the difference in headers..

I am not entirely sure how to configure apache server to use content-length header, its difficult to find searching google. If I find out I will update this reply rather than flooding the bboard.
#7

[eluser]ch5i[/eluser]
Thanks gtech,

learned something new today.

Quote:http://bytes.com/groups/php/10395-chunke...hp-apache2 see Chung Leong’s response

Apache takes care of the correct content length headers (at least for text/html), so there is no point in trying to add them in CI.

In my installation the threshold for chunked transfer-encoding seems to lie at 8000bytes of content:

8000 bytes of content:
Code:
Date: Thu, 11 Jun 2009 17:57:44 GMT
Server: Apache/2.2.6 (Win32) DAV/2 mod_ssl/2.2.6 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.4
X-Powered-By: PHP/5.2.4
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 8000
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html

8001 bytes of content:
Code:
Date: Thu, 11 Jun 2009 17:58:47 GMT
Server: Apache/2.2.6 (Win32) DAV/2 mod_ssl/2.2.6 OpenSSL/0.9.8e mod_autoindex_color PHP/5.2.4
X-Powered-By: PHP/5.2.4
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

br,
Thomas
#8

[eluser]gtech[/eluser]
cool glad you got somewhere.. in that link I gave you it mentioned somthing about the php output buffer determing the chunked threshold, in my php5.ini file output_buffering=off... what is it on your installation?

does it make any difference?
#9

[eluser]ch5i[/eluser]
I have output_buffering=off as well.

Set it to 4096, didn't change anything - maybe there is an option somewhere in httpd.conf?
For me that's good enough, as long as it works Smile




Theme © iAndrew 2016 - Forum software by © MyBB