Exemplo n.º 1
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''

        connection = self.get_connection(request)
        connection.putrequest(request.method, request.path)
        self.send_request_headers(connection, request.headers)
        self.send_request_body(connection, request.body)

        resp = connection.getresponse()
        self.status = int(resp.status)
        self.message = resp.reason
        self.respheader = headers = resp.getheaders()
        respbody = None
        if resp.length is None:
            respbody = resp.read()
        elif resp.length > 0:
            respbody = resp.read(resp.length)

        response = HTTPResponse(int(resp.status), resp.reason, headers,
                                respbody)
        if self.status >= 300:
            raise HTTPError(self.status, self.message, self.respheader,
                            respbody)

        return response
Exemplo n.º 2
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''

        connection = self.get_connection(request)
        connection.putrequest(request.method, request.path)

        if sys.platform.lower().startswith('win'):
            if self.proxy_host and self.proxy_user:
                connection.set_proxy_credentials(self.proxy_user, self.proxy_password)

        self.send_request_headers(connection, request.headers)
        self.send_request_body(connection, request.body)

        resp = connection.getresponse()
        self.status = int(resp.status)
        self.message = resp.reason
        self.respheader = headers = resp.getheaders()
        respbody = None
        if resp.length is None:
            respbody = resp.read()
        elif resp.length > 0:
            respbody = resp.read(resp.length)
    
        response = HTTPResponse(int(resp.status), resp.reason, headers, respbody)
        if self.status >= 300:
            raise HTTPError(self.status, self.message, self.respheader, respbody)
        
        return response
Exemplo n.º 3
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''
        connection = self.get_connection(request)
        try:
            connection.putrequest(request.method, request.path)

            if not self.use_httplib:
                if self.proxy_host and self.proxy_user:
                    connection.set_proxy_credentials(self.proxy_user,
                                                     self.proxy_password)

            self.send_request_headers(connection, request.headers)
            self.send_request_body(connection, request.body)

            if DEBUG_REQUESTS and request.body:
                print('request:')
                try:
                    print(request.body)
                except:
                    pass

            resp = connection.getresponse()
            self.status = int(resp.status)
            self.message = resp.reason
            self.respheader = headers = resp.getheaders()

            # for consistency across platforms, make header names lowercase
            for i, value in enumerate(headers):
                headers[i] = (value[0].lower(), value[1])

            respbody = None
            if resp.length is None:
                respbody = resp.read()
            elif resp.length > 0:
                respbody = resp.read(resp.length)

            if DEBUG_RESPONSES and respbody:
                print('response:')
                try:
                    print(respbody)
                except:
                    pass

            response = HTTPResponse(int(resp.status), resp.reason, headers,
                                    respbody)
            if self.status == 307:
                new_url = urlparse(dict(headers)['location'])
                request.host = new_url.hostname
                request.path = new_url.path
                request.path, request.query = _update_request_uri_query(
                    request)
                return self.perform_request(request)
            if self.status >= 300:
                raise HTTPError(self.status, self.message, self.respheader,
                                respbody)

            return response
        finally:
            connection.close()
Exemplo n.º 4
0
    def _parse_batch_response_part(self, part):
        lines = part.splitlines()

        # First line is the HTTP status/reason
        status, _, reason = lines[0].partition(b' ')[2].partition(b' ')

        # Followed by headers and body
        headers = []
        body = b''
        isBody = False
        for line in lines[1:]:
            if line == b'' and not isBody:
                isBody = True
            elif isBody:
                body += line
            else:
                headerName, _, headerVal = line.partition(b':')
                headers.append((headerName.lower(), headerVal))

        return HTTPResponse(int(status), reason.strip(), headers, body)
Exemplo n.º 5
0
    def perform_request(self, request):
        ''' Sends request to cloud service server and return the response. '''
        connection = self.get_connection(request)
        try:
            connection.putrequest(request.method, request.path)

            if not self.use_httplib:
                if self.proxy_host and self.proxy_user:
                    connection.set_proxy_credentials(self.proxy_user,
                                                     self.proxy_password)

            self.send_request_headers(connection, request.headers)
            self.send_request_body(connection, request.body)

            resp = connection.getresponse()
            self.status = int(resp.status)
            self.message = resp.reason
            self.respheader = headers = resp.getheaders()

            # for consistency across platforms, make header names lowercase
            for i, value in enumerate(headers):
                headers[i] = (value[0].lower(), value[1])

            respbody = None
            if resp.length is None:
                respbody = resp.read()
            elif resp.length > 0:
                respbody = resp.read(resp.length)

            response = HTTPResponse(int(resp.status), resp.reason, headers,
                                    respbody)
            if self.status >= 300:
                raise HTTPError(self.status, self.message, self.respheader,
                                respbody)

            return response
        finally:
            connection.close()