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
|
2011-06-09 00:27:38 -07:00
|
|
|
from PyQt4.QtCore import pyqtSignal, QDateTime
|
2011-04-13 04:07:53 -07:00
|
|
|
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkDiskCache, \
|
|
|
|
|
QNetworkRequest
|
2011-04-12 01:38:19 -07:00
|
|
|
|
2011-05-06 23:46:51 -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):
|
2011-06-08 03:13:18 -07:00
|
|
|
resourceReceived = pyqtSignal('QVariantMap')
|
2011-06-07 02:31:33 -07:00
|
|
|
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-06-08 14:33:41 -07:00
|
|
|
|
2011-05-03 16:49:34 -07:00
|
|
|
self.m_ignoreSslErrors = ignoreSslErrors
|
2011-06-08 14:33:41 -07:00
|
|
|
self.m_idCounter = 0
|
|
|
|
|
self.m_ids = {}
|
|
|
|
|
self.m_started = []
|
2011-05-03 16:49:34 -07:00
|
|
|
|
2011-06-08 03:13:18 -07:00
|
|
|
self.finished.connect(self.handleFinished)
|
2011-04-12 01:38:19 -07:00
|
|
|
|
2011-06-08 03:07:46 -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)
|
|
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('NetworkAccessManagerInit', Bunch(locals()))
|
2011-05-04 02:50:16 -07:00
|
|
|
|
2011-04-12 01:38:19 -07:00
|
|
|
def createRequest(self, op, req, outgoingData):
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('NetworkAccessManagerCreateRequestPre', Bunch(locals()))
|
2011-05-04 10:47:28 -07:00
|
|
|
|
2011-05-03 16:49:34 -07:00
|
|
|
reply = QNetworkAccessManager.createRequest(self, op, req, outgoingData)
|
|
|
|
|
|
2011-06-08 03:07:46 -07:00
|
|
|
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)
|
|
|
|
|
|
2011-06-08 14:33:41 -07:00
|
|
|
self.m_idCounter += 1
|
|
|
|
|
self.m_ids[reply] = self.m_idCounter
|
|
|
|
|
|
2011-06-07 02:31:33 -07:00
|
|
|
data = {
|
2011-06-08 14:33:41 -07:00
|
|
|
'id': self.m_idCounter,
|
2011-06-07 02:31:33 -07:00
|
|
|
'url': req.url().toString(),
|
2011-06-07 02:23:48 -07:00
|
|
|
'method': toString(op),
|
2011-06-09 00:27:38 -07:00
|
|
|
'headers': headers,
|
|
|
|
|
'time': QDateTime.currentDateTime()
|
2011-06-07 02:31:33 -07:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 03:13:18 -07:00
|
|
|
reply.readyRead.connect(self.handleStarted)
|
|
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('NetworkAccessManagerCreateRequestPost', Bunch(locals()))
|
2011-05-04 02:50:16 -07:00
|
|
|
|
2011-06-07 02:31:33 -07:00
|
|
|
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):
|
2011-06-08 03:13:18 -07:00
|
|
|
headers = []
|
|
|
|
|
for header in reply.rawHeaderList():
|
|
|
|
|
header = {
|
|
|
|
|
'name': str(header),
|
|
|
|
|
'value': str(reply.rawHeader(header))
|
|
|
|
|
}
|
|
|
|
|
headers.append(header)
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'stage': 'end',
|
2011-06-08 14:33:41 -07:00
|
|
|
'id': self.m_ids[reply],
|
2011-06-08 03:13:18 -07:00
|
|
|
'url': reply.url().toString(),
|
|
|
|
|
'status': reply.attribute(QNetworkRequest.HttpStatusCodeAttribute),
|
2011-06-09 00:26:35 -07:00
|
|
|
'statusText': reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute),
|
|
|
|
|
'contentType': reply.header(QNetworkRequest.ContentTypeHeader),
|
|
|
|
|
'redirectURL': reply.header(QNetworkRequest.LocationHeader),
|
2011-06-09 00:27:38 -07:00
|
|
|
'headers': headers,
|
|
|
|
|
'time': QDateTime.currentDateTime()
|
2011-06-08 03:13:18 -07:00
|
|
|
}
|
2011-04-12 01:38:19 -07:00
|
|
|
|
2011-06-08 14:33:41 -07:00
|
|
|
del self.m_ids[reply]
|
|
|
|
|
if reply in self.m_started:
|
|
|
|
|
del self.m_started[self.m_started.index(reply)]
|
|
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('NetworkAccessManagerHandleFinished', Bunch(locals()))
|
2011-05-04 02:50:16 -07:00
|
|
|
|
2011-06-08 03:13:18 -07:00
|
|
|
self.resourceReceived.emit(data)
|
|
|
|
|
|
|
|
|
|
def handleStarted(self):
|
|
|
|
|
reply = self.sender()
|
|
|
|
|
if not reply:
|
|
|
|
|
return
|
2011-06-08 14:33:41 -07:00
|
|
|
if reply in self.m_started:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.m_started.append(reply)
|
2011-06-08 03:13:18 -07:00
|
|
|
|
|
|
|
|
headers = []
|
|
|
|
|
for header in reply.rawHeaderList():
|
|
|
|
|
header = {
|
|
|
|
|
'name': str(header),
|
|
|
|
|
'value': str(reply.rawHeader(header))
|
|
|
|
|
}
|
|
|
|
|
headers.append(header)
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'stage': 'start',
|
2011-06-08 14:33:41 -07:00
|
|
|
'id': self.m_ids[reply],
|
2011-06-08 03:13:18 -07:00
|
|
|
'url': reply.url().toString(),
|
|
|
|
|
'status': reply.attribute(QNetworkRequest.HttpStatusCodeAttribute),
|
2011-06-09 00:26:35 -07:00
|
|
|
'statusText': reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute),
|
|
|
|
|
'contentType': reply.header(QNetworkRequest.ContentTypeHeader),
|
|
|
|
|
'bodySize': reply.size(),
|
|
|
|
|
'redirectURL': reply.header(QNetworkRequest.LocationHeader),
|
2011-06-09 00:27:38 -07:00
|
|
|
'headers': headers,
|
|
|
|
|
'time': QDateTime.currentDateTime()
|
2011-06-08 03:13:18 -07:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 04:57:46 -07:00
|
|
|
do_action('NetworkAccessManagerHandleStarted', Bunch(locals()))
|
|
|
|
|
|
2011-06-08 03:13:18 -07:00
|
|
|
self.resourceReceived.emit(data)
|
2011-05-04 02:50:16 -07:00
|
|
|
|
2011-05-06 23:46:51 -07:00
|
|
|
do_action('NetworkAccessManager', Bunch(locals()))
|
2011-06-07 02:31:33 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|