Debian Codegraph @

Source/Control/Interface/SPARQL/CSPARQLHttpConnectionHandlerProcessor.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 "CSPARQLHttpConnectionHandlerProcessor.h"
22
23
24 namespace Konclude {
25
26 namespace Control {
27
28 namespace Interface {
29
30 namespace SPARQL {
31
32
33 CSPARQLHttpConnectionHandlerProcessor::CSPARQLHttpConnectionHandlerProcessor(CConfiguration* loaderConfig, CQtHttpConnectionHandlerReleaser* releaser) : COWLlinkProcessor(false) {
34 mReleaser = releaser;
35 mLoaderConfig = loaderConfig;
36 reasonerCommander = CConfigManagerReader::readCommanderManagerConfig(mLoaderConfig);
37 mSocket = nullptr;
38 mQueued = false;
39 mForceDisconnect = CConfigDataReader::readConfigBoolean(mLoaderConfig,"Konclude.SPARQL.Server.ForceDisconnectAfterProcessing",false);
40 mProcessingCommand = nullptr;
41 startThread();
42 mThreadStartedSemaphore.acquire(1);
43 mOriginPreSynchronizer = preSynchronizer;
44 mDataWritten = false;
45
46 mThreadedWriting = CConfigDataReader::readConfigBoolean(mLoaderConfig, "Konclude.SPARQL.Server.ThreadedSocketWriting", true);
47
48 cint64 writeBufferBlockingLimit = CConfigDataReader::readConfigInteger(mLoaderConfig, "Konclude.SPARQL.Server.WriteBufferBlockingLimit", 20);
49 if (writeBufferBlockingLimit > 0) {
50 mWriteLimitSemaphore = new QSemaphore(writeBufferBlockingLimit);
51 } else {
52 mWriteLimitSemaphore = nullptr;
53 }
54 mBlockingWarned = false;
55
56
57 mStreamSPARQLHeader = QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<sparql xmlns=\"http://www.w3.org/2005/sparql-results#\" xml:base=\"http://www.w3.org/2005/sparql-results#\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\">").toLocal8Bit();
58 mStreamSPARQLFooter = QString("\r\n</sparql>").toLocal8Bit();
59
60 mWrittenContinueSemaphore = nullptr;
61 if (!mThreadedWriting) {
62 mWrittenContinueSemaphore = new QSemaphore();
63 }
64
65 }
66
67
68 CSPARQLHttpConnectionHandlerProcessor::~CSPARQLHttpConnectionHandlerProcessor() {
69 }
70
71 void CSPARQLHttpConnectionHandlerProcessor::threadStarted() {
72 COWLlinkProcessor::threadStarted();
73
74 mParser = new CQtHttpRequestBodyOnlyParser();
75 mSocket = new QTcpSocket();
76 mSocket->moveToThread(this);
77
78 mProcessingRequest = false;
79 mProcessMoreRead = false;
80
81 connect(mSocket,SIGNAL(readyRead()),this,SLOT(connectionRead()));
82 connect(mSocket,SIGNAL(disconnected()),this,SLOT(connectionDisconnect()));
83
84 mThreadStartedSemaphore.release(1);
85 }
86
87 void CSPARQLHttpConnectionHandlerProcessor::threadStopped() {
88 COWLlinkProcessor::threadStopped();
89
90 mSocket->close();
91 delete mSocket;
92 delete mParser;
93
94 if (mWriteLimitSemaphore) {
95 delete mWriteLimitSemaphore;
96 }
97 if (mWrittenContinueSemaphore) {
98 delete mWrittenContinueSemaphore;
99 }
100 }
101
102
103 bool CSPARQLHttpConnectionHandlerProcessor::handleIncomingConnection(int socketDescriptor) {
104 postEvent(new CHandleIncomingHttpConnectionEvent(socketDescriptor));
105 return true;
106 }
107
108
109 bool CSPARQLHttpConnectionHandlerProcessor::isHandlerQueued() {
110 return mQueued;
111 }
112
113 CSPARQLHttpConnectionHandlerProcessor* CSPARQLHttpConnectionHandlerProcessor::setHandlerQueued(bool queued) {
114 mQueued = queued;
115 return this;
116 }
117
118
119
120 bool CSPARQLHttpConnectionHandlerProcessor::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
121 if (COWLlinkProcessor::processCustomsEvents(type,event)) {
122 return true;
123 } else {
124 if (type == CConnectionReadyReadEvent::EVENTTYPE) {
125 CConnectionReadyReadEvent* crre = (CConnectionReadyReadEvent*)event;
126
127 if (!mProcessingRequest) {
128
129 if (!mParser->readFromDevice(mSocket)) {
130 // error
131 QString errorString = QString("HTTP/1.1 400 error parsing request\r\nConnection: close\r\n\r\n400 Parsing error: %1\r\n").arg(mParser->getHttpRequestParsingErrorString());
132 LOG(INFO,"::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor",logTr("Error parsing HTTP request, '%1'.").arg(mParser->getHttpRequestParsingErrorString()),this);
133 mSocket->write(errorString.toLocal8Bit());
134 mSocket->disconnectFromHost();
135 }
136
137 if (mParser->hasHttpRequestParsingCompleted()) {
138 mProcessingRequest = true;
139 mProcessingByteArray = mParser->takeBodyData();
140 if (mProcessingByteArray != 0) {
141 LOG(INFO,"::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor",logTr("HTTP request successfully parsed, %1 Bytes received.").arg(mProcessingByteArray->count()),this);
142
143
144 mSPARQLInterpreter = new CSPARQLRecordResultStreamingInterpreter(this, mOriginPreSynchronizer, mLoaderConfig);
145 mWritingStarted = false;
146 mWritingFailed = false;
147 mDataWritten = false;
148 mChunkPart = 0;
149 defaultCommandDelegater = mSPARQLInterpreter;
150 preSynchronizer = new CPreconditionSynchronizer(mSPARQLInterpreter);
151
152 QString dataString = QString::fromUtf8(mProcessingByteArray->data());
153 CParseProcessSPARQLTextCommand* parseProcSPARQLCommand = new CParseProcessSPARQLTextCommand(dataString);
154 CCommandProcessedCallbackEvent *proComm = new CCommandProcessedCallbackEvent(this, parseProcSPARQLCommand);
155 parseProcSPARQLCommand->setRecorder(mSPARQLInterpreter);
156
157 mProcessingCommand = parseProcSPARQLCommand;
158
159 defaultCommandDelegater->delegateCommand(parseProcSPARQLCommand);
160
161 parseProcSPARQLCommand->addProcessedCallback(proComm);
162
163 delete mProcessingByteArray;
164 } else {
165 // invalid request
166
167 QString koncludeString = QString("%1 %2").arg(CKoncludeInfo::getKoncludeName()).arg(CKoncludeInfo::getKoncludeVersionString());
168 sendData(QString("<h2>%1 SPARQL Sever</h2><p>This response is generated from Konclude's SPARQL server for invalid SPARQL requests. Please make sure that the SPARQL request is appended as content (with correct Content-Length) of your HTTP request. See <a href=\"http://konclude.com\">konclude.com</a> for help and further information.</p>").arg(koncludeString).toLocal8Bit());
169
170 mProcessingRequest = false;
171 if (mForceDisconnect || mParser->hasRequestedCloseConnection()) {
172 mSocket->disconnectFromHost();
173 } else if (mProcessMoreRead) {
174 mProcessMoreRead = false;
175 connectionRead();
176 }
177
178 mParser->reset();
179
180 }
181
182
183 }
184 }
185
186 return true;
187 } else if (type == CConnectionDisconnectedEvent::EVENTTYPE) {
188 CConnectionDisconnectedEvent* cde = (CConnectionDisconnectedEvent*)event;
189 mParser->reset();
190 mProcessMoreRead = false;
191 if (!mProcessingRequest) {
192 mHandlerBusy = false;
193 }
194 mReleaser->releaseConnectionHandler(this);
195 return true;
196
197
198 } else if (type == CHandleIncomingHttpConnectionEvent::EVENTTYPE) {
199 CHandleIncomingHttpConnectionEvent* hihc = (CHandleIncomingHttpConnectionEvent*)event;
200 cint64 socketDescriptor = hihc->getSocketDescriptor();
201 LOG(INFO,"::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor",logTr("Start processing new incoming connection."),this);
202 mSocket->setSocketDescriptor(socketDescriptor);
203 mHandlerBusy = true;
204 return true;
205 } else if (type == CResultStreamingWriteEvent::EVENTTYPE) {
206 CResultStreamingWriteEvent* rswe = (CResultStreamingWriteEvent*)event;
207 QList<CSPARQLResultBufferWriteData>* bufferList = rswe->getBufferList();
208 bool last = rswe->isLast();
209 writeStreamDataToSocket(bufferList, last);
210 mWriteLimitSemaphore->release(1);
211 if (mWrittenContinueSemaphore) {
212 mWrittenContinueSemaphore->release(1);
213 }
214 mDataWritten = true;
215 return true;
216 }
217
218 }
219 return false;
220 }
221
222 COWLlinkProcessor* CSPARQLHttpConnectionHandlerProcessor::initializeOWLlinkContent() {
223 return this;
224 }
225
226
227
228 CSPARQLStreamingWriter* CSPARQLHttpConnectionHandlerProcessor::writeChunk(const QByteArray& byteArray) {
229 QString chunckHeader = QString::number(byteArray.length(), 16) + "\r\n";
230 mSocket->write(chunckHeader.toLocal8Bit());
231 mSocket->write(byteArray);
232 mSocket->write(QString("\r\n").toLocal8Bit());
233 return this;
234 }
235
236
237 bool CSPARQLHttpConnectionHandlerProcessor::writeStreamData(const QList<CSPARQLResultBufferWriteData>& bufferList, bool last) {
238 if (mWriteLimitSemaphore) {
239 if (!mBlockingWarned && mWriteLimitSemaphore->available() <= 2) {
240 mBlockingWarned = true;
241 LOG(WARN, "::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor", logTr("Result sending buffer limit almost reached, result generation may be blocked due to slow network connection."), this);
242 }
243 mWriteLimitSemaphore->acquire(1);
244 }
245 postEvent(new CResultStreamingWriteEvent(bufferList, last));
246 if (!mThreadedWriting && mWrittenContinueSemaphore) {
247 mWrittenContinueSemaphore->acquire();
248 }
249 //writeStreamDataToSocket(buffer, last);
250 //mDataWritten = true;
251 return !mWritingFailed;
252 }
253
254
255 CSPARQLStreamingWriter* CSPARQLHttpConnectionHandlerProcessor::writeStreamDataToSocket(QList<CSPARQLResultBufferWriteData>* bufferList, bool last) {
256 if (mSocket->state() == QTcpSocket::ConnectedState) {
257 if (!mWritingStarted && last && bufferList->size() == 1) {
258 CSPARQLResultBufferWriteData bufferData = bufferList->first();
259 for (cint64 i = bufferData.getWriteCount(); i > 0; --i) {
260 sendData(*bufferData.getBuffer());
261 }
262 delete bufferData.getBuffer();
263 } else {
264 ++mChunkPart;
265 // write with chunked encoding
266 bool firstChunk = false;
267 if (!mWritingStarted) {
268 mWritingStarted = true;
269 QString connectionString = QString("Connection: ");
270 if (mParser->hasRequestedCloseConnection()) {
271 connectionString += QString("close");
272 } else {
273 connectionString += QString("keep-alive");
274 }
275 QString serverString = QString("Server: %1 %2").arg(CKoncludeInfo::getKoncludeName()).arg(CKoncludeInfo::getKoncludeVersionString());
276 QString responseHeadString = QString("HTTP/1.1 200 OK\r\n%1\r\n%2\r\nTransfer-Encoding: chunked\r\n\r\n").arg(serverString).arg(connectionString);
277
278 mSocket->write(responseHeadString.toLocal8Bit());
279
280 writeChunk(mStreamSPARQLHeader);
281 mSocket->flush();
282 firstChunk = true;
283 }
284 cint64 bufferSize = 0;
285 for (CSPARQLResultBufferWriteData bufferData : *bufferList) {
286 for (cint64 i = bufferData.getWriteCount(); i > 0; --i) {
287 writeChunk(*bufferData.getBuffer());
288 bufferSize += bufferData.getBuffer()->size();
289 }
290 delete bufferData.getBuffer();
291 }
292 mSocket->flush();
293 if (last) {
294 writeChunk(mStreamSPARQLFooter);
295 writeChunk(QByteArray());
296 mSocket->flush();
297 LOG(INFO, "::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor", logTr("HTTP request successfully processed, last part of %1 with %2 bytes sent.").arg(mChunkPart).arg(bufferSize), this);
298 } else {
299 LOG(INFO, "::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor", logTr("Sending part %1 with %2 bytes of HTTP response via chunked encoding.").arg(mChunkPart).arg(bufferSize), this);
300 }
301 }
302 } else {
303 mWritingFailed = true;
304 LOG(INFO, "::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor", logTr("Cannot write HTTP response data since connection already closed."), this);
305 }
306 return this;
307 }
308
309
310
311 COWLlinkProcessor* CSPARQLHttpConnectionHandlerProcessor::sendData(const QByteArray& dataArray) {
312 if (mSocket->state() == QTcpSocket::ConnectedState) {
313 QString connectionString = QString("Connection: ");
314 if (mParser->hasRequestedCloseConnection()) {
315 connectionString += QString("close");
316 } else {
317 connectionString += QString("keep-alive");
318 }
319 cint64 contentLength = dataArray.length();
320 if (contentLength > 0) {
321 contentLength += mStreamSPARQLHeader.length();
322 contentLength += mStreamSPARQLFooter.length();
323 }
324 QString serverString = QString("Server: %1 %2").arg(CKoncludeInfo::getKoncludeName()).arg(CKoncludeInfo::getKoncludeVersionString());
325 QString responseHeadString = QString("HTTP/1.1 200 OK\r\n%1\r\n%2\r\nContent-Length: %3\r\n\r\n").arg(serverString).arg(connectionString).arg(contentLength);
326
327 mSocket->write(responseHeadString.toLocal8Bit());
328 if (contentLength > 0) {
329 mSocket->write(mStreamSPARQLHeader);
330 mSocket->write(dataArray);
331 mSocket->write(mStreamSPARQLFooter);
332 }
333 mSocket->flush();
334
335 LOG(INFO,"::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor",logTr("HTTP request successfully processed, %1 bytes sent.").arg(contentLength),this);
336 } else {
337 LOG(INFO,"::Konclude::Control::Interface::SPARQL::SPARQLHTTPConnectionProcessor",logTr("HTTP request successfully processed, but connection already closed."),this);
338 }
339 return this;
340 }
341
342 COWLlinkProcessor* CSPARQLHttpConnectionHandlerProcessor::concludeOWLlinkContent() {
343 if (!mDataWritten) {
344 mDataWritten = true;
345 sendData(QByteArray());
346 }
347 delete mSPARQLInterpreter;
348 delete mProcessingCommand;
349 mProcessingCommand = nullptr;
350 mSPARQLInterpreter = nullptr;
351 mProcessingRequest = false;
352
353
354 if (mForceDisconnect || mParser->hasRequestedCloseConnection()) {
355 mSocket->disconnectFromHost();
356 } else if (mProcessMoreRead) {
357 mProcessMoreRead = false;
358 connectionRead();
359 }
360
361 mParser->reset();
362 return this;
363 }
364
365 void CSPARQLHttpConnectionHandlerProcessor::connectionRead() {
366 postEvent(new CConnectionReadyReadEvent());
367 }
368
369 void CSPARQLHttpConnectionHandlerProcessor::connectionDisconnect() {
370 postEvent(new CConnectionDisconnectedEvent());
371 }
372
373 CConfiguration* CSPARQLHttpConnectionHandlerProcessor::getConfiguration() {
374 if (!reasonerCommander) {
375 return mLoaderConfig;
376 } else {
377 CGetDescriptionCommand *getDesComm = new CGetDescriptionCommand();
378 reasonerCommander->delegateCommand(getDesComm);
379 CCommandExecutedBlocker commExeBlocker;
380 commExeBlocker.waitExecutedCommand(getDesComm);
381 CConfiguration *config = getDesComm->getConfiguration();
382 return config;
383 }
384 }
385
386
387 }; // end namespace SPARQL
388
389 }; // end namespace Interface
390
391 }; // end namespace Control
392
393 }; // end namespace Konclude