Files
phantomjs/python/networkaccessmanager.py
T
IceArmy e3e82648dd Documented run function(s) and passed args in plugincontroller
Reorganize loadPlugins to run any named function with any set of arguments. all dict arguments will be automatically converted to bunches

Put same func hooks into 1 hook that calls different methods (pre and post)
2011-05-04 23:11:18 -07:00

88 lines
3.3 KiB
Python

'''
This file is part of the PyPhantomJS project.
Copyright (C) 2011 James Roe <roejames12@hotmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from PyQt4.QtGui import QDesktopServices
from PyQt4.QtCore import qDebug, qWarning
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkDiskCache, \
QNetworkRequest
class NetworkAccessManager(QNetworkAccessManager):
def __init__(self, diskCacheEnabled, ignoreSslErrors, parent=None):
QNetworkAccessManager.__init__(self, parent)
self.m_ignoreSslErrors = ignoreSslErrors
if parent.m_verbose:
self.finished.connect(self.handleFinished)
if diskCacheEnabled == 'yes':
m_networkDiskCache = QNetworkDiskCache()
m_networkDiskCache.setCacheDirectory(QDesktopServices.storageLocation(QDesktopServices.CacheLocation))
self.setCache(m_networkDiskCache)
# load plugins
loadPlugins(HookNetworkAccessManagerInit, 'run', globals(), locals())
def createRequest(self, op, req, outgoingData):
if op == QNetworkAccessManager.GetOperation:
qDebug('HTTP/1.1 GET Request')
elif op == QNetworkAccessManager.PostOperation:
qDebug('HTTP/1.1 POST Request')
elif op == QNetworkAccessManager.HeadOperation:
qDebug('HTTP/1.1 HEAD Request')
elif op == QNetworkAccessManager.PutOperation:
qDebug('HTTP/1.1 PUT Request')
elif op == QNetworkAccessManager.DeleteOperation:
qDebug('HTTP/1.1 DELETE Request')
elif op == QNetworkAccessManager.CustomOperation:
qDebug('HTTP/1.1 CUSTOM Request')
else:
qWarning('Unexpected HTTP Operation Type')
qDebug('URL %s' % req.url().toString())
# load plugins
loadPlugins(HookNetworkAccessManagerCreateRequest, 'run_pre', globals(), locals())
reply = QNetworkAccessManager.createRequest(self, op, req, outgoingData)
if self.m_ignoreSslErrors == 'yes':
reply.ignoreSslErrors()
# load plugins
loadPlugins(HookNetworkAccessManagerCreateRequest, 'run_post', globals(), locals())
return reply
def handleFinished(self, reply):
qDebug('HTTP/1.1 Response')
qDebug('URL %s' % reply.url().toString())
code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
if code:
qDebug('Status code: %d' % code)
# load plugins
loadPlugins(HookNetworkAccessManagerHandleFinished, 'run', globals(), locals())
headerPairs = reply.rawHeaderPairs()
for pair in headerPairs:
qDebug('"%s" = "%s"' % (pair[0], pair[1]))
# load plugins
loadPlugins(HookNetworkAccessManager, 'run', globals(), locals())