예제 #1
0
 def make_requests_from_url(self,url):
     try:
         course_id = re.findall(r'class.coursera.org/(.*)/class',url)[0]
     except IndexError:
         print "unrecognizable coursera course URL, because course id cannot be extracted"
         return 
     forum_url = "http://class.coursera.org/%s/forum/index" %course_id
     print forum_url
     headers = self._load_header()
     cookie_string = headers["Cookie"]
     del headers["Cookie"]
     cookie = SimpleCookie(cookie_string)
     print headers
     print cookie.keys()
     return Request(forum_url, headers = headers, cookies = cookie)
예제 #2
0
파일: __init__.py 프로젝트: iceoton/gf2
    def __read_cookie(self):
        """Reads the HTTP Cookie and loads the sid and data from it (if any)."""
        try:
            # check the cookie to see if a session has been started
            cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
            self.cookie_keys = filter(is_gaesessions_key, cookie.keys())
            if not self.cookie_keys:
                return  # no session yet
            self.cookie_keys.sort()
            data = ''.join(cookie[k].value for k in self.cookie_keys)
            i = SIG_LEN + SID_LEN
            sig, sid, b64pdump = data[:SIG_LEN], data[SIG_LEN:i], data[i:]
            pdump = b64decode(b64pdump)
            actual_sig = Session.__compute_hmac(self.base_key, sid, pdump)
            if sig == actual_sig:
                self.__set_sid(sid, False)
                # check for expiration and terminate the session if it has expired
                if self.get_expiration() != 0 and time.time() > self.get_expiration():
                    return self.terminate()

                if pdump:
                    self.data = self.__decode_data(pdump)
                else:
                    self.data = None  # data is in memcache/db: load it on-demand
            else:
                logging.warn('cookie with invalid sig received from %s: %s' % (os.environ.get('REMOTE_ADDR'), b64pdump))
        except (CookieError, KeyError, IndexError, TypeError):
            # there is no cookie (i.e., no session) or the cookie is invalid
            self.terminate(False)
예제 #3
0
    def __read_cookie(self):
        """Reads the HTTP Cookie and loads the sid and data from it (if any)."""
        print 'session: __read_cookie'
        try:
            if self.environ.get('HTTP_COOKIE') is None:
                return #no cookies

            #cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
            cookie = SimpleCookie(self.environ.get('HTTP_COOKIE'))
            self.cookie_keys = filter(is_mole_sessions_key, cookie.keys())
            if not self.cookie_keys:
                return  # no session

            self.cookie_keys.sort()
            data = ''.join(cookie[k].value for k in self.cookie_keys)
            i = SIG_LEN + SID_LEN
            sig, sid, b64pdump = data[:SIG_LEN], data[SIG_LEN:i], data[i:]
            pdump = b64decode(b64pdump)
            actual_sig = Session.__compute_hmac(self.base_key, sid, pdump)
            if sig == actual_sig:
                self.__set_sid(sid, False)
                if self.get_expiration() != 0 and time.time() > self.get_expiration():
                    return self.terminate()

                if pdump:
                    self.data = self.__decode_data(pdump)
                else:
                    self.data = None
            else:
                logging.warn('cookie with invalid sig received from %s: %s' % (os.environ.get('REMOTE_ADDR'), b64pdump))
        except (CookieError, KeyError, IndexError, TypeError):
            import traceback;traceback.print_exc()
            logging.error("session error:", exc_info=True)
            self.terminate(False)
예제 #4
0
파일: sessions.py 프로젝트: JoneXiong/Mole
    def __read_cookie(self):
        """Reads the HTTP Cookie and loads the sid and data from it (if any)."""
        print 'session: __read_cookie'
        try:
            if self.environ.get('HTTP_COOKIE') is None:
                return #no cookies

            #cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
            cookie = SimpleCookie(self.environ.get('HTTP_COOKIE'))
            self.cookie_keys = filter(is_mole_sessions_key, cookie.keys())
            if not self.cookie_keys:
                return  # no session

            self.cookie_keys.sort()
            data = ''.join(cookie[k].value for k in self.cookie_keys)
            i = SIG_LEN + SID_LEN
            sig, sid, b64pdump = data[:SIG_LEN], data[SIG_LEN:i], data[i:]
            pdump = b64decode(b64pdump)
            actual_sig = Session.__compute_hmac(self.base_key, sid, pdump)
            if sig == actual_sig:
                self.__set_sid(sid, False)
                if self.get_expiration() != 0 and time.time() > self.get_expiration():
                    return self.terminate()

                if pdump:
                    self.data = self.__decode_data(pdump)
                else:
                    self.data = None
            else:
                logging.warn('cookie with invalid sig received from %s: %s' % (os.environ.get('REMOTE_ADDR'), b64pdump))
        except (CookieError, KeyError, IndexError, TypeError):
            import traceback;traceback.print_exc()
            logging.error("session error:", exc_info=True)
            self.terminate(False)
