예제 #1
0
파일: browser.py 프로젝트: marcjal/spynner
 def get_cookie(line):
     fields = map(str.strip, line.split("\t"))
     if len(fields) != 7:
         return
     domain, domain_flag, path, is_secure, expiration, name, value = fields
     cookie = QNetworkCookie(name, value)
     cookie.setDomain(domain)
     cookie.setPath(path)
     cookie.setSecure(str2bool(is_secure))
     cookie.setExpirationDate(QDateTime.fromTime_t(int(expiration)))
     return cookie
예제 #2
0
파일: ghost.py 프로젝트: vmlellis/Ghost.py
 def toQtCookie(pyCookie):
     qc = QNetworkCookie(pyCookie.name, pyCookie.value)
     qc.setSecure(pyCookie.secure)
     if pyCookie.path_specified:
         qc.setPath(pyCookie.path)
     if pyCookie.domain != "":
         qc.setDomain(pyCookie.domain)
     if pyCookie.expires != 0:
         t = QDateTime()
         t.setTime_t(pyCookie.expires)
         qc.setExpirationDate(t)
     # not yet handled(maybe less useful):
     #   py cookie.rest / QNetworkCookie.setHttpOnly()
     return qc
예제 #3
0
    def load_cookies(self, url, cookie_strings):
        p = urlparse(url)

        cookies = list()
        for c in cookie_strings:
            n = c.find('=')
            name, value = c[:n], c[n+1:]
            cookie = QNetworkCookie(name, value)
            cookie.setDomain(p.netloc)
            cookies.append(cookie)

        cookiejar = QNetworkCookieJar()
        cookiejar.setAllCookies(cookies)
        self.networkAccessManager().setCookieJar(cookiejar)
예제 #4
0
 def get_cookie(line):
     fields = map(str.strip, line.split("\t"))
     if len(fields) != 7:
         return
     domain, domain_flag, path, is_secure, expiration, name, value = fields
     cookie = QNetworkCookie(name, value)
     cookie.setDomain(domain)
     cookie.setPath(path)
     cookie.setSecure(str2bool(is_secure))
     cookie.setExpirationDate(QDateTime.fromTime_t(int(expiration)))
     return cookie
예제 #5
0
 def __init__(self, reply):
     # save shortcuts to URL details
     self.url = reply.url()
     self.host = self.url.host()
     self.path = self.url.path()
     self.qs = self.url.queryItems()
     self.data = reply.data
     self.content_type = reply.content_type
     self.content = common.to_unicode(str(reply.content))
     try:
         self.parsed_content = parser.parse(self.content, self.content_type)
     except ValueError as e:
         print 'Error parsing URL with lxml: {}'.format(self.url.toString())
         self.parsed_content = None
     self.columns = None
     self.cookies = QNetworkCookie.parseCookies(reply.rawHeader('Set-Cookie'))
     # map of Qt verbs
     verbs = {
         QNetworkAccessManager.HeadOperation: 'HEAD',
         QNetworkAccessManager.GetOperation: 'GET',
         QNetworkAccessManager.PutOperation: 'PUT',
         QNetworkAccessManager.PostOperation: 'POST',
         QNetworkAccessManager.DeleteOperation: 'DELETE',
         QNetworkAccessManager.CustomOperation: 'CUSTOM',
     }
     self.verb = verbs[reply.operation()]
     # save request details
     request = reply.orig_request
     self.request_headers = [(header, request.rawHeader(header)) for header in request.rawHeaderList()]
     self.response_headers = [(header, request.rawHeader(header)) for header in reply.rawHeaderList()]
예제 #6
0
 def loadCookies(self, cookies):
     """
     Public method to restore the saved cookies.
     
     @param cookies byte array containing the saved cookies (QByteArray)
     @return list of cookies
     """
     if cookies.isEmpty():
         return []
     
     cookiesList = []
     data = QByteArray(cookies)
     stream = QDataStream(data, QIODevice.ReadOnly)
     
     version = stream.readUInt16()
     if version != self.JAR_VERSION:
         return []
     
     noCookies = stream.readUInt32()
     
     rawCookie = QByteArray()
     while not stream.atEnd():
         stream >> rawCookie
         newCookies = QNetworkCookie.parseCookies(rawCookie)
         for newCookie in newCookies:
             cookiesList.append(newCookie)
     
     return cookiesList
예제 #7
0
파일: webkit.py 프로젝트: Ho1mium/xss_scan
	def __init__(self, url, app):
		global _cookieJar, cookie
		self._url = url
		self._app = app
		QWebPage.__init__(self)
		self.networkAccessManager().setCookieJar(_cookieJar)
		_cookieJar.setCookiesFromUrl(QNetworkCookie.parseCookies(_cookie), QUrl(url))
		self.bind()
예제 #8
0
파일: ghost.py 프로젝트: Awesoham/Ghost.py
 def toQtCookie(PyCookie):
     qc = QNetworkCookie( PyCookie.name, PyCookie.value )
     qc.setSecure(PyCookie.secure)
     if PyCookie.path_specified:
         qc.setPath(PyCookie.path)
     if PyCookie.domain != "" :
         qc.setDomain(PyCookie.domain)
     if PyCookie.expires != 0:
         t = QDateTime()
         t.setTime_t(PyCookie.expires)
         qc.setExpirationDate(t)
     # not yet handled(maybe less useful):
     #   py cookie.rest / QNetworkCookie.setHttpOnly()
     return qc
