Skip to content

Commit

Permalink
Quit the proxy when reading zero or less from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu authored and droidmonkey committed Aug 20, 2018
1 parent 8db604e commit 1d80bdd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/browser/NativeMessagingBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected slots:

protected:
virtual void readLength() = 0;
virtual void readStdIn(const quint32 length) = 0;
virtual bool readStdIn(const quint32 length) = 0;
void readNativeMessages();
QString jsonToString(const QJsonObject& json) const;
void sendReply(const QJsonObject& json);
Expand Down
7 changes: 4 additions & 3 deletions src/browser/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ void NativeMessagingHost::readLength()
}
}

void NativeMessagingHost::readStdIn(const quint32 length)
bool NativeMessagingHost::readStdIn(const quint32 length)
{
if (length <= 0) {
return;
return false;
}

QByteArray arr;
Expand All @@ -129,14 +129,15 @@ void NativeMessagingHost::readStdIn(const quint32 length)
int c = std::getchar();
if (c == EOF) {
// message ended prematurely, ignore it and return
return;
return false;
}
arr.append(static_cast<char>(c));
}

if (arr.length() > 0) {
sendReply(m_browserClients.readResponse(arr));
}
return true;
}

void NativeMessagingHost::newLocalConnection()
Expand Down
2 changes: 1 addition & 1 deletion src/browser/NativeMessagingHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public slots:

private:
void readLength();
void readStdIn(const quint32 length);
bool readStdIn(const quint32 length);
void sendReplyToAllClients(const QJsonObject& json);

private slots:
Expand Down
27 changes: 22 additions & 5 deletions src/proxy/NativeMessagingHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,36 @@ NativeMessagingHost::~NativeMessagingHost()
#endif
}

void NativeMessagingHost::readNativeMessages()
{
#ifdef Q_OS_WIN
quint32 length = 0;
while (m_running.load() && !std::cin.eof()) {
length = 0;
std::cin.read(reinterpret_cast<char*>(&length), 4);
if (!readStdIn(length)) {
QCoreApplication::quit();
}
QThread::msleep(1);
}
#endif
}

void NativeMessagingHost::readLength()
{
quint32 length = 0;
std::cin.read(reinterpret_cast<char*>(&length), 4);
if (!std::cin.eof() && length > 0) {
readStdIn(length);
} else {
QCoreApplication::quit();
QCoreApplication::quit();
}
}

void NativeMessagingHost::readStdIn(const quint32 length)
bool NativeMessagingHost::readStdIn(const quint32 length)
{
if (length <= 0) {
return;
return false;
}

QByteArray arr;
Expand All @@ -73,7 +88,7 @@ void NativeMessagingHost::readStdIn(const quint32 length)
int c = std::getchar();
if (c == EOF) {
// message ended prematurely, ignore it and return
return;
return false;
}
arr.append(static_cast<char>(c));
}
Expand All @@ -82,6 +97,8 @@ void NativeMessagingHost::readStdIn(const quint32 length)
m_localSocket->write(arr.constData(), arr.length());
m_localSocket->flush();
}

return true;
}

void NativeMessagingHost::newLocalMessage()
Expand All @@ -92,7 +109,7 @@ void NativeMessagingHost::newLocalMessage()

QByteArray arr = m_localSocket->readAll();
if (!arr.isEmpty()) {
sendReply(arr);
sendReply(arr);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/proxy/NativeMessagingHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public slots:
void socketStateChanged(QLocalSocket::LocalSocketState socketState);

private:
void readNativeMessages();
void readLength();
void readStdIn(const quint32 length);
bool readStdIn(const quint32 length);

private:
QLocalSocket* m_localSocket;
QLocalSocket* m_localSocket;
};

#endif // NATIVEMESSAGINGHOST_H

0 comments on commit 1d80bdd

Please sign in to comment.