예제 #5
0
    def __read_cookie(self):
        """Reads the HTTP Cookie and loads the sid and data from it (if any)."""
        try:
            # check the cookie to see if a session has been started
            cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
            self.cookie_keys = filter(is_gaesessions_key, cookie.keys())
            if not self.cookie_keys:
                return  # no session yet
            self.cookie_keys.sort()
            data = ''.join(cookie[k].value for k in self.cookie_keys)
            i = SIG_LEN + SID_LEN
            sig, sid, b64pdump = data[:SIG_LEN], data[SIG_LEN:i], data[i:]
            pdump = b64decode(b64pdump)
            actual_sig = Session.__compute_hmac(self.base_key, sid, pdump)
            if sig == actual_sig:
                self.__set_sid(sid, False)
                # check for expiration and terminate the session if it has expired
                if self.get_expiration() != 0 and time.time() > self.get_expiration():
                    return self.terminate()

                if pdump:
                    self.data = self.__decode_data(pdump)
                else:
                    self.data = None  # data is in memcache/db: load it on-demand
            else:
                logging.warn('cookie with invalid sig received from %s: %s' % (os.environ.get('REMOTE_ADDR'), b64pdump))
        except (CookieError, KeyError, IndexError, TypeError):
            # there is no cookie (i.e., no session) or the cookie is invalid
            self.terminate(False)
    def renderPage(self):
        cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
        printout = ''
        self.cookie_keys = cookie.keys()
        if not self.cookie_keys:
            self.response.out.write('No session yet')
            return
        self.cookie_keys.sort()
        data = ''.join(cookie[k].value for k in self.cookie_keys)
        printout += data + '\n\n\n'

        i = gaesessions.SIG_LEN + gaesessions.SID_LEN
        sig, b64pdump = data[:gaesessions.SIG_LEN], data[i:]
        printout += 'sig = ' + sig + '\n'
        b64pdump += "=" * ((4 - len(b64pdump) % 4) % 4)
        printout += 'len = ' + str(len(b64pdump)) + '\n'
        printout += 'padding = ' + str(((4 - len(b64pdump) % 4) % 4)) + '\n\n'

        try:
            pdump = b64decode(b64pdump)

            if pdump:
                printout += printdict(decode_data(pdump))
            else:
                printout += 'data is in memcache/db: load it on-demand'
        except:
            lens = len(b64pdump)
            lenx = lens - (lens % 4 if lens % 4 else 4)
            try:
                printout += b64decode(b64pdump[:lenx])
            except:
                printout += str(sys.exc_info())

        session = sessionmanager.getsession(self)

        template_values = {
            'appId': conf.FBAPI_APP_ID,
            'token': session['access_token'],
            'app': session['appid'],
            'conf': conf,
            'me': session['me'],
            'cookie': printout,
            'isdesktop': session and session['isdesktop'] or False,
            'header': '',
            'code': self.request.get('code', None)
        }

        root = os.path.normpath(
            os.path.join(os.path.dirname(__file__), os.path.pardir))
        self.response.out.write(
            template.render(os.path.join(root, 'templates/_header.html'),
                            template_values))
        self.response.out.write(
            template.render(
                os.path.join(root, 'admin/templates/dumpcookie.html'),
                template_values))
        self.response.out.write(
            template.render(os.path.join(root, 'templates/_footer.html'),
                            template_values))
예제 #7
0
    def _parse_cookies(cookies):
        if not cookies:
            return {}

        cookie = SimpleCookie()
        cookie.load(cookies)

        return {key: cookie[key].coded_value for key in cookie.keys()}
예제 #8
0
파일: __init__.py 프로젝트: konteck/core
    def __get__(self, item, cls):
        if len(self) == 0:
            c = SimpleCookie(app.request.get('HTTP_COOKIE', ''))

            # TODO Fix for GAE
            for key in c.keys():
                self[key] = c.get(key).value

        return self
def parse_cookie(cookie):
    if cookie == '':
        return {}
    c = SimpleCookie()
    c.load(cookie)
    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict
예제 #10
0
파일: __init__.py 프로젝트: 0xmilk/appscale
def parse_cookie(cookie):
    if cookie == '':
        return {}
    c = SimpleCookie()
    c.load(cookie)
    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict
예제 #11
0
파일: __init__.py 프로젝트: konteck/core
    def COOKIES(self):
        if 'CORE_COOKIES' not in self:
            c = SimpleCookie(self.get('HTTP_COOKIE', ''))

            cd = {}

            # TODO Fix for GAE
            for key in c.keys():
                cd[key] = c.get(key).value

            self['CORE_COOKIES'] = Hashmap(cd)

        return self['CORE_COOKIES']
