public
Description: jQuery Douban plugin
Homepage: http://code.google.com/p/jquery-douban
Clone URL: git://github.com/wuyuntao/jquery-douban.git
jquery-douban / apps / proxy.py
100644 61 lines (49 sloc) 1.668 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: UTF-8 -*-
import logging
import re
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
 
DEBUG = True
 
class DoubanProxy(webapp.RequestHandler):
    """ Simple cross-origin proxy for Gears requests to douban """
    def get(self):
        proxy(self)
    def post(self):
        proxy(self)
    def put(self):
        proxy(self)
    def delete(self):
        proxy(self)
 
re_douban = re.compile(r'^http:\/\/(\w+\.douban\.com)(\/.*)$', re.I)
 
def proxy(handler):
    url = handler.request.get('url')
    # If URL is not douban, throw a 403 error
    if not re_douban.match(url):
        return handler.error(403)
 
    # Set correct headers
    headers = {
        'Authorization': handler.request.headers.get('Authorization', ''),
        'Content-Type': handler.request.headers.get('Content-Type', ''),
    }
 
    # Set upload data
    payload = handler.request.body
 
    try:
        # Fetch result
        response = urlfetch.fetch(payload=payload, url=url, method=handler.request.method, headers=headers)
 
        # Set status code and return the response
        handler.response.set_status(response.status_code)
        return handler.response.out.write(response.content)
    except:
        logging.error("Failed to %s url: %s" % (handler.request.method, url))
        # If failed to get response from douban, throw a 400 error
        return handler.error(400)
 
# URL patterns
urls = (
    ('/proxy', DoubanProxy),
)
 
def main():
    application = webapp.WSGIApplication(urls, debug=DEBUG)
    run_wsgi_app(application)
 
if __name__ == '__main__':
    main()