1

squid を使用して Web ページ リクエストのページ コンテンツを変更しようとしています。ページ上の画像を反転する方法を示す逆さまの ternetチュートリアルに従いました。

ページの実際の html を変更する必要があります。チュートリアルと同じことをしようとしてきましたが、画像を編集する代わりに、html ページを編集しようとしています。以下は、私がそれを行うために使用しているphpスクリプトです。

すべての jpg 画像は反転されますが、ページのコンテンツは編集されません。書き込まれた編集済みの index.html ファイルには編集済みのコンテンツが含まれていますが、ユーザーが受け取るページには編集済みのコンテンツが含まれていません。

#!/usr/bin/php
<?php
$temp = array();
while ( $input = fgets(STDIN) ) {
    $micro_time = microtime();

    // Split the output (space delimited) from squid into an array.
    $temp = split(' ', $input);

    //Flip jpg images, this works correctly
    if (preg_match("/.*\.jpg/i", $temp[0])) {
        system("/usr/bin/wget -q -O /var/www/cache/$micro_time.jpg ". $temp[0]);
        system("/usr/bin/mogrify -flip /var/www/cache/$micro_time.jpg");
        echo "http://127.0.0.1/cache/$micro_time.jpg\n";
    }

    //Don't edit files that are obviously not html. $temp[0] contains url of file to get
    elseif (preg_match("/(jpg|png|gif|css|js|\(|\))/i", $temp[0], $matches)) {
        echo $input;
    }   

    //Otherwise, could be html (e.g. `wget http://www.google.com` downloads index.html)
    else{ 
        $time = time() . microtime();       //For unique directory names
        $time = preg_replace("/ /", "", $time); //Simplify things by removing the spaces
        mkdir("/var/www/cache/". $time);    //Create unique folder
        system("/usr/bin/wget -q --directory-prefix=\"/var/www/cache/$time/\" ". $temp[0]);
        $filename = system("ls /var/www/cache/$time/");     //Get filename of downloaded file

        //File is html, edit the content (this does not work)
        if(preg_match("/.*\.html/", $filename)){

            //Get the html file contents  
            $contentfh = fopen("/var/www/cache/$time/". $filename, 'r');
            $content = fread($contentfh, filesize("/var/www/cache/$time/". $filename));
            fclose($contentfh);

            //Edit the html file contents
            $content = preg_replace("/<\/body>/i", "<!-- content served by proxy --></body>", $content);

            //Write the edited file
            $contentfh = fopen("/var/www/cache/$time/". $filename, 'w');
            fwrite($contentfh, $content);
            fclose($contentfh);

            //Return the edited page
            echo "http://127.0.0.1/cache/$time/$filename\n";
        }               
        //Otherwise file is not html, don't edit
        else{
            echo $input;
        }
    }
}
?>
4

2 に答える 2

0

それが問題の原因かどうかはわかりませんが、コードにはかなりの誤りがあります。

マイクロタイムに基づいてリクエストを分離します - これはトラフィック量が比較的少ない場合にのみ確実に機能します - リダイレクタの複数のインスタンスが実行されている場合、元の (perl) コードが壊れる可能性があることに注意してください。

ファイル拡張子に基づいてコンテンツ タイプを識別しようとしました - これはリストに一致するファイルに対して機能します - しかし、リストに一致しないものは text/html でなければならないということには従いません - 本当に確認する必要がありますオリジンサーバーから返された MIME タイプ。

コードにエラー チェック/デバッグがありません。簡単に書き込めるエラー ストリームはありませんが、エラーをファイルや syslog に書き込んだり、fopen/ fread ステートメントが機能しないか、保存されたファイルに .html 拡張子がない場合。

C.

于 2010-03-24T13:00:57.077 に答える
0

ダンスガーディアンを見てください。PCRE を使用してその場でコンテンツを変更します:リンク(最後の 2 つのトピックを参照)

于 2010-03-24T10:12:15.673 に答える