Esempio n. 1
0
    def testSetCredentials(self):
        request = self._get_request(self.wrapper)
        self.assertFalse(request.has_header('Authorization'))

        self.wrapper.setCredentials('login', 'password')
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header('Authorization'))

        # expected header for login:password
        # should succeed for python 3 since pull request #72
        self.assertEqual("Basic bG9naW46cGFzc3dvcmQ=", request.get_header('Authorization'))
    def http_request(self, request):
        data = request.get_data()
        if data is not None and type(data) != str:
            v_files = []
            v_vars = []
            try:
                 for(key, value) in list(data.items()):
                     if type(value) == file:
                         v_files.append((key, value))
                     else:
                         v_vars.append((key, value))
            except TypeError:
                systype, value, traceback = sys.exc_info()
                raise TypeError("not a valid non-string sequence or mapping object").with_traceback(traceback)

            if len(v_files) == 0:
                data = urllib.parse.urlencode(v_vars, doseq)
            else:
                boundary, data = self.multipart_encode(v_vars, v_files)
                contenttype = 'multipart/form-data; boundary=%s' % boundary
                if request.has_header('Content-type'):
                    pass # print "Replacing %s with %s" % (request.get_header('content-type'), contenttype)
                request.add_unredirected_header('Content-type', contenttype)

            request.add_data(data)

        return request
Esempio n. 3
0
 def http_request(self, request):
     data = request.get_data()
     if data is not None and type(data) != str:
         v_files = []
         v_vars = []
         try:
             for(key, value) in list(data.items()):
                 if hasattr(value, 'read'):
                     v_files.append((key, value))
                 else:
                     v_vars.append((key, value))
         except TypeError:
             raise TypeError
         if len(v_files) == 0:
             data = urllib.parse.urlencode(v_vars, doseq)
         else:
             boundary, data = self.multipart_encode(v_vars, v_files)
             contenttype = 'multipart/form-data; boundary=%s' % boundary
             if (
                 request.has_header('Content-Type') and
                 request.get_header('Content-Type').find(
                     'multipart/form-data') != 0
             ):
                 six.print_(
                     "Replacing %s with %s" % (
                         request.get_header('content-type'),
                         'multipart/form-data'
                     )
                 )
             request.add_unredirected_header('Content-Type', contenttype)
         request.add_data(data)
     return request
Esempio n. 4
0
    def http_request(self, request):
        try:
            data = request.get_data()
        except AttributeError:
            data = request.data
        if data is not None and type(data) != str:
            v_files = []
            v_vars = []
            try:
                for (key, value) in list(data.items()):
                    if hasattr(value, 'read'):
                        v_files.append((key, value))
                    else:
                        v_vars.append((key, value))
            except TypeError:
                raise TypeError
            if len(v_files) == 0:
                data = urllib.parse.urlencode(v_vars, doseq)
            else:
                boundary, data = self.multipart_encode(v_vars, v_files)
                contenttype = 'multipart/form-data; boundary=%s' % boundary
                if (request.has_header('Content-Type') and request.get_header(
                        'Content-Type').find('multipart/form-data') != 0):
                    six.print_("Replacing %s with %s" %
                               (request.get_header('content-type'),
                                'multipart/form-data'))
                request.add_unredirected_header('Content-Type', contenttype)
            try:
                request.add_data(data)
            except AttributeError:
                request.data = data

        return request
