Example #1
0
    def request(self, method, url=None, body=None, headers=None,
            mimetype=None, serialize=False):

        if url:
            if url[0] != '/':
                url = '/' + url
        else:
            url = ''

        url = self.path + url
        if body:
            if method == 'GET':
                url = '%s?%s' % (url, body)
                body = None
            elif serialize:
                if mimetype:
                    body = formats.serialize(mimetype, body)
                else:
                    raise ValueError(mimetype)

        headers = headers or {}
        if 'Content-Type' not in headers and mimetype:
            headers['Content-Type'] = mimetype

        connection = self.implementation(self.host, timeout=self.timeout)
        try:
            connection.request(method, url, body, headers)
        except socket.error, exception:
            if exception.errno in (errno.EACCES, errno.EPERM, errno.ECONNREFUSED):
                raise ConnectionRefused(url)
            elif exception.errno == errno.ETIMEDOUT:
                raise ConnectionTimedOut(url)
            else:
                raise ConnectionFailed(url)
Example #2
0
def http_request(method, url, body=None, mimetype=None, headers=None,
        timeout=None, serialize=False):

    scheme, host, path = urlparse(url)[:3]
    if scheme == 'https':
        connection = HTTPSConnection(host=host, timeout=timeout)
    else:
        connection = HTTPConnection(host=host, timeout=timeout)

    if body:
        if method == 'GET':
            path = '%s?%s' % (path, body)
            body = None
        elif serialize:
            if mimetype:
                body = formats.serialize(mimetype, body)
            else:
                raise ValueError(mimetype)

    headers = headers or {}
    if 'Content-Type' not in headers and mimetype:
        headers['Content-Type'] = mimetype

    connection.request(method, path, body, headers)
    response = connection.getresponse()

    headers = dict((key.title(), value) for key, value in response.getheaders())
    content = response.read() or None

    mimetype = response.getheader('Content-Type', None)
    return Response(response.status, response.reason, mimetype, content, headers)
Example #3
0
File: http.py Project: siq/mesh
    def request(self, method, url=None, body=None, headers=None,
            mimetype=None, serialize=False):

        if url:
            if url[0] != '/':
                url = '/' + url
        else:
            url = ''
        url = self.path + url
        #TODO: added for defect 11503 to escape space in url. Not sure if
        # this is the best place to encode. httpserver might be a better place.
        url = url.replace(" ", "%20")

        multipart = isinstance(body, MultipartEncoder)
        if body and not multipart:
            if method == 'GET':
                url = '%s?%s' % (url, body)
                body = None
            elif serialize:
                if mimetype:
                    body = formats.serialize(mimetype, body)
                else:
                    raise ValueError(mimetype)

        headers = headers or {}
        if 'Content-Type' not in headers and mimetype:
            headers['Content-Type'] = mimetype

        connection = self.implementation(self.host, timeout=self.timeout)
        try:
            if multipart:
                self._send_multipart_request(connection, method, url, body, headers)
            else:
                connection.request(method, url, body, headers)
        except socket.error, exception:
            if exception.errno in (errno.EACCES, errno.EPERM, errno.ECONNREFUSED):
                raise ConnectionRefused(url)
            elif exception.errno == errno.ETIMEDOUT:
                raise ConnectionTimedOut(url)
            else:
                raise ConnectionFailed(url)