mirror of
https://github.com/GoldenCheetah/GoldenCheetah.git
synced 2026-02-13 08:08:42 +00:00
Enable API WebServices when GC is running
.. but disable logging if not running as a server .. let user define if API services start in preferences
This commit is contained in:
@@ -31,7 +31,7 @@ HttpConnectionHandler::HttpConnectionHandler(QSettings* settings, HttpRequestHan
|
||||
connect(&readTimer, SIGNAL(timeout()), SLOT(readTimeout()));
|
||||
readTimer.setSingleShot(true);
|
||||
|
||||
qDebug("HttpConnectionHandler (%p): constructed", this);
|
||||
wDebug("HttpConnectionHandler (%p): constructed", this);
|
||||
this->start();
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ HttpConnectionHandler::~HttpConnectionHandler() {
|
||||
quit();
|
||||
wait();
|
||||
delete socket;
|
||||
qDebug("HttpConnectionHandler (%p): destroyed", this);
|
||||
wDebug("HttpConnectionHandler (%p): destroyed", this);
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ void HttpConnectionHandler::createSocket() {
|
||||
QSslSocket* sslSocket=new QSslSocket();
|
||||
sslSocket->setSslConfiguration(*sslConfiguration);
|
||||
socket=sslSocket;
|
||||
qDebug("HttpConnectionHandler (%p): SSL is enabled", this);
|
||||
wDebug("HttpConnectionHandler (%p): SSL is enabled", this);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
@@ -61,7 +61,7 @@ void HttpConnectionHandler::createSocket() {
|
||||
|
||||
|
||||
void HttpConnectionHandler::run() {
|
||||
qDebug("HttpConnectionHandler (%p): thread started", this);
|
||||
wDebug("HttpConnectionHandler (%p): thread started", this);
|
||||
try {
|
||||
exec();
|
||||
}
|
||||
@@ -69,12 +69,12 @@ void HttpConnectionHandler::run() {
|
||||
qCritical("HttpConnectionHandler (%p): an uncatched exception occured in the thread",this);
|
||||
}
|
||||
socket->close();
|
||||
qDebug("HttpConnectionHandler (%p): thread stopped", this);
|
||||
wDebug("HttpConnectionHandler (%p): thread stopped", this);
|
||||
}
|
||||
|
||||
|
||||
void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor) {
|
||||
qDebug("HttpConnectionHandler (%p): handle new connection", this);
|
||||
wDebug("HttpConnectionHandler (%p): handle new connection", this);
|
||||
busy = true;
|
||||
Q_ASSERT(socket->isOpen()==false); // if not, then the handler is already busy
|
||||
|
||||
@@ -91,7 +91,7 @@ void HttpConnectionHandler::handleConnection(tSocketDescriptor socketDescriptor)
|
||||
#ifndef QT_NO_OPENSSL
|
||||
// Switch on encryption, if SSL is configured
|
||||
if (sslConfiguration) {
|
||||
qDebug("HttpConnectionHandler (%p): Starting encryption", this);
|
||||
wDebug("HttpConnectionHandler (%p): Starting encryption", this);
|
||||
((QSslSocket*)socket)->startServerEncryption();
|
||||
}
|
||||
#endif
|
||||
@@ -115,7 +115,7 @@ void HttpConnectionHandler::setBusy() {
|
||||
|
||||
|
||||
void HttpConnectionHandler::readTimeout() {
|
||||
qDebug("HttpConnectionHandler (%p): read timeout occured",this);
|
||||
wDebug("HttpConnectionHandler (%p): read timeout occured",this);
|
||||
|
||||
//Commented out because QWebView cannot handle this.
|
||||
//socket->write("HTTP/1.1 408 request timeout\r\nConnection: close\r\n\r\n408 request timeout\r\n");
|
||||
@@ -128,7 +128,7 @@ void HttpConnectionHandler::readTimeout() {
|
||||
|
||||
|
||||
void HttpConnectionHandler::disconnected() {
|
||||
qDebug("HttpConnectionHandler (%p): disconnected", this);
|
||||
wDebug("HttpConnectionHandler (%p): disconnected", this);
|
||||
socket->close();
|
||||
readTimer.stop();
|
||||
busy = false;
|
||||
@@ -138,7 +138,7 @@ void HttpConnectionHandler::read() {
|
||||
// The loop adds support for HTTP pipelinig
|
||||
while (socket->bytesAvailable()) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpConnectionHandler (%p): read input",this);
|
||||
wDebug("HttpConnectionHandler (%p): read input",this);
|
||||
#endif
|
||||
|
||||
// Create new HttpRequest object if necessary
|
||||
@@ -170,7 +170,7 @@ void HttpConnectionHandler::read() {
|
||||
// If the request is complete, let the request mapper dispatch it
|
||||
if (currentRequest->getStatus()==HttpRequest::complete) {
|
||||
readTimer.stop();
|
||||
qDebug("HttpConnectionHandler (%p): received request",this);
|
||||
wDebug("HttpConnectionHandler (%p): received request",this);
|
||||
HttpResponse response(socket);
|
||||
try {
|
||||
requestHandler->service(*currentRequest, response);
|
||||
@@ -184,7 +184,7 @@ void HttpConnectionHandler::read() {
|
||||
response.write(QByteArray(),true);
|
||||
}
|
||||
|
||||
qDebug("HttpConnectionHandler (%p): finished request",this);
|
||||
wDebug("HttpConnectionHandler (%p): finished request",this);
|
||||
|
||||
// Close the connection after delivering the response, if requested
|
||||
if (QString::compare(currentRequest->getHeader("Connection"),"close",Qt::CaseInsensitive)==0) {
|
||||
|
||||
@@ -26,7 +26,7 @@ HttpConnectionHandlerPool::~HttpConnectionHandlerPool() {
|
||||
delete handler;
|
||||
}
|
||||
delete sslConfiguration;
|
||||
qDebug("HttpConnectionHandlerPool (%p): destroyed", this);
|
||||
wDebug("HttpConnectionHandlerPool (%p): destroyed", this);
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ void HttpConnectionHandlerPool::cleanup() {
|
||||
if (++idleCounter > maxIdleHandlers) {
|
||||
pool.removeOne(handler);
|
||||
delete handler;
|
||||
qDebug("HttpConnectionHandlerPool: Removed connection handler (%p), pool size is now %i",handler,pool.size());
|
||||
wDebug("HttpConnectionHandlerPool: Removed connection handler (%p), pool size is now %i",handler,pool.size());
|
||||
break; // remove only one handler in each interval
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,7 @@ void HttpConnectionHandlerPool::loadSslConfig() {
|
||||
sslConfiguration->setPeerVerifyMode(QSslSocket::VerifyNone);
|
||||
sslConfiguration->setProtocol(QSsl::TlsV1SslV3);
|
||||
|
||||
qDebug("HttpConnectionHandlerPool: SSL settings loaded");
|
||||
wDebug("HttpConnectionHandlerPool: SSL settings loaded");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,5 +23,9 @@
|
||||
/** Get the library version number */
|
||||
DECLSPEC const char* getQtWebAppLibVersion();
|
||||
|
||||
/** wDebug() uses QT QMessageLogger but as info to log
|
||||
rather than interfering with normal qDebug usage **/
|
||||
#define wDebug QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).warning
|
||||
|
||||
#endif // HTTPGLOBAL_H
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ HttpListener::HttpListener(QSettings* settings, HttpRequestHandler* requestHandl
|
||||
|
||||
HttpListener::~HttpListener() {
|
||||
close();
|
||||
qDebug("HttpListener: destroyed");
|
||||
wDebug("HttpListener: destroyed");
|
||||
}
|
||||
|
||||
|
||||
@@ -40,14 +40,14 @@ void HttpListener::listen() {
|
||||
qCritical("HttpListener: Cannot bind on port %i: %s",port,qPrintable(errorString()));
|
||||
}
|
||||
else {
|
||||
qDebug("HttpListener: Listening on port %i",port);
|
||||
wDebug("HttpListener: Listening on port %i",port);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void HttpListener::close() {
|
||||
QTcpServer::close();
|
||||
qDebug("HttpListener: closed");
|
||||
wDebug("HttpListener: closed");
|
||||
if (pool) {
|
||||
delete pool;
|
||||
pool=NULL;
|
||||
@@ -56,7 +56,7 @@ void HttpListener::close() {
|
||||
|
||||
void HttpListener::incomingConnection(tSocketDescriptor socketDescriptor) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpListener: New connection");
|
||||
wDebug("HttpListener: New connection");
|
||||
#endif
|
||||
|
||||
HttpConnectionHandler* freeHandler=NULL;
|
||||
@@ -74,7 +74,7 @@ void HttpListener::incomingConnection(tSocketDescriptor socketDescriptor) {
|
||||
}
|
||||
else {
|
||||
// Reject the connection
|
||||
qDebug("HttpListener: Too many incoming connections");
|
||||
wDebug("HttpListener: Too many incoming connections");
|
||||
QTcpSocket* socket=new QTcpSocket(this);
|
||||
socket->setSocketDescriptor(socketDescriptor);
|
||||
connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()));
|
||||
|
||||
@@ -18,14 +18,14 @@ HttpRequest::HttpRequest(QSettings* settings) {
|
||||
|
||||
void HttpRequest::readRequest(QTcpSocket* socket) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: read request");
|
||||
wDebug("HttpRequest: read request");
|
||||
#endif
|
||||
int toRead=maxSize-currentSize+1; // allow one byte more to be able to detect overflow
|
||||
lineBuffer.append(socket->readLine(toRead));
|
||||
currentSize+=lineBuffer.size();
|
||||
if (!lineBuffer.contains('\r') && !lineBuffer.contains('\n')) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: collecting more parts until line break");
|
||||
wDebug("HttpRequest: collecting more parts until line break");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
@@ -48,14 +48,14 @@ void HttpRequest::readRequest(QTcpSocket* socket) {
|
||||
|
||||
void HttpRequest::readHeader(QTcpSocket* socket) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: read header");
|
||||
wDebug("HttpRequest: read header");
|
||||
#endif
|
||||
int toRead=maxSize-currentSize+1; // allow one byte more to be able to detect overflow
|
||||
lineBuffer.append(socket->readLine(toRead));
|
||||
currentSize+=lineBuffer.size();
|
||||
if (!lineBuffer.contains('\r') && !lineBuffer.contains('\n')) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: collecting more parts until line break");
|
||||
wDebug("HttpRequest: collecting more parts until line break");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
@@ -68,13 +68,13 @@ void HttpRequest::readHeader(QTcpSocket* socket) {
|
||||
QByteArray value=newData.mid(colon+1).trimmed();
|
||||
headers.insert(currentHeader,value);
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: received header %s: %s",currentHeader.data(),value.data());
|
||||
wDebug("HttpRequest: received header %s: %s",currentHeader.data(),value.data());
|
||||
#endif
|
||||
}
|
||||
else if (!newData.isEmpty()) {
|
||||
// received another line - belongs to the previous header
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: read additional line of header");
|
||||
wDebug("HttpRequest: read additional line of header");
|
||||
#endif
|
||||
// Received additional line of previous header
|
||||
if (headers.contains(currentHeader)) {
|
||||
@@ -84,7 +84,7 @@ void HttpRequest::readHeader(QTcpSocket* socket) {
|
||||
else {
|
||||
// received an empty line - end of headers reached
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: headers completed");
|
||||
wDebug("HttpRequest: headers completed");
|
||||
#endif
|
||||
// Empty line received, that means all headers have been received
|
||||
// Check for multipart/form-data
|
||||
@@ -101,7 +101,7 @@ void HttpRequest::readHeader(QTcpSocket* socket) {
|
||||
}
|
||||
if (expectedBodySize==0) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: expect no body");
|
||||
wDebug("HttpRequest: expect no body");
|
||||
#endif
|
||||
status=complete;
|
||||
}
|
||||
@@ -115,7 +115,7 @@ void HttpRequest::readHeader(QTcpSocket* socket) {
|
||||
}
|
||||
else {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: expect %i bytes body",expectedBodySize);
|
||||
wDebug("HttpRequest: expect %i bytes body",expectedBodySize);
|
||||
#endif
|
||||
status=waitForBody;
|
||||
}
|
||||
@@ -127,7 +127,7 @@ void HttpRequest::readBody(QTcpSocket* socket) {
|
||||
if (boundary.isEmpty()) {
|
||||
// normal body, no multipart
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: receive body");
|
||||
wDebug("HttpRequest: receive body");
|
||||
#endif
|
||||
int toRead=expectedBodySize-bodyData.size();
|
||||
QByteArray newData=socket->read(toRead);
|
||||
@@ -140,7 +140,7 @@ void HttpRequest::readBody(QTcpSocket* socket) {
|
||||
else {
|
||||
// multipart body, store into temp file
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: receiving multipart body");
|
||||
wDebug("HttpRequest: receiving multipart body");
|
||||
#endif
|
||||
if (!tempFile.isOpen()) {
|
||||
tempFile.open();
|
||||
@@ -158,7 +158,7 @@ void HttpRequest::readBody(QTcpSocket* socket) {
|
||||
}
|
||||
else if (fileSize>=expectedBodySize) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: received whole multipart body");
|
||||
wDebug("HttpRequest: received whole multipart body");
|
||||
#endif
|
||||
tempFile.flush();
|
||||
if (tempFile.error()) {
|
||||
@@ -173,7 +173,7 @@ void HttpRequest::readBody(QTcpSocket* socket) {
|
||||
|
||||
void HttpRequest::decodeRequestParams() {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: extract and decode request parameters");
|
||||
wDebug("HttpRequest: extract and decode request parameters");
|
||||
#endif
|
||||
// Get URL parameters
|
||||
QByteArray rawParameters;
|
||||
@@ -211,13 +211,13 @@ void HttpRequest::decodeRequestParams() {
|
||||
|
||||
void HttpRequest::extractCookies() {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: extract cookies");
|
||||
wDebug("HttpRequest: extract cookies");
|
||||
#endif
|
||||
foreach(QByteArray cookieStr, headers.values("Cookie")) {
|
||||
QList<QByteArray> list=HttpCookie::splitCSV(cookieStr);
|
||||
foreach(QByteArray part, list) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: found cookie %s",part.data());
|
||||
wDebug("HttpRequest: found cookie %s",part.data());
|
||||
#endif // Split the part into name and value
|
||||
QByteArray name;
|
||||
QByteArray value;
|
||||
@@ -325,12 +325,12 @@ QByteArray HttpRequest::urlDecode(const QByteArray source) {
|
||||
|
||||
|
||||
void HttpRequest::parseMultiPartFile() {
|
||||
qDebug("HttpRequest: parsing multipart temp file");
|
||||
wDebug("HttpRequest: parsing multipart temp file");
|
||||
tempFile.seek(0);
|
||||
bool finished=false;
|
||||
while (!tempFile.atEnd() && !finished && !tempFile.error()) {
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: reading multpart headers");
|
||||
wDebug("HttpRequest: reading multpart headers");
|
||||
#endif
|
||||
QByteArray fieldName;
|
||||
QByteArray fileName;
|
||||
@@ -349,11 +349,11 @@ void HttpRequest::parseMultiPartFile() {
|
||||
fileName=line.mid(start+11,end-start-11);
|
||||
}
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: multipart field=%s, filename=%s",fieldName.data(),fileName.data());
|
||||
wDebug("HttpRequest: multipart field=%s, filename=%s",fieldName.data(),fileName.data());
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
qDebug("HttpRequest: ignoring unsupported content part %s",line.data());
|
||||
wDebug("HttpRequest: ignoring unsupported content part %s",line.data());
|
||||
}
|
||||
}
|
||||
else if (line.isEmpty()) {
|
||||
@@ -362,7 +362,7 @@ void HttpRequest::parseMultiPartFile() {
|
||||
}
|
||||
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: reading multpart data");
|
||||
wDebug("HttpRequest: reading multpart data");
|
||||
#endif
|
||||
QTemporaryFile* uploadedFile=0;
|
||||
QByteArray fieldValue;
|
||||
@@ -375,20 +375,20 @@ void HttpRequest::parseMultiPartFile() {
|
||||
// last field was a form field
|
||||
fieldValue.remove(fieldValue.size()-2,2);
|
||||
parameters.insert(fieldName,fieldValue);
|
||||
qDebug("HttpRequest: set parameter %s=%s",fieldName.data(),fieldValue.data());
|
||||
wDebug("HttpRequest: set parameter %s=%s",fieldName.data(),fieldValue.data());
|
||||
}
|
||||
else if (!fileName.isEmpty() && !fieldName.isEmpty()) {
|
||||
// last field was a file
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: finishing writing to uploaded file");
|
||||
wDebug("HttpRequest: finishing writing to uploaded file");
|
||||
#endif
|
||||
uploadedFile->resize(uploadedFile->size()-2);
|
||||
uploadedFile->flush();
|
||||
uploadedFile->seek(0);
|
||||
parameters.insert(fieldName,fileName);
|
||||
qDebug("HttpRequest: set parameter %s=%s",fieldName.data(),fileName.data());
|
||||
wDebug("HttpRequest: set parameter %s=%s",fieldName.data(),fileName.data());
|
||||
uploadedFiles.insert(fieldName,uploadedFile);
|
||||
qDebug("HttpRequest: uploaded file size is %i",(int) uploadedFile->size());
|
||||
wDebug("HttpRequest: uploaded file size is %i",(int) uploadedFile->size());
|
||||
}
|
||||
if (line.contains(boundary+"--")) {
|
||||
finished=true;
|
||||
@@ -419,7 +419,7 @@ void HttpRequest::parseMultiPartFile() {
|
||||
qCritical("HttpRequest: cannot read temp file, %s",qPrintable(tempFile.errorString()));
|
||||
}
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpRequest: finished parsing multipart temp file");
|
||||
wDebug("HttpRequest: finished parsing multipart temp file");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ HttpRequestHandler::~HttpRequestHandler() {}
|
||||
|
||||
void HttpRequestHandler::service(HttpRequest& request, HttpResponse& response) {
|
||||
qCritical("HttpRequestHandler: you need to override the service() function");
|
||||
qDebug("HttpRequestHandler: request=%s %s %s",request.getMethod().data(),request.getPath().data(),request.getVersion().data());
|
||||
wDebug("HttpRequestHandler: request=%s %s %s",request.getMethod().data(),request.getPath().data(),request.getVersion().data());
|
||||
response.setStatus(501,"not implemented");
|
||||
response.write("501 not implemented",true);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ HttpSession::HttpSession(bool canStore) {
|
||||
dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
|
||||
dataPtr->id=QUuid::createUuid().toString().toLocal8Bit();
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpSession: created new session data with id %s",dataPtr->id.data());
|
||||
wDebug("HttpSession: created new session data with id %s",dataPtr->id.data());
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
@@ -29,7 +29,7 @@ HttpSession::HttpSession(const HttpSession& other) {
|
||||
dataPtr->lock.lockForWrite();
|
||||
dataPtr->refCount++;
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
|
||||
wDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
|
||||
#endif
|
||||
dataPtr->lock.unlock();
|
||||
}
|
||||
@@ -42,7 +42,7 @@ HttpSession& HttpSession::operator= (const HttpSession& other) {
|
||||
dataPtr->lock.lockForWrite();
|
||||
dataPtr->refCount++;
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
|
||||
wDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
|
||||
#endif
|
||||
dataPtr->lastAccess=QDateTime::currentMSecsSinceEpoch();
|
||||
dataPtr->lock.unlock();
|
||||
@@ -52,7 +52,7 @@ HttpSession& HttpSession::operator= (const HttpSession& other) {
|
||||
oldPtr->lock.lockForRead();
|
||||
refCount=oldPtr->refCount--;
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpSession: refCount of %s is %i",oldPtr->id.data(),oldPtr->refCount);
|
||||
wDebug("HttpSession: refCount of %s is %i",oldPtr->id.data(),oldPtr->refCount);
|
||||
#endif
|
||||
oldPtr->lock.unlock();
|
||||
if (refCount==0) {
|
||||
@@ -68,11 +68,11 @@ HttpSession::~HttpSession() {
|
||||
dataPtr->lock.lockForRead();
|
||||
refCount=--dataPtr->refCount;
|
||||
#ifdef SUPERVERBOSE
|
||||
qDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
|
||||
wDebug("HttpSession: refCount of %s is %i",dataPtr->id.data(),dataPtr->refCount);
|
||||
#endif
|
||||
dataPtr->lock.unlock();
|
||||
if (refCount==0) {
|
||||
qDebug("HttpSession: deleting data");
|
||||
wDebug("HttpSession: deleting data");
|
||||
delete dataPtr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ HttpSessionStore::HttpSessionStore(QSettings* settings, QObject* parent)
|
||||
cleanupTimer.start(60000);
|
||||
cookieName=settings->value("cookieName","sessionid").toByteArray();
|
||||
expirationTime=settings->value("expirationTime",3600000).toInt();
|
||||
qDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
|
||||
wDebug("HttpSessionStore: Sessions expire after %i milliseconds",expirationTime);
|
||||
}
|
||||
|
||||
HttpSessionStore::~HttpSessionStore()
|
||||
@@ -35,7 +35,7 @@ QByteArray HttpSessionStore::getSessionId(HttpRequest& request, HttpResponse& re
|
||||
// Clear the session ID if there is no such session in the storage.
|
||||
if (!sessionId.isEmpty()) {
|
||||
if (!sessions.contains(sessionId)) {
|
||||
qDebug("HttpSessionStore: received invalid session cookie with ID %s",sessionId.data());
|
||||
wDebug("HttpSessionStore: received invalid session cookie with ID %s",sessionId.data());
|
||||
sessionId.clear();
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,7 @@ HttpSession HttpSessionStore::getSession(HttpRequest& request, HttpResponse& res
|
||||
QByteArray cookieComment=settings->value("cookieComment").toByteArray();
|
||||
QByteArray cookieDomain=settings->value("cookieDomain").toByteArray();
|
||||
HttpSession session(true);
|
||||
qDebug("HttpSessionStore: create new session with ID %s",session.getId().data());
|
||||
wDebug("HttpSessionStore: create new session with ID %s",session.getId().data());
|
||||
sessions.insert(session.getId(),session);
|
||||
response.setCookie(HttpCookie(cookieName,session.getId(),expirationTime/1000,cookiePath,cookieComment,cookieDomain));
|
||||
mutex.unlock();
|
||||
@@ -97,7 +97,7 @@ void HttpSessionStore::timerEvent() {
|
||||
HttpSession session=prev.value();
|
||||
qint64 lastAccess=session.getLastAccess();
|
||||
if (now-lastAccess>expirationTime) {
|
||||
qDebug("HttpSessionStore: session %s expired",session.getId().data());
|
||||
wDebug("HttpSessionStore: session %s expired",session.getId().data());
|
||||
sessions.erase(prev);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,11 +27,11 @@ StaticFileController::StaticFileController(QSettings* settings, QObject* parent)
|
||||
docroot=QFileInfo(configFile.absolutePath(),docroot).absoluteFilePath();
|
||||
}
|
||||
}
|
||||
qDebug("StaticFileController: docroot=%s, encoding=%s, maxAge=%i",qPrintable(docroot),qPrintable(encoding),maxAge);
|
||||
wDebug("StaticFileController: docroot=%s, encoding=%s, maxAge=%i",qPrintable(docroot),qPrintable(encoding),maxAge);
|
||||
maxCachedFileSize=settings->value("maxCachedFileSize","65536").toInt();
|
||||
cache.setMaxCost(settings->value("cacheSize","1000000").toInt());
|
||||
cacheTimeout=settings->value("cacheTime","60000").toInt();
|
||||
qDebug("StaticFileController: cache timeout=%i, size=%i",cacheTimeout,cache.maxCost());
|
||||
wDebug("StaticFileController: cache timeout=%i, size=%i",cacheTimeout,cache.maxCost());
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
|
||||
QByteArray document=entry->document; //copy the cached document, because other threads may destroy the cached entry immediately after mutex unlock.
|
||||
QByteArray filename=entry->filename;
|
||||
mutex.unlock();
|
||||
qDebug("StaticFileController: Cache hit for %s",path.data());
|
||||
wDebug("StaticFileController: Cache hit for %s",path.data());
|
||||
setContentType(filename,response);
|
||||
response.setHeader("Cache-Control","max-age="+QByteArray::number(maxAge/1000));
|
||||
response.write(document);
|
||||
@@ -53,7 +53,7 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
|
||||
else {
|
||||
mutex.unlock();
|
||||
// The file is not in cache.
|
||||
qDebug("StaticFileController: Cache miss for %s",path.data());
|
||||
wDebug("StaticFileController: Cache miss for %s",path.data());
|
||||
// Forbid access to files outside the docroot directory
|
||||
if (path.contains("/..")) {
|
||||
qWarning("StaticFileController: detected forbidden characters in path %s",path.data());
|
||||
@@ -67,7 +67,7 @@ void StaticFileController::service(HttpRequest& request, HttpResponse& response)
|
||||
}
|
||||
// Try to open the file
|
||||
QFile file(docroot+path);
|
||||
qDebug("StaticFileController: Open file %s",qPrintable(file.fileName()));
|
||||
wDebug("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));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/*
|
||||
#endif
|
||||
* Copyright (c) 2012 Mark Liversedge (liversedge@gmail.com)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
@@ -241,6 +242,17 @@ GeneralPage::GeneralPage(Context *context) : context(context)
|
||||
warnOnExit->setChecked(appsettings->cvalue(NULL, GC_WARNEXIT, true).toBool());
|
||||
configLayout->addWidget(warnOnExit, 13,1, Qt::AlignLeft);
|
||||
|
||||
//
|
||||
// Run API web services when running
|
||||
//
|
||||
int offset=0;
|
||||
#ifdef GC_WANT_HTTP
|
||||
offset += 1;
|
||||
startHttp = new QCheckBox(tr("Enable API Web Services"), this);
|
||||
startHttp->setChecked(appsettings->cvalue(NULL, GC_START_HTTP, true).toBool());
|
||||
configLayout->addWidget(startHttp, 14,1, Qt::AlignLeft);
|
||||
#endif
|
||||
|
||||
//
|
||||
// Athlete directory (home of athletes)
|
||||
//
|
||||
@@ -252,9 +264,9 @@ GeneralPage::GeneralPage(Context *context) : context(context)
|
||||
athleteBrowseButton = new QPushButton(tr("Browse"));
|
||||
athleteBrowseButton->setFixedWidth(120);
|
||||
|
||||
configLayout->addWidget(athleteLabel, 14,0, Qt::AlignRight);
|
||||
configLayout->addWidget(athleteDirectory, 14,1);
|
||||
configLayout->addWidget(athleteBrowseButton, 14,2);
|
||||
configLayout->addWidget(athleteLabel, 14 + offset,0, Qt::AlignRight);
|
||||
configLayout->addWidget(athleteDirectory, 14 + offset,1);
|
||||
configLayout->addWidget(athleteBrowseButton, 14 + offset,2);
|
||||
|
||||
connect(athleteBrowseButton, SIGNAL(clicked()), this, SLOT(browseAthleteDir()));
|
||||
|
||||
@@ -270,9 +282,9 @@ GeneralPage::GeneralPage(Context *context) : context(context)
|
||||
workoutBrowseButton = new QPushButton(tr("Browse"));
|
||||
workoutBrowseButton->setFixedWidth(120);
|
||||
|
||||
configLayout->addWidget(workoutLabel, 15,0, Qt::AlignRight);
|
||||
configLayout->addWidget(workoutDirectory, 15,1);
|
||||
configLayout->addWidget(workoutBrowseButton, 15,2);
|
||||
configLayout->addWidget(workoutLabel, 15 + offset,0, Qt::AlignRight);
|
||||
configLayout->addWidget(workoutDirectory, 15 + offset,1);
|
||||
configLayout->addWidget(workoutBrowseButton, 15 + offset,2);
|
||||
|
||||
connect(workoutBrowseButton, SIGNAL(clicked()), this, SLOT(browseWorkoutDir()));
|
||||
|
||||
@@ -284,6 +296,7 @@ GeneralPage::GeneralPage(Context *context) : context(context)
|
||||
b4.lts = perfManLTSVal.toInt();
|
||||
b4.sts = perfManSTSVal.toInt();
|
||||
b4.warn = warnOnExit->isChecked();
|
||||
b4.starthttp = startHttp->isChecked();
|
||||
}
|
||||
|
||||
void
|
||||
@@ -345,11 +358,19 @@ GeneralPage::saveClicked()
|
||||
appsettings->setValue(GC_SB_NAME, appsettings->value(this, GC_SB_NAME,tr("Stress Balance")));
|
||||
appsettings->setValue(GC_SB_ACRONYM, appsettings->value(this, GC_SB_ACRONYM,tr("SB")));
|
||||
|
||||
#ifdef GC_WANT_HTTP
|
||||
// start http
|
||||
appsettings->setValue(GC_START_HTTP, startHttp->isChecked());
|
||||
#endif
|
||||
|
||||
qint32 state=0;
|
||||
|
||||
// general stuff changed ?
|
||||
if (b4.wheel != wheelSizeEdit->text().toInt() ||
|
||||
b4.crank != crankLengthCombo->currentIndex() ||
|
||||
#ifdef GC_WANT_HTTP
|
||||
b4.starthttp != startHttp->isChecked() ||
|
||||
#endif
|
||||
b4.hyst != hystedit->text().toFloat())
|
||||
state += CONFIG_GENERAL;
|
||||
|
||||
|
||||
@@ -75,6 +75,9 @@ class GeneralPage : public QWidget
|
||||
QCheckBox *garminSmartRecord;
|
||||
QCheckBox *warnOnExit;
|
||||
QCheckBox *useCPForFTPCheckBox;
|
||||
#ifdef GC_WANT_HTTP
|
||||
QCheckBox *startHttp;
|
||||
#endif
|
||||
QLineEdit *wheelSizeEdit;
|
||||
QLineEdit *garminHWMarkedit;
|
||||
QLineEdit *hystedit;
|
||||
@@ -103,6 +106,9 @@ class GeneralPage : public QWidget
|
||||
int wbal;
|
||||
int lts,sts;
|
||||
bool warn;
|
||||
#ifdef GC_WANT_HTTP
|
||||
bool starthttp;
|
||||
#endif
|
||||
} b4;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#define GC_HOMEDIR "homedirectory"
|
||||
#define GC_VERSION_USED "versionused"
|
||||
#define GC_START_HTTP "starthttp"
|
||||
#define GC_SAFEEXIT "safeexit"
|
||||
#define GC_SETTINGS_CO "goldencheetah.org"
|
||||
#define GC_SETTINGS_APP "GoldenCheetah"
|
||||
|
||||
45
src/main.cpp
45
src/main.cpp
@@ -72,8 +72,34 @@ QString gcroot;
|
||||
|
||||
QApplication *application;
|
||||
|
||||
bool nogui;
|
||||
#ifdef GC_WANT_HTTP
|
||||
#include "APIWebService.h"
|
||||
#if QT_VERSION > 0x50000
|
||||
void myMessageOutput(QtMsgType type, const QMessageLogContext &, const QString &string)
|
||||
{
|
||||
const char *msg = string.toLocal8Bit().constData();
|
||||
#else
|
||||
void myMessageOutput(QtMsgType type, const char *msg)
|
||||
{
|
||||
#endif
|
||||
//in this function, you can write the message to any stream!
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
fprintf(stderr, "Debug: %s\n", msg);
|
||||
break;
|
||||
case QtWarningMsg: // supress warnings unless server mode
|
||||
if (nogui) fprintf(stderr, "Warning: %s\n", msg);
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
fprintf(stderr, "Critical: %s\n", msg);
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
fprintf(stderr, "Fatal: %s\n", msg);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
HttpListener *listener;
|
||||
void sigabort(int)
|
||||
{
|
||||
@@ -102,6 +128,7 @@ main(int argc, char *argv[])
|
||||
bool debug = false;
|
||||
#endif
|
||||
bool server = false;
|
||||
nogui = false;
|
||||
bool help = false;
|
||||
|
||||
// honour command line switches
|
||||
@@ -126,7 +153,7 @@ main(int argc, char *argv[])
|
||||
|
||||
} else if (arg == "--server") {
|
||||
#ifdef GC_WANT_HTTP
|
||||
server = true;
|
||||
nogui = server = true;
|
||||
#else
|
||||
fprintf(stderr, "HTTP support not compiled in, exiting.\n");
|
||||
exit(1);
|
||||
@@ -314,11 +341,20 @@ main(int argc, char *argv[])
|
||||
|
||||
// The API server offers webservices (default port 12021, see httpserver.ini)
|
||||
// This is to enable integration with R and similar
|
||||
if (server) {
|
||||
if (appsettings->value(NULL, GC_START_HTTP).toBool() || server) {
|
||||
|
||||
// does the ini file exist ?
|
||||
// notifications etc
|
||||
if (nogui) {
|
||||
qDebug()<<"Starting GoldenCheetah API web-services... (hit ^C to close)";
|
||||
qDebug()<<"Athlete directory:"<<home.absolutePath();
|
||||
} else {
|
||||
// switch off warnings if in gui mode
|
||||
#if QT_VERSION > 0x50000
|
||||
qInstallMessageHandler(myMessageOutput);
|
||||
#else
|
||||
qInstallMsgHandler(myMessageOutput);
|
||||
#endif
|
||||
}
|
||||
|
||||
QString httpini = home.absolutePath() + "/httpserver.ini";
|
||||
if (!QFile(httpini).exists()) {
|
||||
@@ -346,6 +382,8 @@ main(int argc, char *argv[])
|
||||
QSettings* settings=new QSettings(httpini,QSettings::IniFormat,application);
|
||||
listener=new HttpListener(settings,new APIWebService(home, application),application);
|
||||
|
||||
// if not going on to launch a gui...
|
||||
if (nogui) {
|
||||
// catch ^C exit
|
||||
signal(SIGINT, sigabort);
|
||||
|
||||
@@ -358,6 +396,7 @@ main(int argc, char *argv[])
|
||||
// and done
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// lets attempt to open as asked/remembered
|
||||
|
||||
Reference in New Issue
Block a user