Forum Moderators: coopster

Message Too Old, No Replies

Conteg v0.13 available on PHP Classes

Content-Negotiation PHP Class available for download

         

AlexK

7:13 pm on Mar 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Info on Content Negotiation:
Most folks reading this post will have a fair idea about Content-Negotiation, but it does no harm to re-iterate it, in simple fashion. Search for the "Live HTTP headers" extension for FireFox to be able to follow this on your own computer.

A browsers requests a static html file:
The browser sends a "Request header" for the file. This includes headers which state the browser capability. They look something like this:

GET /download/Conteg.include.txt HTTP/1.1
Host: www.example.co.uk
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Mnenhy/0.7.2.0
Accept: application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;<my-break>
<continued>q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.webmasterworld.com/php/3201440.htm

The web-server examines these headers to discover what the browser can and cannot accept--which is referred to as the process of "Content-Negotiation" (I think it should be "Header-Negotiation")--and responds with another set of headers (the "Response headers") before the actual content. Note that the headers are always in plain-text (HTTP), whilst the content may not be.

This is what the "Response headers" to the above look like:

HTTP/1.x 200 OK
Date: Sat, 10 Mar 2007 18:35:02 GMT
Server: Apache
Last-Modified: Wed, 21 Feb 2007 07:13:27 GMT
Etag: "2d7a87-19632-4b7d33c0"
Accept-Ranges: bytes
Content-Length: 103986
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/plain

The interesting feature comes when the page is re-requested (perhaps the <Back> button is pressed). The "Request headers" now include 3 extra lines:

GET /download/Conteg.include.txt HTTP/1.1
Host: www.example.co.uk
...
If-Modified-Since: Wed, 21 Feb 2007 07:13:27 GMT
If-None-Match: "2d7a87-19632-4b7d33c0"
Cache-Control: max-age=0

...and the response is accordingly utterly different:

HTTP/1.x 304 Not Modified
Date: Sat, 10 Mar 2007 18:18:58 GMT
Server: Apache
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Etag: "2d7a87-19632-4b7d33c0"
Vary: Host

In this case, that has saved the site a 100K+ download, plus the browser seems far more responsive.

Now, here is the kicker:

Webservers do this by default for all static files. They never do it for dynamic (PHP) content.

Conteg is designed to replace that missing functionality.

Here is the change-log:

Change Log:
  • 05 Mar 07 : 0.13.1: Added 'cpu_number' to Constructor parameter ($_num_cpu) bugfix for $referer (['uri'] not included in array)
  • 18 Feb 07 : 0.13: Added $referer, getReferer(), '404_to_410', $_no410, 'http_status', $_httpStatus, 'msie_error_fix', $_noMSErrorFix + sendStatusHeader() bugfix.
  • 04 Oct 06 : 0.12.3: Bugfix for 'expiry' (was always 1 hr) (thank you Bob).
  • 18 Aug 06 : 0.12.2: Added $_num_cpu for SMP boxes (like mine!).
  • 02 Ju1 06 : 0.12.1: Added sendStatusHeader() + 404 responses (used to create custom Error-pages).
  • 04 Jun 06 : 0.12: Added requestNoCache(), requestNoStore(), $_cache_control_response + $_cache_control_request arrays.
  • 13 Mar 06 : 0.11: BugFix in _initResponse() [INM not tripped but IMS is];
  • 23 Feb 06 : 0.11: 'other_var' added to setup() (ref: weak eTags) (bugfix).
  • 25 Aug 05 : 0.10: Gzip_encode rewritten; Request + Response headers added.
  • 16 Sep 05 : 0.10: Content-Negotiation completed for compression-Encoding.
  • 09 Sep 05 : 0.10: Browser detect added to negotiateEncoding() to fix blanks.

[edited by: coopster at 5:37 pm (utc) on Mar. 12, 2007]
[edit reason] updated url to phpclasses page [/edit]

AlexK

9:19 am on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Very bad form to reply to your own post 2 times in a row, but this is to announce that the "bare" Conteg download is now v0.13.1:(that download does *not* include the help-files available at PHP-Classes)

The changes are:

  1. $_num_cpu
    is now set by the
    setup()
    parameter
    'cpu_number'
    (default 1).
  2. The Referer array returned by
    getReferer()
    now includes
    [ 'uri' ]
    as it was always meant to (bugfix).

(1) above means there is now no need under any circumstances to change any of the Class coding before using it. People with SMP servers will want to change
'cpu_number'
to the number of CPUs within their machine (affects the load-balanced compression). Note that intel Xeon (and other HT) CPUs report 2 x cpu for each physical cpu, and
'cpu_number'
wants to be changed accordingly; I have no comparable info for Dual-Core CPUs.

PHP-Classes info:
After just 1 day, the "Week Rank" for Conteg is 43 (out of about 4,000 I believe) which is quite remarkable.

[edited by: coopster at 5:43 pm (utc) on Mar. 12, 2007]
[edit reason] updated url to phpclasses page [/edit]

AlexK

2:20 am on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thought I'd give a bump to this page.

In the first week 182 people downloaded the Class, and it was #18 on the weekly list. Most of that was folks coming from this page, so I'm interested to hear any feedback on either Conteg or PHP-Classes, especially since it is my first experience of the latter.

[edited by: coopster at 3:51 am (utc) on Mar. 27, 2007]
[edit reason] removed comment [/edit]

AlexK

3:11 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Manuel Lemos (the operator of PHP-Classes) has nominated Conteg for an "Innovation Award" for March. Now, this may well just be an SEO trick for the site but - hey! - it certainly worked on me (I was chuffed).

Here is the text of the nomination, available on the Conteg package page [phpclasses.org]:

March 2007 Innovation Award Nominee
.
HTTP is a protocol that provides several means to negotiate the content that is served.
.
Negotiating content provides several ways to save bandwidth and server CPU usage by either skip serving cached content or compress the content before serving.
.
This class can be use to perform several content negotiating actions, thus simplifying the process of making Web applications more efficient.
.
Manuel Lemos

I would quibble with the word "several" - there are currently 46 different keys that can be used within the setup parameter array - but still, very nice.

Vote now!