<commit_before># This program is free software; you can redistribute it and/or modify
# it under the terms of the (LGPL) GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the 
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library Lesser General Public License for more details at
# ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: <NAME> ( <EMAIL> )

# modifications by: <NAME> (<EMAIL>)

"""
Contains classes for basic HTTP transport implementations.
"""

import urllib2 as u2
import base64
import socket
from sudsds.transport import *
from sudsds.properties import Unskin
from urlparse import urlparse
from cookielib import CookieJar
from logging import getLogger
import httplib
import ssl
from pyopenssl_wrapper import PyOpenSSLSocket

log = getLogger(__name__)


class SUDSHTTPRedirectHandler(u2.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        """Return a Request or None in response to a redirect.

        This is called by the http_error_30x methods,
        it was taken from the original Python version and modified
        to use POST when redirection takes place.
        This allows a SOAP message to be redirected without a loss
        of content.
        """
        m = req.get_method()
        if (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
            or code in (301, 302, 303) and m == "POST"):
            newurl = newurl.replace(' ', '%20')
            newheaders = dict((k,v) for k,v in req.headers.items()
                              if k.lower() not in ("content-length", "content-type")
                             )
            log.debug("Redirecting to %s", newurl)
            return u2.Request(newurl,
                              data=req.data, # here we pass the original data
                              headers=newheaders,
                              origin_req_host=req.get_origin_req_host(),
                              unverifiable=True,
                              )
        else:
            raise u2.HTTPError(req.get_full_url(), code, msg, headers, fp)


class MyHTTPResponse(httplib.HTTPResponse):
  
  def __init__(self, sock, debuglevel=0, strict=0, method=None):
    
    httplib.HTTPResponse.__init__(self, sock, debuglevel, strict, method)


class CheckingHTTPSConnection(httplib.HTTPSConnection):
  """based on httplib.HTTPSConnection code - extended to support 
  server certificate verification and client certificate authorization"""
  
  response_class = MyHTTPResponse
  
  FORCE_SSL_VERSION = None
  SERVER_CERT_CHECK = True # might be turned off when a workaround is needed
  
  def __init__(self, host, ca_certs=None, cert_verifier=None, **kw):
    """cert_verifier is a function returning either True or False
    based on whether the certificate was found to be OK"""
    httplib.HTTPSConnection.__init__(self, host, **kw)
    self.ca_certs = ca_certs
    self.cert_verifier = cert_verifier
    
  def connect(self):
    sock = socket.create_connection((self.host, self.port), self.timeout)
    if hasattr(self, '_tunnel_host') and self._tunnel_host:
        self.sock = sock
        self._tunnel()
    if self.FORCE_SSL_VERSION:
      add = {'ssl_version': self.FORCE_SSL_VERSION}
    else:
      add = {}
    if self.SERVER_CERT_CHECK and self.ca_certs:
      add['cert_reqs'] = ssl.CERT_REQUIRED
    else:
      add['cert_reqs'] = ssl.CERT_NONE
    wrap_class = ssl.SSLSocket
    if self.key_file and self.cert_file:
      add['keyfile'] = self.key_file
      add['certfile'] = self.cert_file
      wrap_class = PyOpenSSLSocket

    self.sock = wrap_class(sock, ca_certs=self.ca_certs, **add)
      #if self.cert_verifier and self.SERVER_CERT_CHECK:
      #  if not self.cert_verifier(self.sock.getpeercert()):
      #    raise Exception("Server certificate did not pass security check.",
      #                    self.sock.getpeercert())


class CheckingHTTPSHandler(u2.HTTPSHandler):
  <commit_msg>make loading of PyOpenSSL lazy to avoid crash using username based login<commit_after># This program is free software; you can redistribute it and/or modify
# it under the terms of the (LGPL) GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the 
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library Lesser General Public License for more details at
# ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: <NAME> ( <EMAIL> )

# modifications by: <NAME> (<EMAIL>)

"""
Contains classes for basic HTTP transport implementations.
"""

import urllib2 as u2
import base64
import socket
from sudsds.transport import *
from sudsds.properties import Unskin
from urlparse import urlparse
from cookielib import CookieJar
from logging import getLogger
import httplib
import ssl

log = getLogger(__name__)


class SUDSHTTPRedirectHandler(u2.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        """Return a Request or None in response to a redirect.

        This is called by the http_error_30x methods,
        it was taken from the original Python version and modified
        to use POST when redirection takes place.
        This allows a SOAP message to be redirected without a loss
        of content.
        """
        m = req.get_method()
        if (code in (301, 302, 303, 307) and m in ("GET", "HEAD")
            or code in (301, 302, 303) and m == "POST"):
            newurl = newurl.replace(' ', '%20')
            newheaders = dict((k,v) for k,v in req.headers.items()
                              if k.lower() not in ("content-length", "content-type")
                             )
            log.debug("Redirecting to %s", newurl)
            return u2.Request(newurl,
                              data=req.data, # here we pass the original data
                              headers=newheaders,
                              origin_req_host=req.get_origin_req_host(),
                              unverifiable=True,
                              )
        else:
            raise u2.HTTPError(req.get_full_url(), code, msg, headers, fp)


class MyHTTPResponse(httplib.HTTPResponse):
  
  def __init__(self, sock, debuglevel=0, strict=0, method=None):
    
    httplib.HTTPResponse.__init__(self, sock, debuglevel, strict, method)


class CheckingHTTPSConnection(httplib.HTTPSConnection):
  """based on httplib.HTTPSConnection code - extended to support 
  server certificate verification and client certificate authorization"""
  
  response_class = MyHTTPResponse
  
  FORCE_SSL_VERSION = None
  SERVER_CERT_CHECK = True # might be turned off when a workaround is needed
  
  def __init__(self, host, ca_certs=None, cert_verifier=None, **kw):
    """cert_verifier is a function returning either True or False
    based on whether the certificate was found to be OK"""
    httplib.HTTPSConnection.__init__(self, host, **kw)
    self.ca_certs = ca_certs
    self.cert_verifier = cert_verifier
    
  def connect(self):
    sock = socket.create_connection((self.host, self.port), self.timeout)
    if hasattr(self, '_tunnel_host') and self._tunnel_host:
        self.sock = sock
        self._tunnel()
    if self.FORCE_SSL_VERSION:
      add = {'ssl_version': self.FORCE_SSL_VERSION}
    else:
      add = {}
    if self.SERVER_CERT_CHECK and self.ca_certs:
      add['cert_reqs'] = ssl.CERT_REQUIRED
    else:
      add['cert_reqs'] = ssl.CERT_NONE
    wrap_class = ssl.SSLSocket
    if self.key_file and self.cert_file:
      from pyopenssl_wrapper import PyOpenSSLSocket
      wrap_class = PyOpenSSLSocket
      add['keyfile'] = self.key_file
      add['certfile'] = self.cert_file

    self.sock = wrap_class(sock, ca_certs=self.ca_certs, **add)
      #if self.cert_verifier and self.SERVER_CERT_CHECK:
      #  if not self.cert_verifier(self.sock.getpeercert()):
      #    raise Exception("Server certificate did not pass security check.",
      #                    self.sock.getpeercert())


class CheckingHTTPSHandler(u2.HTTPSHandler):
  
{'fraction_non_alphanumeric': 0.08467153284671533, 'fraction_numerical': 0.011229646266142616, 'mean_word_length': 2.852076124567474, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3361970', 'n_tokens_mistral': 2647, 'n_tokens_neox': 2554, 'n_words': 873}