35 m_requestStarted(false),
36 m_headersComplete(false),
40 m_method(QStringLiteral()),
51 m_content = QByteArray();
52 m_headers = QMap<QString,QString>();
67 return m_headersComplete;
74 m_requestStarted =
false;
75 m_headersComplete =
false;
78 m_contentReceived = 0;
79 m_method = QStringLiteral();
80 m_content = QByteArray();
81 m_headers = QMap<QString,QString>();
94 if (Socket->state() != QAbstractSocket::ConnectedState)
98 if (m_headersRead >= 200)
100 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Read 200 lines of headers - aborting"));
105 if (!m_headersComplete)
108 if (!m_requestStarted && Socket->bytesAvailable() >= 7 )
110 QByteArray buf(7,
' ');
111 (void)Socket->peek(buf.data(), 7);
112 if (!buf.startsWith(
"HTTP"))
113 if (!buf.startsWith(
"GET"))
114 if (!buf.startsWith(
"PUT"))
115 if(!buf.startsWith(
"POST"))
116 if(!buf.startsWith(
"OPTIONS"))
117 if (!buf.startsWith(
"HEAD"))
118 if (!buf.startsWith(
"DELETE"))
120 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Invalid HTTP start ('%1')- aborting").arg(buf.constData()));
125 while (Socket->canReadLine() && m_headersRead < 200)
127 QByteArray line = Socket->readLine().trimmed();
130 if (line.size() > 1000)
132 LOG(VB_GENERAL, LOG_ERR, QStringLiteral(
"Header is too long - aborting"));
139 m_headersComplete =
true;
143 if (!m_requestStarted)
145 LOG(VB_NETWORK, LOG_DEBUG, QString(line));
151 int index = line.indexOf(
":");
155 QByteArray key = line.left(index).trimmed();
156 QByteArray value = line.mid(index + 1).trimmed();
158 if (key ==
"Content-Length")
159 m_contentLength = value.toULongLong();
161 LOG(VB_NETWORK, LOG_DEBUG, QStringLiteral(
"%1: %2").arg(key.data(), value.data()));
163 m_headers.insert(key, value);
167 m_requestStarted =
true;
172 if (!m_headersComplete)
176 if (Socket->state() != QAbstractSocket::ConnectedState)
180 while ((m_contentReceived < m_contentLength) && Socket->bytesAvailable() &&
181 Socket->state() == QAbstractSocket::ConnectedState)
183 static quint64 MAX_CHUNK = 32 * 1024;
184 m_content.append(Socket->read(qMax(m_contentLength - m_contentReceived, qMax(MAX_CHUNK, (quint64)Socket->bytesAvailable()))));
185 m_contentReceived = m_content.size();
189 if (m_contentReceived < m_contentLength)
193 if (Socket->state() != QAbstractSocket::ConnectedState)
QString GetMethod(void) const
bool Read(QTcpSocket *Socket)
Read and parse data from the given socket.
bool HeadersComplete(void) const
#define LOG(_MASK_, _LEVEL_, _STRING_)
void TakeRequest(QByteArray &Content, QMap< QString, QString > &Headers)
Take ownership of the contents and headers. New owner is responsible for deleting.
void Reset(void)
Reset the read state.