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;
53 using QNetworkReply::setRawHeader;
56 QByteArray postData() const;
57 QString postContentType() const;
60 virtual qint64 readData(char * data, qint64 maxSize);
61 virtual qint64 writeData(char const * data, qint64 maxSize);
64 void bufferPostData();
65 void postDataReadable();
66 void postDataComplete();
67 void processRequest();
73 Publisher * publisher_;
74 QIODevice * postDevice_;
81 prefix_ pykit::detail::InternalServerReply::
82 InternalServerReply(QNetworkAccessManager::Operation operation,
83 QNetworkRequest const & networkRequest, QIODevice * postData,
84 Publisher * publisher)
85 : publisher_ (publisher), postDevice_ (postData)
87 setRequest(networkRequest);
88 setOperation(operation);
89 setUrl(networkRequest.url());
90 open(ReadWrite | Unbuffered);
91 buffer_.open(ReadWrite | Unbuffered);
94 QMetaObject::invokeMethod(this, "bufferPostData", Qt::QueuedConnection);
96 QMetaObject::invokeMethod(this, "processRequest", Qt::QueuedConnection);
99 prefix_ void pykit::detail::InternalServerReply::bufferPostData()
101 QVariant cl (request().header(QNetworkRequest::ContentLengthHeader));
102 connect(postDevice_, SIGNAL(readyRead()), this, SLOT(postDataReadable()));
103 connect(postDevice_, SIGNAL(readChannelFinished()), this, SLOT(postDataComplete()));
104 postData_.open(WriteOnly);
108 prefix_ bool pykit::detail::InternalServerReply::readPostData()
111 qint64 n (postDevice_->bytesAvailable());
116 n = postDevice_->read(data.data(), n);
121 postData_.write(data);
128 prefix_ void pykit::detail::InternalServerReply::postDataReadable()
130 if (readPostData()) {
131 disconnect(postDevice_, SIGNAL(readyRead()), this, SLOT(postDataReadable()));
132 disconnect(postDevice_, SIGNAL(readChannelFinished()), this, SLOT(postDataComplete()));
137 prefix_ void pykit::detail::InternalServerReply::postDataComplete()
140 disconnect(postDevice_, SIGNAL(readyRead()), this, SLOT(postDataReadable()));
141 disconnect(postDevice_, SIGNAL(readChannelFinished()), this, SLOT(postDataComplete()));
145 prefix_ void pykit::detail::InternalServerReply::processRequest()
147 Request request (*this);
148 publisher_->publish(request);
151 setHeader(QNetworkRequest::ContentLengthHeader, QVariant(size()));
153 QMetaObject::invokeMethod(this, "complete", Qt::QueuedConnection);
156 prefix_ void pykit::detail::InternalServerReply::complete()
159 emit readChannelFinished();
163 prefix_ bool pykit::detail::InternalServerReply::atEnd()
165 { return buffer_.atEnd(); }
167 prefix_ qint64 pykit::detail::InternalServerReply::bytesAvailable()
169 { return buffer_.bytesAvailable(); }
171 prefix_ qint64 pykit::detail::InternalServerReply::bytesToWrite()
173 { return buffer_.bytesToWrite(); }
175 prefix_ bool pykit::detail::InternalServerReply::canReadLine()
177 { return buffer_.canReadLine(); }
179 prefix_ bool pykit::detail::InternalServerReply::isSequential()
181 { return buffer_.isSequential(); }
183 prefix_ qint64 pykit::detail::InternalServerReply::pos()
185 { return buffer_.pos(); }
187 prefix_ bool pykit::detail::InternalServerReply::reset()
188 { return buffer_.reset(); }
190 prefix_ bool pykit::detail::InternalServerReply::seek(qint64 pos)
191 { return buffer_.seek(pos); }
193 prefix_ qint64 pykit::detail::InternalServerReply::size()
195 { return buffer_.size(); }
197 prefix_ bool pykit::detail::InternalServerReply::waitForBytesWritten(int msecs)
198 { return buffer_.waitForBytesWritten(msecs); }
200 prefix_ bool pykit::detail::InternalServerReply::waitForReadyRead(int msecs)
201 { return buffer_.waitForReadyRead(msecs); }
203 prefix_ qint64 pykit::detail::InternalServerReply::readData(char * data, qint64 maxSize)
204 { return buffer_.read(data,maxSize); }
206 prefix_ qint64 pykit::detail::InternalServerReply::writeData(char const * data, qint64 maxSize)
207 { return buffer_.write(data,maxSize); }
209 prefix_ void pykit::detail::InternalServerReply::abort()
212 prefix_ void pykit::detail::InternalServerReply::clearResponse()
214 buffer_.buffer().clear();
217 prefix_ QByteArray pykit::detail::InternalServerReply::postData()
220 return postData_.buffer();
223 prefix_ QString pykit::detail::InternalServerReply::postContentType()
226 return request().header(QNetworkRequest::ContentTypeHeader).toString();
229 ///////////////////////////////////////////////////////////////////////////
232 prefix_ void pykit::Request::write(std::string const & data)
234 reply_.write(QByteArray(data.data(),data.size()));
237 prefix_ void pykit::Request::reset()
239 reply_.clearResponse();
242 prefix_ void pykit::Request::setContentType(std::string const & contentType)
244 reply_.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(contentType.c_str()));
247 prefix_ void pykit::Request::setLocation(std::string const & location)
249 reply_.setHeader(QNetworkRequest::LocationHeader, QVariant(location.c_str()));
252 prefix_ void pykit::Request::setHeader(std::string const & header, std::string const & value)
254 reply_.setRawHeader(header.c_str(), value.c_str());
257 prefix_ QUrl pykit::Request::url()
263 prefix_ QByteArray pykit::Request::postData()
266 return reply_.postData();
269 prefix_ QByteArray pykit::Request::operation()
272 static char const * const operationNames[]
273 = { "", "HEAD", "GET", "PUT", "POST", "DELETE", "" };
274 return operationNames[reply_.operation()];
277 prefix_ QString pykit::Request::postContentType()
280 return reply_.postContentType();
283 prefix_ pykit::Request::Request(detail::InternalServerReply & reply)
287 ///////////////////////////////////////////////////////////////////////////
288 // pykit::InternalNetworkAccessManager
290 prefix_ pykit::InternalNetworkAccessManager::
291 InternalNetworkAccessManager(QNetworkAccessManager * manager, QObject * parent,
292 Publisher * publisher)
293 : QNetworkAccessManager(parent), publisher_ (publisher)
295 setCache(manager->cache());
296 setCookieJar(manager->cookieJar());
297 setProxy(manager->proxy());
298 setProxyFactory(manager->proxyFactory());
301 prefix_ QNetworkReply *
302 pykit::InternalNetworkAccessManager::createRequest(Operation operation,
303 QNetworkRequest const & request,
306 if (request.url().host() != "pykit")
307 return QNetworkAccessManager::createRequest(operation, request, device);
308 return new detail::InternalServerReply(operation, request, device, publisher_);
311 #include "Publisher.moc"
313 ///////////////////////////////cc.e////////////////////////////////////////
315 //#include "Publisher.mpp"
321 // comment-column: 40
322 // c-file-style: "j32"
323 // indent-tabs-mode: nil
324 // ispell-local-dictionary: "american"
325 // compile-command: "scons -U"