예제 #12
0
class Response(object):
    """
    Describes an HTTP response. Currently very simple since the actual body
    of the request is handled separately.
    """
    def __init__(self):
        """
        Create a new Response defaulting to HTML content and "200 OK" status
        """
        self.status = "200 OK"
        self.headers = HeaderDict({"content-type": "text/html"})
        # Allow cross-site scripting
        self.headers["Access-Control-Allow-Origin"] = "*"
        self.headers[
            "Access-Control-Allow-Headers"] = "Content-Type, X-Requested-With, X-Requested-By"
        self.headers["Access-Control-Allow-Methods"] = "POST, GET"
        self.cookies = SimpleCookie()

    def set_content_type(self, type):
        """
        Sets the Content-Type header
        """
        self.headers["content-type"] = type

    def send_redirect(self, url):
        """
        Send an HTTP redirect response to (target `url`)
        """
        raise httpexceptions.HTTPFound(url, headers=self.wsgi_headeritems())

    def wsgi_headeritems(self):
        """
        Return headers in format appropriate for WSGI `start_response`
        """
        result = self.headers.headeritems()
        # Add cookie to header
        for name in self.cookies.keys():
            crumb = self.cookies[name]
            header, value = str(crumb).split(': ', 1)
            result.append((header, value))
        return result

    def wsgi_status(self):
        """
        Return status line in format appropriate for WSGI `start_response`
        """
        if isinstance(self.status, int):
            exception = httpexceptions.get_exception(self.status)
            return "%d %s" % (exception.code, exception.title)
        else:
            return self.status
예제 #13
0
def parse_cookie(cookie):
    """ borrowed from django.http """
    if cookie == '':
        return {}
    try:
        c = SimpleCookie()
        c.load(cookie)
    except CookieError:
        return {}

    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict        
예제 #14
0
def parse_cookie(cookie):
    if cookie == '':
        return {}
    try:
        c = SimpleCookie()
        c.load(cookie)
    except CookieError:
        # Invalid cookie
        return {}

    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict
예제 #15
0
def parse_cookie(cookie):
    if cookie == '':
        return {}
    try:
        c = SimpleCookie()
        c.load(cookie)
    except CookieError:
        # Invalid cookie
        return {}

    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict
예제 #16
0
 def renderPage(self):
     cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
     printout = ''
     self.cookie_keys = cookie.keys()
     if not self.cookie_keys:
         self.response.out.write('No session yet')
         return
     self.cookie_keys.sort()
     data = ''.join(cookie[k].value for k in self.cookie_keys)
     printout += data + '\n\n\n'
     
     i = gaesessions.SIG_LEN + gaesessions.SID_LEN
     sig, b64pdump = data[:gaesessions.SIG_LEN], data[i:]
     printout += 'sig = ' + sig + '\n'
     b64pdump += "=" * ((4 - len(b64pdump) % 4) % 4)
     printout += 'len = ' + str(len(b64pdump)) + '\n'
     printout += 'padding = ' + str(((4 - len(b64pdump) % 4) % 4)) + '\n\n'
     
     try:
         pdump = b64decode(b64pdump)
         
         if pdump:
             printout += printdict(decode_data(pdump))
         else:
             printout += 'data is in memcache/db: load it on-demand'
     except:
         lens = len(b64pdump)
         lenx = lens - (lens % 4 if lens % 4 else 4)
         try:
             printout += b64decode(b64pdump[:lenx])
         except:
             printout += str(sys.exc_info())
     
     session = sessionmanager.getsession(self)
     
     template_values = {
             'appId': conf.FBAPI_APP_ID,
             'token': session['access_token'], 
             'app': session['appid'],
             'conf': conf,
             'me': session['me'],
             'cookie': printout,
             'isdesktop': session and session['isdesktop'] or False,
             'header': '',
             'code': self.request.get('code', None) }
     
     root = os.path.normpath(os.path.join(os.path.dirname(__file__), os.path.pardir))
     self.response.out.write(template.render(os.path.join(root, 'templates/_header.html'), template_values))
     self.response.out.write(template.render(os.path.join(root, 'admin/templates/dumpcookie.html'), template_values))
     self.response.out.write(template.render(os.path.join(root, 'templates/_footer.html'), template_values))
