Esempio n. 1
0
    def request(self, url, method="GET", body="", headers={}):
        """
        Actually sends the request
        """

        # objectify the url
        url = URL.objectify(url)

        proxies = None
        if self.proxy is not None:
            proxies = {url.scheme: self.proxy}
            log.debug("using proxy - %s" % (proxies))

        # ensure that url is a unicode string
        url = str(url)

        combined_headers = dict(self.headers)
        combined_headers.update(headers)
        if body is None or body == "" and "Content-Type" in combined_headers:
            del combined_headers["Content-Type"]

        log.debug(
            "sending request - method={0}, url={1}, headers={2}\nbody:\n{3}"
            .format(method, url, combined_headers, body))
        auth = None
        if self.auth is None and self.username is not None:
            auth = requests.auth.HTTPDigestAuth(self.username, self.password)
        else:
            auth = self.auth

        r = requests.request(method, url, data=to_wire(body),
                             headers=combined_headers, proxies=proxies,
                             auth=auth, verify=self.ssl_verify_cert)
        response = DAVResponse(r)

        # If server supports BasicAuth and not DigestAuth, let's try again:
        if response.status == 401 and self.auth is None and auth is not None:
            auth = requests.auth.HTTPBasicAuth(self.username, self.password)
            r = requests.request(method, url, data=to_wire(body),
                                 headers=combined_headers, proxies=proxies,
                                 auth=auth, verify=self.ssl_verify_cert)
            response = DAVResponse(r)

        # this is an error condition the application wants to know
        if response.status == requests.codes.forbidden or \
                response.status == requests.codes.unauthorized:
            ex = error.AuthorizationError()
            ex.url = url
            ex.reason = response.reason
            raise ex

        # let's save the auth object and remove the user/pass information
        if not self.auth and auth:
            self.auth = auth
            del self.username
            del self.password

        return response
Esempio n. 2
0
File: proxy.py Progetto: vr/caldav
 def do_GET(self):
     (scm, netloc, path, params, query,
      fragment) = urlparse(self.path, 'http')
     if scm not in ('http', 'ftp') or fragment or not netloc:
         self.send_error(400, "bad url %s" % self.path)
         return
     soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     try:
         if scm == 'http':
             if self._connect_to(netloc, soc):
                 self.log_request()
                 soc.send(
                     to_wire("%s %s %s\r\n" %
                             (self.command,
                              urlunparse(('', '', path, params, query, '')),
                              self.request_version)))
                 self.headers['Connection'] = 'close'
                 del self.headers['Proxy-Connection']
                 for key_val in list(self.headers.items()):
                     soc.send(to_wire("%s: %s\r\n" % key_val))
                 soc.send(to_wire("\r\n"))
                 self._read_write(soc)
         elif scm == 'ftp':
             # fish out user and password information
             i = netloc.find('@')
             if i >= 0:
                 login_info, netloc = netloc[:i], netloc[i + 1:]
                 try:
                     user, passwd = login_info.split(':', 1)
                 except ValueError:
                     user, passwd = "anonymous", None
             else:
                 user, passwd = "anonymous", None
             self.log_request()
             try:
                 ftp = ftplib.FTP(netloc)
                 ftp.login(user, passwd)
                 if self.command == "GET":
                     ftp.retrbinary("RETR %s" % path, self.connection.send)
                 ftp.quit()
             except Exception as e:
                 self.server.logger.log(logging.WARNING,
                                        "FTP Exception: %s", e)
     finally:
         soc.close()
         self.connection.close()
Esempio n. 3
0
 def do_GET(self):
     (scm, netloc, path, params, query, fragment) = urlparse(
         self.path, 'http')
     if scm not in ('http', 'ftp') or fragment or not netloc:
         self.send_error(400, "bad url %s" % self.path)
         return
     soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     try:
         if scm == 'http':
             if self._connect_to(netloc, soc):
                 self.log_request()
                 soc.send(to_wire("%s %s %s\r\n" % (self.command,
                                                    urlunparse(('', '', path,
                                                             params, query,
                                                             '')),
                                                    self.request_version)))
                 self.headers['Connection'] = 'close'
                 del self.headers['Proxy-Connection']
                 for key_val in list(self.headers.items()):
                     soc.send(to_wire("%s: %s\r\n" % key_val))
                 soc.send(to_wire("\r\n"))
                 self._read_write(soc)
         elif scm == 'ftp':
             # fish out user and password information
             i = netloc.find ('@')
             if i >= 0:
                 login_info, netloc = netloc[:i], netloc[i+1:]
                 try: user, passwd = login_info.split (':', 1)
                 except ValueError: user, passwd = "anonymous", None
             else: user, passwd ="anonymous", None
             self.log_request ()
             try:
                 ftp = ftplib.FTP (netloc)
                 ftp.login (user, passwd)
                 if self.command == "GET":
                     ftp.retrbinary ("RETR %s"%path, self.connection.send)
                 ftp.quit ()
             except Exception as e:
                 self.server.logger.log (logging.WARNING, "FTP Exception: %s",
                                         e)
     finally:
         soc.close()
         self.connection.close()
Esempio n. 4
0
    def request(self, url, method="GET", body="", headers={}):
        """
        Actually sends the request
        """

        # objectify the url
        url = URL.objectify(url)

        proxies = None
        if self.proxy is not None:
            proxies = {url.scheme: self.proxy}
            log.debug("using proxy - %s" % (proxies))

        # ensure that url is a unicode string
        url = str(url)

        combined_headers = self.headers
        combined_headers.update(headers)
        if body is None or body == "" and "Content-Type" in combined_headers:
            del combined_headers["Content-Type"]

        log.debug("sending request - method={0}, url={1}, headers={2}\nbody:\n{3}".format(method, url, combined_headers, body))
        auth = None
        if self.auth is None and self.username is not None:
            auth = requests.auth.HTTPDigestAuth(self.username, self.password)
        else:
            auth = self.auth

        r = requests.request(method, url, data=to_wire(body), headers=combined_headers, proxies=proxies, auth=auth, verify=self.ssl_verify_cert)

        response = DAVResponse(r)

        ## If server supports BasicAuth and not DigestAuth, let's try again:
        if response.status == 401 and self.auth is None and auth is not None:
            # if there were redirects, we need to continue with them
            if r.history:
                url = r.url

            auth = requests.auth.HTTPBasicAuth(self.username, self.password)

            r = requests.request(method, url, data=to_wire(body), headers=combined_headers, proxies=proxies, auth=auth,
                                 verify=self.ssl_verify_cert)

            response = DAVResponse(r)

            if r.history:
                # requests do not redirect with body
                r = requests.request(method, r.url, data=to_wire(body), headers=combined_headers, proxies=proxies,
                                     auth=auth, verify=self.ssl_verify_cert)
                response = DAVResponse(r)

        self._last_response = r

        # this is an error condition the application wants to know
        if response.status == requests.codes.forbidden or \
                response.status == requests.codes.unauthorized:
            ex = error.AuthorizationError()
            ex.url = url
            ex.reason = response.reason
            raise ex

        ## let's save the auth object and remove the user/pass information
        if not self.auth and auth:
            self.auth = auth
            del self.username
            del self.password

        return response