1
2
3
4
5
6
7 from StringIO import StringIO
8 import urlparse
9 import urllib
10
11 try:
12 from webob import Request as BaseRequest
13 except ImportError:
14 raise ImportError('WebOb (http://pypi.python.org/pypi/WebOb) is required')
15
16 from .wsgi_proxy import Proxy
17
18 __doc__ = '''Subclasses of webob.Request who use restkit to get a
19 webob.Response via restkit.ext.wsgi_proxy.Proxy.
20
21 Example::
22
23 >>> req = Request.blank('http://pypi.python.org/pypi/restkit')
24 >>> resp = req.get_response()
25 >>> print resp #doctest: +ELLIPSIS
26 200 OK
27 Date: ...
28 Transfer-Encoding: chunked
29 Content-Type: text/html; charset=utf-8
30 Server: Apache/2...
31 <BLANKLINE>
32 <?xml version="1.0" encoding="UTF-8"?>
33 ...
34
35
36 '''
37
38 PROXY = Proxy(allowed_methods=['GET', 'POST', 'HEAD', 'DELETE', 'PUT', 'PURGE'])
39
43 - def __get__(self, instance, klass):
44 if not instance:
45 return self
46 instance.method = self.name.upper()
47 def req(*args, **kwargs):
48 return instance.get_response(*args, **kwargs)
49 return req
50
51
53 get = Method('get')
54 post = Method('post')
55 put = Method('put')
56 head = Method('head')
57 delete = Method('delete')
59 if self.content_length < 0:
60 self.content_length = 0
61 if self.method in ('DELETE', 'GET'):
62 self.body = ''
63 elif self.method == 'POST' and self.POST:
64 body = urllib.urlencode(self.POST.copy())
65 stream = StringIO(body)
66 stream.seek(0)
67 self.body_file = stream
68 self.content_length = stream.len
69 if 'form' not in self.content_type:
70 self.content_type = 'application/x-www-form-urlencoded'
71 self.server_name = self.host
72 return BaseRequest.get_response(self, PROXY)
73
74 __call__ = get_response
75
77 path = url_or_path.lstrip('/')
78 if '?' in path:
79 path, self.query_string = path.split('?', 1)
80 if path.startswith('http'):
81 url = path
82 else:
83 self.path_info = '/'+path
84 url = self.url
85 self.scheme, self.host, self.path_info = urlparse.urlparse(url)[0:3]
86