예제 #17
0
class Response(object):
    """
    Describes an HTTP response. Currently very simple since the actual body
    of the request is handled separately.
    """

    def __init__(self):
        """
        Create a new Response defaulting to HTML content and "200 OK" status
        """
        self.status = "200 OK"
        self.headers = HeaderDict({"content-type": "text/html"})
        self.cookies = SimpleCookie()

    def set_content_type(self, type):
        """
        Sets the Content-Type header
        """
        self.headers["content-type"] = type

    def get_content_type(self):
        return self.headers["content-type"]

    def send_redirect(self, url):
        """
        Send an HTTP redirect response to (target `url`)
        """
        raise httpexceptions.HTTPFound(url.encode("utf-8"), headers=self.wsgi_headeritems())

    def wsgi_headeritems(self):
        """
        Return headers in format appropriate for WSGI `start_response`
        """
        result = self.headers.headeritems()
        # Add cookie to header
        for name in self.cookies.keys():
            crumb = self.cookies[name]
            header, value = str(crumb).split(": ", 1)
            result.append((header, value))
        return result

    def wsgi_status(self):
        """
        Return status line in format appropriate for WSGI `start_response`
        """
        if isinstance(self.status, int):
            exception = httpexceptions.get_exception(self.status)
            return "%d %s" % (exception.code, exception.title)
        else:
            return self.status
예제 #18
0
def parse_cookie(cookie):
    if cookie == '':
        return {}
    if not isinstance(cookie, BaseCookie):
        try:
            c = SimpleCookie()
            c.load(cookie)
        except CookieError:
            # Invalid cookie
            return {}
    else:
        c = cookie
    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict
예제 #19
0
def parse_cookie(cookie):
    if cookie == '':
        return {}
    if not isinstance(cookie, BaseCookie):
        try:
            c = SimpleCookie()
            c.load(cookie)
        except CookieError:
            # Invalid cookie
            return {}
    else:
        c = cookie
    cookiedict = {}
    for key in c.keys():
        cookiedict[key] = c.get(key).value
    return cookiedict
예제 #20
0
def get_the_cookie(url, username=None, password=None):
    req = urlopen(url)
    data = req.read()
    hidden_inputs = re.findall('<INPUT.*HIDDEN.*NAME="(.*)".*VALUE="(.*)">', data)
    if username is None:
        username = raw_input('Username: '******'username', username))
    hidden_inputs.append(('passcode', password))
    qs = urlencode(hidden_inputs)

    action = urljoin(url, re.findall('<FORM action="([^"]*)" .*>', data)[0])
    req2 = urlopen(action, data=qs)
    cookie = SimpleCookie(req2.info()['set-cookie'])
    return cookie.keys()[0] + "=" + cookie.values()[0].value
예제 #21
0
    def load_cookie(self, cookie_string):
        """Reads the HTTP Cookie and loads the sid and data from it (if any)."""
        try:
            # check the cookie to see if a session has been started
            cookie = SimpleCookie(cookie_string)
            logging.info("JUST GOT COOKIE WHICH IS: {}".format(cookie))
            self.cookie_keys = filter(is_gaesessions_key, cookie.keys())
            if not self.cookie_keys:
                logging.info("NO APPROPRIATE KEYS")
                return  # no session yet
            self.cookie_keys.sort()
            logging.info("THE KEYS: {}".format(self.cookie_keys))
            data = "".join(cookie[k].value for k in self.cookie_keys)
            logging.info("DATA: {}".format(data))
            i = SIG_LEN + SID_LEN
            logging.info("i: {}".format(i))
            sig, sid, b64pdump = data[:SIG_LEN], data[SIG_LEN:i], data[i:]
            logging.info("sig: {}".format(sig))
            logging.info("sid: {}".format(sid))
            logging.info("b64pdump: {}".format(b64pdump))
            pdump = b64decode(b64pdump)
            actual_sig = Session.__compute_hmac(self.base_key, sid, pdump)
            if sig == actual_sig:
                logging.info("SIG MATCH")
                self.__set_sid(sid, False)
                # check for expiration and terminate the session if it has expired
                if self.get_expiration() != 0 and time.time() > self.get_expiration():
                    logging.info("EXPIRED")
                    return self.terminate()

                if pdump:
                    logging.info("LOADING NEW DATA")
                    self.data = self.__decode_data(pdump)
                else:
                    logging.info("SETTING NONE DATA")
                    self.data = None  # data is in memcache/db: load it on-demand
            else:
                logging.warn("cookie with invalid sig received from %s: %s" % (os.environ.get("REMOTE_ADDR"), b64pdump))
        except (CookieError, KeyError, IndexError, TypeError), e:
            logging.info("OOPSIE: {}".format(e))
            logging.info(traceback.print_exc(e))
            # there is no cookie (i.e., no session) or the cookie is invalid
            self.terminate(False)
