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 std::cerr << "postdata contentlength: " << (cl.isValid() ? cl.toString().toStdString() : "invalid") << "\n";
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_ QUrl pykit::Request::url()
258 prefix_ QByteArray pykit::Request::postData()
261 return reply_.postData();
264 prefix_ QByteArray pykit::Request::operation()
267 static char const * const operationNames[]
268 = { "", "HEAD", "GET", "PUT", "POST", "DELETE", "" };
269 return operationNames[reply_.operation()];
272 prefix_ QString pykit::Request::postContentType()
275 return reply_.postContentType();
278 prefix_ pykit::Request::Request(detail::InternalServerReply & reply)
282 ///////////////////////////////////////////////////////////////////////////
283 // pykit::InternalNetworkAccessManager
285 prefix_ pykit::InternalNetworkAccessManager::
286 InternalNetworkAccessManager(QNetworkAccessManager * manager, QObject * parent,
287 Publisher * publisher)
288 : QNetworkAccessManager(parent), publisher_ (publisher)
290 setCache(manager->cache());
291 setCookieJar(manager->cookieJar());
292 setProxy(manager->proxy());
293 setProxyFactory(manager->proxyFactory());
296 prefix_ QNetworkReply *
297 pykit::InternalNetworkAccessManager::createRequest(Operation operation,
298 QNetworkRequest const & request,
301 if (request.url().scheme() != "srv")
302 return QNetworkAccessManager::createRequest(operation, request, device);
304 return new detail::InternalServerReply(operation, request, device, publisher_);
307 #include "Publisher.moc"
309 ///////////////////////////////cc.e////////////////////////////////////////
311 //#include "Publisher.mpp"
317 // comment-column: 40
318 // c-file-style: "j32"
319 // indent-tabs-mode: nil
320 // ispell-local-dictionary: "american"
321 // compile-command: "scons -U"