Debian Codegraph @

Source/Control/Interface/OWLlink/COWLlinkHttpConnectionHandlerProcessor.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 "COWLlinkHttpConnectionHandlerProcessor.h"
22
23
24 namespace Konclude {
25
26 namespace Control {
27
28 namespace Interface {
29
30 namespace OWLlink {
31
32
33 COWLlinkHttpConnectionHandlerProcessor::COWLlinkHttpConnectionHandlerProcessor(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.OWLlink.Server.ForceDisconnectAfterProcessing",false);
40 mProcessingCommand = nullptr;
41 startThread();
42 mThreadStartedSemaphore.acquire(1);
43 }
44
45
46 COWLlinkHttpConnectionHandlerProcessor::~COWLlinkHttpConnectionHandlerProcessor() {
47 }
48
49 void COWLlinkHttpConnectionHandlerProcessor::threadStarted() {
50 COWLlinkProcessor::threadStarted();
51
52 mParser = new CQtHttpRequestBodyOnlyParser();
53 mSocket = new QTcpSocket();
54 mSocket->moveToThread(this);
55
56 mProcessingRequest = false;
57 mProcessMoreRead = false;
58
59 connect(mSocket,SIGNAL(readyRead()),this,SLOT(connectionRead()));
60 connect(mSocket,SIGNAL(disconnected()),this,SLOT(connectionDisconnect()));
61
62 mThreadStartedSemaphore.release(1);
63 }
64
65 void COWLlinkHttpConnectionHandlerProcessor::threadStopped() {
66 COWLlinkProcessor::threadStopped();
67
68 mSocket->close();
69 delete mSocket;
70 delete mParser;
71 }
72
73
74 bool COWLlinkHttpConnectionHandlerProcessor::handleIncomingConnection(int socketDescriptor) {
75 postEvent(new CHandleIncomingHttpConnectionEvent(socketDescriptor));
76 return true;
77 }
78
79
80 bool COWLlinkHttpConnectionHandlerProcessor::isHandlerQueued() {
81 return mQueued;
82 }
83
84 COWLlinkHttpConnectionHandlerProcessor* COWLlinkHttpConnectionHandlerProcessor::setHandlerQueued(bool queued) {
85 mQueued = queued;
86 return this;
87 }
88
89
90
91 bool COWLlinkHttpConnectionHandlerProcessor::processCustomsEvents(QEvent::Type type, CCustomEvent *event) {
92 if (COWLlinkProcessor::processCustomsEvents(type,event)) {
93 return true;
94 } else {
95 if (type == CConnectionReadyReadEvent::EVENTTYPE) {
96 CConnectionReadyReadEvent* crre = (CConnectionReadyReadEvent*)event;
97
98 if (!mProcessingRequest) {
99
100 if (!mParser->readFromDevice(mSocket)) {
101 // error
102 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());
103 LOG(INFO,"::Konclude::Control::Interface::OWLlink::OWLlinkHTTPConnectionProcessor",logTr("Error parsing HTTP request, '%1'.").arg(mParser->getHttpRequestParsingErrorString()),this);
104 mSocket->write(errorString.toLocal8Bit());
105 mSocket->disconnectFromHost();
106 }
107
108 if (mParser->hasHttpRequestParsingCompleted()) {
109 mProcessingRequest = true;
110 mProcessingByteArray = mParser->takeBodyData();
111 if (mProcessingByteArray != 0) {
112 LOG(INFO,"::Konclude::Control::Interface::OWLlink::OWLlinkHTTPConnectionProcessor",logTr("HTTP request successfully parsed, %1 Bytes received.").arg(mProcessingByteArray->count()),this);
113
114
115 //QFile commFile("Debugging/Communication/http-owllink-requests.txt");
116 //if (commFile.open(QIODevice::Append)) {
117 // commFile.write(*mProcessingByteArray);
118 // commFile.close();
119 //}
120
121
122 COWLlinkQtXMLCommandParser *owllinkCommandParser = new COWLlinkQtXMLCommandParser(mLoaderConfig);
123 mOwllinkInterpreter = new COWLLinkRecordInterpreter(preSynchronizer,mLoaderConfig);
124 defaultCommandDelegater = mOwllinkInterpreter;
125
126 CParseOWLlinkCommandsCommand *parseCommand = new CParseOWLlinkCommandsCommand(mProcessingByteArray,mSocket->peerAddress().toString());
127 parseCommand->setRecorder(mOwllinkInterpreter);
128 parseCommand->setReportErrorFromSubCommands(false);
129
130 CCommandProcessedCallbackEvent *proComm = new CCommandProcessedCallbackEvent(this,parseCommand);
131 parseCommand->addProcessedCallback(proComm);
132
133
134 owllinkCommandParser->realizeCommand(parseCommand,mOwllinkInterpreter);
135 mProcessingCommand = parseCommand;
136
137 delete owllinkCommandParser;
138 delete mProcessingByteArray;
139 } else {
140 // invalid request
141
142 QString koncludeString = QString("%1 %2").arg(CKoncludeInfo::getKoncludeName()).arg(CKoncludeInfo::getKoncludeVersionString());
143 sendData(QString("<h2>%1 OWLlink Sever</h2><p>This response is generated from Konclude's OWLlink server for invalid OWLlink requests. Please make sure that the OWLlink request is appended in OWL 2 XML serialisation 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());
144
145 mProcessingRequest = false;
146 if (mForceDisconnect || mParser->hasRequestedCloseConnection()) {
147 mSocket->disconnectFromHost();
148 } else if (mProcessMoreRead) {
149 mProcessMoreRead = false;
150 connectionRead();
151 }
152
153 mParser->reset();
154
155 }
156
157
158 }
159 }
160
161 return true;
162 } else if (type == CConnectionDisconnectedEvent::EVENTTYPE) {
163 CConnectionDisconnectedEvent* cde = (CConnectionDisconnectedEvent*)event;
164 mParser->reset();
165 mProcessMoreRead = false;
166 if (!mProcessingRequest) {
167 mHandlerBusy = false;
168 }
169 mReleaser->releaseConnectionHandler(this);
170 return true;
171
172
173 } else if (type == CHandleIncomingHttpConnectionEvent::EVENTTYPE) {
174 CHandleIncomingHttpConnectionEvent* hihc = (CHandleIncomingHttpConnectionEvent*)event;
175 cint64 socketDescriptor = hihc->getSocketDescriptor();
176 LOG(INFO,"::Konclude::Control::Interface::OWLlink::OWLlinkHTTPConnectionProcessor",logTr("Start processing new incoming connection."),this);
177 mSocket->setSocketDescriptor(socketDescriptor);
178 mHandlerBusy = true;
179 return true;
180 }
181
182 }
183 return false;
184 }
185
186 COWLlinkProcessor* COWLlinkHttpConnectionHandlerProcessor::initializeOWLlinkContent() {
187 return this;
188 }
189
190
191
192 COWLlinkProcessor* COWLlinkHttpConnectionHandlerProcessor::sendData(const QByteArray& dataArray) {
193 if (mSocket->state() == QTcpSocket::ConnectedState) {
194
195 QString connectionString = QString("Connection: ");
196 if (mParser->hasRequestedCloseConnection()) {
197 connectionString += QString("close");
198 } else {
199 connectionString += QString("keep-alive");
200 }
201 QString serverString = QString("Server: %1 %2").arg(CKoncludeInfo::getKoncludeName()).arg(CKoncludeInfo::getKoncludeVersionString());
202 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(dataArray.length());
203
204 mSocket->write(responseHeadString.toLocal8Bit());
205
206 //QFile commFile("Debugging/Communication/http-owllink-responses.txt");
207 //if (commFile.open(QIODevice::Append)) {
208 // commFile.write(dataArray);
209 // commFile.close();
210 //}
211
212 mSocket->write(dataArray);
213
214
215 LOG(INFO,"::Konclude::Control::Interface::OWLlink::OWLlinkHTTPConnectionProcessor",logTr("HTTP request successfully processed, %1 Bytes sent.").arg(dataArray.count()),this);
216 } else {
217 LOG(INFO,"::Konclude::Control::Interface::OWLlink::OWLlinkHTTPConnectionProcessor",logTr("HTTP request successfully processed, but connection already closed."),this);
218 }
219 return this;
220 }
221
222 COWLlinkProcessor* COWLlinkHttpConnectionHandlerProcessor::concludeOWLlinkContent() {
223 sendData(mOwllinkInterpreter->getByteArray());
224 delete mOwllinkInterpreter;
225 delete mProcessingCommand;
226 mProcessingCommand = nullptr;
227 mOwllinkInterpreter = nullptr;
228 mProcessingRequest = false;
229
230
231 if (mForceDisconnect || mParser->hasRequestedCloseConnection()) {
232 mSocket->disconnectFromHost();
233 } else if (mProcessMoreRead) {
234 mProcessMoreRead = false;
235 connectionRead();
236 }
237
238 mParser->reset();
239 return this;
240 }
241
242 void COWLlinkHttpConnectionHandlerProcessor::connectionRead() {
243 postEvent(new CConnectionReadyReadEvent());
244 }
245
246 void COWLlinkHttpConnectionHandlerProcessor::connectionDisconnect() {
247 postEvent(new CConnectionDisconnectedEvent());
248 }
249
250 CConfiguration* COWLlinkHttpConnectionHandlerProcessor::getConfiguration() {
251 if (!reasonerCommander) {
252 return mLoaderConfig;
253 } else {
254 CGetDescriptionCommand *getDesComm = new CGetDescriptionCommand();
255 reasonerCommander->delegateCommand(getDesComm);
256 CCommandExecutedBlocker commExeBlocker;
257 commExeBlocker.waitExecutedCommand(getDesComm);
258 CConfiguration *config = getDesComm->getConfiguration();
259 return config;
260 }
261 }
262
263
264 }; // end namespace OWLlink
265
266 }; // end namespace Interface
267
268 }; // end namespace Control
269
270 }; // end namespace Konclude