SVN commit 576600 by wildfox: Readded POST support - this time way better with KIO. -> Finally receivedResponse() is called once, instead of multiple times (because of libcurl!) -> retrieveCharset() could be implemented. Also changed indention of these two classes, to follow the WebKit style guide - as I plan to submit them later... M +5 -1 TransferJobClient.h M +3 -4 TransferJobInternal.h M +5 -0 qt/FrameQt.cpp M +52 -46 qt/TransferJobManager.cpp M +9 -9 qt/TransferJobManager.h M +26 -29 qt/TransferJobQt.cpp --- branches/work/unity/WebKit/WebCore/platform/TransferJobClient.h #576599:576600 @@ -38,6 +38,10 @@ #endif #endif +#if PLATFORM(QT) +#include +#endif + namespace WebCore { #ifdef __APPLE__ @@ -45,7 +49,7 @@ typedef NSURLResponse* PlatformResponse; #elif PLATFORM(QT) typedef void* PlatformData; - typedef char* PlatformResponse; + typedef QString PlatformResponse; #else // Not sure what the strategy for this will be on other platforms. typedef struct PlatformDataStruct *PlatformData; --- branches/work/unity/WebKit/WebCore/platform/TransferJobInternal.h #576599:576600 @@ -39,10 +39,6 @@ typedef void CURL; #endif -#if PLATFORM(QT) -#include -#endif - // The allocations and releases in TransferJobInternal are // Cocoa-exception-free (either simple Foundation classes or // KWQResourceLoader which avoids doing work in dealloc). @@ -145,6 +141,9 @@ #if PLATFORM(GDK) CURL *m_handle; #endif +#if PLATFORM(QT) + QString m_response; +#endif }; } // namespace WebCore --- branches/work/unity/WebKit/WebCore/platform/qt/FrameQt.cpp #576599:576600 @@ -150,6 +150,11 @@ { qDebug("openURL(%s)", url.url().latin1()); didOpenURL(url); + + // ResourceRequest request(resourceRequest()); + // request.m_responseMIMEType = "image/svg+xml"; + // setResourceRequest(request); + begin(url); TransferJob* job = new TransferJob(this, "GET", url); job->start(document()->docLoader()); --- branches/work/unity/WebKit/WebCore/platform/qt/TransferJobManager.cpp #576599:576600 @@ -35,9 +35,11 @@ namespace WebCore { -static TransferJobManager *s_self = 0; +static TransferJobManager* s_self = 0; -TransferJobManager::TransferJobManager() : m_jobToKioMap(), m_kioToJobMap() +TransferJobManager::TransferJobManager() + : m_jobToKioMap() + , m_kioToJobMap() { } @@ -45,36 +47,36 @@ { } -TransferJobManager *TransferJobManager::self() +TransferJobManager* TransferJobManager::self() { - if(!s_self) + if (!s_self) s_self = new TransferJobManager(); return s_self; } -void TransferJobManager::slotData(KIO::Job *kioJob, const QByteArray &data) +void TransferJobManager::slotData(KIO::Job* kioJob, const QByteArray& data) { qDebug("TransferJobManager::slotData() kioJob=%p", kioJob); - TransferJob *job = 0; + TransferJob* job = 0; // Check if we know about 'kioJob'... - QMap::const_iterator it = m_kioToJobMap.find(kioJob); - if(it != m_kioToJobMap.end()) + QMap::const_iterator it = m_kioToJobMap.find(kioJob); + if (it != m_kioToJobMap.end()) job = it.value(); - if(!job) + if (!job) return; - TransferJobInternal *d = job->getInternal(); - if(!d || !d->client) + TransferJobInternal* d = job->getInternal(); + if (!d || !d->client) return; d->client->receivedData(job, data.data(), data.size()); } -void TransferJobManager::slotMimetype(KIO::Job *job, const QString &type) +void TransferJobManager::slotMimetype(KIO::Job* job, const QString& type) { qDebug("TransferJobManager::slotMimetype(), job=%p, type=%s", job, qPrintable(type)); } @@ -83,42 +85,49 @@ { qDebug("TransferJobManager::slotResult(), kjob=%p", kjob); - KIO::Job *kioJob = qobject_cast(kjob); - if(!kioJob) + KIO::Job* kioJob = qobject_cast(kjob); + if (!kioJob) return; - TransferJob *job = 0; + TransferJob* job = 0; // Check if we know about 'kioJob'... - QMap::const_iterator it = m_kioToJobMap.find(kioJob); - if(it != m_kioToJobMap.end()) + QMap::const_iterator it = m_kioToJobMap.find(kioJob); + if (it != m_kioToJobMap.end()) job = it.value(); - if(!job) + if (!job) return; job->setError(kjob->error()); remove(job); } -void TransferJobManager::remove(TransferJob *job) +void TransferJobManager::remove(TransferJob* job) { qDebug("TransferJobManager::remove(), job=%p", job); - TransferJobInternal *d = job->getInternal(); - if(!d || !d->client) + TransferJobInternal* d = job->getInternal(); + if (!d || !d->client) return; - KIO::Job *kioJob = 0; + KIO::Job* kioJob = 0; // Check if we know about 'job'... - QMap::const_iterator it = m_jobToKioMap.find(job); - if(it != m_jobToKioMap.end()) + QMap::const_iterator it = m_jobToKioMap.find(job); + if (it != m_jobToKioMap.end()) kioJob = it.value(); - if(!kioJob) + if (!kioJob) return; + if (job->method() == "POST") { + // Will take care of informing our client... + // This must be called before receivedAllData(), + // otherwhise assembleResponseHeaders() is called too early... + job->receivedResponse(kioJob->queryMetaData("HTTP-Headers")); + } + d->client->receivedAllData(job, 0); d->client->receivedAllData(job); @@ -126,40 +135,37 @@ m_kioToJobMap.remove(kioJob); } -void TransferJobManager::add(TransferJob *job) +void TransferJobManager::add(TransferJob* job) { - TransferJobInternal *d = job->getInternal(); + TransferJobInternal* d = job->getInternal(); DeprecatedString url = d->URL.url(); qDebug("TransferJobManager::add(), job=%p, url=%s", job, url.latin1()); - if(job->method() == "GET") - { - KIO::Job *kioJob = KIO::get(KUrl(toQString(url)), false, false); + KIO::Job* kioJob = 0; - m_jobToKioMap.insert(job, kioJob); - m_kioToJobMap.insert(kioJob, job); + if (job->method() == "GET") + kioJob = KIO::get(KUrl(toQString(url)), false, false); + else if (job->method() == "POST") { + DeprecatedString postData = job->postData().flattenToString(); + QByteArray postDataArray(postData.ascii(), postData.length()); - QObject::connect(kioJob, SIGNAL(data(KIO::Job *, const QByteArray &)), this, SLOT(slotData(KIO::Job *, const QByteArray &))); - QObject::connect(kioJob, SIGNAL(mimetype(KIO::Job *, const QString &)), this, SLOT(slotMimetype(KIO::Job *, const QString &))); - QObject::connect(kioJob, SIGNAL(result(KJob *)), this, SLOT(slotResult(KJob *))); + kioJob = KIO::http_post(KUrl(toQString(url)), postDataArray, false); + kioJob->addMetaData("PropagateHttpHeader", "true"); + kioJob->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded"); } - /* - if(job->method() == "POST") { - DeprecatedString postData = job->postData().flattenToString(); + Q_ASSERT(kioJob != 0); - char *postDataString = (char *) malloc(postData.length() + 1); - strncpy(postDataString, postData.ascii(), postData.length()); - postDataString[postData.length()] = '\0'; + QObject::connect(kioJob, SIGNAL(data(KIO::Job*, const QByteArray&)), this, SLOT(slotData(KIO::Job*, const QByteArray&))); + QObject::connect(kioJob, SIGNAL(mimetype(KIO::Job*, const QString&)), this, SLOT(slotMimetype(KIO::Job*, const QString&))); + QObject::connect(kioJob, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*))); - // TODO: Do it properly after we got rid of libcurl! (also leaks the headerlist. hmpf.) - curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDS, postDataString); - } - */ + m_jobToKioMap.insert(job, kioJob); + m_kioToJobMap.insert(kioJob, job); } -void TransferJobManager::cancel(TransferJob *job) +void TransferJobManager::cancel(TransferJob* job) { qDebug("TransferJobManager::cancel(), job=%p", job); --- branches/work/unity/WebKit/WebCore/platform/qt/TransferJobManager.h #576599:576600 @@ -40,25 +40,25 @@ class TransferJobManager : public QObject { Q_OBJECT public: - static TransferJobManager *self(); + static TransferJobManager* self(); - void add(TransferJob *job); - void cancel(TransferJob *job); + void add(TransferJob*); + void cancel(TransferJob*); public Q_SLOTS: - void slotData(KIO::Job *job, const QByteArray &data); - void slotMimetype(KIO::Job *job, const QString &type); - void slotResult(KJob *job); + void slotData(KIO::Job*, const QByteArray& data); + void slotMimetype(KIO::Job*, const QString& type); + void slotResult(KJob*); private: TransferJobManager(); ~TransferJobManager(); - void remove(TransferJob *job); + void remove(TransferJob*); // KIO Job <-> WebKit Job mapping - QMap m_jobToKioMap; - QMap m_kioToJobMap; + QMap m_jobToKioMap; + QMap m_kioToJobMap; }; } --- branches/work/unity/WebKit/WebCore/platform/qt/TransferJobQt.cpp #576599:576600 @@ -27,11 +27,14 @@ #include "config.h" #include "HelperQt.h" -#include "TransferJob.h" +#include +#include + #include "DocLoader.h" -#include "TransferJobInternal.h" +#include "TransferJob.h" #include "TransferJobManager.h" +#include "TransferJobInternal.h" namespace WebCore { @@ -44,7 +47,7 @@ cancel(); } -bool TransferJob::start(DocLoader *docLoader) +bool TransferJob::start(DocLoader*) { TransferJobManager::self()->add(this); return true; @@ -57,50 +60,44 @@ void TransferJob::assembleResponseHeaders() const { - /* if (!d->assembledResponseHeaders) { - d->responseHeaders = DeprecatedString(d->response.constData(), d->response.length()); - + d->responseHeaders = DeprecatedString(d->m_response.constData(), d->m_response.length()); d->assembledResponseHeaders = true; - - // TODO: Move the client activation to receivedResponse(), once - // we use KIO, and receivedResponse() is called only once. - if (d->client) - d->client->receivedResponse(const_cast(this), (char *) d->response.data()); - - d->response = QString(); // Reset + d->m_response = QString(); // Reset } - */ } void TransferJob::retrieveCharset() const { if (!d->retrievedCharset) { d->retrievedCharset = true; - } - // TODO: We can just parse the headers here, but once we use KIO - // we can set the response parameter to sth. else than a "char*". - // I save my time but not implementing it for now :-) - notImplemented(); + int pos = d->responseHeaders.find("content-type:", 0, false); + + if (pos > -1) { + pos += 13; + int index = d->responseHeaders.find('\n', pos); + QString type = toQString(d->responseHeaders.mid(pos, (index - pos))); + index = type.indexOf(';'); + + if (index > -1) { + QString encoding = type.mid(index + 1).remove(QRegExp("charset[ ]*=[ ]*", Qt::CaseInsensitive)).trimmed(); + d->metaData.set("charset", fromQString(encoding)); + } + } + } } -void TransferJob::receivedResponse(char* response) +void TransferJob::receivedResponse(QString response) { Q_ASSERT(method() == "POST"); d->assembledResponseHeaders = false; d->retrievedCharset = false; + d->m_response = response; - // TODO: This is flawed: - // - usually receivedResponse() should be called _once_, when the - // response is available - seems very unflexible to do that with libcurl - // (so let's wait until it dies and do it properly with KIO then.) - // - QString::fromLatin1() is also wrong, of course. - // - // Anyway, let's collect the response data here, as the TransferJobManager - // calls us for every line of the header it receives. - // FIXME> d->response += QString::fromLatin1(response); + if (d->client) + d->client->receivedResponse(const_cast(this), response); } } // namespace WebCore