예제 #9
0
    def cookiesForUrl(self, url):
        settings = QSettings(self.m_cookiesFile, QSettings.IniFormat)
        cookieList = []

        settings.beginGroup(url.host())

        for cname in settings.childKeys():
            cookieList.append(QNetworkCookie(cname, settings.value(cname)))

        return cookieList
예제 #10
0
    def load(self):
        cookieFile = self._cookiesFilePath()

        if os.path.exists(cookieFile):
            if os.stat(cookieFile).st_size:
                self.setAllCookies(
                    QNetworkCookie.parseCookies(
                        QByteArray(open(cookieFile).read())))
        else:
            self.setAllCookies([])
예제 #11
0
    def _load(self):
        self.cookie = QNetworkCookie('Settings',
                                     'Player~others|Bandwidth~ALL|Codec~ALL')
        self.cookie.setDomain('.shoutcast.com')
        self.cookie.setExpirationDate(QDateTime())
        self.cookie.setPath('/')

        self.webView = QWebView(self)
        self.webPage = self.webView.page()
        self.cookiejar = QNetworkCookieJar()
        self.cookiejar.setAllCookies([self.cookie])
        self.webPage.networkAccessManager().setCookieJar(self.cookiejar)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.webView)
        self.webView.load(QUrl(HOMEURL))
        self.webPage.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.connect(self.webPage, SIGNAL('linkClicked(const QUrl&)'),
                     self._processLink)
예제 #12
0
    def import_raw_cookie_list(self, raw_cookie_list):
        cookieJar = self.get_global_cookie_jar()
        # merge cookies
        cookie_list = cookieJar.allCookies()
        for raw_cookie in raw_cookie_list:
            cookies = QNetworkCookie.parseCookies(raw_cookie)
            for cookie in cookies:
                if cookie not in cookie_list:
                    cookie_list.append(cookie)

        cookieJar.setAllCookies(cookie_list)
        self.signal_cookie_jar_updated()
	def _parse_cookie_attribs_into_QtCookies_list(self, cookie_attrs, default_domain):
		#ugly, but works around bugs in parseCookies
		cookies = []

		for cookie_attr in cookie_attrs:
			# parsing every attribute on its own because parser seems to be <censored>!
			tmp_cookie_list = QNetworkCookie.parseCookies(cookie_attr)
			if tmp_cookie_list:
				tmp_cookie = tmp_cookie_list[0]
				if not tmp_cookie.domain():
					tmp_cookie.setDomain(QString(default_domain))
				cookies.append(tmp_cookie)

		return cookies
예제 #14
0
    def load_cookies_from_file(self, filename):
        cookieList = []

        if filename.endswith('.xml.bz2'):
            source = bz2.BZ2File(filename, 'r')
        elif filename.endswith('.xml.xz'):
            source = lzma.LZMAFile(filename, 'r')
        elif filename.endswith('.xml'):
            source = open(filename, 'rb')
        else:
            raise Exception('unhandled file type: %s' % (filename))

        # http://effbot.org/zone/element-iterparse.htm#incremental-parsing
        context = etree.iterparse(source,
                                  events=('start', 'end'),
                                  huge_tree=True)
        iterator = iter(context)
        root = None

        in_cookies = False
        in_cookie = False
        while True:
            try:
                event, elem = next(iterator)
                tag = elem.tag
                if not in_cookies and 'cookies' == tag and 'start' == event:
                    in_cookies = True
                elif in_cookies and 'cookie' == tag and 'start' == event:
                    in_cookie = True
                elif in_cookie and 'raw' == tag and 'end' == event:
                    value = str(elem.text)
                    encoding = None
                    if 'encoding' in elem.attrib:
                        encoding = str(elem.attrib['encoding'])
                    if 'base64' == encoding:
                        value = base64.b64decode(value)
                    cookies = QNetworkCookie.parseCookies(value)
                    for cookie in cookies:
                        cookieList.append(cookie)
                elif 'cookies' == tag and 'end' == event:
                    in_cookies = False
                elif in_cookie and 'cookie' == tag and 'end' == event:
                    in_cookie = False

            except StopIteration:
                source.close()
                break

        return cookieList
예제 #15
0
    def __init__(self, url, app):
        global _cookieJar, cookie
        self._url = url
        self._app = app
        QWebPage.__init__(self)
        self.networkAccessManager().setCookieJar(_cookieJar)
        _cookieJar.setCookiesFromUrl(QNetworkCookie.parseCookies(_cookie), QUrl(url))
        self.bind()
        self._app.exec_()

        self.user_agent_for_url = user_agent
        self.js_alert = alert
        self.js_prompt = alert
        self.js_confirm = alert
        self.js_console_message = console_message
