Copy wsgi.input to a tempfile so it can be reused.

def _copy_body_to_tempfile(cls, environ):
        """
        Copy wsgi.input to a tempfile so it can be reused.
        """
        try:
            length = int(environ.get('CONTENT_LENGTH', 0))
        except ValueError:
            length = 0

        try:
            fileobj = tempfile.SpooledTemporaryFile(1024*1024)
        except AttributeError:  # pragma: nocover
            fileobj = tempfile.TemporaryFile()  # py25 fallback
        if length:
            remaining = length
            while remaining > 0:
                data = environ['wsgi.input'].read(min(remaining, 65536))
                if not data:
                    raise IOError(
                        "Client disconnected (%s more bytes were expected)"
                        % remaining
                    )
                fileobj.write(data)
                remaining -= len(data)

        fileobj.seek(0)
        environ['wsgi.input'] = fileobj
        return fileobj, length