Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add wsgi_proxy from @gawel_ + do some fixes

  • Loading branch information...
commit c410da74c4c2ca9a8e9977fe04a854c2d3bf7531 1 parent b3a3cde
@benoitc benoitc authored
View
7 THANKS
@@ -0,0 +1,7 @@
+Restkit THANKS
+=====================
+
+A number of people have contributed to Restkit by reporting problems,
+suggesting improvements or submitting changes. Some of these people are:
+
+Gael Pasgrimaud <gael@gawel.org>
View
40 restkit/ext/wsgi_proxy.py
@@ -1,34 +1,31 @@
-# -*- coding: utf-8 -*-
+# -*- coding: utf-8 -
+#
+# This file is part of restkit released under the MIT license.
+# See the NOTICE for more information.
+
import urlparse
-from restkit import ConnectionPool
-from restkit import request
-from restkit import ResourceNotFound
+from restkit import ConnectionPool, request, ResourceNotFound
from restkit.sock import CHUNK_SIZE
ALLOWED_METHODS = ['GET', 'HEAD']
BLOCK_SIZE = 4096 * 16
-class IterResponse(object):
+class ResponseIter(object):
+
def __init__(self, response):
- self.response = response
- self._len = int(self.response.headers['content-length'])
- self._read = 0
-
- def __iter__(self):
- return self
+ response.CHUNK_SIZE = BLOCK_SIZE
+ self.body = response.body_file
def next(self):
- size = BLOCK_SIZE
- if self._read >= self._len:
+ data = self.body.read(BLOCK_SIZE)
+ if not data:
raise StopIteration
- elif self._read + size > self._len:
- size = self._len - self._read
- self._read = self._len
- else:
- self._read += size
- return self.response.body_file.read(size)
+ return data
+
+ def __iter__(self):
+ return self
class Proxy(object):
"""A proxy wich redirect the request to SERVER_NAME:SERVER_PORT and send HTTP_HOST header"""
@@ -101,10 +98,7 @@ def __call__(self, environ, start_response):
start_response(response.status, response.http_client.parser.headers)
- if 'content-length' in response:
- return IterResponse(response)
- else:
- return [response.body]
+ return ResponseIter(response)
class TransparentProxy(Proxy):
"""A proxy based on HTTP_HOST environ variable"""
View
12 restkit/pool.py
@@ -53,10 +53,12 @@ def get(self, address):
self._lock.acquire()
try:
host = self.hosts.get(address)
- if host and host.pool:
- socket = host.pool.popleft()
- self.hosts[address] = host
- return socket
+ if not host:
+ return None
+
+ if host.pool:
+ return host.pool.popleft()
+
return None
finally:
self._lock.release()
@@ -72,7 +74,6 @@ def put(self, address, socket):
sock.close(socket)
return
host.pool.append(socket)
- self.hosts[address] = host
finally:
self._lock.release()
@@ -84,6 +85,5 @@ def clean(self, address):
while host.pool:
socket = host.pool.popleft()
sock.close(socket)
- self.hosts[address] = host
finally:
self._lock.release()
View
44 restkit/tee.py
@@ -14,10 +14,12 @@
from StringIO import StringIO
import tempfile
-from restkit.sock import MAX_BODY, CHUNK_SIZE
+from restkit import sock
class TeeInput(object):
+ CHUNK_SIZE = sock.CHUNK_SIZE
+
def __init__(self, socket, parser, buf, maybe_close=None):
self.buf = StringIO()
self.parser = parser
@@ -26,7 +28,7 @@ def __init__(self, socket, parser, buf, maybe_close=None):
self._is_socket = True
self._len = parser.content_len
- if self._len and self._len < MAX_BODY:
+ if self._len and self._len < sock.MAX_BODY:
self.tmp = StringIO()
else:
self.tmp = tempfile.TemporaryFile()
@@ -47,7 +49,7 @@ def len(self):
if self._is_socket:
self.tmp.seek(0, 2)
while True:
- if not self._tee(CHUNK_SIZE):
+ if not self._tee(self.CHUNK_SIZE):
break
self._len = self._tmp_size()
return self._len
@@ -57,7 +59,7 @@ def seek(self, offset, whence=0):
if self._is_socket:
self.tmp.seek(0, 2)
while True:
- if not self._tee(CHUNK_SIZE):
+ if not self._tee(self.CHUNK_SIZE):
break
self.tmp.seek(offset, whence)
@@ -73,7 +75,7 @@ def read(self, length=-1):
buf = StringIO()
buf.write(self.tmp.read())
while True:
- chunk = self._tee(CHUNK_SIZE)
+ chunk = self._tee(self.CHUNK_SIZE)
if not chunk:
break
buf.write(chunk)
@@ -91,29 +93,31 @@ def read(self, length=-1):
def readline(self, size=-1):
if not self._is_socket:
- return self.tmp.readline(size)
-
+ return self.tmp.readline()
orig_size = self._tmp_size()
if self.tmp.tell() == orig_size:
- if not self._tee(CHUNK_SIZE):
+ if not self._tee(self.CHUNK_SIZE):
return ''
self.tmp.seek(orig_size)
# now we can get line
line = self.tmp.readline()
- i = line.find("\n")
- if i == -1:
- while True:
- orig_size = self.tmp.tell()
- if not self._tee(CHUNK_SIZE):
- break
- self.tmp.seek(orig_size)
- line += self.tmp.readline()
- i = line.find("\n")
- if i != -1:
- break
- return line
+ if line.find("\n") >=0:
+ return line
+
+ buf = StringIO()
+ buf.write(line)
+ while True:
+ orig_size = self.tmp.tell()
+ data = self._tee(self.CHUNK_SIZE)
+ if not data:
+ break
+ self.tmp.seek(orig_size)
+ buf.write(self.tmp.readline())
+ if data.find("\n") >= 0:
+ break
+ return buf.getvalue()
def readlines(self, sizehint=0):
total = 0
Please sign in to comment.
Something went wrong with that request. Please try again.