예제 #22
0
class Request(object):
    """Provide a consistent way to treat a Request."""
    def __init__(self):
        self.post = {}
        self.get = {}
        self.url = ''
        self.path_info = ''
        self.remote_address = ''
        self.remove_base_url = False  # needed for WSGI
        self.headers = {'Content-Type': 'text/html'}

        self._incookies = SimpleCookie()
        self._outcookies = SimpleCookie()

    def setHeader(self, header, value):
        if not hasattr(self, 'headers'):
            self.headers = dict()
        self.headers[header] = value

    def setHeaders(self, headers={}):
        for header, value in headers.iteritems():
            self.setHeader(header, value)

    def cookieHeaders(self):
        for name in self._outcookies.keys():
            path = self._outcookies[name].get('path')
            if path:
                path = path.replace(' ', '%20') \
                  .replace(';', '%3B') \
                  .replace(',', '%3C')
            self._outcookies[name]['path'] = path

        cookies = self._outcookies.output(header='')
        for cookie in cookies.splitlines():
            self.setHeader('Set-Cookie', cookie.strip())
        # XXX overwrite header 'Set-Cookie' :S

        return self.headers['Set-Cookie']

    def write(self, content):
        raise NotImplementedError

    def writeResponse(self, response):
        self.write('Status: %d\r\n' % response.status_code)
        for header, value in response.headers.iteritems():
            self.write('%s: %s\r\n' % (header, value))
        self.write('\r\n')
        self.write(response.content)

    def setCookie(self, key, value, path='/', expires=None):
        self._outcookies[key] = value
        # make sure path does not contain trailing slash (#334)
        if path != '/':
            path = path.rstrip('/')

        self._outcookies[key]['path'] = path
        if expires:
            self._outcookies[key]['expires'] = expires

    def getCookie(self, key, default=None):
        try:
            value = self._incookies[key]
        except KeyError:
            return default

        if not value.value:
            return default

        return value.value

    def is_ajax(self):
        return 'ajax' in self.post or 'ajax' in self.get
예제 #23
0
    def mitm_response(self, data):
        lines = data.split("\r\n")
        r = HTTPResponse(FakeSocket(data))
        r.begin()

        # response line
        self.doc.response.status = r.status
        self.doc.response.responseline = lines[0]

        # headers
        ct = ""
        cookies = list()
        for header in r.getheaders():
            name = header[0]
            value = header[1]
            self.doc.add_parsed_response_header(name, value)
            if name == "content-type":
                ct = value
            elif name == "set-cookie":
                cookies.append(value)

        # content type
        try:
            m = reContentType.search(ct)
            self.doc.response.content_type = m.group(1)
        except:
            pass

        # cookies
        for cookie in cookies:
            # TODO: the following code extracts only partial cookie data - check/rewrite
            try:
                pc = SimpleCookie(cookie)
                for name in pc.keys():
                    c = pc[name]
                    try:
                        value = c.value
                    except AttributeError:
                        value = None
                    try:
                        domain = c.domain
                    except AttributeError:
                        domain = None
                    try:
                        path = c.path
                    except AttributeError:
                        path = None
                    try:
                        exp = c.expires
                    except AttributeError:
                        exp = None
                    self.doc.add_response_cookie(name, value, domain, path, exp)
            except:
                pass

        # body
        bodybytes = r.read()
        self.doc.response.body = bodybytes.decode(args.charset, args.encodingerrors)

        try:
            self.doc.save(storeResponseBody)
        except:
            pass
        return data
예제 #24
0
                        event['http']['response']['server-family'] = res_headers['server'].split('/')[0]
                    if 'accept-encoding' in req_headers:
                        req_headers['accept-encoding'] = args_and_weight(req_headers['accept-encoding'])
                    if 'accept-charset' in req_headers:
                        req_headers['accept-charset'] = args_and_weight(req_headers['accept-charset'])
                    if 'accept-language' in req_headers:
                        req_headers['accept-language'] = args_and_weight(req_headers['accept-language'])
                    if 'accept' in req_headers:
                        req_headers['accept'] = args_and_weight(req_headers['accept'])
                    if 'content-length' in req_headers:
                        req_headers['content-length'] = int(req_headers['content-length'])
                    if 'content-length' in res_headers:
                        try:
                            res_headers['content-length'] = int(res_headers['content-length'])
                        except ValueError as e:
                            print "content length is rotten:", e
                    if 'cookie' in req_headers:
                        cookie = SimpleCookie()
                        try:
                            cookie.load(req_headers['cookie'])
                            event['http']['request']['cookie'] = dict([(k, cookie[k].value) for k in cookie.keys()])
                        except CookieError as e:
                            print "oups' cookie: ", e
                    event['http']['request']['headers'] = req_headers
                    event['http']['response']['headers'] = res_headers

                    logstash.sendall(json.dumps(event, separators=(',', ':')) + "\n")
                except socket.error as e:
                    print "Oups", e
                    logstash = None