Esempio n. 5
0
    def http_request(self, request):
        print(request.__dict__)
        data = request.data
        if data is not None and type(data) != str:
            v_files = []
            v_vars = []
            try:
                 for key, value in data.items():
                     if isinstance(value,IOBase):
                         v_files.append((key, value))
                     else:
                         v_vars.append((key, value))
            except TypeError:
                systype, value, traceback = sys.exc_info()
                raise TypeError("not a valid non-string sequence or mapping object")

            if len(v_files) == 0:
                data = urllib.urlencode(v_vars, doseq)
            else:
                boundary, data = MultipartPostHandler.multipart_encode(v_vars, v_files)

                contenttype = 'multipart/form-data; boundary=%s' % boundary
                if(request.has_header('Content-Type')
                   and request.get_header('Content-Type').find('multipart/form-data') != 0):
                    print("Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data'))
                request.add_unredirected_header('Content-Type', contenttype)

            request.data = data.encode('utf-8')
        
        return request
Esempio n. 6
0
def http_request(request):
    data = request.data
    if data is not None and type(data) != str:  
        v_files = []  
        v_vars = []  
        try:  
            for(key, value) in data.items():
                if type(value) == io.BufferedReader:  
                    v_files.append((key, value))  
                else:  
                    v_vars.append((key, value))  
        except TypeError:  
            systype, value, traceback = sys.exc_info()  
            raise TypeError
            
        if len(v_files) == 0:  
            data = urllib.urlencode(v_vars, doseq)  
        else:  
            boundary, data = multipart_encode(v_vars, v_files)
            contenttype = 'multipart/form-data; boundary=%s' % boundary
            if( request.has_header('Content-Type') and request.get_header['Content-Type'].find('multipart/form-data') != 0):  
                print("Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data'))
            request.add_unredirected_header('Content-Type', contenttype)
        request.data = data.encode('utf-8')
    return request
Esempio n. 7
0
 def ensure_content_type(self, request):
     """Get 'Content-type' header or default it to JSON."""
     # pylint: disable=no-self-use
     # pylint: disable=fixme
     # TODO Refactor HTTP request sending into separate module.
     # see https://github.com/raphaelhuefner/allbar/issues/1
     if not request.has_header('Content-type'):
         request.add_header('Content-type', 'application/json')
     return request.get_header('Content-type')
Esempio n. 8
0
 def http_request(self, request):
     if not request.has_header('Cookie'):
         request.add_unredirected_header('Cookie', self.cookie)
         request.add_header('Cookie', self.cookie)
     else:
         cookie = request.get_header('Cookie')
         request.add_unredirected_header('Cookie', cookie + '; ' + cookie)
     self.cookiejar.add_cookie_header(request)
     return request
Esempio n. 9
0
 def http_request(self, request):
     if not request.has_header(self.AUTH_HEADER):
         url = request.get_full_url()
         user, password = self._creds_mgr.get_credentials(url)
         if user is not None and password is not None:
             creds = base64.b64encode("%s:%s" % (user, password))
             auth = "Basic %s" % creds
             request.add_unredirected_header(self.AUTH_HEADER, auth)
     return request
Esempio n. 10
0
    def do_request_(self, request):
        host = request.get_host()
        if not host:
            raise URLError('no host given')

        if request.has_data():  # POST
            data = request.get_data()
            if not request.has_header('Content-type'):
                request.add_unredirected_header(
                    'Content-type', 'application/x-www-form-urlencoded')

        scheme, sel = urllib.parse.splittype(request.get_selector())
        sel_host, sel_path = urllib.parse.splithost(sel)
        if not request.has_header('Host'):
            request.add_unredirected_header('Host', sel_host or host)
        for name, value in self.parent.addheaders:
            name = name.capitalize()
            if not request.has_header(name):
                request.add_unredirected_header(name, value)

        return request
Esempio n. 11
0
 def test_custom_headers(self):
     url = support.TEST_HTTP_URL
     with support.transient_internet(url):
         opener = urllib.request.build_opener()
         request = urllib.request.Request(url)
         self.assertFalse(request.header_items())
         opener.open(request)
         self.assertTrue(request.header_items())
         self.assertTrue(request.has_header('User-agent'))
         request.add_header('User-Agent', 'Test-Agent')
         opener.open(request)
         self.assertEqual(request.get_header('User-agent'), 'Test-Agent')
Esempio n. 12
0
 def test_custom_headers(self):
     url = "http://www.example.com"
     with support.transient_internet(url):
         opener = urllib.request.build_opener()
         request = urllib.request.Request(url)
         self.assertFalse(request.header_items())
         opener.open(request)
         self.assertTrue(request.header_items())
         self.assertTrue(request.has_header('User-agent'))
         request.add_header('User-Agent','Test-Agent')
         opener.open(request)
         self.assertEqual(request.get_header('User-agent'),'Test-Agent')
Esempio n. 13
0
    def do_request_(self, request):
        host = request.get_host()
        if not host:
            raise URLError('no host given')

        if request.has_data():  # POST
            data = request.get_data()
            if not request.has_header('Content-type'):
                request.add_unredirected_header(
                    'Content-type',
                    'application/x-www-form-urlencoded')

        scheme, sel = urllib.parse.splittype(request.get_selector())
        sel_host, sel_path = urllib.parse.splithost(sel)
        if not request.has_header('Host'):
            request.add_unredirected_header('Host', sel_host or host)
        for name, value in self.parent.addheaders:
            name = name.capitalize()
            if not request.has_header(name):
                request.add_unredirected_header(name, value)

        return request
Esempio n. 14
0
    def testSetHTTPAuth(self):
        self.assertRaises(TypeError, self.wrapper.setHTTPAuth, 123)
        self.wrapper.setCredentials("login", "password")
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header("Authorization"))
        self.assertIsNone(urllib.request._opener)

        self.wrapper.setHTTPAuth(DIGEST)
        self.assertIsNone(urllib.request._opener)
        request = self._get_request(self.wrapper)
        self.assertFalse(request.has_header("Authorization"))
        self.assertEqual(self.wrapper.http_auth, DIGEST)
        self.assertIsInstance(urllib.request._opener,
                              urllib.request.OpenerDirector)

        self.wrapper.setHTTPAuth(DIGEST)
        self.wrapper.setCredentials("login", "password")
        request = self._get_request(self.wrapper)
        self.assertEqual(self.wrapper.http_auth, DIGEST)
        self.assertEqual(self.wrapper.user, "login")
        self.assertEqual(self.wrapper.passwd, "password")
        self.assertEqual(self.wrapper.realm, "SPARQL")
        self.assertNotEqual(self.wrapper.realm, "SPARQL Endpoint")

        self.wrapper.setHTTPAuth(DIGEST)
        self.wrapper.setCredentials("login",
                                    "password",
                                    realm="SPARQL Endpoint")
        request = self._get_request(self.wrapper)
        self.assertEqual(self.wrapper.http_auth, DIGEST)
        self.assertEqual(self.wrapper.user, "login")
        self.assertEqual(self.wrapper.passwd, "password")
        self.assertEqual(self.wrapper.realm, "SPARQL Endpoint")
        self.assertNotEqual(self.wrapper.realm, "SPARQL")

        self.assertRaises(ValueError, self.wrapper.setHTTPAuth, "OAuth")

        self.wrapper.http_auth = "OAuth"
        self.assertRaises(NotImplementedError, self._get_request, self.wrapper)
Esempio n. 15
0
    def testAddCustomHttpHeader(self):
        request = self._get_request(self.wrapper)
        self.assertFalse(request.has_header('Foo'))

        # Add new header field name
        self.wrapper.addCustomHttpHeader('Foo', 'bar')
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header('Foo'))
        self.assertEqual("bar", request.get_header('Foo'))

        # Override a new field name
        self.wrapper.addCustomHttpHeader('Foo', 'bar')
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header('Foo'))
        self.assertEqual("bar", request.get_header('Foo'))
        self.wrapper.addCustomHttpHeader('Foo', 'bar_2')
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header('Foo'))
        self.assertEqual("bar_2", request.get_header('Foo'))

        # Override header field name
        self.wrapper.addCustomHttpHeader('User-agent', 'Another UA')
        request = self._get_request(self.wrapper)
        self.assertEqual("Another UA", request.get_header('User-agent'))
