Bläddra i källkod

Merge pull request #182 from dalf/enh-proxy

[enh] image-proxy : handle ETag and date related headers, add hash to URL
Adam Tauber 10 år sedan
förälder
incheckning
285f991cd0

+ 2
- 2
searx/templates/courgette/result_templates/code.html Visa fil

@@ -1,9 +1,9 @@
1 1
 <div class="result {{ result.class }}">
2 2
     <h3 class="result_title">{% if result['favicon'] %}<img width="14" height="14" class="favicon" src="static/{{theme}}/img/icon_{{result['favicon']}}.ico" alt="{{result['favicon']}}" />{% endif %}<a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
3 3
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span>{% endif %}
4
-    <p class="content">{% if result.img_src %}<img src="{{ result.img_src }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
4
+    <p class="content">{% if result.img_src %}<img src="{{ image_proxify(result.img_src) }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
5 5
     {% if result.repository %}<p class="content"><a href="{{ result.repository|safe }}">{{ result.repository }}</a></p>{% endif %}
6 6
     {{ result.codelines|code_highlighter(result.code_language)|safe }}
7 7
 
8 8
     <p class="url">{{ result.pretty_url }}</p>
9
-</div>
9
+</div>

+ 2
- 2
searx/templates/courgette/result_templates/videos.html Visa fil

@@ -5,6 +5,6 @@
5 5
 
6 6
     <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
7 7
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span><br />{% endif %}
8
-    <a href="{{ result.url }}"><img width="400" src="{{ result.thumbnail }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
8
+    <a href="{{ result.url }}"><img width="400" src="{{ image_proxify(result.thumbnail) }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
9 9
     <p class="url">{{ result.pretty_url }}</p>
10
-</div>
10
+</div>

+ 1
- 1
searx/templates/default/infobox.html Visa fil

@@ -1,6 +1,6 @@
1 1
 <div class="infobox">
2 2
     <h2>{{ infobox.infobox }}</h2>
3
-    {% if infobox.img_src %}<img src="{{ infobox.img_src }}" title="{{ infobox.infobox|striptags }}" alt="{{ infobox.infobox|striptags }}" />{% endif %}
3
+    {% if infobox.img_src %}<img src="{{ image_proxify(infobox.img_src) }}" title="{{ infobox.infobox|striptags }}" alt="{{ infobox.infobox|striptags }}" />{% endif %}
4 4
     <p>{{ infobox.entity }}</p>
5 5
     <p>{{ infobox.content | safe }}</p>
6 6
     {% if infobox.attributes %}

+ 1
- 1
searx/templates/default/result_templates/code.html Visa fil

@@ -2,7 +2,7 @@
2 2
     <h3 class="result_title"> {% if result['favicon'] %}<img width="14" height="14" class="favicon" src="static/{{theme}}/img/icon_{{result['favicon']}}.ico" alt="{{result['favicon']}}" />{% endif %}<a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
3 3
     <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a></p>
4 4
     {% if result.publishedDate %}<p class="published_date">{{ result.publishedDate }}</p>{% endif %}
5
-    <p class="content">{% if result.img_src %}<img src="{{ result.img_src }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
5
+    <p class="content">{% if result.img_src %}<img src="{{ image_proxify(result.img_src) }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
6 6
     {% if result.repository %}<p class="result-content"><a href="{{ result.repository|safe }}">{{ result.repository }}</a></p>{% endif %}
7 7
 
8 8
     {{ result.codelines|code_highlighter(result.code_language)|safe }}

+ 1
- 1
searx/templates/default/result_templates/default.html Visa fil

@@ -2,5 +2,5 @@
2 2
     <h3 class="result_title">{% if "icon_"~result.engine~".ico" in favicons %}<img width="14" height="14" class="favicon" src="{{ url_for('static', filename='img/icons/icon_'+result.engine+'.ico') }}" alt="{{result.engine}}" />{% endif %}<a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
3 3
     <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a>
4 4
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span>{% endif %}</p>
5
-    <p class="content">{% if result.img_src %}<img src="{{ result.img_src }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
5
+    <p class="content">{% if result.img_src %}<img src="{{ image_proxify(result.img_src) }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
6 6
 </div>

+ 1
- 1
searx/templates/default/result_templates/map.html Visa fil

@@ -8,6 +8,6 @@
8 8
         <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
9 9
         <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a>
