Reading HTTP headers - qt

I am trying to connect my application with a web service and here ,a user suggested to send custom headers back to my application.
I am using this code
void Coonnec::serviceRequestFinished(QNetworkReply *reply)
{
QByteArray bytes = reply->readAll();
if (reply->error() != QNetworkReply::NoError) {
qDebug() << "Reply error: " + reply->errorString();
}
else
{
qDebug() << "Uploaded: " + QDateTime::currentDateTime().toString();
qDebug() << reply->rawHeaderList();
}
reply->close();
bytes.clear();
reply->deleteLater();
}
from php i send this header
header('XAppRequest-Status: complete');
When running the application i can see that i get this header but i can't take the value of it cause
reply->rawHeader(bytes);
returns nothing.
How can i take the value 'complete'?

I suggest to connect a slot to the void QNetworkReply::metaDataChanged () signal of your reply.
The Qt doc says
This signal is emitted whenever the metadata in this reply changes.
metadata is any information that is not the content (data) itself,
including the network headers. In the majority of cases, the metadata
will be known fully by the time the first byte of data is received.
However, it is possible to receive updates of headers or other
metadata during the processing of the data.
I do use web-services/client with Qt and I noticed that some header's information are not available when I expected it to be ! I had to 'wait' for this signal to check the header content.

Related

Cross origin error with QNetwork webassembly

I am trying to make a post/get call using QNetworkAccessManager in my c++ code which is exposed to webassembly using emscripten.
When the exposed function is compiled and loaded in Javascript, the requests are getting blocked due to cross-origin request.
I am running the local server from the folder that contains wasm, js and html files.
[Edited]: After some reading on CORS, I tried by adding additional header parameters to request:
request.setRawHeader("Access-Control-Request-Origin", "*");
But this also didn't work.
QNetworkRequest request(this->server);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader("Access-Control-Allow-Origin", "*"); request.setRawHeader("Access-Control-Allow-Methods", "POST");
// building the json and network request object
QByteArray byte_payload = QJsonDocument(payload).toJson();
QEventLoop loop;
QObject::connect(&manager, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit);
loop.exec();
std::cout << "Request completed" << std::endl;
QByteArray reponse = reply->readAll();
std::cout << reponse.toStdString() << std:: endl;
Could you let me know how to resolve this?
[Edited]:
I tried to enable the node server for cros by using cors package. This enables the first OPTIONS call to succeed but the actual POST call that happens is send with incorrect payload.

Sending TCP data without recieving (boost asio)

I'm working my way through boost's asio tutorial. I'm looking into their chat example. More specifically, I'm trying to split their chat client from a sender+receiver, to just a sender and just a receiver, but I'm seeing some behaviour that I can't explain.
The setup consists of:
boost::asio::io_service io_service;
tcp::resolver::iterator endpoint = resolver.resolve(...);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service));
boost::asio::async_connect(socket, endpoint, bind(handle_connect, ... ));
The sending portion effectively conisists of:
while (std::cin.getline(str))
io_service.post( do_write, str );
and
void do_write (string str)
{
boost::asio::async_write(socket, str, bind( handle_write, ... ));
}
The receive section consists of
void handle_connect(...)
{
boost::asio::async_read(socket, read_msg_, bind(handle_read, ...));
}
void handle_read(...)
{
std::cout << read_msg_;
boost::asio::async_read(socket, read_msg_, bind(handle_read, ...));
}
If I comment out the content of handle_connect to isolate the send portion, my other client (compiled using the original code) does not receive anything. If I revert, then comment out the content of handle_read, my other client only receives the first message.
Why is it necessary to call async_read() in order to be able to post() an async_write()?
The full unmodified code is linked above.
The problem here is that, your io_service is running out of work and stops processing requests even before you start sending your chat messages.
If you comment out the body of handle_connect, then the only work it had to do was to dispatch the handle_connect handler and then execute it once the connection was done.
std::size_t scheduler::run(asio::error_code& ec)
{
.....
mutex::scoped_lock lock(mutex_);
std::size_t n = 0;
for (; do_run_one(lock, this_thread, ec); lock.lock())
if (n != (std::numeric_limits<std::size_t>::max)())
++n;
return n;
}
So, you have to provide it with something in it's operation queue. This was done with handle_read_header handler in the original code as this handler would always be in the need of servicing till the client gets something from the server.
You can do what you want to do by providing work to the io_service.
asio::io_context io_context;
asio::io_context::work wrk(io_context); // make `run` run forever
tcp::resolver resolver(io_context);
tcp::resolver::results_type endpoints = resolver.resolve(argv[1], argv[2]);
chat_client c(io_context, endpoints);
asio::thread t(boost::bind(&asio::io_context::run, &io_context));

QNetworkAccesssManager icinga / nagios acknowledge

