ctkSoapConnectionRunnable.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // Qt includes
  16. #include <QTcpSocket>
  17. // CTK includes
  18. #include "ctkSoapConnectionRunnable_p.h"
  19. #include "ctkSoapLog.h"
  20. //----------------------------------------------------------------------------
  21. ctkSoapConnectionRunnable::ctkSoapConnectionRunnable(int socketDescriptor)
  22. : socketDescriptor(socketDescriptor), isAboutToQuit(0)
  23. {
  24. connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()));
  25. }
  26. //----------------------------------------------------------------------------
  27. ctkSoapConnectionRunnable::~ctkSoapConnectionRunnable()
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. void ctkSoapConnectionRunnable::run()
  32. {
  33. QTcpSocket tcpSocket;
  34. if (!tcpSocket.setSocketDescriptor(socketDescriptor))
  35. {
  36. // error handling
  37. return;
  38. }
  39. const int timeout = 1 * 1000;
  40. while (tcpSocket.state() == QTcpSocket::ConnectedState &&
  41. isAboutToQuit.fetchAndAddOrdered(0) == 0)
  42. {
  43. tcpSocket.waitForReadyRead(timeout);
  44. readClient(tcpSocket);
  45. }
  46. }
  47. //----------------------------------------------------------------------------
  48. void ctkSoapConnectionRunnable::readClient(QTcpSocket& socket)
  49. {
  50. QString requestType;
  51. int contentLength = -1;
  52. //qDebug() << socket->readAll();
  53. while (socket.canReadLine()) {
  54. QString line = socket.readLine();
  55. CTK_SOAP_LOG_LOWLEVEL( << line );
  56. if(line.contains("?wsdl HTTP"))
  57. {
  58. requestType = "?wsdl";
  59. }
  60. if(line.contains("?xsd=1"))
  61. {
  62. requestType = "?xsd=1";
  63. }
  64. if(line.contains("SoapAction"))
  65. {
  66. requestType = line;
  67. }
  68. if(line.contains("Content-Length: "))
  69. {
  70. contentLength = line.section(':',1).trimmed().toInt();
  71. }
  72. if (line.trimmed().isEmpty())
  73. {
  74. QString content;
  75. if(requestType.startsWith("?"))
  76. {
  77. QByteArray body = socket.readAll();
  78. emit incomingWSDLMessage(requestType, &content);
  79. }
  80. else
  81. {
  82. // Read the http body, which contains the soap message
  83. int bytesRead = 0;
  84. QByteArray body;
  85. while(body.size() < contentLength)
  86. {
  87. QByteArray bodyPart = socket.read(contentLength);
  88. CTK_SOAP_LOG_LOWLEVEL( << bodyPart );
  89. bytesRead += bodyPart.size();
  90. body.append(bodyPart);
  91. CTK_SOAP_LOG_LOWLEVEL( << " Expected content-length: " << contentLength << ". Bytes read so far: " << body.size() );
  92. if (body.size()<contentLength)
  93. {
  94. qCritical() << " Message body too small. Trying to read more.";
  95. socket.waitForReadyRead(-1);
  96. }
  97. }
  98. if(body.trimmed().isEmpty()==false)
  99. {
  100. QtSoapMessage msg;
  101. if (!msg.setContent(body))
  102. {
  103. qCritical() << "QtSoap import failed:" << msg.errorString();
  104. return;
  105. }
  106. QtSoapMessage reply;
  107. CTK_SOAP_LOG(<< "###################" << msg.toXmlString());
  108. emit incomingSoapMessage(msg, &reply);
  109. if (reply.isFault())
  110. {
  111. qCritical() << "QtSoap reply faulty";
  112. return;
  113. }
  114. CTK_SOAP_LOG_LOWLEVEL( << "SOAP reply:" );
  115. content = reply.toXmlString();
  116. }
  117. }
  118. QByteArray block;
  119. block.append("HTTP/1.1 200 OK\n");
  120. block.append("Content-Type: text/xml;charset=utf-8\n");
  121. block.append("Content-Length: ").append(QString::number(content.size())).append("\n");
  122. block.append("\n");
  123. block.append(content);
  124. CTK_SOAP_LOG_LOWLEVEL( << block );
  125. socket.write(block);
  126. requestType = "";
  127. contentLength = -1;
  128. }
  129. }
  130. }
  131. void ctkSoapConnectionRunnable::aboutToQuit()
  132. {
  133. isAboutToQuit.testAndSetOrdered(0, 1);
  134. }