예제 #25
0
class Request(object):
    """This class is used to abstract the interface between different frontends.

    Trac modules must use this interface. It is not allowed to have
    frontend (cgi, tracd, mod_python) specific code in the modules.
    """

    method = None
    scheme = None
    server_name = None
    server_port = None
    remote_addr = None
    remote_user = None

    args = None
    hdf = None
    authname = None
    perm = None
    session = None
    _headers = None  # additional headers to send

    def __init__(self):
        self.incookie = Cookie()
        self.outcookie = Cookie()
        self._headers = []

    def get_header(self, name):
        """Return the value of the specified HTTP header, or `None` if there's
        no such header in the request.
        """
        raise NotImplementedError

    def send_response(self, code):
        """Set the status code of the response."""
        raise NotImplementedError

    def send_header(self, name, value):
        """Send the response header with the specified name and value."""
        raise NotImplementedError

    def end_headers(self):
        """Must be called after all headers have been sent and before the actual
        content is written.
        """
        raise NotImplementedError

    def _send_cookie_headers(self):
        # Cookie values can not contain " ,;" characters, so escape them
        for name in self.outcookie.keys():
            path = self.outcookie[name].get('path')
            if path:
                path = path.replace(' ', '%20') \
                           .replace(';', '%3B') \
                           .replace(',', '%3C')
            self.outcookie[name]['path'] = path

        cookies = self.outcookie.output(header='')
        for cookie in cookies.splitlines():
            self.send_header('Set-Cookie', cookie.strip())

    def check_modified(self, timesecs, extra=''):
        """Check the request "If-None-Match" header against an entity tag
        generated from the specified last modified time in seconds (`timesecs`),
        optionally appending an `extra` string to indicate variants of the
        requested resource.

        If the generated tag matches the "If-None-Match" header of the request,
        this method sends a "304 Not Modified" response to the client.
        Otherwise, it adds the entity tag as as "ETag" header to the response so
        that consequetive requests can be cached.
        """
        etag = 'W"%s/%d/%s"' % (self.authname, timesecs, extra)
        inm = self.get_header('If-None-Match')
        if (not inm or inm != etag):
            self._headers.append(('ETag', etag))
        else:
            self.send_response(304)
            self.end_headers()
            raise RequestDone()

    def redirect(self, url):
        """Send a redirect to the client, forwarding to the specified URL. The
        `url` may be relative or absolute, relative URLs will be translated
        appropriately.
        """
        if self.session:
            self.session.save()  # has to be done before the redirect is sent
        self.send_response(302)
        if not url.startswith('http://') and not url.startswith('https://'):
            # Make sure the URL is absolute
            url = absolute_url(self, url)
        self.send_header('Location', url)
        self.send_header('Content-Type', 'text/plain')
        self.send_header('Pragma', 'no-cache')
        self.send_header('Cache-control', 'no-cache')
        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
        self._send_cookie_headers()
        self.end_headers()

        if self.method != 'HEAD':
            self.write('Redirecting...')

        raise RequestDone

    def display(self, template, content_type='text/html', response=200):
        """Render the response using the ClearSilver template given by the
        `template` parameter, which can be either the name of the template file,
        or an already parsed `neo_cs.CS` object.
        """
        assert self.hdf, 'HDF dataset not available'
        if self.args.has_key('hdfdump'):
            # FIXME: the administrator should probably be able to disable HDF
            #        dumps
            content_type = 'text/plain'
            data = str(self.hdf)
        else:
            data = self.hdf.render(template)

        self.send_response(response)
        self.send_header('Cache-control', 'must-revalidate')
        self.send_header('Expires', 'Fri, 01 Jan 1999 00:00:00 GMT')
        self.send_header('Content-Type', content_type + ';charset=utf-8')
        self.send_header('Content-Length', len(data))
        for name, value in self._headers:
            self.send_header(name, value)
        self._send_cookie_headers()
        self.end_headers()

        if self.method != 'HEAD':
            self.write(data)

        raise RequestDone

    def send_file(self, path, mimetype=None):
        """Send a local file to the browser.
        
        This method includes the "Last-Modified", "Content-Type" and
        "Content-Length" headers in the response, corresponding to the file
        attributes. It also checks the last modification time of the local file
        against the "If-Modified-Since" provided by the user agent, and sends a
        "304 Not Modified" response if it matches.
        """
        if not os.path.isfile(path):
            raise TracError, "File %s not found" % path

        stat = os.stat(path)
        last_modified = http_date(stat.st_mtime)
        if last_modified == self.get_header('If-Modified-Since'):
            self.send_response(304)
            self.end_headers()
            raise RequestDone

        self.send_response(200)
        if not mimetype:
            mimetype = mimetypes.guess_type(path)[0]
        self.send_header('Content-Type', mimetype)
        self.send_header('Content-Length', stat.st_size)
        self.send_header('Last-Modified', last_modified)
        self._send_cookie_headers()
        self.end_headers()

        if self.method != 'HEAD':
            try:
                fd = open(path, 'rb')
                while True:
                    data = fd.read(4096)
                    if not data:
                        break
                    self.write(data)
            finally:
                fd.close()

        raise RequestDone

    def read(self, size):
        """Read the specified number of bytes from the request body."""
        raise NotImplementedError

    def write(self, data):
        """Write the given data to the response body."""
        raise NotImplementedError
