diff options
author | emkael <emkael@tlen.pl> | 2015-02-09 20:14:59 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2015-02-09 20:14:59 +0100 |
commit | 17167b1c0e088000164b87ef0a02237a3ffc107c (patch) | |
tree | 79c61deeea11e57b3c8e99dc39ca76cee491a745 /http/pic | |
parent | dd3d2509f6048e11f9b2b127f6c7acb80a506d8d (diff) |
* mod_python port
Diffstat (limited to 'http/pic')
-rw-r--r-- | http/pic/.htaccess | 7 | ||||
-rw-r--r-- | http/pic/fetch.php | 13 | ||||
-rw-r--r-- | http/pic/fetch.py | 27 |
3 files changed, 32 insertions, 15 deletions
diff --git a/http/pic/.htaccess b/http/pic/.htaccess index 615bc19..65ceb47 100644 --- a/http/pic/.htaccess +++ b/http/pic/.htaccess @@ -1,6 +1,9 @@ +AddHandler mod_python .py +PythonHandler fetch +PythonDebug On + RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d -RewriteRule .* fetch.php [QSA,L] - +RewriteRule .* fetch.py [QSA,L] diff --git a/http/pic/fetch.php b/http/pic/fetch.php deleted file mode 100644 index 1fb94cd..0000000 --- a/http/pic/fetch.php +++ /dev/null @@ -1,13 +0,0 @@ -<?php - -$resource = @file_get_contents('http://msc.com.pl/cezar' . $_SERVER['REQUEST_URI']); -if ($resource) { - $filename = array_pop(explode('/', $_SERVER['REQUEST_URI'])); - file_put_contents($filename, $resource); - foreach ($http_response_header as $header) { - header($header); - } - readfile($filename); -} - -?> diff --git a/http/pic/fetch.py b/http/pic/fetch.py new file mode 100644 index 0000000..0cc3556 --- /dev/null +++ b/http/pic/fetch.py @@ -0,0 +1,27 @@ +# coding=utf-8 + +from mod_python import apache +import os, urllib2 + +CEZAR_URL = 'http://msc.com.pl/cezar' + +def handler(req): + orig_req = req + while True: + if orig_req.prev: + orig_req = orig_req.prev + else: + break + + remote_resource = CEZAR_URL + orig_req.uri + request = urllib2.Request(remote_resource) + request.add_header('User-Agent', orig_req.headers_in['User-Agent']) + file_name = os.path.join(os.path.dirname(__file__), remote_resource.split('/')[-1]) + try: + response = urllib2.urlopen(request) + open(file_name, 'w+').write(response.read()) + req.content_type = response.headers['Content-Type'] + req.write(open(file_name, 'r').read()) + return apache.OK + except urllib2.URLError: + return apache.HTTP_NOT_FOUND |