Connecting Tech Pros Worldwide Forums | Help | Site Map

trouble measuring filesize of gzipped data

Marek Möhling
Guest
 
Posts: n/a
#1: Jul 17 '05
My server (Apache/1.3.28 - PHP/4.3.3)
is configured to receive gzipped data via:
Header append Accept-Encoding "gzip, deflate"

PHP is configured to send gzipped data via:
php_value output_handler ob_gzhandler

When downloading files from servers that send gzipped data as well,
I'd like to get the filesize before and after my server decompresses them.

============================================
// same result with fopen()
$file = file_get_contents("http://someUrl");
============================================

strlen($file) yields the decompressed filesize, however.
How can I measure the filesize of the compressed data?


Anyone any idea?
Marek

bn548mm@g214mx.net
(remove numbers to despam)



Senator Jay Billington Bulworth
Guest
 
Posts: n/a
#2: Jul 17 '05

re: trouble measuring filesize of gzipped data


"Marek Möhling" <nospam@nospam.org> wrote in
news:cu1fmh$rk8$05$1@news.t-online.com:
[color=blue]
> My server (Apache/1.3.28 - PHP/4.3.3)
> is configured to receive gzipped data via:
> Header append Accept-Encoding "gzip, deflate"
>
> PHP is configured to send gzipped data via:
> php_value output_handler ob_gzhandler
>
> When downloading files from servers that send gzipped data as well,
> I'd like to get the filesize before and after my server decompresses
> them.
>
> ============================================
> // same result with fopen()
> $file = file_get_contents("http://someUrl");
> ============================================
>
> strlen($file) yields the decompressed filesize, however.
> How can I measure the filesize of the compressed data?[/color]

This won't be 100% accurate, but you could estimate the gzipped size
using:

strlen(gzcompress($file));

Also, an "I'm not sure that does what you think it does" note. The Apache
directive only applies when people are requesting web pages from your
server. This is good, and can save you some bandwidth, but it doesn't have
any effect on your PHP scripts when they download remote files. Your
scripts are downloading the raw, uncompressed data.

hth


--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources
Marek Möhling
Guest
 
Posts: n/a
#3: Jul 17 '05

re: trouble measuring filesize of gzipped data


> This won't be 100% accurate, but you could estimate the gzipped size[color=blue]
> using:
> strlen(gzcompress($file));[/color]

Thanks governor, an obvious solution, I was sitting on my brains...-) I
guess.
Then again, this would give a false result when the remote server was
sending uncompressed data.
[color=blue]
> Also, an "I'm not sure that does what you think it does" note. The Apache
> directive only applies when people are requesting web pages from your
> server. This is good, and can save you some bandwidth, but it doesn't have
> any effect on your PHP scripts when they download remote files. Your
> scripts are downloading the raw, uncompressed data.[/color]

IMO when the remote server is configured to sent compressed data,
that's what
a) my server
b) my PHP script
get. I guess that my server handles the decoding and
passes it to my script.

http://web-sniffer.net
detects correctly wether a server uses compression or not.
I checked it by requesting my domain with:
"php_value output_handler ob_gzhandler"
set and unset by htaccess.

So somehow it's possible...
I guess that opening a socket myself using PEAR:Net would be needed,
(bypassing file_get_contents and it ilk) but I can't figure out the
details.




Marek

bn548mm@g214mx.net
(remove numbers to despam)







