mirror of
https://github.com/YACReader/yacreader
synced 2025-07-21 22:44:56 -04:00
Modificaci?n del SERVIDOR que mejora la estabilidad del sistema.
Se ha eliminado el soporte del servidor para conexiones persistentes. A?adido soporte para buscar aleatoriamente un puerto v?lido, si el seleccionado por el usuario no esta disponible
This commit is contained in:
@ -129,6 +129,7 @@ void HttpConnectionHandler::read() {
|
||||
readTimer.stop();
|
||||
qDebug("HttpConnectionHandler (%p): received request",this);
|
||||
HttpResponse response(&socket);
|
||||
//response.setHeader("Connection","close"); No funciona bien con NSURLConnection
|
||||
try {
|
||||
requestHandler->service(*currentRequest, response);
|
||||
}
|
||||
@ -140,15 +141,18 @@ void HttpConnectionHandler::read() {
|
||||
if (!response.hasSentLastPart()) {
|
||||
response.write(QByteArray(),true);
|
||||
}
|
||||
|
||||
socket.disconnectFromHost(); //CAMBIADO s<>lo se van a soportar conexiones NO persistentes
|
||||
|
||||
// Close the connection after delivering the response, if requested
|
||||
if (QString::compare(currentRequest->getHeader("Connection"),"close",Qt::CaseInsensitive)==0) {
|
||||
socket.disconnectFromHost();
|
||||
}
|
||||
else {
|
||||
// Start timer for next request
|
||||
int readTimeout=settings->value("readTimeout",10000).toInt();
|
||||
readTimer.start(readTimeout);
|
||||
}
|
||||
//if (QString::compare(currentRequest->getHeader("Connection"),"close",Qt::CaseInsensitive)==0) {
|
||||
// socket.disconnectFromHost();
|
||||
//}
|
||||
//else {
|
||||
// // Start timer for next request
|
||||
// int readTimeout=settings->value("readTimeout",10000).toInt();
|
||||
// readTimer.start(readTimeout);
|
||||
//}
|
||||
// Prepare for next request
|
||||
delete currentRequest;
|
||||
currentRequest=0;
|
||||
|
@ -16,9 +16,16 @@ HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandl
|
||||
// Start listening
|
||||
int port=settings->value("port").toInt();
|
||||
listen(QHostAddress::Any, port);
|
||||
if (!isListening()) {
|
||||
qCritical("HttpListener: Cannot bind on port %i: %s",port,qPrintable(errorString()));
|
||||
//Cambiado
|
||||
int i = 0;
|
||||
while (!isListening() && i < 1000) {
|
||||
listen(QHostAddress::Any, (rand() % 45535)+20000);
|
||||
i++;
|
||||
}
|
||||
if(!isListening())
|
||||
{
|
||||
qCritical("HttpListener: Cannot bind on port %i: %s",port,qPrintable(errorString()));
|
||||
}
|
||||
else {
|
||||
qDebug("HttpListener: Listening on port %i",port);
|
||||
}
|
||||
|
@ -14,12 +14,12 @@ HttpResponse::HttpResponse(QTcpSocket* socket) {
|
||||
}
|
||||
|
||||
void HttpResponse::setHeader(QByteArray name, QByteArray value) {
|
||||
Q_ASSERT(sentHeaders==false);
|
||||
//Q_ASSERT(sentHeaders==false);
|
||||
headers.insert(name,value);
|
||||
}
|
||||
|
||||
void HttpResponse::setHeader(QByteArray name, int value) {
|
||||
Q_ASSERT(sentHeaders==false);
|
||||
//Q_ASSERT(sentHeaders==false);
|
||||
headers.insert(name,QByteArray::number(value));
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ void HttpResponse::setStatus(int statusCode, QByteArray description) {
|
||||
}
|
||||
|
||||
void HttpResponse::writeHeaders() {
|
||||
Q_ASSERT(sentHeaders==false);
|
||||
//Q_ASSERT(sentHeaders==false);
|
||||
QByteArray buffer;
|
||||
buffer.append("HTTP/1.1 ");
|
||||
buffer.append(QByteArray::number(statusCode));
|
||||
@ -67,7 +67,7 @@ void HttpResponse::writeToSocket(QByteArray data) {
|
||||
}
|
||||
|
||||
void HttpResponse::write(QByteArray data, bool lastPart) {
|
||||
Q_ASSERT(sentLastPart==false);
|
||||
//Q_ASSERT(sentLastPart==false);
|
||||
if (sentHeaders==false) {
|
||||
QByteArray connectionMode=headers.value("Connection");
|
||||
if (!headers.contains("Content-Length") && !headers.contains("Transfer-Encoding") && connectionMode!="close" && connectionMode!="Close") {
|
||||
@ -115,7 +115,7 @@ bool HttpResponse::hasSentLastPart() const {
|
||||
|
||||
|
||||
void HttpResponse::setCookie(const HttpCookie& cookie) {
|
||||
Q_ASSERT(sentHeaders==false);
|
||||
//Q_ASSERT(sentHeaders==false);
|
||||
if (!cookie.getName().isEmpty()) {
|
||||
cookies.insert(cookie.getName(),cookie);
|
||||
}
|
||||
|
@ -42,61 +42,69 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
|
||||
response.setStatus(403,"forbidden");
|
||||
response.write("403 forbidden",true);
|
||||
}
|
||||
|
||||
//TODO(DONE) carga sensible al dispositivo y a la localizaci<63>n
|
||||
QString stringPath = path;
|
||||
QStringList paths = QString(path).split('/');
|
||||
QString fileName = paths.last();
|
||||
stringPath.remove(fileName);
|
||||
HttpSession session=Static::sessionStore->getSession(request,response,false);
|
||||
QString device = session.getDeviceType();
|
||||
fileName = getDeviceAwareFileName(fileName, device, request.getHeader("Accept-Language"), stringPath);
|
||||
QString newPath = stringPath.append(fileName);
|
||||
path = newPath.toLocal8Bit();
|
||||
|
||||
//CAMBIADO
|
||||
response.setHeader("Connection","close");
|
||||
//END_TODO
|
||||
|
||||
// Check if we have the file in cache
|
||||
qint64 now=QDateTime::currentMSecsSinceEpoch();
|
||||
mutex.lock();
|
||||
CacheEntry* entry=cache.object(path);
|
||||
if (entry && (cacheTimeout==0 || entry->created>now-cacheTimeout)) {
|
||||
QByteArray document=entry->document; //copy the cached document, because other threads may destroy the cached entry immediately after mutex unlock.
|
||||
mutex.unlock();
|
||||
qDebug("StaticFileController: Cache hit for %s",path.data());
|
||||
setContentType(path,response);
|
||||
response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
||||
response.write(document);
|
||||
}
|
||||
else {
|
||||
mutex.unlock();
|
||||
qDebug("StaticFileController: Cache miss for %s",path.data());
|
||||
//qint64 now=QDateTime::currentMSecsSinceEpoch();
|
||||
// mutex.lock();
|
||||
// CacheEntry* entry=cache.object(path);
|
||||
//if (entry && (cacheTimeout==0 || entry->created>now-cacheTimeout)) {
|
||||
// QByteArray document=entry->document; //copy the cached document, because other threads may destroy the cached entry immediately after mutex unlock.
|
||||
// mutex.unlock();
|
||||
// qDebug("StaticFileController: Cache hit for %s",path.data());
|
||||
// setContentType(path,response);
|
||||
// response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
||||
// response.write(document);
|
||||
//}
|
||||
//else {
|
||||
|
||||
// mutex.unlock();
|
||||
//qDebug("StaticFileController: Cache miss for %s",path.data());
|
||||
// The file is not in cache.
|
||||
// If the filename is a directory, append index.html.
|
||||
if (QFileInfo(docroot+path).isDir()) {
|
||||
path+="/index.html";
|
||||
}
|
||||
|
||||
//TODO(DONE) carga sensible al dispositivo y a la localizaci<63>n
|
||||
QString stringPath = path;
|
||||
QStringList paths = QString(path).split('/');
|
||||
QString fileName = paths.last();
|
||||
stringPath.remove(fileName);
|
||||
HttpSession session=Static::sessionStore->getSession(request,response);
|
||||
QString device = session.getDeviceType();
|
||||
fileName = getDeviceAwareFileName(fileName, device, request.getHeader("Accept-Language"), stringPath);
|
||||
QString newPath = stringPath.append(fileName);
|
||||
//END_TODO
|
||||
|
||||
QFile file(docroot+path);
|
||||
if (file.exists()) {
|
||||
qDebug("StaticFileController: Open file %s",qPrintable(file.fileName()));
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
setContentType(path,response);
|
||||
response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
||||
if (file.size()<=maxCachedFileSize) {
|
||||
// Return the file content and store it also in the cache
|
||||
entry=new CacheEntry();
|
||||
//response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
||||
//if (file.size()<=maxCachedFileSize) {
|
||||
// // Return the file content and store it also in the cache
|
||||
// entry=new CacheEntry();
|
||||
// while (!file.atEnd() && !file.error()) {
|
||||
// QByteArray buffer=file.read(65536);
|
||||
// response.write(buffer);
|
||||
// entry->document.append(buffer);
|
||||
// }
|
||||
// entry->created=now;
|
||||
// mutex.lock();
|
||||
// cache.insert(request.getPath(),entry,entry->document.size());
|
||||
// mutex.unlock();
|
||||
//}
|
||||
//else {
|
||||
// Return the file content, do not store in cache*/
|
||||
while (!file.atEnd() && !file.error()) {
|
||||
QByteArray buffer=file.read(65536);
|
||||
response.write(buffer);
|
||||
entry->document.append(buffer);
|
||||
}
|
||||
entry->created=now;
|
||||
mutex.lock();
|
||||
cache.insert(request.getPath(),entry,entry->document.size());
|
||||
mutex.unlock();
|
||||
}
|
||||
else {
|
||||
// Return the file content, do not store in cache
|
||||
while (!file.atEnd() && !file.error()) {
|
||||
response.write(file.read(65536));
|
||||
}
|
||||
response.write(file.read(131072));
|
||||
//}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
@ -110,7 +118,7 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
|
||||
response.setStatus(404,"not found");
|
||||
response.write("404 not found",true);
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
void StaticFileController::setContentType(QString fileName, HttpResponse& response) const {
|
||||
@ -182,7 +190,7 @@ QString StaticFileController::getDeviceAwareFileName(QString fileName, QString d
|
||||
QString baseName = fi.baseName();
|
||||
QString extension = fi.completeSuffix();
|
||||
|
||||
QString completeFileName = getLocalizedFileName(fileName+"_"+device+"."+extension,locales,path);
|
||||
QString completeFileName = getLocalizedFileName(baseName+"_"+device+"."+extension,locales,path);
|
||||
|
||||
if(QFile(docroot+"/"+path+completeFileName).exists())
|
||||
return completeFileName; //existe un archivo espec<65>fico para este dispositivo y locales
|
||||
|
Reference in New Issue
Block a user