trouble measuring filesize of gzipped data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marek Möhling

    #1

    trouble measuring filesize of gzipped data

    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_conten ts("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

    #2
    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_conten ts("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(gzcompre ss($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@fu ng.arg');
    --------------------------|---------------------------------
    <http://www.phplabs.com/> | PHP scripts, webmaster resources

    Comment

    • Marek Möhling

      #3
      Re: trouble measuring filesize of gzipped data

      > This won't be 100% accurate, but you could estimate the gzipped size[color=blue]
      > using:
      > strlen(gzcompre ss($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.


      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_conten ts 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:Xns95F3E4F B51ECDCANDLETRU CK@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_conten ts("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(gzcompre ss($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@fu ng.arg');
      > --------------------------|---------------------------------
      > <http://www.phplabs.com/> | PHP scripts, webmaster resources[/color]




      Comment

      • Senator Jay Billington Bulworth

        #4
        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_conten ts 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=2805 1&edit=1> which might help
        you out.

        hth


        --
        Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fu ng.arg');
        --------------------------|---------------------------------
        <http://www.phplabs.com/> | PHP scripts, webmaster resources

        Comment

        • Marek Möhling

          #5
          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.gzenco de.php
          // there might be better versions
          function gzdecode($strin g){
          $string = substr($string, 10);
          return gzinflate($stri ng);
          }

          function get_gzipped_dat a($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_dat a($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)


          Comment

          Working...