Debian Codegraph @

Source/Network/HTTP/CQtHttpRequestBodyOnlyParser.cpp

raw
1 /*
2 * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team.
3 *
4 * This file is part of the reasoning system Konclude.
5 * For details and support, see <http://konclude.com/>.
6 *
7 * Konclude is free software: you can redistribute it and/or modify
8 * it under the terms of version 3 of the GNU Lesser General Public
9 * License (LGPLv3) as published by the Free Software Foundation.
10 *
11 * Konclude is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU (Lesser) General Public License for more details.
15 *
16 * You should have received a copy of the GNU (Lesser) General Public
17 * License along with Konclude. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 #include "CQtHttpRequestBodyOnlyParser.h"
22
23
24 namespace Konclude {
25
26 namespace Network {
27
28 namespace HTTP {
29
30 CQtHttpRequestBodyOnlyParser::CQtHttpRequestBodyOnlyParser() {
31 mHeaderLineReadBufferSize = 20000;
32 // maximum header size 10 KByte
33 mMaxHeaderDataSize = 1024*10;
34 mHeaderLineReadBuffer = new char[mHeaderLineReadBufferSize+1];
35 mHeaderLineReadBuffer[mHeaderLineReadBufferSize] = '\0';
36 // maximum body size 2 GByte
37 mMaxBodyDataSize = Q_INT64_C(1024*1024*1024*2);
38 mBodyData = nullptr;
39
40 mHeaderContentLengthString = QString("Content-Length").toUpper();
41 mHeaderConnectionString = QString("Connection").toUpper();
42 mHeaderConnectionCloseString = QString("close").toUpper();
43
44 reset();
45 }
46
47 CQtHttpRequestBodyOnlyParser::~CQtHttpRequestBodyOnlyParser() {
48 delete mBodyData;
49 }
50
51 CQtHttpRequestParser* CQtHttpRequestBodyOnlyParser::reset() {
52 mHeaderLineReadBufferPosition = 0;
53 mReadBodyDataSize = 0;
54 mErrorString.clear();
55 mHeaderData.clear();
56 delete mBodyData;
57 mBodyData = nullptr;
58 mRequestError = false;
59 mRequestCompleted = false;
60 mHeaderCompleted = false;
61 mHeaderMethodProtocolURIParsed = false;
62 mAfterwardsCloseConnection = false;
63 mReadHeaderDataSize = 0;
64 mExpectedBodyLength = 0;
65 return this;
66 }
67
68
69 QByteArray* CQtHttpRequestBodyOnlyParser::takeBodyData() {
70 QByteArray* bodyData = mBodyData;
71 mBodyData = nullptr;
72 return bodyData;
73 }
74
75
76 bool CQtHttpRequestBodyOnlyParser::readFromDevice(QIODevice* readDevice) {
77 cint64 availableBytesCount = readDevice->bytesAvailable();
78 while (availableBytesCount > 1 && !mRequestError && !mRequestCompleted) {
79 if (!mHeaderCompleted) {
80 if (mHeaderLineReadBufferSize - mHeaderLineReadBufferPosition <= 0) {
81 mRequestError = true;
82 mErrorString = QString("Line in header is longer than allowed, '%1' is longer than %2").arg(QString::fromUtf8(mHeaderLineReadBuffer)).arg(mHeaderLineReadBufferSize);
83 } else if (mMaxHeaderDataSize - mReadHeaderDataSize <= 0) {
84 mRequestError = true;
85 mErrorString = QString("Header is longer than allowed");
86 } else {
87 cint64 maxToReadLine = mHeaderLineReadBufferSize - mHeaderLineReadBufferPosition + 1;
88 bool lineCompleted = false;
89 cint64 maxToAvailableRead = qMin(readDevice->bytesAvailable(),maxToReadLine);
90 maxToAvailableRead = qMin(maxToAvailableRead,mMaxHeaderDataSize-mReadHeaderDataSize+1);
91 cint64 read = readDevice->readLine(&mHeaderLineReadBuffer[mHeaderLineReadBufferPosition],maxToAvailableRead);
92 cint64 newPos = mHeaderLineReadBufferPosition+read;
93 if (mHeaderLineReadBuffer[newPos-1] == '\n') {
94 lineCompleted = true;
95 }
96 //if (newPos == 1 && mHeaderLineReadBuffer[newPos-1] == 13) {
97 // lineCompleted = true;
98 // if (mExpectedBodyLength == 0) {
99 // mRequestCompleted = true;
100 // }
101 //}
102 mHeaderLineReadBufferPosition = newPos;
103 mReadHeaderDataSize += mHeaderLineReadBufferPosition;
104 if (lineCompleted) {
105 if (!mHeaderMethodProtocolURIParsed) {
106 mHeaderMethodProtocolURIParsed = true;
107 parseHttpMethodProtocolURI();
108 } else {
109 parseHttpRequestHeader();
110 }
111 mHeaderLineReadBufferPosition = 0;
112 }
113 }
114 } else {
115 if (mExpectedBodyLength <= 0) {
116 mRequestCompleted = true;
117 } else {
118 if (mExpectedBodyLength > mMaxBodyDataSize) {
119 mRequestError = true;
120 mErrorString = QString("Expected body '%1' is longer than allowed '%2'").arg(mExpectedBodyLength).arg(mMaxBodyDataSize);
121 } else {
122 if (mBodyData == nullptr) {
123 mBodyData = new QByteArray();
124 mBodyData->resize(mExpectedBodyLength);
125 }
126 cint64 maxReadBodyData = mExpectedBodyLength - mReadBodyDataSize;
127 maxReadBodyData = qMin(maxReadBodyData,mHeaderLineReadBufferSize);
128 qint64 read = readDevice->read(mHeaderLineReadBuffer,maxReadBodyData);
129 mBodyData->replace(mReadBodyDataSize,read,mHeaderLineReadBuffer, read);
130 mReadBodyDataSize += read;
131
132 if (mReadBodyDataSize >= mExpectedBodyLength) {
133 mRequestCompleted = true;
134 }
135 }
136 }
137 }
138 availableBytesCount = readDevice->bytesAvailable();
139 }
140 return !mRequestError;
141 }
142
143 cint64 CQtHttpRequestBodyOnlyParser::getNextCharacterPosition(char character, cint64 searchStartPos) {
144 for (cint64 i = searchStartPos; i < mHeaderLineReadBufferSize; ++i) {
145 if (mHeaderLineReadBuffer[i] == character) {
146 return i;
147 }
148 }
149 return mHeaderLineReadBufferSize;
150 }
151
152 void CQtHttpRequestBodyOnlyParser::parseHttpMethodProtocolURI() {
153 cint64 currSpacePos = 0;
154 cint64 nextSpacePos = getNextCharacterPosition(' ',currSpacePos);
155
156 bool methodProtocolURIParseError = false;
157
158 if (nextSpacePos == mHeaderLineReadBufferSize) {
159 methodProtocolURIParseError = true;
160 } else {
161 mRequestMethod = QString::fromUtf8(&mHeaderLineReadBuffer[currSpacePos],nextSpacePos-currSpacePos);
162 currSpacePos = nextSpacePos+1;
163
164 nextSpacePos = getNextCharacterPosition(' ',currSpacePos);
165 if (nextSpacePos == mHeaderLineReadBufferSize) {
166 methodProtocolURIParseError = true;
167 } else {
168 mRequestURIString = QString::fromUtf8(&mHeaderLineReadBuffer[currSpacePos],nextSpacePos-currSpacePos);
169 currSpacePos = nextSpacePos+1;
170
171 nextSpacePos = mHeaderLineReadBufferPosition-2;
172 mRequestProtocol = QString::fromUtf8(&mHeaderLineReadBuffer[currSpacePos],nextSpacePos-currSpacePos);
173 }
174 }
175
176 if (methodProtocolURIParseError) {
177 mRequestError = true;
178 mErrorString = QString("Could not parse method, protocol and uri from request '%1'").arg(QString::fromUtf8(mHeaderLineReadBuffer));
179 }
180 }
181
182 void CQtHttpRequestBodyOnlyParser::parseHttpRequestHeader() {
183 if (mHeaderLineReadBufferPosition == 2) {
184 // empty new line, end of header
185 mHeaderCompleted = true;
186 } else {
187 cint64 doublePointPos = getNextCharacterPosition(':',0);
188 QString headerKey = QString::fromUtf8(mHeaderLineReadBuffer,doublePointPos);
189 if (QString::compare(headerKey.trimmed(),mHeaderContentLengthString,Qt::CaseInsensitive) == 0) {
190 QString contentLengthValueString = QString::fromUtf8(&mHeaderLineReadBuffer[doublePointPos+1],mHeaderLineReadBufferPosition-3-doublePointPos);
191 bool successfulParsedExpectedContentLength = false;
192 QString trimmedContentLengthValueString = contentLengthValueString.trimmed();
193 mExpectedBodyLength = trimmedContentLengthValueString.toLong(&successfulParsedExpectedContentLength);
194 if (!successfulParsedExpectedContentLength) {
195 mRequestError = true;
196 mErrorString = QString("Could not parse content length value from '%1'").arg(QString::fromUtf8(mHeaderLineReadBuffer));
197 }
198 } else {
199 if (QString::compare(headerKey.trimmed(),mHeaderConnectionString,Qt::CaseInsensitive) == 0) {
200 QString connectionValueString = QString::fromUtf8(&mHeaderLineReadBuffer[doublePointPos+1],mHeaderLineReadBufferPosition-3-doublePointPos);
201 if (QString::compare(connectionValueString,mHeaderConnectionString,Qt::CaseInsensitive) == 0) {
202 mAfterwardsCloseConnection = true;
203 }
204 }
205 }
206 }
207 }
208
209
210 CHttpRequest* CQtHttpRequestBodyOnlyParser::takeParsedHttpRequest() {
211 CQtHttpRequest* request = new CQtHttpRequest(mRequestURIString,mBodyData);
212 return request;
213 }
214
215 bool CQtHttpRequestBodyOnlyParser::hasHttpRequestParsingError() {
216 return mRequestError;
217 }
218
219 bool CQtHttpRequestBodyOnlyParser::hasHttpRequestParsingCompleted() {
220 return mRequestCompleted;
221 }
222
223 QString CQtHttpRequestBodyOnlyParser::getHttpRequestParsingErrorString() {
224 return mErrorString;
225 }
226
227 bool CQtHttpRequestBodyOnlyParser::hasRequestedCloseConnection() {
228 return mAfterwardsCloseConnection;
229 }
230
231
232 }; // end namespace HTTP
233
234 }; // end namespace Network
235
236 }; // end namespace Konclude
237