예제 #16
0
    def __init__(self, my_cookie_dict):
        super(QWebView, self).__init__()
        # 将字典转化成QNetworkCookieJar的格式
        self.cookie_jar = QNetworkCookieJar()
        cookies = []
        for key, values in my_cookie_dict.items():
            my_cookie = QNetworkCookie(QByteArray(key), QByteArray(values))
            #my_cookie.setDomain('.baidu.com')
            cookies.append(my_cookie)
        self.cookie_jar.setAllCookies(cookies)
        # 如果没有在前面设置domain,那么可以在这里指定一个url作为domain
        # self.cookie_jar.setCookiesFromUrl(cookies, QUrl('https://www.baidu.com/'))

        # 最后cookiejar替换完成
        self.page().networkAccessManager().setCookieJar(self.cookie_jar)
	def _createCookieJarfromInjectedResponse(self, default_domain):
		#ugly, but works around bugs in parseCookies
		cookies = []

		for cookie_header in self._getCookieHeaders():
			tmp_cookieList = QNetworkCookie.parseCookies(cookie_header)
			tmp_cookie = tmp_cookieList[0]
			if not tmp_cookie.domain():
				tmp_cookie.setDomain(QString(default_domain))

			cookies = cookies + tmp_cookieList

		cj = QNetworkCookieJar()
		cj.setAllCookies(cookies)
		return cj
예제 #18
0
파일: webkit.py 프로젝트: zil0ng/webscan
    def __init__(self, url, app):
        global _cookieJar, cookie
        self._url = url
        self._app = app
        QWebPage.__init__(self)
        self.networkAccessManager().setCookieJar(_cookieJar)
        _cookieJar.setCookiesFromUrl(QNetworkCookie.parseCookies(_cookie),
                                     QUrl(url))
        self.bind()
        self._app.exec_()

        self.user_agent_for_url = user_agent
        self.js_alert = alert
        self.js_prompt = alert
        self.js_confirm = alert
        self.js_console_message = console_message
예제 #19
0
파일: CookiesTab.py 프로젝트: Averroes/raft
    def load_cookies_from_file(self, filename):
        cookieList = []

        if filename.endswith('.xml.bz2'):
            source = bz2.BZ2File(filename, 'r')
        elif filename.endswith('.xml.xz'):
            source = lzma.LZMAFile(filename, 'r')
        elif filename.endswith('.xml'):
            source = open(filename, 'rb')
        else:
            raise Exception('unhandled file type: %s' % (filename))

        # http://effbot.org/zone/element-iterparse.htm#incremental-parsing
        context = etree.iterparse(source, events=('start', 'end'), huge_tree = True)
        iterator = iter(context)
        root = None

        in_cookies = False
        in_cookie = False
        while True:
            try:
                event, elem = next(iterator)
                tag = elem.tag
                if not in_cookies and 'cookies' == tag and 'start' == event:
                    in_cookies = True
                elif in_cookies and 'cookie' == tag and 'start' == event:
                    in_cookie = True
                elif in_cookie and 'raw' == tag and 'end' == event:
                    value = str(elem.text)
                    encoding = None
                    if 'encoding' in elem.attrib:
                        encoding = str(elem.attrib['encoding'])
                    if 'base64' == encoding:
                        value = base64.b64decode(value)
                    cookies = QNetworkCookie.parseCookies(value)
                    for cookie in cookies:
                        cookieList.append(cookie)
                elif 'cookies' == tag and 'end' == event:
                    in_cookies = False
                elif in_cookie and 'cookie' == tag and 'end' == event:
                    in_cookie = False

            except StopIteration:
                source.close()
                break

        return cookieList
예제 #20
0
    def _load(self):
        self.cookie = QNetworkCookie('Settings', 'Player~others|Bandwidth~ALL|Codec~ALL')
        self.cookie.setDomain('.shoutcast.com')
        self.cookie.setExpirationDate(QDateTime())
        self.cookie.setPath('/')

        self.webView = QWebView(self)
        self.webPage = self.webView.page()
        self.cookiejar = QNetworkCookieJar()
        self.cookiejar.setAllCookies([self.cookie])
        self.webPage.networkAccessManager().setCookieJar(self.cookiejar)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.webView)
        self.webView.load(QUrl(HOMEURL))
        self.webPage.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.connect(self.webPage, SIGNAL('linkClicked(const QUrl&)'), self._processLink)
예제 #21
0
class ShoutCastForm(PluginBase.PluginBase):
    '''Grab Shoutcast streams and save them as "bookmarks" - and play them on
       the currently selected server.

       General shoutcast information is not preserved between runs. Also, the
       shoutcast server/API is pretty lame so timeouts actually occur quite
       frequently.
    '''
    moduleName = '&Shoutcast'
    moduleIcon = "network-workgroup"

    def load(self):
        pass

    def event(self, event):
        if event.type() == QEvent.Paint:
            if not hasattr(self, 'webView'):
                self._load()
                self.event = super(ShoutCastForm, self).event
        return False

    def _load(self):
        self.cookie = QNetworkCookie('Settings', 'Player~others|Bandwidth~ALL|Codec~ALL')
        self.cookie.setDomain('.shoutcast.com')
        self.cookie.setExpirationDate(QDateTime())
        self.cookie.setPath('/')

        self.webView = QWebView(self)
        self.webPage = self.webView.page()
        self.cookiejar = QNetworkCookieJar()
        self.cookiejar.setAllCookies([self.cookie])
        self.webPage.networkAccessManager().setCookieJar(self.cookiejar)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.webView)
        self.webView.load(QUrl(HOMEURL))
        self.webPage.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.connect(self.webPage, SIGNAL('linkClicked(const QUrl&)'), self._processLink)

    def _processLink(self, url):
        if url.host() == TUNEIN:
            self._playStation(unicode(url.toString()))
        else:
            self.webView.load(url)
            self.webView.show()

    def _playStation(self, url):
        try:
            streamList = streamTools.getStreamList(url)
        except streamTools.ParseError:
            return
        if streamList:
            self.modelManager.playQueue.extend(streamList)