"Senator Jay Billington Bulworth" <f@fung.arg> wrote in message
news:Xns95F3E4FB51ECDCANDLETRUCK@65.24.7.150...[color=blue]
> "Marek Möhling" <nospam@nospam.org> wrote in
> news:cu1fmh$rk8$05$1@news.t-online.com:
>[color=green]
>> My server (Apache/1.3.28 - PHP/4.3.3)
>> is configured to receive gzipped data via:
>> Header append Accept-Encoding "gzip, deflate"
>>
>> PHP is configured to send gzipped data via:
>> php_value output_handler ob_gzhandler
>>
>> When downloading files from servers that send gzipped data as well,
>> I'd like to get the filesize before and after my server decompresses
>> them.
>>
>> ============================================
>> // same result with fopen()
>> $file = file_get_contents("http://someUrl");
>> ============================================
>>
>> strlen($file) yields the decompressed filesize, however.
>> How can I measure the filesize of the compressed data?[/color]
>
> This won't be 100% accurate, but you could estimate the gzipped size
> using:
>
> strlen(gzcompress($file));
>
> Also, an "I'm not sure that does what you think it does" note. The Apache
> directive only applies when people are requesting web pages from your
> server. This is good, and can save you some bandwidth, but it doesn't have
> any effect on your PHP scripts when they download remote files. Your
> scripts are downloading the raw, uncompressed data.
>
> hth
>
>
> --
> Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
> --------------------------|---------------------------------
> <http://www.phplabs.com/> | PHP scripts, webmaster resources[/color]




Senator Jay Billington Bulworth
Guest
 
Posts: n/a
#4: Jul 17 '05

re: trouble measuring filesize of gzipped data


"Marek Möhling" <nospam@nospam.org> wrote in
news:cu1kc2$1jd$03$1@news.t-online.com:
[color=blue][color=green]
>> Also, an "I'm not sure that does what you think it does"[/color][/color]
note. The[color=blue][color=green]
>> Apache directive only applies when people are requesting web[/color][/color]
pages[color=blue][color=green]
>> from your server. This is good, and can save you some[/color][/color]
bandwidth, but[color=blue][color=green]
>> it doesn't have any effect on your PHP scripts when they[/color][/color]
download[color=blue][color=green]
>> remote files. Your scripts are downloading the raw,[/color][/color]
uncompressed[color=blue][color=green]
>> data.[/color]
>
> IMO when the remote server is configured to sent compressed[/color]
data,[color=blue]
> that's what
> a) my server
> b) my PHP script
> get. I guess that my server handles the decoding and
> passes it to my script.[/color]

It's sort of the other way around. When your script sends a
request to
another website, that request isn't coming from Apache, it's
coming from
PHP. PHP then sends the result of your script to Apache, and
Apache
sends it to your web browser.
[color=blue]
> So somehow it's possible...
> I guess that opening a socket myself using PEAR:Net would be[/color]
needed,[color=blue]
> (bypassing file_get_contents and it ilk) but I can't figure[/color]
out the[color=blue]
> details.[/color]

There is probably a way to do it via sockets. I played around
but couldn't figure out how to decode the gzipped data properly,
gzuncompress() gave me a data error. I found this suggestion
<http://bugs.php.net/bug.php?id=28051&edit=1> which might help
you out.

hth


--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources
Marek Möhling
Guest
 
Posts: n/a
#5: Jul 17 '05

re: trouble measuring filesize of gzipped data


Thanks for your help,
I sorted it out, putting bits & pieces together;
all hail to the internet!
....It helps us to solve trouble we wouldn't have without it %&$§!!!


<?php

// my server sends gzipped data if client allows
$url = "www.byteshift.de";

// see: de.php.net/manual/en/function.gzencode.php
// there might be better versions
function gzdecode($string){
$string = substr($string, 10);
return gzinflate($string);
}

function get_gzipped_data($url){
$http_response = '';
$fp = fsockopen($url, 80);
fputs($fp, "GET / HTTP/1.1\r\n");
fputs($fp, "Accept-Encoding: gzip\r\n");

fputs($fp, "Host: $url\r\n\r\n");
while (!feof($fp))
$http_response .= fgets($fp, 128);
fclose($fp);
return $http_response;
}

preg_match("/^(.+)\r?\n\r?\n\w+\r?\n(.+)$/s",
get_gzipped_data($url),
$matches);
$header = $matches[1];
$body = $matches[2];
$html = gzdecode($body);
$strlen_uncomp = strlen(file_get_contents("http://$url/"));
$strlen_decomp = strlen($body);

echo "
strlen_uncomp: $strlen_uncomp Kb
strlen_decomp: $strlen_decomp Kb
=============================
$html
";
?>


Marek

bn548mm@g214mx.net
(remove numbers to despam)


Closed Thread