This file is indexed.

/usr/share/pyshared/restkit/client.py is in python-restkit 3.3.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# -*- coding: utf-8 -
#
# This file is part of restkit released under the MIT license.
# See the NOTICE for more information.
import base64
import errno
import logging
import os
import time
import socket
import traceback
import types
import urlparse

try:
    import ssl # python 2.6
    _ssl_wrapper = ssl.wrap_socket
    have_ssl = True
except ImportError:
    if hasattr(socket, "ssl"):
        from httplib import FakeSocket
        from restkit.sock import trust_all_certificates

        @trust_all_certificates
        def _ssl_wrapper(sck, **kwargs):
            ssl_sck = socket.ssl(sck, **kwargs)
            return FakeSocket(sck, ssl_sck)
        have_ssl = True
    else:
        have_ssl = False

try:
    from http_parser.http import HttpStream
    from http_parser.reader import SocketReader
except ImportError:
    raise ImportError("""http-parser isn't installed.
        
        pip install http-parser""")

from restkit import __version__

from restkit.conn import Connection
from restkit.errors import RequestError, RequestTimeout, RedirectLimit, \
NoMoreData, ProxyError
from restkit.globals import get_manager
from restkit.sock import close, send, sendfile, sendlines, send_chunk, \
validate_ssl_args
from restkit.util import parse_netloc, rewrite_location
from restkit.wrappers import Request, Response

MAX_CLIENT_TIMEOUT=300
MAX_CLIENT_CONNECTIONS = 5
MAX_CLIENT_TRIES = 5
CLIENT_WAIT_TRIES = 0.3
MAX_FOLLOW_REDIRECTS = 5
USER_AGENT = "restkit/%s" % __version__

log = logging.getLogger(__name__)