I have a function who sending ack into the icinga / nagios server.
Function - sendAcknowledge
void MNetworkConnector::sendAcknowledge(QString service, QString host)
{
QNetworkAccessManager *ackmanager;
ackmanager = new QNetworkAccessManager();
QString ackcommand = "http://nagioscore.demos.nagios.com/nagios/cgi-bin/cmd.cgi?cmd_typ=34&cmd_mod=2&host=#host#&service=#service#&com_author=nagiosadmin&com_data=Sent:+mMonitor&btnSubmit=Commit";
service = service.replace(" ", "+");
ackcommand = ackcommand.replace("#host#", host).replace("#service#", service);
connect(ackmanager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished2(QNetworkReply*)));
QNetworkRequest myReq;
QUrl myUrl(ackcommand);
myUrl.setUserName("nagiosadmin");
myUrl.setPassword("nagiosadmin");
myReq.setUrl(myUrl);
myReq.setRawHeader("Referer", "http://nagioscore.demos.nagios.com/nagios/cgi-bin/cmd.cgi");
ackmanager->get(myReq);
}
Function - replyFinished2
void MNetworkConnector::replyFinished2(QNetworkReply *r)
{
qDebug() << "reply output:" << r->readAll();
}
System returned several errors.
If I test this source on my corporate icinga server, I have error:
Not all commands could be send off successfully - Not Authorized
If I test it manualy on my corporate icinga server, I have error: Error: This appears to be a CSRF attack! The command wasn't issued via Classic-UI itself!
If I test it manualy on nagios test site, I have no error. Set service ack is Ok.
If I test this source I have error:Sorry, but you are not authorized to commit the specified command.
What's wrong? Thank you for all your ideas. At first I need solved problem in Icinga, the nagios test page I used only as alternative tests.
When the website requests authentication QNetworkAccessManager will emit the authenticationRequired() signal. Try connecting the signal with a slot and then set the username and password on the QAuthenticator object passed as argument to the slot.
Connection:
connect(ackmanager, SIGNAL(authenticationRequired(QNetworkReply *, QAuthenticator *)), this, SLOT(authenticationRequired(QNetworkReply *, QAuthenticator *)));
Slot:
void MNetworkConnector::authenticationRequired(QNetworkReply *r, QAuthenticator *authenticator)
{
authenticator->setUser("nagiosadmin");
authenticator->setPassword("nagiosadmin");
}
The problem was sensitive to uppercase and lowercase letters in the HOST.

How to use post() in qt?

This is my program. In this program I want to send request to a website (for example: http://www.adobe.com/products/muse.html)
I want to show the html code that return me in plain text box.
QUrl url("http://www.adobe.com/products/muse.html")
I want to give html code in "thisfile"
file.setFileName("thisfile.html");
if (!file.open(QIODevice::WriteOnly))
{
std::cerr << "Error: Cannot write file "
<< qPrintable(file.fileName()) << ": "
<< qPrintable(file.errorString()) << std::endl
return false;
}
http.setHost(url.host(),80);
http.post(url.toString(),"term=yyyy&loc=en_us&siteSection=products%3Amuse",&file);
This code doesn't work correctly and when I show the file give me false html code. What do I have to do?
Use http.get() instead of http.post() as POST method requires to set other Headers used by server.
QHttp::get() method is asynchronous too.
As your case is simple enough just to retrieve HTML response, you should go for HTTP GET IMHO. See difference between GET and POST method.
And if you have to use HTTP POST only, then check this.
QNetworkRequest request;
request.setUrl(QUrl("thisfile.html"));
QNetworkReply *reply = manager->post(request, "term=yyyy&loc=en_us&siteSection=products%3Amuse");
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
Look at QNetworkAccessManager at qt docs
You have to read information and save it to file at readyRead function

Persistent Qt Local Socket IPC

I'm developing an application that uses IPC between a local server and a client application. There is nothing particular to it, as it's structured like the Qt documentation and examples.
The problem is that the client sends packets frequently and connecting/disconnecting from the server local socket (named pipe on NT) is very slow. So what I'm trying to achieve is a "persistent" connection between the two applications.
The client application connects to the local server (QLocalServer) without any problem:
void IRtsClientImpl::ConnectToServer(const QString& name)
{
connect(_socket, SIGNAL(connected()), this, SIGNAL(connected()));
_blockSize = 0;
_socket->abort();
_socket->connectToServer(name, QIODevice::ReadWrite);
}
And sends requests also in the traditional Qt manner:
void IRtsClientImpl::SendRequest( quint8 cmd, const QVariant* const param_array,
unsigned int cParams )
{
// Send data through socket
QByteArray hdr(PROTO_BLK_HEADER_PROJ);
QByteArray dataBlock;
QDataStream out(&dataBlock, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_5);
quint8 command = cmd;
out << blocksize_t(0) // block size
<< hdr // header
<< quint32(PROTO_VERSION_PROJ) // protocol version
<< command // command
<< cParams; // number of valid parameters
for (unsigned int i = 0; i < cParams; ++i)
out << param_array[i];
// Write the current block size
out.device()->seek(0);
out << dataBlock.size() - sizeof(blocksize_t);
_socket->write(dataBlock);
}
No problem. But the trick resides on the readyRead() signal in the server-side. Here's the current implementation of the readyRead() handling slot:
void IRtsServerImpl::onReadyRead()
{
QDataStream in(_lsock);
in.setVersion(QDataStream::Qt_4_5);
if (_blocksize == 0)
{
qDebug("Bytes Available on socket: %d", _lsock->bytesAvailable());
if (_lsock->bytesAvailable() < sizeof(blocksize_t))
return;
in >> _blocksize;
}
// We need more data?
if (_lsock->bytesAvailable() < _blocksize)
return;
ReadRequest(in);
// Reset
_blocksize = 0;
}
Without setting _blocksize to zero I could not receive more data, only the first block group (I would expect an entire block to arrive without segmentation since this is through a pipe, but it does not, go figure). I expect that behavior, sure, since the _blocksize does not represent the current stream flow anymore. All right, resetting _blocksize does the trick, but I can't resend another packet from the client without getting an increasing array of bytes on the socket. What I want is to process the request in ReadRequest and receive the next data blocks without resorting to connecting/reconnecting the applications involved.
Maybe I should 'regulate' the rate of the incoming data?
Thank you very much.

Resources