예제 #22
0
    def __init__(self, parent=None, allowed=None, storage=None):
        """ Load cookies from a file

        """
        super(CookieJar, self).__init__(parent)
        print "INIT CookieJar"
        if not allowed:
            self.allowed = []
        else:
            self.allowed = allowed
        if storage:
            try:
                with open(storage,"r") as readfile:
                    cookies = [QNetworkCookie.parseCookies(k)
                            for k in readfile.readlines()]
                    cookies = [x for y in cookies for x in y] # flatten
                    self.setAllCookies(cookies)
            except IOError:
                print "LOAD COOKIES: empty?"
예제 #23
0
    def __init__(self, parent=None, allowed=None, storage=None):
        """ Load cookies from a file

        """
        super(CookieJar, self).__init__(parent)
        print "INIT CookieJar"
        if not allowed:
            self.allowed = []
        else:
            self.allowed = allowed
        if storage:
            try:
                with open(storage, "r") as readfile:
                    cookies = [
                        QNetworkCookie.parseCookies(k)
                        for k in readfile.readlines()
                    ]
                    cookies = [x for y in cookies for x in y]  # flatten
                    self.setAllCookies(cookies)
            except IOError:
                print "LOAD COOKIES: empty?"
예제 #24
0
    def doUpdateSiteMap(self):
        if not self.qlock.tryLock():
            return
        try:

            if self.fillAll:
                self.fillAll = False
                self.treeViewModel.clearModel()
                self.lastId = 0

            rows = self.Data.get_sitemap_info(self.cursor, self.lastId)

            global_cookie_jar = self.framework.get_global_cookie_jar()

            count = 0
            for row in rows:
                count += 1
                if 0 == (count % 100):
                    self.yieldCurrentThread()

                rowItems = [m or '' for m in list(row)]

                Id = str(rowItems[0])
                try:
                    self.lastId = int(Id)
                except ValueError:
                    pass

                # XXX: review all for bytes usage
                if isinstance(rowItems[1], bytes):
                    url = str(rowItems[1], 'utf-8', 'ignore')
                else:
                    url = str(rowItems[1])
                status = str(rowItems[2])
                response_headers = str(rowItems[3])
                request_headers = str(rowItems[4])
                # TODO: make configurable
                if status in ('400', '404', '500', '501'):
                    continue

                # TODO:
                m = self.re_set_cookie.search(response_headers)
                if m:
                    setCookies = m.group(1)
                    cookieList = QNetworkCookie.parseCookies(setCookies)
                    global_cookie_jar.setCookiesFromUrl(
                        cookieList, QUrl.fromEncoded(url))

                parsed = urlparse.urlsplit(url)
                hostname = ''
                if not parsed.hostname:
                    m = self.re_host_name.search(request_headers)
                    if m:
                        hostname = m.group(1).rstrip()
                else:
                    hostname = parsed.hostname

                hostname = hostname.lower()
                hostloc = urlparse.urlunsplit(
                    (parsed.scheme, parsed.netloc, '/', '', ''))

                rootNode = self.treeViewModel.findOrAddNode(hostname)
                hostLocNode = rootNode.findOrAddNode(self.treeViewModel,
                                                     hostloc)
                pathval = parsed.path

                # add directories
                parentNode = hostLocNode
                parentNode.setResponseId(None, hostloc)
                lastSlash = 0
                slash = 0
                while True:
                    slash = pathval.find('/', slash + 1)
                    if slash < 0:
                        break
                    dirname = pathval[lastSlash + 1:slash + 1]
                    parentNode = parentNode.findOrAddNode(
                        self.treeViewModel, dirname)
                    parentNode.setResponseId(
                        None,
                        urlparse.urlunsplit((parsed.scheme, parsed.netloc,
                                             pathval[0:slash + 1], '', '')))
                    lastSlash = slash

                # add file element
                if lastSlash + 1 < len(pathval):
                    filename = pathval[lastSlash + 1:]
                    parentNode = parentNode.findOrAddNode(
                        self.treeViewModel, filename)
                    parentNode.setResponseId(
                        None,
                        urlparse.urlunsplit(
                            (parsed.scheme, parsed.netloc, pathval, '', '')))

                # add query
                if parsed.query:
                    parentNode = parentNode.findOrAddNode(
                        self.treeViewModel, '?' + parsed.query)

                # store the latest Id
                # TODO: should determine best candidate to display
                parentNode.setResponseId(Id, url)

        finally:
            self.qlock.unlock()
