Esempio n. 1
0
 def make_connection(self, host):
     _host, _port = m2urllib.splitport(host)
     if sys.version[0] == '2':
         return httpslib.HTTPS(_host, int(_port), ssl_context=self.ssl_ctx)
     elif sys.version[:3] == '1.5':
         return httpslib.HTTPS(self.ssl_ctx, _host, int(_port))
     else:
         raise RuntimeError, 'unsupported Python version'
Esempio n. 2
0
 def test_HTTPS_secure_context_fail(self):
     pid = self.start_server(self.args)
     try:
         from M2Crypto import httpslib
         ctx = SSL.Context()
         ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, 9)
         ctx.load_verify_locations('test/server.pem')
         c = httpslib.HTTPS(srv_host, srv_port, ssl_context=ctx)
         c.putrequest('GET', '/')
         c.putheader('Accept', 'text/html')
         c.putheader('Accept', 'text/plain')
         self.assertRaises(SSL.SSLError, c.endheaders)
         c.close()
     finally:
         self.stop_server(pid)
Esempio n. 3
0
 def test_HTTPS(self):
     pid = self.start_server(self.args)
     try:
         from M2Crypto import httpslib
         c = httpslib.HTTPS(srv_host, srv_port)
         c.putrequest('GET', '/')
         c.putheader('Accept', 'text/html')
         c.putheader('Accept', 'text/plain')
         c.endheaders()
         err, msg, headers = c.getreply()
         assert err == 200, err
         f = c.getfile()
         data = f.read()
         c.close()
     finally:
         self.stop_server(pid)
     self.failIf(string.find(data, 's_server -quiet -www') == -1)
 def test_makefile_timeout(self):
     # httpslib uses makefile to read the response
     pid = self.start_server(self.args)
     try:
         from M2Crypto import httpslib
         c = httpslib.HTTPS(srv_host, self.srv_port)
         c.putrequest('GET', '/')
         c.putheader('Accept', 'text/html')
         c.putheader('Accept', 'text/plain')
         c.endheaders()
         c._conn.sock.settimeout(100)
         err, msg, headers = c.getreply()
         assert err == 200, err
         f = c.getfile()
         data = f.read()
         c.close()
     finally:
         self.stop_server(pid)
     self.assertIn('s_server -quiet -www', data)
Esempio n. 5
0
    def request(self, host, handler, request_body, verbose=0):
        # Handle username and password.
        user_passwd, host_port = m2urllib.splituser(host)
        _host, _port = m2urllib.splitport(host_port)
        h = httpslib.HTTPS(_host, int(_port), ssl_context=self.ssl_ctx)

        if verbose:
            h.set_debuglevel(1)

        # Check cert ::Addition by RogerB
        self.check_cert(h)

        # What follows is as in xmlrpclib.Transport. (Except the authz bit.)
        h.putrequest("POST", handler)

        # required by HTTP/1.1
        h.putheader("Host", _host)

        # required by XML-RPC
        h.putheader("User-Agent", self.user_agent)
        h.putheader('Keep-Alive', '1')
        h.putheader("Content-Type", "text/xml")
        h.putheader("Content-Length", str(len(request_body)))

        # Authorisation.
        if user_passwd is not None:
            auth = string.strip(base64.encodestring(user_passwd))
            h.putheader('Authorization', 'Basic %s' % auth)

        h.endheaders()

        if request_body:
            h.send(request_body)

        errcode, errmsg, headers = h.getreply()

        if errcode != 200:
            print errcode, errmsg, headers
            raise xmlrpclib.ProtocolError(host + handler, errcode, errmsg,
                                          headers)

        self.verbose = verbose
        return self.parse_response(h.getfile())
Esempio n. 6
0
 def test_HTTPS_secure_context(self):
     pid = self.start_server(self.args)
     try:
         from M2Crypto import httpslib
         ctx = SSL.Context()
         ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, 9)
         ctx.load_verify_locations('test/ca.pem')
         c = httpslib.HTTPS(srv_host, srv_port, ssl_context=ctx)
         c.putrequest('GET', '/')
         c.putheader('Accept', 'text/html')
         c.putheader('Accept', 'text/plain')
         c.endheaders()
         err, msg, headers = c.getreply()
         assert err == 200, err
         f = c.getfile()
         data = f.read()
         c.close()
     finally:
         self.stop_server(pid)
     self.failIf(string.find(data, 's_server -quiet -www') == -1)
 def test_makefile_timeout_fires(self):
     # This is convoluted because (openssl s_server -www) starts
     # writing the response as soon as it receives the first line of
     # the request, so it's possible for it to send the response
     # before the request is sent and there would be no timeout.  So,
     # let the server spend time reading from an empty pipe
     FIFO_NAME = 'test_makefile_timeout_fires_fifo'  # noqa
     os.mkfifo('tests/' + FIFO_NAME)
     pipe_pid = os.fork()
     try:
         if pipe_pid == 0:
             try:
                 f = open('tests/' + FIFO_NAME, 'w')
                 try:
                     time.sleep(sleepTime + 1)
                     f.write('Content\n')
                 finally:
                     f.close()
             finally:
                 os._exit(0)
         self.args[self.args.index('-www')] = '-WWW'
         pid = self.start_server(self.args)
         try:
             from M2Crypto import httpslib
             c = httpslib.HTTPS(srv_host, self.srv_port)
             c.putrequest('GET', '/' + FIFO_NAME)
             c.putheader('Accept', 'text/html')
             c.putheader('Accept', 'text/plain')
             c.endheaders()
             c._conn.sock.settimeout(0.0000000001)
             self.assertRaises(socket.timeout, c.getreply)
             c.close()
         finally:
             self.stop_server(pid)
     finally:
         os.kill(pipe_pid, signal.SIGTERM)
         os.waitpid(pipe_pid, 0)
         os.unlink('tests/' + FIFO_NAME)