Esempio n. 16
0
    def testAddCustomHttpHeader(self):
        request = self._get_request(self.wrapper)
        self.assertFalse(request.has_header("Foo"))

        # Add new header field name
        self.wrapper.addCustomHttpHeader("Foo", "bar")
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header("Foo"))
        self.assertEqual("bar", request.get_header("Foo"))

        # Override a new field name
        self.wrapper.addCustomHttpHeader("Foo", "bar")
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header("Foo"))
        self.assertEqual("bar", request.get_header("Foo"))
        self.wrapper.addCustomHttpHeader("Foo", "bar_2")
        request = self._get_request(self.wrapper)
        self.assertTrue(request.has_header("Foo"))
        self.assertEqual("bar_2", request.get_header("Foo"))

        # Override header field name
        self.wrapper.addCustomHttpHeader("User-agent", "Another UA")
        request = self._get_request(self.wrapper)
        self.assertEqual("Another UA", request.get_header("User-agent"))
Esempio n. 17
0
    def testClearCustomHttpHeader(self):
        request = self._get_request(self.wrapper)
        self.assertFalse(request.has_header('Foo'))

        # Add new header field name
        self.wrapper.addCustomHttpHeader('Foo_1', 'bar_1')
        self.wrapper.addCustomHttpHeader('Foo_2', 'bar_2')
        self.wrapper.addCustomHttpHeader('Foo_3', 'bar_3')


        self.assertFalse(self.wrapper.clearCustomHttpHeader('Foo_4'))
        self.assertTrue(self.wrapper.clearCustomHttpHeader('Foo_3'))

        customHttpHeaders = self.wrapper.customHttpHeaders

        self.assertTrue('Foo_1' in customHttpHeaders)
        self.assertTrue('Foo_2' in customHttpHeaders)
        self.assertEqual('bar_1', customHttpHeaders['Foo_1'])
        self.assertEqual('bar_2', customHttpHeaders['Foo_2'])

        self.assertFalse(self.wrapper.clearCustomHttpHeader('Foo_3'), 'already cleaned')