예제 #25
0
class ShoutCastForm(PluginBase.PluginBase):
    '''Grab Shoutcast streams and save them as "bookmarks" - and play them on
       the currently selected server.

       General shoutcast information is not preserved between runs. Also, the
       shoutcast server/API is pretty lame so timeouts actually occur quite
       frequently.
    '''
    moduleName = '&Shoutcast'
    moduleIcon = "network-workgroup"

    def load(self):
        pass

    def event(self, event):
        if event.type() == QEvent.Paint:
            if not hasattr(self, 'webView'):
                self._load()
                self.event = super(ShoutCastForm, self).event
        return False

    def _load(self):
        self.cookie = QNetworkCookie('Settings',
                                     'Player~others|Bandwidth~ALL|Codec~ALL')
        self.cookie.setDomain('.shoutcast.com')
        self.cookie.setExpirationDate(QDateTime())
        self.cookie.setPath('/')

        self.webView = QWebView(self)
        self.webPage = self.webView.page()
        self.cookiejar = QNetworkCookieJar()
        self.cookiejar.setAllCookies([self.cookie])
        self.webPage.networkAccessManager().setCookieJar(self.cookiejar)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.webView)
        self.webView.load(QUrl(HOMEURL))
        self.webPage.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.connect(self.webPage, SIGNAL('linkClicked(const QUrl&)'),
                     self._processLink)

    def _processLink(self, url):
        if url.host() == TUNEIN:
            self._playStation(url.toString())
        else:
            self.webView.load(url)
            self.webView.show()

    def _playStation(self, url):
        data = self._retreivePLS(url)
        adrlist = self._parsePLS(data)
        self.mpdclient.send('command_list_ok_begin')
        try:
            for address in adrlist:
                self.mpdclient.send('add', (address, ))
        finally:
            self.mpdclient.send('command_list_end')

    def _retreivePLS(self, url):
        conn = httplib.HTTPConnection(TUNEIN)
        conn.request("GET", TUNEINFORMAT % url.split('=')[-1])
        resp = conn.getresponse()
        if resp.status == 200:
            return resp.read().split('\n')
        else:
            raise httplib.HTTPException('Got bad status code.')

    def _parsePLS(self, data):
        adrlist = []
        state = ''
        while data:
            line = data.pop(0)
            if state == '' and line == '[playlist]':
                state = 'playlist'
            elif state == 'playlist':
                if '=' in line:
                    key, value = line.split('=', 1)
                    if key.startswith('File'):
                        adrlist.append(value)
            else:
                raise httplib.HTTPException(
                    'Encountered error during parsing of the playlist.')
        return adrlist
예제 #26
0
    def doUpdateSiteMap(self):
        if not self.qlock.tryLock():
            return
        try:

            if self.fillAll:
                self.fillAll = False
                self.treeViewModel.clearModel()
                self.lastId = 0

            rows = self.Data.get_sitemap_info(self.cursor, self.lastId)

            global_cookie_jar = self.framework.get_global_cookie_jar()

            count = 0
            for row in rows:
                count += 1
                if 0 == (count % 100):
                    self.yieldCurrentThread()

                rowItems = [m or "" for m in list(row)]

                Id = str(rowItems[0])
                try:
                    self.lastId = int(Id)
                except ValueError:
                    pass

                # XXX: review all for bytes usage
                if isinstance(rowItems[1], bytes):
                    url = str(rowItems[1], "utf-8", "ignore")
                else:
                    url = str(rowItems[1])
                status = str(rowItems[2])
                response_headers = str(rowItems[3])
                request_headers = str(rowItems[4])
                # TODO: make configurable
                if status in ("400", "404", "500", "501"):
                    continue

                # TODO:
                m = self.re_set_cookie.search(response_headers)
                if m:
                    setCookies = m.group(1)
                    cookieList = QNetworkCookie.parseCookies(setCookies)
                    global_cookie_jar.setCookiesFromUrl(cookieList, QUrl.fromEncoded(url))

                parsed = urlparse.urlsplit(url)
                hostname = ""
                if not parsed.hostname:
                    m = self.re_host_name.search(request_headers)
                    if m:
                        hostname = m.group(1).rstrip()
                else:
                    hostname = parsed.hostname

                hostname = hostname.lower()
                hostloc = urlparse.urlunsplit((parsed.scheme, parsed.netloc, "/", "", ""))

                rootNode = self.treeViewModel.findOrAddNode(hostname)
                hostLocNode = rootNode.findOrAddNode(self.treeViewModel, hostloc)
                pathval = parsed.path

                # add directories
                parentNode = hostLocNode
                parentNode.setResponseId(None, hostloc)
                lastSlash = 0
                slash = 0
                while True:
                    slash = pathval.find("/", slash + 1)
                    if slash < 0:
                        break
                    dirname = pathval[lastSlash + 1 : slash + 1]
                    parentNode = parentNode.findOrAddNode(self.treeViewModel, dirname)
                    parentNode.setResponseId(
                        None, urlparse.urlunsplit((parsed.scheme, parsed.netloc, pathval[0 : slash + 1], "", ""))
                    )
                    lastSlash = slash

                # add file element
                if lastSlash + 1 < len(pathval):
                    filename = pathval[lastSlash + 1 :]
                    parentNode = parentNode.findOrAddNode(self.treeViewModel, filename)
                    parentNode.setResponseId(None, urlparse.urlunsplit((parsed.scheme, parsed.netloc, pathval, "", "")))

                # add query
                if parsed.query:
                    parentNode = parentNode.findOrAddNode(self.treeViewModel, "?" + parsed.query)

                # store the latest Id
                # TODO: should determine best candidate to display
                parentNode.setResponseId(Id, url)

        finally:
            self.qlock.unlock()