Esempio n. 8
0
 def make_connection(self, host, port=None):
     if port is None:
         host, port = m2urllib.splitport(host)
     return httpslib.HTTPS(host, int(port), ssl_context=self.ssl_ctx)
Esempio n. 9
0
 def make_connection(self, host):
     _host, _port = m2urllib.splitport(host)
     return httpslib.HTTPS(_host, int(_port), ssl_context=self.ssl_ctx)
Esempio n. 10
0
    def fetch(self, url, postdata=None, server=None, port=None, protocol=None):
        '''Run a single test request to the indicated url. Use the POST data
        if supplied.

        Raises failureException if the returned data contains any of the
        strings indicated to be Error Content.
        Returns a HTTPReponse object wrapping the response from the server.
        '''
        # see if the url is fully-qualified (not just a path)
        #print len(urlparse.urlparse(url)),urlparse.urlparse(url)
        t_protocol, t_server, t_url, x, t_args, x = urlparse.urlparse(url)
        if t_server:
            protocol = t_protocol
            if ':' in t_server:
                server, port = t_server.split(':')
            else:
                server = t_server
                if protocol == 'https':
                    port = '443'
                else:
                    port = '80'
            url = t_url
            if t_args:
                url = url + '?' + t_args
            # ignore the machine name if the redirect is to localhost
            #if t_server == 'localhost':
            #    server = None

        # TODO: allow override of the server and port from the URL!
        if server is None: server = self.server
        if port is None: port = self.port
        if protocol is None: protocol = self.protocol

        # timing start mark
        self.appendTiming((url, time.time(), 'start'))

        # do the protocolly stuff
        if protocol == 'http':
            h = httplib.HTTP(server, int(port))
            if int(port) == 80:
                host_header = server
            else:
                host_header = '%s:%s' % (server, port)
        elif protocol == 'https':
            if httpslib is None:
                raise ValueError, "Can't fetch HTTPS: M2Crypto not installed"
            h = httpslib.HTTPS(server, int(port))
            if int(port) == 443:
                host_header = server
            else:
                host_header = '%s:%s' % (server, port)
        else:
            print "\n", protocol, server, port, "\n"
            self.appendTiming((url, time.time(), 'invalid protocol'))
            raise ValueError, protocol

        h._http_vsn = 11
        h._http_vsn_str = 'HTTP/1.1'

        params = None
        if postdata:
            # Do a post with the data file
            params = mimeEncode(postdata)
            h.putrequest('POST', url)
            h.putheader('Content-type',
                        'multipart/form-data; boundary=%s' % boundary)
            h.putheader('Content-length', str(len(params)))
        else:
            # Normal GET
            h.putrequest('GET', url)

        # Other Full Request headers
        if self.authinfo:
            h.putheader('Authorization', "Basic %s" % self.authinfo)
        h.putheader('Host', host_header)
        h.putheader('User-Agent', Chrome)
        # Send cookies
        #  - check the domain, max-age (seconds), path and secure
        #    (http://www.ietf.org/rfc/rfc2109.txt)
        cookies_used = []
        for domain, cookies in self.cookies.items():
            # check cookie domain
            if not server.endswith(domain):
                continue
            for path, cookies in cookies.items():
                # check that the path matches
                urlpath = urlparse.urlparse(url)[2]
                if not urlpath.startswith(path):
                    continue
                for sendcookie in cookies.values():
                    # and that the cookie is or isn't secure
                    if sendcookie['secure'] and protocol != 'https':
                        continue
                    # TODO: check max-age
                    h.putheader('Cookie', sendcookie.OutputString())
                    cookies_used.append(sendcookie.key)

        # finish the headers
        h.endheaders()

        if params is not None:
            h.send(params)

        # handle the reply
        errcode, errmsg, headers = h.getreply()
        self.appendTiming((url, time.time(), 'first byte'))

        # if the response is HTML, parse it for images
        sucker = None
        #print errcode,headers['Content-Type'];
        if errcode == 200 and 'html' in headers['Content-Type']:
            sucker = IMGHandler(url, self, self.options.threads)


#        if errcode == 200 and 'css' in headers['Content-Type']:
#            sucker = CSSHandler(url, self, self.options.threads)

# get the body and save it
        f = h.getfile()
        g = cStringIO.StringIO()
        data = f.read(128)
        while data:
            if sucker is not None:
                sucker.feed(data)
            g.write(data)
            data = f.read(128)
        if sucker is not None:
            sucker.close()
        response = HTTPResponse(self.options, self.cookies, protocol, server,
                                port, url, errcode, errmsg, headers,
                                g.getvalue())
        f.close()
        self.appendTiming((url, time.time(), 'end - %s' % errcode))

        if errcode not in (200, 301, 302):
            raise HTTPError(response)

        # decode the cookies
        if self.accept_cookies:
            try:
                # decode the cookies and update the cookies store
                self.cookies_lock.acquire()
                cookie.decodeCookies(url, server, headers, self.cookies)
                self.cookies_lock.release()
            except:
                raise

        if sucker is not None:
            sucker.finished.wait()

        return response