6 \brief InternalNetworkAccessManager non-inline non-template implementation */
8 #include "Publisher.hh"
9 //#include "Publisher.ih"
13 #include <QNetworkProxy>
15 #include "Publisher.hh"
16 #include <boost/python.hpp>
18 //#include "Publisher.mpp"
20 ///////////////////////////////cc.p////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////////////
23 // pykit::detail::InternalServerReply
28 class InternalServerReply
29 : public QNetworkReply
33 InternalServerReply(QNetworkAccessManager::Operation operation,
34 QNetworkRequest const & networkRequest,
35 QIODevice * postData, Publisher * publisher);
37 // Forwarded to buffer
38 virtual bool atEnd() const;
39 virtual qint64 bytesAvailable() const;
40 virtual qint64 bytesToWrite() const;
41 virtual bool canReadLine() const;
42 virtual bool isSequential() const;
43 virtual qint64 pos() const;
45 virtual bool seek(qint64 pos);
46 virtual qint64 size() const;
47 virtual bool waitForBytesWritten(int msecs);
48 virtual bool waitForReadyRead(int msecs);
52 using QNetworkReply::setHeader;
55 QByteArray postData() const;
56 QString postContentType() const;
59 virtual qint64 readData(char * data, qint64 maxSize);
60 virtual qint64 writeData(char const * data, qint64 maxSize);
63 void bufferPostData();
64 void postDataReadable();
65 void postDataComplete();
66 void processRequest();
72 Publisher * publisher_;
73 QIODevice * postDevice_;
80 prefix_ pykit::detail::InternalServerReply::
81 InternalServerReply(QNetworkAccessManager::Operation operation,
82 QNetworkRequest const & networkRequest, QIODevice * postData,
83 Publisher * publisher)
84 : publisher_ (publisher), postDevice_ (postData)
86 setRequest(networkRequest);
87 setOperation(operation);
88 setUrl(networkRequest.url());
89 open(ReadWrite | Unbuffered);
90 buffer_.open(ReadWrite | Unbuffered);
93 QMetaObject::invokeMethod(this, "bufferPostData", Qt::QueuedConnection);
95 QMetaObject::invokeMethod(this, "processRequest", Qt::QueuedConnection);
98 prefix_ void pykit::detail::InternalServerReply::bufferPostData()
100 QVariant cl (request().header(QNetworkRequest::ContentLengthHeader));
101 connect(postDevice_, SIGNAL(readyRead()), this, SLOT(postDataReadable()));
102 connect(postDevice_, SIGNAL(readChannelFinished()), this, SLOT(postDataComplete()));
103 postData_.open(WriteOnly);
107 prefix_ bool pykit::detail::InternalServerReply::readPostData()
110 qint64 n (postDevice_->bytesAvailable());
115 n = postDevice_->read(data.data(), n);
120 postData_.write(data);
127 prefix_ void pykit::detail::InternalServerReply::postDataReadable()
129 if (readPostData()) {
130 disconnect(postDevice_, SIGNAL(readyRead()), this, SLOT(postDataReadable()));
131 disconnect(postDevice_, SIGNAL(readChannelFinished()), this, SLOT(postDataComplete()));
136 prefix_ void pykit::detail::InternalServerReply::postDataComplete()
139 disconnect(postDevice_, SIGNAL(readyRead()), this, SLOT(postDataReadable()));
140 disconnect(postDevice_, SIGNAL(readChannelFinished()), this, SLOT(postDataComplete()));
144 prefix_ void pykit::detail::InternalServerReply::processRequest()
146 Request request (*this);
147 publisher_->publish(request);
150 setHeader(QNetworkRequest::ContentLengthHeader, QVariant(size()));
152 QMetaObject::invokeMethod(this, "complete", Qt::QueuedConnection);
155 prefix_ void pykit::detail::InternalServerReply::complete()
158 emit readChannelFinished();
162 prefix_ bool pykit::detail::InternalServerReply::atEnd()
164 { return buffer_.atEnd(); }
166 prefix_ qint64 pykit::detail::InternalServerReply::bytesAvailable()
168 { return buffer_.bytesAvailable(); }
170 prefix_ qint64 pykit::detail::InternalServerReply::bytesToWrite()
172 { return buffer_.bytesToWrite(); }
174 prefix_ bool pykit::detail::InternalServerReply::canReadLine()
176 { return buffer_.canReadLine(); }
178 prefix_ bool pykit::detail::InternalServerReply::isSequential()
180 { return buffer_.isSequential(); }
182 prefix_ qint64 pykit::detail::InternalServerReply::pos()
184 { return buffer_.pos(); }
186 prefix_ bool pykit::detail::InternalServerReply::reset()
187 { return buffer_.reset(); }
189 prefix_ bool pykit::detail::InternalServerReply::seek(qint64 pos)
190 { return buffer_.seek(pos); }
192 prefix_ qint64 pykit::detail::InternalServerReply::size()
194 { return buffer_.size(); }
196 prefix_ bool pykit::detail::InternalServerReply::waitForBytesWritten(int msecs)
197 { return buffer_.waitForBytesWritten(msecs); }
199 prefix_ bool pykit::detail::InternalServerReply::waitForReadyRead(int msecs)
200 { return buffer_.waitForReadyRead(msecs); }
202 prefix_ qint64 pykit::detail::InternalServerReply::readData(char * data, qint64 maxSize)
203 { return buffer_.read(data,maxSize); }
205 prefix_ qint64 pykit::detail::InternalServerReply::writeData(char const * data, qint64 maxSize)
206 { return buffer_.write(data,maxSize); }
208 prefix_ void pykit::detail::InternalServerReply::abort()
211 prefix_ void pykit::detail::InternalServerReply::clearResponse()
213 buffer_.buffer().clear();
216 prefix_ QByteArray pykit::detail::InternalServerReply::postData()
219 return postData_.buffer();
222 prefix_ QString pykit::detail::InternalServerReply::postContentType()
225 return request().header(QNetworkRequest::ContentTypeHeader).toString();
228 ///////////////////////////////////////////////////////////////////////////
231 prefix_ void pykit::Request::write(std::string const & data)
233 reply_.write(QByteArray(data.data(),data.size()));
236 prefix_ void pykit::Request::reset()
238 reply_.clearResponse();
241 prefix_ void pykit::Request::setContentType(std::string const & contentType)
243 reply_.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType.c_str()));
246 prefix_ void pykit::Request::setLocation(std::string const & location)
248 reply_.setHeader(QNetworkRequest::LocationHeader, QVariant(location.c_str()));
251 prefix_ QUrl pykit::Request::url()
257 prefix_ QByteArray pykit::Request::postData()
260 return reply_.postData();
263 prefix_ QByteArray pykit::Request::operation()
266 static char const * const operationNames[]
267 = { "", "HEAD", "GET", "PUT", "POST", "DELETE", "" };
268 return operationNames[reply_.operation()];
271 prefix_ QString pykit::Request::postContentType()
274 return reply_.postContentType();
277 prefix_ pykit::Request::Request(detail::InternalServerReply & reply)
281 ///////////////////////////////////////////////////////////////////////////
282 // pykit::InternalNetworkAccessManager
284 prefix_ pykit::InternalNetworkAccessManager::
285 InternalNetworkAccessManager(QNetworkAccessManager * manager, QObject * parent,
286 Publisher * publisher)
287 : QNetworkAccessManager(parent), publisher_ (publisher)
289 setCache(manager->cache());
290 setCookieJar(manager->cookieJar());
291 setProxy(manager->proxy());
292 setProxyFactory(manager->proxyFactory());
295 prefix_ QNetworkReply *
296 pykit::InternalNetworkAccessManager::createRequest(Operation operation,
297 QNetworkRequest const & request,
300 if (request.url().host() != "pykit")
301 return QNetworkAccessManager::createRequest(operation, request, device);
302 return new detail::InternalServerReply(operation, request, device, publisher_);
305 #include "Publisher.moc"
307 ///////////////////////////////cc.e////////////////////////////////////////
309 //#include "Publisher.mpp"
315 // comment-column: 40
316 // c-file-style: "j32"
317 // indent-tabs-mode: nil
318 // ispell-local-dictionary: "american"
319 // compile-command: "scons -U"