예제 #27
0
파일: cookies.py 프로젝트: 0xmilk/splash
    def har_cookie2qt(cls, cookie):
        qcookie = QNetworkCookie()
        qcookie.setName(cookie["name"])
        qcookie.setValue(cookie["value"])

        if 'domain' in cookie:
            qcookie.setDomain(cookie["domain"])

        if 'httpOnly' in cookie:
            qcookie.setHttpOnly(cookie["httpOnly"])

        if 'secure' in cookie:
            qcookie.setSecure(cookie["secure"])

        if 'path' in cookie:
            qcookie.setPath(cookie["path"])

        if cookie.get('expires'):
            expires = QDateTime.fromString(cookie["expires"], Qt.ISODate)
            qcookie.setExpirationDate(expires)

        return qcookie
예제 #28
0
파일: browser.py 프로젝트: pombredanne/wkit
    def request(self, url=None, user_agent=None, cookies=None,
                timeout=DEFAULT_PAGE_LOAD_TIMEOUT,
                referer=None, method='get', data=None,
                headers=None, proxy=None, wait=True):
        # Reset things bound to previous response
        self._response = None
        self.resource_list = []
        self._page_loaded = False
        #self.view.setHtml('', QUrl('blank://'))

        # Proxy
        if proxy:
            self.manager.setup_proxy(proxy)

        # User-Agent
        if user_agent is None:
            user_agent = DEFAULT_USER_AGENT
        self.page.set_user_agent(user_agent)

        # Cookies
        if cookies is None:
            cookies = {}
        cookie_obj_list = []
        for name, value in cookies.items():
            domain = ('.' + urlsplit(url).netloc).split(':')[0]
            #print 'CREATE COOKIE %s=%s' % (name, value)
            #print 'DOMAIN = %s' % domain
            cookie_obj = QNetworkCookie(name, value)
            cookie_obj.setDomain(domain)
            cookie_obj_list.append(cookie_obj)
        #self.cookie_jar.setAllCookies(cookie_obj_list)

        # HTTP Method
        method_obj = getattr(QNetworkAccessManager, '%sOperation'
                             % method.capitalize())
        # Ensure that Content-Type is correct if method is post
        if method == 'post':
            headers['Content-Type'] = 'application/x-www-form-urlencoded'

        # POST Data
        if data is None:
            data = QByteArray()

        # Build Request object
        req = QNetworkRequest(QUrl(url))

        # Referer
        if referer:
            req.setRawHeader('Referer', referer)

        # Headers
        if headers is None:
            headers = {}
        for name, value in headers.items():
            req.setRawHeader(name, value)
        self.content_type_stats = Counter()
        
        # Spawn request
        self.view.load(req, method_obj, data)

        if wait:
            self.wait_for_page_loaded(timeout=timeout)
            return self.get_page_response()
        else:
            return None
예제 #29
0
class ShoutCastForm(PluginBase.PluginBase):
    '''Grab Shoutcast streams and save them as "bookmarks" - and play them on
       the currently selected server.

       General shoutcast information is not preserved between runs. Also, the
       shoutcast server/API is pretty lame so timeouts actually occur quite
       frequently.
    '''
    moduleName = '&Shoutcast'
    moduleIcon = "network-workgroup"

    def load(self):
        pass

    def event(self, event):
        if event.type() == QEvent.Paint:
            if not hasattr(self, 'webView'):
                self._load()
                self.event = super(ShoutCastForm, self).event
        return False

    def _load(self):
        self.cookie = QNetworkCookie('Settings', 'Player~others|Bandwidth~ALL|Codec~ALL')
        self.cookie.setDomain('.shoutcast.com')
        self.cookie.setExpirationDate(QDateTime())
        self.cookie.setPath('/')

        self.webView = QWebView(self)
        self.webPage = self.webView.page()
        self.cookiejar = QNetworkCookieJar()
        self.cookiejar.setAllCookies([self.cookie])
        self.webPage.networkAccessManager().setCookieJar(self.cookiejar)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.webView)
        self.webView.load(QUrl(HOMEURL))
        self.webPage.setLinkDelegationPolicy(QWebPage.DelegateExternalLinks)
        self.connect(self.webPage, SIGNAL('linkClicked(const QUrl&)'), self._processLink)

    def _processLink(self, url):
        if url.host() == TUNEIN:
            self._playStation(url.toString())
        else:
            self.webView.load(url)
            self.webView.show()

    def _playStation(self, url):
        data = self._retreivePLS(url)
        adrlist = self._parsePLS(data)
        self.mpdclient.send('command_list_ok_begin')
        try:
            for address in adrlist:
                self.mpdclient.send('add', (address,))
        finally:
            self.mpdclient.send('command_list_end')

    def _retreivePLS(self, url):
        conn = httplib.HTTPConnection(TUNEIN)
        conn.request("GET", TUNEINFORMAT % url.split('=')[-1])
        resp = conn.getresponse()
        if resp.status == 200:
            return resp.read().split('\n')
        else:
            raise httplib.HTTPException('Got bad status code.')

    def _parsePLS(self, data):
        adrlist = []
        state = ''
        while data:
            line = data.pop(0)
            if state == '' and line == '[playlist]':
                state = 'playlist'
            elif state == 'playlist':
                if '=' in line:
                    key, value = line.split('=', 1)
                    if key.startswith('File'):
                        adrlist.append(value)
            else:
                raise httplib.HTTPException('Encountered error during parsing of the playlist.')
        return adrlist