예제 #26
0
    def mitm_response(self, data):
        lines = data.split("\r\n")
        r = HTTPResponse(FakeSocket(data))
        r.begin()

        # response line
        self.doc.response.status = r.status
        self.doc.response.responseline = lines[0].decode(args.charset, args.encodingerrors)

        # headers
        ct = ""
        cookies = list()
        for header in r.getheaders():
            name = header[0].decode(args.charset, args.encodingerrors)
            value = header[1].decode(args.charset, args.encodingerrors)
            self.doc.add_parsed_response_header(name, value)
            if name == "content-type":
                ct = value
            elif name == "set-cookie":
                cookies.append(value)

        # content type
        try:
            m = reContentType.search(ct)
            self.doc.response.content_type = m.group(1)
        except:
            pass

        # cookies
        for cookie in cookies:
            # TODO: the following code extracts only partial cookie data - check/rewrite
            try:
                pc = SimpleCookie(cookie)
                for name in pc.keys():
                    c = pc[name]
                    try:
                        value = c.value
                    except AttributeError:
                        value = None
                    try:
                        domain = c.domain
                    except AttributeError:
                        domain = None
                    try:
                        path = c.path
                    except AttributeError:
                        path = None
                    try:
                        exp = c.expires
                    except AttributeError:
                        exp = None
                    self.doc.add_response_cookie(name, value, domain, path, exp)
            except:
                pass

        # body
        bodybytes = r.read()
        self.doc.response.body = bodybytes.decode(args.charset, args.encodingerrors)

        self.doc.save(storeResponseBody)
        return data
예제 #27
0
파일: webapp.py 프로젝트: 20c/vodka1
  def __call__(self, environ, start_response):
    self.set_profiling()
    rv = self.application(environ, start_response)
    t_p = self.start_profile()
    request =  environ.get("request", {})
    headers = request.get("headers", [])

    sesconf = self.sesconf

    # if expireAllSessions command is set, expire all sessions

    if commandQueue.get("expireAllSessions", False):
      self.del_all() 
      command("expireAllSessions", False)
    
    self.end_profile("ses1", environ, t_p)
   
    # if request is done skip execution of this module

    if request.get("done", False):
      self.end_profile("session-handler", environ, t_p)
      return rv
    
    # timeout expired sessions
    
    # self.cleanup(request)

    expires = request["now"] + datetime.timedelta(seconds=int(sesconf.get("timeout", 60*60*24)))

    # load cookie

    cookie = SimpleCookie(environ.get('HTTP_COOKIE')) 
    cookie_name = sesconf.get("cookie_name", "SID")
    cookie_path = sesconf.get("cookie_path", "/")
    cookie_secure = sesconf.get("cookie_secure", "yes")

    cookie_support = len(cookie.keys())

    data = cookie.get(cookie_name, None)

    self.end_profile("ses2", environ, t_p)

    if data:
      
      cookie_support = "ok"
   
      #print "cookie data found, trying to load session %s" % str(data.value)
      
      # session id found in cookie, attempt to load session

      request["session"] = self.load_session(data.value)
      
      if request["session"]:
        request["session"].expires = expires
      cookie[cookie_name]["expires"] = expires.ctime() 
      cookie[cookie_name]['path'] = cookie_path
      
      if(cookie_secure == "yes"):
        cookie[cookie_name]['secure'] = True


    if (not data or not request["session"]) and cookie_support:
      sid = self.generate_id()
      
      #log.error(
      #  "Creating new session object for %s, %s, cookie status: %s, sessions: %s" % 
      #  (sid, str(request), str(data), self.cache.keys())
      #)

      # session id not foind in cookie, create new session and send cookie data
      
      self.cache[sid] = SessionObject(
        id = sid, expires = expires
      )
      if self.profile:
        profile["sessions"] += 1
      request["session"] = self.load_session(sid)
      request["created_session"] = sid 

      cookie[cookie_name] = sid
      cookie[cookie_name]["expires"] = expires.ctime() 
      cookie[cookie_name]['path'] = cookie_path
      if(cookie_secure == "yes"):
        cookie[cookie_name]['secure'] = True
      #print "Cookie set for %s" % str(sid)
    elif not cookie_support:
      request["session"] = SessionObject(id=self.generate_id(), expires=expires)
      cookie["cookie_ok"] = "ok"
      cookie["cookie_ok"]["path"] = "/"
      if(cookie_secure == "yes"):
        cookie["cookie_ok"]['secure'] = True
      request["cookies_out"]["cookie_ok"] = cookie
      #cookie["cookie_ok"]["expires"] = expires.ctime()
      #print "Cookie support not established yet, no session object stored"

    self.end_profile("ses3", environ, t_p)

    request["cookies_out"][cookie_name] = cookie
   
    #headers.append(('Set-Cookie', cookie[cookie_name].OutputString()))

    self.end_profile("session-handler", environ, t_p)

    for app_id, app in app_map.items():
      if hasattr(app, "prepare_request"):
        app.prepare_request(request, environ)

    return rv
