/*============================================================================= Library: CTK Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =============================================================================*/ // Qt includes #include // CTK includes #include "ctkSoapConnectionRunnable_p.h" #include "ctkSoapLog.h" //---------------------------------------------------------------------------- ctkSoapConnectionRunnable::ctkSoapConnectionRunnable(int socketDescriptor) : socketDescriptor(socketDescriptor), isAboutToQuit(0) { connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit())); } //---------------------------------------------------------------------------- ctkSoapConnectionRunnable::~ctkSoapConnectionRunnable() { } //---------------------------------------------------------------------------- void ctkSoapConnectionRunnable::run() { QTcpSocket tcpSocket; if (!tcpSocket.setSocketDescriptor(socketDescriptor)) { // error handling return; } const int timeout = 1 * 1000; while (tcpSocket.state() == QTcpSocket::ConnectedState && isAboutToQuit.fetchAndAddOrdered(0) == 0) { tcpSocket.waitForReadyRead(timeout); readClient(tcpSocket); } } //---------------------------------------------------------------------------- void ctkSoapConnectionRunnable::readClient(QTcpSocket& socket) { QString requestType; int contentLength = -1; //qDebug() << socket->readAll(); while (socket.canReadLine()) { QString line = socket.readLine(); CTK_SOAP_LOG_LOWLEVEL( << line ); if(line.contains("?wsdl HTTP")) { requestType = "?wsdl"; } if(line.contains("?xsd=1")) { requestType = "?xsd=1"; } if(line.contains("SoapAction")) { requestType = line; } if(line.contains("Content-Length: ")) { contentLength = line.section(':',1).trimmed().toInt(); } if (line.trimmed().isEmpty()) { QString content; if(requestType.startsWith("?")) { QByteArray body = socket.readAll(); emit incomingWSDLMessage(requestType, &content); } else { // Read the http body, which contains the soap message int bytesRead = 0; QByteArray body; while(body.size() < contentLength) { QByteArray bodyPart = socket.read(contentLength); CTK_SOAP_LOG_LOWLEVEL( << bodyPart ); bytesRead += bodyPart.size(); body.append(bodyPart); CTK_SOAP_LOG_LOWLEVEL( << " Expected content-length: " << contentLength << ". Bytes read so far: " << body.size() ); if (body.size()