E-book viewer: Use a custom 404 response for files not found in the book instead of letting Qt handle it. Shaves a few milliseconds of load time

This commit is contained in:
Kovid Goyal 2017-02-18 15:19:53 +05:30
parent 52f04cb714
commit f19d5a51ae

View File

@ -63,6 +63,35 @@ class NetworkReply(QNetworkReply):
self.finished.emit() self.finished.emit()
class NotFound(QNetworkReply):
def __init__(self, parent, request):
QNetworkReply.__init__(self, parent)
self.setOpenMode(QNetworkReply.ReadOnly | QNetworkReply.Unbuffered)
self.setHeader(QNetworkRequest.ContentTypeHeader, 'application/octet-stream')
self.setHeader(QNetworkRequest.ContentLengthHeader, 0)
self.setRequest(request)
self.setUrl(request.url())
QTimer.singleShot(0, self.finalize_reply)
def bytesAvailable(self):
return 0
def isSequential(self):
return True
def abort(self):
pass
def readData(self, maxlen):
return b''
def finalize_reply(self):
self.setAttribute(QNetworkRequest.HttpStatusCodeAttribute, 404)
self.setAttribute(QNetworkRequest.HttpReasonPhraseAttribute, "Not Found")
self.finished.emit()
def normpath(p): def normpath(p):
return os.path.normcase(os.path.abspath(p)) return os.path.normcase(os.path.abspath(p))
@ -151,6 +180,7 @@ class NetworkAccessManager(QNetworkAccessManager):
except Exception: except Exception:
import traceback import traceback
self.load_error.emit(name, traceback.format_exc()) self.load_error.emit(name, traceback.format_exc())
if DEBUG: if DEBUG:
prints('URL not found in book: %r' % qurl.toString()) prints('URL not found in book: %r' % qurl.toString())
return NotFound(self, request)
return QNetworkAccessManager.createRequest(self, operation, request) return QNetworkAccessManager.createRequest(self, operation, request)