class Client(object):

    """ A client handle a connection at a time. A client is threadsafe,
    but an handled shouldn't be shared between threads. All connections
    are shared between threads via a pool.

    >>> from restkit import *
    >>> c = Client()
    >>> r = c.request("http://google.com")
    r>>> r.status
    '301 Moved Permanently'
    >>> r.body_string()
    '<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
    >>> c.follow_redirect = True
    >>> r = c.request("http://google.com")
    >>> r.status
    '200 OK'

    """

    version = (1, 1)
    response_class=Response

    def __init__(self,
            follow_redirect=False,
            force_follow_redirect=False,
            max_follow_redirect=MAX_FOLLOW_REDIRECTS,
            filters=None,
            decompress=True,
            max_status_line_garbage=None,
            max_header_count=0,
            manager=None,
            response_class=None,
            timeout=None,
            use_proxy=False,
            max_tries=5,
            wait_tries=1.0,
            **ssl_args):
        """
        Client parameters
        ~~~~~~~~~~~~~~~~~

        :param follow_redirect: follow redirection, by default False
        :param max_ollow_redirect: number of redirections available
        :filters: http filters to pass
        :param decompress: allows the client to decompress the response
        body
        :param max_status_line_garbage: defines the maximum number of ignorable
        lines before we expect a HTTP response's status line. With
        HTTP/1.1 persistent connections, the problem arises that broken
        scripts could return a wrong Content-Length (there are more
        bytes sent than specified).  Unfortunately, in some cases, this
        cannot be detected after the bad response, but only before the
        next one. So the client is abble to skip bad lines using this
        limit. 0 disable garbage collection, None means unlimited number
        of tries.
        :param max_header_count:  determines the maximum HTTP header count
        allowed. by default no limit.
        :param manager: the manager to use. By default we use the global
        one.
        :parama response_class: the response class to use
        :param timeout: the default timeout of the connection
        (SO_TIMEOUT)

        :param max_tries: the number of tries before we give up a
        connection
        :param wait_tries: number of time we wait between each tries.
        :param ssl_args: named argument, see ssl module for more
        informations
        """
        self.follow_redirect = follow_redirect
        self.force_follow_redirect = force_follow_redirect
        self.max_follow_redirect = max_follow_redirect
        self.decompress = decompress
        self.filters = filters or []
        self.max_status_line_garbage = max_status_line_garbage
        self.max_header_count = max_header_count
        self.use_proxy = use_proxy

        self.request_filters = []
        self.response_filters = []
        self.load_filters()


        # set manager
        if manager is None:
            manager = get_manager()
        self._manager = manager

        # change default response class
        if response_class is not None:
            self.response_class = response_class

        self.max_tries = max_tries
        self.wait_tries = wait_tries
        self.timeout = timeout

        self._nb_redirections = self.max_follow_redirect
        self._url = None
        self._initial_url = None
        self._write_cb = None
        self._headers = None
        self._sock_key = None
        self._sock = None
        self._original = None

        self.method = 'GET'
        self.body = None
        self.ssl_args = ssl_args or {}

    def load_filters(self):
        """ Populate filters from self.filters.
        Must be called each time self.filters is updated.
        """
        for f in self.filters:
            if hasattr(f, "on_request"):
                self.request_filters.append(f)
            if hasattr(f, "on_response"):
                self.response_filters.append(f)

    def connect(self, addr, is_ssl):
        """ create a socket """
        if log.isEnabledFor(logging.DEBUG):
            log.debug("create new connection")
        for res in socket.getaddrinfo(addr[0], addr[1], 0,
                socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res

            try:
                sck = socket.socket(af, socktype, proto)

                if self.timeout is not None:
                    sck.settimeout(self.timeout)

                sck.connect(sa)

                if is_ssl:
                    if not have_ssl:
                        raise ValueError("https isn't supported.  On python 2.5x,"
                                        + " https support requires ssl module "
                                        + "(http://pypi.python.org/pypi/ssl) "
                                        + "to be intalled.")
                    validate_ssl_args(self.ssl_args)
                    sck = _ssl_wrapper(sck, **self.ssl_args)

                return sck
            except socket.error, ex:
                if ex == 24: # too many open files
                    raise
                else:
                    close(sck)
        raise socket.error, "Can't connect to %s" % str(addr)

    def get_connection(self, request):
        """ get a connection from the pool or create new one. """

        addr = parse_netloc(request.parsed_url)
        is_ssl = request.is_ssl()

        extra_headers = []
        sck = None
        if self.use_proxy:
            sck, addr, extra_headers = self.proxy_connection(request,
                    addr, is_ssl)
        if not sck:
            sck = self._manager.find_socket(addr, is_ssl)
            if sck is None:
                sck = self.connect(addr, is_ssl)

        # set socket timeout in case default has changed
        if self.timeout is not None:
            sck.settimeout(self.timeout)

        connection = Connection(sck, self._manager, addr,
                ssl=is_ssl, extra_headers=extra_headers)
        return connection

    def proxy_connection(self, request, req_addr, is_ssl):
        """ do the proxy connection """
        proxy_settings = os.environ.get('%s_proxy' %
                request.parsed_url.scheme)

        if proxy_settings and proxy_settings is not None:
            request.is_proxied = True

            proxy_settings, proxy_auth =  _get_proxy_auth(proxy_settings)
            addr = parse_netloc(urlparse.urlparse(proxy_settings))

            if is_ssl:
                if proxy_auth:
                    proxy_auth = 'Proxy-authorization: %s' % proxy_auth
                proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % req_addr

                user_agent = request.headers.iget('user_agent')
                if not user_agent:
                    user_agent = "User-Agent: restkit/%s\r\n" % __version__

                proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth,
                        user_agent)

                sck = self._manager.find_socket(addr, is_ssl)
                if sck is None:
                    sck = self.connect(addr, is_ssl)

                send(sck, proxy_pieces)
                unreader = http.Unreader(sck)
                resp = http.Request(unreader)
                body = resp.body.read()
                if resp.status_int != 200:
                    raise ProxyError("Tunnel connection failed: %d %s" %
                            (resp.status_int, body))

                return sck, addr, []
            else:
                headers = []
                if proxy_auth:
                    headers = [('Proxy-authorization', proxy_auth)]

                sck = self._manager.find_socket(addr, is_ssl)
                if sck is None:
                    sck = self.connect(addr, is_ssl)
                return sck, addr, headers
        return None, req_addr, []

    def make_headers_string(self, request, extra_headers=None):
        """ create final header string """
        headers = request.headers.copy()
        if extra_headers is not None:
            for k, v in extra_headers:
                headers[k] = v

        if not request.body and request.method in ('POST', 'PUT',):
            headers['Content-Length'] = 0

        if self.version == (1,1):
            httpver = "HTTP/1.1"
        else:
            httpver = "HTTP/1.0"

        ua = headers.iget('user_agent')
        if not ua:
            ua = USER_AGENT
        host = request.host

        accept_encoding = headers.iget('accept-encoding')
        if not accept_encoding:
            accept_encoding = 'identity'

        if request.is_proxied:
            full_path = ("https://" if request.is_ssl() else "http://") + request.host + request.path
        else:
            full_path = request.path

        lheaders = [
            "%s %s %s\r\n" % (request.method, full_path, httpver),
            "Host: %s\r\n" % host,
            "User-Agent: %s\r\n" % ua,
            "Accept-Encoding: %s\r\n" % accept_encoding
        ]

        lheaders.extend(["%s: %s\r\n" % (k, str(v)) for k, v in \
                headers.items() if k.lower() not in \
                ('user-agent', 'host', 'accept-encoding',)])
        if log.isEnabledFor(logging.DEBUG):
            log.debug("Send headers: %s" % lheaders)
        return "%s\r\n" % "".join(lheaders)

    def perform(self, request):
        """ perform the request. If an error happen it will first try to
        restart it """

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to perform request: %s %s %s" %
                    (request.host, request.method, request.path))

        tries = self.max_tries
        wait = self.wait_tries
        while tries > 0:
            try:
                # get or create a connection to the remote host
                connection = self.get_connection(request)
                sck = connection.socket()

                # send headers
                msg = self.make_headers_string(request,
                        connection.extra_headers)

                # send body
                if request.body is not None:
                    chunked = request.is_chunked()
                    if request.headers.iget('content-length') is None and \
                            not chunked:
                        raise RequestError(
                                "Can't determine content length and " +
                                "Transfer-Encoding header is not chunked")


                    # handle 100-Continue status
                    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
                    hdr_expect = request.headers.iget("expect")
                    if hdr_expect is not None and \
                            hdr_expect.lower() == "100-continue":
                        sck.sendall(msg)
                        msg = None
                        resp = http.Request(http.Unreader(self._sock))
                        if resp.status_int != 100:
                            self.reset_request()
                            if log.isEnabledFor(logging.DEBUG):
                                log.debug("return response class")
                            return self.response_class(connection,
                                    request, resp)

                    chunked = request.is_chunked()
                    if log.isEnabledFor(logging.DEBUG):
                        log.debug("send body (chunked: %s)" % chunked)


                    if isinstance(request.body, types.StringTypes):
                        if msg is not None:
                            send(sck, msg + request.body, chunked)
                        else:
                            send(sck, request.body, chunked)
                    else:
                        if msg is not None:
                            sck.sendall(msg)

                        if hasattr(request.body, 'read'):
                            if hasattr(request.body, 'seek'): request.body.seek(0)
                            sendfile(sck, request.body, chunked)
                        else:
                            sendlines(sck, request.body, chunked)
                    if chunked:
                        send_chunk(sck, "")
                else:
                    sck.sendall(msg)

                return self.get_response(request, connection)
            except socket.gaierror, e:
                try:
                    connection.close()
                except:
                    pass

                raise RequestError(str(e))
            except socket.timeout, e:
                try:
                    connection.close()
                except:
                    pass

                if tries <= 0:
                    raise RequestTimeout(str(e))
            except socket.error, e:
                if log.isEnabledFor(logging.DEBUG):
                    log.debug("socket error: %s" % str(e))
                try:
                    connection.close()
                except:
                    pass

                if e[0] not in (errno.EAGAIN, errno.ECONNABORTED,
                        errno.EPIPE, errno.ECONNREFUSED,
                        errno.ECONNRESET, errno.EBADF) or tries <= 0:
                    raise RequestError("socket.error: %s" % str(e))
            except (StopIteration, NoMoreData):
                connection.close()
                if tries <= 0:
                    raise
                else:
                    if request.body is not None:
                        if not hasattr(request.body, 'read') and \
                                not isinstance(request.body, types.StringTypes):
                            raise RequestError("connection closed and can't"
                                    + "be resent")
            except Exception:
                # unkown error
                log.debug("unhandled exception %s" %
                        traceback.format_exc())
                raise

            # time until we retry.
            time.sleep(wait)
            wait = wait * 2
            tries = tries - 1


    def request(self, url, method='GET', body=None, headers=None):
        """ perform immediatly a new request """

        request = Request(url, method=method, body=body,
                headers=headers)

        # apply request filters
        # They are applied only once time.
        for f in self.request_filters:
            ret = f.on_request(request)
            if isinstance(ret, Response):
                # a response instance has been provided.
                # just return it. Useful for cache filters
                return ret

        # no response has been provided, do the request
        self._nb_redirections = self.max_follow_redirect
        return self.perform(request)

    def redirect(self, resp, location, request):
        """ reset request, set new url of request and perform it """
        if self._nb_redirections <= 0:
            raise RedirectLimit("Redirection limit is reached")

        if request.initial_url is None:
            request.initial_url = self.url

        # make sure location follow rfc2616
        location = rewrite_location(request.url, location)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Redirect to %s" % location)

        # change request url and method if needed
        request.url = location

        self._nb_redirections -= 1

        #perform a new request
        return self.perform(request)

    def get_response(self, request, connection):
        """ return final respons, it is only accessible via peform
        method """
        if log.isEnabledFor(logging.DEBUG):
            log.debug("Start to parse response")

        p = HttpStream(SocketReader(connection.socket()), kind=1, 
                decompress=self.decompress)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("Got response: %s" % p.status())
            log.debug("headers: [%s]" % p.headers())

        location = p.headers().get('location')
        
        if self.follow_redirect:
            if p.status_code() in (301, 302, 307,):
                if request.method in ('GET', 'HEAD',) or \
                        self.force_follow_redirect:
                    if hasattr(self.body, 'read'):
                        try:
                            self.body.seek(0)
                        except AttributeError:
                            connection.release()
                            raise RequestError("Can't redirect %s to %s "
                                    "because body has already been read"
                                    % (self.url, location))
                    connection.release()
                    return self.redirect(p, location, request)

            elif p.status_code() == 303 and self.method == "POST":
                connection.release()
                request.method = "GET"
                request.body = None
                return self.redirect(p, location, request)

        # create response object
        resp = self.response_class(connection, request, p)

        # apply response filters
        for f in self.response_filters:
            f.on_response(resp, request)

        if log.isEnabledFor(logging.DEBUG):
            log.debug("return response class")

        # return final response
        return resp


def _get_proxy_auth(proxy_settings):
    proxy_username = os.environ.get('proxy-username')
    if not proxy_username:
        proxy_username = os.environ.get('proxy_username')
    proxy_password = os.environ.get('proxy-password')
    if not proxy_password:
        proxy_password = os.environ.get('proxy_password')

    proxy_password = proxy_password or ""

    if not proxy_username:
        u = urlparse.urlparse(proxy_settings)
        if u.username:
            proxy_password = u.password or proxy_password
            proxy_settings = urlparse.urlunparse((u.scheme,
                u.netloc.split("@")[-1], u.path, u.params, u.query,
                u.fragment))

    if proxy_username:
        user_auth = base64.encodestring('%s:%s' % (proxy_username,
                                    proxy_password))
        return proxy_settings, 'Basic %s\r\n' % (user_auth.strip())
    else:
        return proxy_settings, ''