10 10
         {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span>{% endif %}</p>
11
-        <p class="content">{% if result.img_src %}<img src="{{ result.img_src }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
11
+        <p class="content">{% if result.img_src %}<img src="{{ image_proxify(result.img_src) }}" class="image" />{% endif %}{% if result.content %}{{ result.content|safe }}<br class="last"/>{% endif %}</p>
12 12
     </div>
13 13
 </div>

+ 1
- 1
searx/templates/default/result_templates/videos.html Visa fil

@@ -1,6 +1,6 @@
1 1
 <div class="result">
2 2
     <h3 class="result_title">{% if "icon_"~result.engine~".ico" in favicons %}<img width="14" height="14" class="favicon" src="{{ url_for('static', filename='img/icons/icon_'+result.engine+'.ico') }}" alt="{{result.engine}}" />{% endif %}<a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
3 3
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span><br />{% endif %}
4
-    <a href="{{ result.url }}"><img class="thumbnail" src="{{ result.thumbnail }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
4
+    <a href="{{ result.url }}"><img class="thumbnail" src="{{ image_proxify(result.thumbnail) }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
5 5
     <p class="url">{{ result.url }}</p>
6 6
 </div>

+ 1
- 1
searx/templates/oscar/infobox.html Visa fil

@@ -3,7 +3,7 @@
3 3
         <h4 class="panel-title">{{ infobox.infobox }}</h4>
4 4
     </div>
5 5
     <div class="panel-body">
6
-        {% if infobox.img_src %}<img class="img-responsive center-block infobox_part" src="{{ infobox.img_src }}" alt="{{ infobox.infobox }}" />{% endif %}
6
+        {% if infobox.img_src %}<img class="img-responsive center-block infobox_part" src="{{ image_proxify(infobox.img_src) }}" alt="{{ infobox.infobox }}" />{% endif %}
7 7
         {% if infobox.content %}<p class="infobox_part">{{ infobox.content }}</p>{% endif %}
8 8
 
9 9
         {% if infobox.attributes %}

+ 1
- 1
searx/templates/oscar/result_templates/videos.html Visa fil

@@ -15,7 +15,7 @@
15 15
 
16 16
 <div class="container-fluid">
17 17
     <div class="row">
18
-        <a href="{{ result.url }}"><img class="thumbnail col-xs-6 col-sm-4 col-md-4 result-content" src="{{ result.thumbnail|safe }}" alt="{{ result.title|striptags }} {{ result.engine }}" /></a>
18
+        <a href="{{ result.url }}"><img class="thumbnail col-xs-6 col-sm-4 col-md-4 result-content" src="{{ image_proxify(result.thumbnail) }}" alt="{{ result.title|striptags }} {{ result.engine }}" /></a>
19 19
         {% if result.content %}<p class="col-xs-12 col-sm-8 col-md-8 result-content">{{ result.content|safe }}</p>{% endif %}
20 20
     </div>
21 21
 </div>

+ 8
- 0
searx/utils.py Visa fil

@@ -206,3 +206,11 @@ def format_date_by_locale(date_string, locale_string):
206 206
     except:
207 207
         logger.warning('cannot set original locale: {0}'.format(orig_locale))
208 208
     return formatted_date
209
+
210
+
211
+def dict_subset(d, properties):
212
+    result = {}
213
+    for k in properties:
214
+        if k in d:
215
+            result[k] = d[k]
216
+    return result

+ 20
- 4
searx/webapp.py Visa fil

@@ -25,6 +25,7 @@ if __name__ == '__main__':
25 25
 import json
26 26
 import cStringIO
27 27
 import os
28
+import hashlib
28 29
 
29 30
 from datetime import datetime, timedelta
30 31
 from requests import get as http_get
@@ -41,7 +42,7 @@ from searx.engines import (
41 42
 )
42 43
 from searx.utils import (
43 44
     UnicodeWriter, highlight_content, html_to_text, get_themes,
44
-    get_static_files, get_result_templates, gen_useragent
45
+    get_static_files, get_result_templates, gen_useragent, dict_subset
45 46
 )
46 47
 from searx.version import VERSION_STRING
47 48
 from searx.languages import language_codes
@@ -216,8 +217,10 @@ def image_proxify(url):
216 217
     if not settings['server'].get('image_proxy') and not request.cookies.get('image_proxy'):
217 218
         return url
218 219
 
220
+    h = hashlib.sha256(url + settings['server']['secret_key']).hexdigest()
221
+
219 222
     return '{0}?{1}'.format(url_for('image_proxy'),
220
-                            urlencode(dict(url=url)))
223
+                            urlencode(dict(url=url, h=h)))
221 224
 
222 225
 
223 226
 def render(template_name, override_theme=None, **kwargs):
@@ -562,10 +565,21 @@ def image_proxy():
562 565
     if not url:
563 566
         return '', 400
564 567
 
568
+    h = hashlib.sha256(url + settings['server']['secret_key']).hexdigest()
569
+
570
+    if h != request.args.get('h'):
571
+        return '', 400
572
+
573
+    headers = dict_subset(request.headers, {'If-Modified-Since', 'If-None-Match'})
574
+    headers['User-Agent'] = gen_useragent()
575
+
565 576
     resp = http_get(url,
566 577
                     stream=True,
567 578
                     timeout=settings['server'].get('request_timeout', 2),
568
-                    headers={'User-Agent': gen_useragent()})
579
+                    headers=headers)
580
+
581
+    if resp.status_code == 304:
582
+        return '', resp.status_code
569 583
 
570 584
     if resp.status_code != 200:
571 585
         logger.debug('image-proxy: wrong response code: {0}'.format(resp.status_code))
@@ -586,7 +600,9 @@ def image_proxy():
586 600
             return '', 502  # Bad gateway - file is too big (>5M)
587 601
         img += chunk
588 602
 
589
-    return Response(img, mimetype=resp.headers['content-type'])
603
+    headers = dict_subset(resp.headers, {'Content-Length', 'Length', 'Date', 'Last-Modified', 'Expires', 'Etag'})
604
+
605
+    return Response(img, mimetype=resp.headers['content-type'], headers=headers)
590 606
 
591 607
 
592 608
 @app.route('/stats', methods=['GET'])