예제 #30
0
파일: CookiesTab.py 프로젝트: Averroes/raft
    def handle_cookiesSave_clicked(self):
        cookieJar = self.framework.get_global_cookie_jar()
        cookieList = cookieJar.allCookies()
        domain = str(self.mainWindow.cookiesCookieJarDomainEdit.text())
        name = str(self.mainWindow.cookiesCookieJarNameEdit.text())
        value = str(self.mainWindow.cookiesCookieJarValueEdit.text())
        if not (domain and name and value):
            return
        cookie = self.find_cookie_by_domain_and_name(cookieList, domain, name)
        if cookie is None:
            # new
            cookie = QNetworkCookie(name, value)
            index = -1
        else:
            index = cookieList.index(cookie)

        cookie.setName(name)
        cookie.setDomain(domain)
        cookie.setValue(value)
        cookie.setPath(str(self.mainWindow.cookiesCookieJarPathEdit.text()))
        if self.mainWindow.cookiesCookieJarSessionCookieCheckbox.isChecked():
            cookie.setExpirationDate(QDateTime())
        else:
            cookie.setExpirationDate(self.mainWindow.cookiesCookieJarExpiryEdit.dateTime())
        cookie.setSecure(self.mainWindow.cookiesCookieJarSecureCheckbox.isChecked())
        cookie.setHttpOnly(self.mainWindow.cookiesCookieJarHttpOnlyCheckbox.isChecked())

        if -1 == index:
            cookieList.append(cookie)
        else:
            cookieList[index] = cookie

        cookieJar.setAllCookies(cookieList)
        self.populate_cookie_jar_tree()
예제 #31
0
    def har_cookie2qt(cls, cookie):
        qcookie = QNetworkCookie()
        qcookie.setName(cookie["name"])
        qcookie.setValue(cookie["value"])

        if 'domain' in cookie:
            qcookie.setDomain(cookie["domain"])

        if 'httpOnly' in cookie:
            qcookie.setHttpOnly(cookie["httpOnly"])

        if 'secure' in cookie:
            qcookie.setSecure(cookie["secure"])

        if 'path' in cookie:
            qcookie.setPath(cookie["path"])

        if cookie.get('expires'):
            expires = QDateTime.fromString(cookie["expires"], Qt.ISODate)
            qcookie.setExpirationDate(expires)

        return qcookie
예제 #32
0
    def request(self,
                url,
                user_agent='Mozilla',
                cookies={},
                timeout=15,
                method='get',
                data=None,
                headers={}):
        url_info = urlsplit(url)

        self.resource_list = []
        loop = QEventLoop()
        self.view.loadFinished.connect(loop.quit)

        # Timeout
        timer = QTimer()
        timer.setSingleShot(True)
        timer.timeout.connect(loop.quit)
        timer.start(timeout * 1000)

        # User-Agent
        self.page.user_agent = user_agent

        # Cookies
        cookie_obj_list = []
        for name, value in cookies.items():
            domain = ('.' + url_info.netloc).split(':')[0]
            #print 'CREATE COOKIE %s=%s' % (name, value)
            #print 'DOMAIN = %s' % domain
            cookie_obj = QNetworkCookie(name, value)
            cookie_obj.setDomain(domain)
            cookie_obj_list.append(cookie_obj)
        self.cookie_jar.setAllCookies(cookie_obj_list)

        # Method
        method_obj = getattr(QNetworkAccessManager,
                             '%sOperation' % method.capitalize())

        # Ensure that Content-Type is correct if method is post
        if method == 'post':
            headers['Content-Type'] = 'application/x-www-form-urlencoded'

        # Post data
        if data is None:
            data = QByteArray()

        # Request object
        request_obj = QNetworkRequest(QUrl(url))

        # Headers
        for name, value in headers.items():
            request_obj.setRawHeader(name, value)

        # Make a request
        self.view.load(request_obj, method_obj, data)

        loop.exec_()

        if timer.isActive():
            request_resource = None
            url = str(self.page.mainFrame().url().toString()).rstrip('/')
            for res in self.resource_list:
                if url == res.url or url == res.url.rstrip('/'):
                    request_resource = res
                    break
            if request_resource:
                return self.build_response(request_resource)
            else:
                raise KitError('Request was successfull but it is not possible '\
                               'to associate the request to one of received responses')
        else:
            raise KitError('Timeout while loading %s' % url)