예제 #28
0
파일: request.py 프로젝트: andy-deng/submin
class Request(object):
	"""Provide a consistent way to treat a Request."""

	def __init__(self):
		self.post = {}
		self.get = {}
		self.url = ''
		self.path_info = ''
		self.remote_address = ''
		self.remove_base_url = False # needed for WSGI
		self.headers = {'Content-Type': 'text/html'}

		self._incookies = SimpleCookie()
		self._outcookies = SimpleCookie()

	def setHeader(self, header, value):
		if not hasattr(self, 'headers'):
			self.headers = dict()
		self.headers[header] = value

	def setHeaders(self, headers={}):
		for header, value in headers.iteritems():
			self.setHeader(header, value)

	def cookieHeaders(self):
		for name in self._outcookies.keys():
			path = self._outcookies[name].get('path')
			if path:
				path = path.replace(' ', '%20') \
						.replace(';', '%3B') \
						.replace(',', '%3C')
			self._outcookies[name]['path'] = path

		cookies = self._outcookies.output(header='')
		for cookie in cookies.splitlines():
			self.setHeader('Set-Cookie', cookie.strip())
		# XXX overwrite header 'Set-Cookie' :S

		return self.headers['Set-Cookie']

	def write(self, content):
		raise NotImplementedError

	def writeResponse(self, response):
		self.write('Status: %d\r\n' % response.status_code)
		for header, value in response.headers.iteritems():
			self.write('%s: %s\r\n' % (header, value))
		self.write('\r\n')
		self.write(response.content)

	def setCookie(self, key, value, path='/', expires=None):
		self._outcookies[key] = value
		# make sure path does not contain trailing slash (#334)
		if path != '/':
			path = path.rstrip('/')

		self._outcookies[key]['path'] = path
		if expires:
			self._outcookies[key]['expires'] = expires

	def getCookie(self, key, default=None):
		try:
			value = self._incookies[key]
		except KeyError:
			return default
			
		if not value.value:
			return default
		
		return value.value

	def is_ajax(self):
		return 'ajax' in self.post or 'ajax' in self.get
login_url = 'http://internal.host.name/Plone/login_form'

headers = {}
headers['Content-type'] = 'application/x-www-form-urlencoded'
headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers['Leocornus-Header'] = 'Leocornus Django PloneProxy (httplib2)'

login_form = {}
login_form['__ac_name'] = 'username'
login_form['__ac_password'] = '******'
login_form['cookies_enabled'] = '1'
login_form['js_enabled'] = '0'
login_form['form.submitted'] = '1'

response, content = http.request(login_url, 'POST', headers=headers, body=urllib.urlencode(login_form))

print content
print '=============================================='
print response
print '=============================================='

cookie = SimpleCookie()
cookie.load(response['set-cookie'])

for key in cookie.keys():
    print key
    print '===================='
    print cookie.get(key).value
    print '=============================================='
예제 #30
0
headers = {}
headers['Content-type'] = 'application/x-www-form-urlencoded'
headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers['Leocornus-Header'] = 'Leocornus Django PloneProxy (httplib2)'

login_form = {}
login_form['__ac_name'] = 'username'
login_form['__ac_password'] = '******'
login_form['cookies_enabled'] = '1'
login_form['js_enabled'] = '0'
login_form['form.submitted'] = '1'

response, content = http.request(login_url,
                                 'POST',
                                 headers=headers,
                                 body=urllib.urlencode(login_form))

print content
print '=============================================='
print response
print '=============================================='

cookie = SimpleCookie()
cookie.load(response['set-cookie'])

for key in cookie.keys():
    print key
    print '===================='
    print cookie.get(key).value
    print '=============================================='