소스 검색

Merge pull request #182 from dalf/enh-proxy

[enh] image-proxy : handle ETag and date related headers, add hash to URL
Adam Tauber 10 년 전
부모
커밋
285f991cd0

+ 2
- 2
searx/templates/courgette/result_templates/code.html 파일 보기

1
 <div class="result {{ result.class }}">
1
 <div class="result {{ result.class }}">
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>
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
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span>{% endif %}
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
     {% if result.repository %}<p class="content"><a href="{{ result.repository|safe }}">{{ result.repository }}</a></p>{% endif %}
5
     {% if result.repository %}<p class="content"><a href="{{ result.repository|safe }}">{{ result.repository }}</a></p>{% endif %}
6
     {{ result.codelines|code_highlighter(result.code_language)|safe }}
6
     {{ result.codelines|code_highlighter(result.code_language)|safe }}
7
 
7
 
8
     <p class="url">{{ result.pretty_url }}</p>
8
     <p class="url">{{ result.pretty_url }}</p>
9
-</div>
9
+</div>

+ 2
- 2
searx/templates/courgette/result_templates/videos.html 파일 보기

5
 
5
 
6
     <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
6
     <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
7
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span><br />{% endif %}
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
     <p class="url">{{ result.pretty_url }}</p>
9
     <p class="url">{{ result.pretty_url }}</p>
10
-</div>
10
+</div>

+ 1
- 1
searx/templates/default/infobox.html 파일 보기

1
 <div class="infobox">
1
 <div class="infobox">
2
     <h2>{{ infobox.infobox }}</h2>
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
     <p>{{ infobox.entity }}</p>
4
     <p>{{ infobox.entity }}</p>
5
     <p>{{ infobox.content | safe }}</p>
5
     <p>{{ infobox.content | safe }}</p>
6
     {% if infobox.attributes %}
6
     {% if infobox.attributes %}

+ 1
- 1
searx/templates/default/result_templates/code.html 파일 보기

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>
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
     <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a></p>
3
     <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a></p>
4
     {% if result.publishedDate %}<p class="published_date">{{ result.publishedDate }}</p>{% endif %}
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
     {% if result.repository %}<p class="result-content"><a href="{{ result.repository|safe }}">{{ result.repository }}</a></p>{% endif %}
6
     {% if result.repository %}<p class="result-content"><a href="{{ result.repository|safe }}">{{ result.repository }}</a></p>{% endif %}
7
 
7
 
8
     {{ result.codelines|code_highlighter(result.code_language)|safe }}
8
     {{ result.codelines|code_highlighter(result.code_language)|safe }}

+ 1
- 1
searx/templates/default/result_templates/default.html 파일 보기

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>
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
     <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a>
3
     <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a>
4
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span>{% endif %}</p>
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
 </div>
6
 </div>

+ 1
- 1
searx/templates/default/result_templates/map.html 파일 보기

8
         <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
8
         <h3 class="result_title"><a href="{{ result.url }}">{{ result.title|safe }}</a></h3>
9
         <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a>
9
         <p class="url">{{ result.pretty_url }} <a class="cache_link" href="https://web.archive.org/web/{{ result.url }}">cached</a>
10
         {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span>{% endif %}</p>
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
     </div>
12
     </div>
13
 </div>
13
 </div>

+ 1
- 1
searx/templates/default/result_templates/videos.html 파일 보기

1
 <div class="result">
1
 <div class="result">
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>
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
     {% if result.publishedDate %}<span class="published_date">{{ result.publishedDate }}</span><br />{% endif %}
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
     <p class="url">{{ result.url }}</p>
5
     <p class="url">{{ result.url }}</p>
6
 </div>
6
 </div>

+ 1
- 1
searx/templates/oscar/infobox.html 파일 보기

3
         <h4 class="panel-title">{{ infobox.infobox }}</h4>
3
         <h4 class="panel-title">{{ infobox.infobox }}</h4>
4
     </div>
4
     </div>
5
     <div class="panel-body">
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
         {% if infobox.content %}<p class="infobox_part">{{ infobox.content }}</p>{% endif %}
7
         {% if infobox.content %}<p class="infobox_part">{{ infobox.content }}</p>{% endif %}
8
 
8
 
9
         {% if infobox.attributes %}
9
         {% if infobox.attributes %}

+ 1
- 1
searx/templates/oscar/result_templates/videos.html 파일 보기

15
 
15
 
16
 <div class="container-fluid">
16
 <div class="container-fluid">
17
     <div class="row">
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
         {% if result.content %}<p class="col-xs-12 col-sm-8 col-md-8 result-content">{{ result.content|safe }}</p>{% endif %}
19
         {% if result.content %}<p class="col-xs-12 col-sm-8 col-md-8 result-content">{{ result.content|safe }}</p>{% endif %}
20
     </div>
20
     </div>
21
 </div>
21
 </div>

+ 8
- 0
searx/utils.py 파일 보기

206
     except:
206
     except:
207
         logger.warning('cannot set original locale: {0}'.format(orig_locale))
207
         logger.warning('cannot set original locale: {0}'.format(orig_locale))
208
     return formatted_date
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 파일 보기

25
 import json
25
 import json
26
 import cStringIO
26
 import cStringIO
27
 import os
27
 import os
28
+import hashlib
28
 
29
 
29
 from datetime import datetime, timedelta
30
 from datetime import datetime, timedelta
30
 from requests import get as http_get
31
 from requests import get as http_get
41
 )
42
 )
42
 from searx.utils import (
43
 from searx.utils import (
43
     UnicodeWriter, highlight_content, html_to_text, get_themes,
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
 from searx.version import VERSION_STRING
47
 from searx.version import VERSION_STRING
47
 from searx.languages import language_codes
48
 from searx.languages import language_codes
216
     if not settings['server'].get('image_proxy') and not request.cookies.get('image_proxy'):
217
     if not settings['server'].get('image_proxy') and not request.cookies.get('image_proxy'):
217
         return url
218
         return url
218
 
219
 
220
+    h = hashlib.sha256(url + settings['server']['secret_key']).hexdigest()
221
+
219
     return '{0}?{1}'.format(url_for('image_proxy'),
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
 def render(template_name, override_theme=None, **kwargs):
226
 def render(template_name, override_theme=None, **kwargs):
562
     if not url:
565
     if not url:
563
         return '', 400
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
     resp = http_get(url,
576
     resp = http_get(url,
566
                     stream=True,
577
                     stream=True,
567
                     timeout=settings['server'].get('request_timeout', 2),
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
     if resp.status_code != 200:
584
     if resp.status_code != 200:
571
         logger.debug('image-proxy: wrong response code: {0}'.format(resp.status_code))
585
         logger.debug('image-proxy: wrong response code: {0}'.format(resp.status_code))
586
             return '', 502  # Bad gateway - file is too big (>5M)
600
             return '', 502  # Bad gateway - file is too big (>5M)
587
         img += chunk
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
 @app.route('/stats', methods=['GET'])
608
 @app.route('/stats', methods=['GET'])