예제 #33
0
파일: __init__.py 프로젝트: Scaurus/grab
    def request(self, url, user_agent='Mozilla', cookies=None, timeout=15,
                method='get', data=None, headers=None):
        if cookies is None:
            cookies = {}
        if headers is None:
            headers = {}
        url_info = urlsplit(url)

        self.resource_list = []
        loop = QEventLoop()
        self.view.loadFinished.connect(loop.quit)

        # Timeout
        timer = QTimer()
        timer.setSingleShot(True)
        timer.timeout.connect(loop.quit)
        timer.start(timeout * 1000)

        # User-Agent
        self.page.user_agent = user_agent

        # Cookies
        cookie_obj_list = []
        for name, value in cookies.items():
            domain = ('.' + url_info.netloc).split(':')[0]
            #print 'CREATE COOKIE %s=%s' % (name, value)
            #print 'DOMAIN = %s' % domain
            cookie_obj = QNetworkCookie(name, value)
            cookie_obj.setDomain(domain)
            cookie_obj_list.append(cookie_obj)
        self.cookie_jar.setAllCookies(cookie_obj_list)

        # Method
        method_obj = getattr(QNetworkAccessManager, '%sOperation'
                             % method.capitalize())

        # Ensure that Content-Type is correct if method is post
        if method == 'post':
            headers['Content-Type'] = 'application/x-www-form-urlencoded'

        # Post data
        if data is None:
            data = QByteArray()

        # Request object
        request_obj = QNetworkRequest(QUrl(url))

        # Headers
        for name, value in headers.items():
            request_obj.setRawHeader(name, value)

        # Make a request
        self.view.load(request_obj, method_obj, data)

        loop.exec_()

        if timer.isActive():
            request_resource = None
            url = str(self.page.mainFrame().url().toString()).rstrip('/')
            for res in self.resource_list:
                if url == res.url or url == res.url.rstrip('/'):
                    request_resource = res
                    break
            if request_resource:
                return self.build_response(request_resource)
            else:
                raise KitError('Request was successful but it is not possible'
                               ' to associate the request to one of received'
                               ' responses')
        else:
            raise KitError('Timeout while loading %s' % url)
예제 #34
0
    def handle_cookiesSave_clicked(self):
        cookieJar = self.framework.get_global_cookie_jar()
        cookieList = cookieJar.allCookies()
        domain = str(self.mainWindow.cookiesCookieJarDomainEdit.text())
        name = str(self.mainWindow.cookiesCookieJarNameEdit.text())
        value = str(self.mainWindow.cookiesCookieJarValueEdit.text())
        if not (domain and name and value):
            return
        cookie = self.find_cookie_by_domain_and_name(cookieList, domain, name)
        if cookie is None:
            # new
            cookie = QNetworkCookie(name, value)
            index = -1
        else:
            index = cookieList.index(cookie)

        cookie.setName(name)
        cookie.setDomain(domain)
        cookie.setValue(value)
        cookie.setPath(str(self.mainWindow.cookiesCookieJarPathEdit.text()))
        if self.mainWindow.cookiesCookieJarSessionCookieCheckbox.isChecked():
            cookie.setExpirationDate(QDateTime())
        else:
            cookie.setExpirationDate(
                self.mainWindow.cookiesCookieJarExpiryEdit.dateTime())
        cookie.setSecure(
            self.mainWindow.cookiesCookieJarSecureCheckbox.isChecked())
        cookie.setHttpOnly(
            self.mainWindow.cookiesCookieJarHttpOnlyCheckbox.isChecked())

        if -1 == index:
            cookieList.append(cookie)
        else:
            cookieList[index] = cookie

        cookieJar.setAllCookies(cookieList)
        self.populate_cookie_jar_tree()
예제 #35
0
    def load(self):
      try:
        ds = pickle.load(open(self.m_cookiesFile, 'rb'))
      except:
        ds = []
      qs = []
      t = int(time.time())
      for d in ds:
        if d['expirationDate'] > t and not d['isSessionCookie']:
          c = QNetworkCookie(d['name'],d['value'])
          c.setDomain(d['domain'])
          c.setExpirationDate(QDateTime.fromTime_t(d['expirationDate']))
          c.setHttpOnly(d['isHttpOnly'])
          c.setSecure(d['isSecure'])
          c.setPath(d['path'])
          qs.append(c)

      #for c in cs
      self.setAllCookies(qs)
예제 #36
0
 def line2qcookie(line):
         domain,initial_dot,path,isSecure,expires,name,value=line.split()
         isSecure=(isSecure=="TRUE")
         dt=QDateTime()
         dt.setTime_t(int(expires))
         expires=dt
         c=QNetworkCookie()
         c.setDomain(domain)
         c.setPath(path)
         c.setSecure(isSecure)
         c.setExpirationDate(expires)
         c.setName(name)
         c.setValue(value)
         return c