Files

164 lines
5.3 KiB
Python
Raw Permalink Normal View History

2011-04-12 01:38:19 -07:00
'''
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/>.
2011-04-12 19:36:25 -07:00
'''
2011-04-12 01:38:19 -07:00
2011-04-12 19:36:25 -07:00
from PyQt4.QtGui import QDesktopServices
from PyQt4.QtCore import pyqtSignal, QDateTime
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkDiskCache, \
QNetworkRequest
2011-04-12 01:38:19 -07:00
from plugincontroller import Bunch, do_action
2011-05-31 16:23:06 -07:00
2011-04-12 01:38:19 -07:00
class NetworkAccessManager(QNetworkAccessManager):
resourceReceived = pyqtSignal('QVariantMap')
resourceRequested = pyqtSignal('QVariantMap')
2011-05-03 16:49:34 -07:00
def __init__(self, diskCacheEnabled, ignoreSslErrors, parent=None):
2011-04-12 01:38:19 -07:00
QNetworkAccessManager.__init__(self, parent)
2011-05-03 16:49:34 -07:00
self.m_ignoreSslErrors = ignoreSslErrors
self.m_idCounter = 0
self.m_ids = {}
self.m_started = []
2011-05-03 16:49:34 -07:00
self.finished.connect(self.handleFinished)
2011-04-12 01:38:19 -07:00
if diskCacheEnabled:
2011-04-12 19:36:25 -07:00
m_networkDiskCache = QNetworkDiskCache()
m_networkDiskCache.setCacheDirectory(QDesktopServices.storageLocation(QDesktopServices.CacheLocation))
self.setCache(m_networkDiskCache)
do_action('NetworkAccessManagerInit', Bunch(locals()))
2011-04-12 01:38:19 -07:00
def createRequest(self, op, req, outgoingData):
do_action('NetworkAccessManagerCreateRequestPre', Bunch(locals()))
2011-05-03 16:49:34 -07:00
reply = QNetworkAccessManager.createRequest(self, op, req, outgoingData)
if self.m_ignoreSslErrors:
2011-05-03 16:49:34 -07:00
reply.ignoreSslErrors()
2011-06-07 02:23:48 -07:00
headers = []
for header in req.rawHeaderList():
header = {
'name': str(header),
'value': str(req.rawHeader(header))
}
headers.append(header)
self.m_idCounter += 1
self.m_ids[reply] = self.m_idCounter
data = {
'id': self.m_idCounter,
'url': req.url().toString(),
2011-06-07 02:23:48 -07:00
'method': toString(op),
'headers': headers,
'time': QDateTime.currentDateTime()
}
reply.readyRead.connect(self.handleStarted)
do_action('NetworkAccessManagerCreateRequestPost', Bunch(locals()))
self.resourceRequested.emit(data)
2011-05-03 16:49:34 -07:00
return reply
2011-04-12 01:38:19 -07:00
def handleFinished(self, reply):
headers = []
for header in reply.rawHeaderList():
header = {
'name': str(header),
'value': str(reply.rawHeader(header))
}
headers.append(header)
data = {
'stage': 'end',
'id': self.m_ids[reply],
'url': reply.url().toString(),
'status': reply.attribute(QNetworkRequest.HttpStatusCodeAttribute),
'statusText': reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute),
'contentType': reply.header(QNetworkRequest.ContentTypeHeader),
'redirectURL': reply.header(QNetworkRequest.LocationHeader),
'headers': headers,
'time': QDateTime.currentDateTime()
}
2011-04-12 01:38:19 -07:00
del self.m_ids[reply]
if reply in self.m_started:
del self.m_started[self.m_started.index(reply)]
do_action('NetworkAccessManagerHandleFinished', Bunch(locals()))
self.resourceReceived.emit(data)
def handleStarted(self):
reply = self.sender()
if not reply:
return
if reply in self.m_started:
return
self.m_started.append(reply)
headers = []
for header in reply.rawHeaderList():
header = {
'name': str(header),
'value': str(reply.rawHeader(header))
}
headers.append(header)
data = {
'stage': 'start',
'id': self.m_ids[reply],
'url': reply.url().toString(),
'status': reply.attribute(QNetworkRequest.HttpStatusCodeAttribute),
'statusText': reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute),
'contentType': reply.header(QNetworkRequest.ContentTypeHeader),
'bodySize': reply.size(),
'redirectURL': reply.header(QNetworkRequest.LocationHeader),
'headers': headers,
'time': QDateTime.currentDateTime()
}
2011-06-08 04:57:46 -07:00
do_action('NetworkAccessManagerHandleStarted', Bunch(locals()))
self.resourceReceived.emit(data)
do_action('NetworkAccessManager', Bunch(locals()))
def toString(op):
verb = '?'
if op == QNetworkAccessManager.HeadOperation:
verb = 'HEAD'
elif op == QNetworkAccessManager.GetOperation:
verb = 'GET'
elif op == QNetworkAccessManager.PutOperation:
verb = 'PUT'
elif op == QNetworkAccessManager.PostOperation:
verb = 'POST'
elif op == QNetworkAccessManager.DeleteOperation:
verb = 'DELETE'
return verb