Esempio n. 18
0
    def testClearCustomHttpHeader(self):
        request = self._get_request(self.wrapper)
        self.assertFalse(request.has_header("Foo"))

        # Add new header field name
        self.wrapper.addCustomHttpHeader("Foo_1", "bar_1")
        self.wrapper.addCustomHttpHeader("Foo_2", "bar_2")
        self.wrapper.addCustomHttpHeader("Foo_3", "bar_3")

        self.assertFalse(self.wrapper.clearCustomHttpHeader("Foo_4"))
        self.assertTrue(self.wrapper.clearCustomHttpHeader("Foo_3"))

        customHttpHeaders = self.wrapper.customHttpHeaders

        self.assertTrue("Foo_1" in customHttpHeaders)
        self.assertTrue("Foo_2" in customHttpHeaders)
        self.assertEqual("bar_1", customHttpHeaders["Foo_1"])
        self.assertEqual("bar_2", customHttpHeaders["Foo_2"])

        self.assertFalse(self.wrapper.clearCustomHttpHeader("Foo_3"),
                         "already cleaned")
Esempio n. 19
0
    def http_request(self, request):
        data = request.get_data()
        def isfiledata(p_str):
            import re

            r_c = re.compile("^f'(.*)'$")
            rert = r_c.search(str(p_str))
            #rert = re.search("^f'(.*)'$", p_str)
            if rert:
                return rert.group(1)
            else:
                return None

        if data is not None and type(data) != str:
            v_files = []
            v_vars = []
            try:
                 for(key, value) in list(data.items()):
                     if  isfiledata(value):                       # type(value) == file:
                         v_files.append((key, value))
                     else:
                         v_vars.append((key, value))
            except TypeError:
                systype, value, traceback = sys.exc_info()
                raise TypeError("not a valid non-string sequence or mapping object").with_traceback(traceback)

            if len(v_files) == 0:
                data = urllib.parse.urlencode(v_vars, doseq)
            else:
                boundary, data = self.multipart_encode(v_vars, v_files)

                contenttype = 'multipart/form-data; boundary=%s' % boundary
                if(request.has_header('Content-Type')
                   and request.get_header('Content-Type').find('multipart/form-data') != 0):
                    print("Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data'))
                request.add_unredirected_header('Content-Type', contenttype)

            request.add_data(data)
        
        return request
Esempio n. 20
0
 def http_request(self, request):
     if ((self.referer is not None) and not request.has_header("Referer")):
         request.add_unredirected_header("Referer", self.referer)
     return request
Esempio n. 21
0
 def http_request(self, request):
     if ((self.referer is not None) and
         not request.has_header("Referer")):
         request.add_unredirected_header("Referer", self.referer)
     return request