HttpRequest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "HttpRequest.h"
  2. #include "Klient.h"
  3. #include <Zeit.h>
  4. #include <iostream>
  5. using namespace Framework;
  6. using namespace Network;
  7. using namespace HTTP;
  8. PostRequest::PostRequest(const char* path, const char* host, const char* data, const char* contentType, unsigned short port, bool useSSL)
  9. : ReferenceCounter()
  10. {
  11. this->path = path;
  12. this->host = host;
  13. this->data = data;
  14. this->contentType = contentType;
  15. this->port = port;
  16. this->useSSL = useSSL;
  17. }
  18. Answer* PostRequest::execute() const
  19. {
  20. Text message = "POST ";
  21. message += path;
  22. message += " HTTP/1.1\r\n";
  23. message += "Host: ";
  24. message += host;
  25. message += "\r\nContent-Type: ";
  26. message += contentType;
  27. message += "; charset=latin-1\r\nContent-Length: ";
  28. message += data.getLength();
  29. message += "\r\n\r\n";
  30. message += data;
  31. SSLKlient httpsK;
  32. Klient httpK;
  33. if (useSSL)
  34. {
  35. if (!httpsK.verbinde(port, host))
  36. return 0;
  37. httpsK.sende(message, message.getLength());
  38. }
  39. else
  40. {
  41. if (!httpK.verbinde(port, host))
  42. return 0;
  43. httpK.sende(message, message.getLength());
  44. }
  45. int length = -1;
  46. bool lastn = 0;
  47. Text answer;
  48. do
  49. {
  50. char buff[2];
  51. buff[1] = 0;
  52. if ((useSSL && httpsK.getNachricht(buff, 1)) || (!useSSL && httpK.getNachricht(buff, 1)))
  53. {
  54. answer += buff;
  55. if (buff[0] == '\n')
  56. {
  57. if (length <= 0)
  58. {
  59. int p = answer.positionVon("Content-Length: ");
  60. if (p >= 0)
  61. {
  62. p += 16;
  63. Text* len = answer.getTeilText(p);
  64. len->remove(len->positionVon("\n"), len->getLength());
  65. len->remove("\r");
  66. length = (int)*len;
  67. len->release();
  68. }
  69. }
  70. else if (lastn)
  71. { // header der antwort ist vorbei
  72. char* buff = new char[length + 1];
  73. buff[length] = 0;
  74. if (length > 0)
  75. {
  76. if (useSSL)
  77. httpsK.getNachricht(buff, length);
  78. else
  79. httpK.getNachricht(buff, length);
  80. }
  81. answer += buff;
  82. delete[] buff;
  83. break;
  84. }
  85. lastn = 1;
  86. }
  87. else if (buff[0] != '\r')
  88. lastn = 0;
  89. }
  90. else
  91. break;
  92. } while ((useSSL && httpsK.hatNachricht(1000)) || (!useSSL && httpK.hatNachricht(1000)));
  93. if (answer.getLength())
  94. return new Answer(answer);
  95. return 0;
  96. }
  97. Answer::Answer(const char* answer)
  98. : ReferenceCounter()
  99. {
  100. all = answer;
  101. TextReader reader(new Text(answer));
  102. // parse header
  103. Text* line = reader.leseZeile();
  104. line->remove("\r\n");
  105. line->remove("\n");
  106. header += *line;
  107. header += "\n";
  108. int trenn = line->positionVon(" ");
  109. Text* tmp = line->getTeilText(0, trenn);
  110. this->protocol = *tmp;
  111. tmp->release();
  112. line->remove(0, trenn + 1);
  113. trenn = line->positionVon(" ");
  114. tmp = line->getTeilText(0, trenn);
  115. this->statusNumber = (int)*tmp;
  116. tmp->release();
  117. line->remove(0, trenn + 1);
  118. this->statusText = *line;
  119. line->release();
  120. while (!reader.istEnde())
  121. {
  122. Text* line = reader.leseZeile();
  123. line->remove("\r\n");
  124. line->remove("\n");
  125. if (line->getLength() > 0)
  126. {
  127. header += *line;
  128. header += "\n";
  129. }
  130. if (line->positionVon("Date:") == 0)
  131. this->date = line->getText() + 6;
  132. if (line->positionVon("Content-Type:") == 0)
  133. this->contentType = line->getText() + 14;
  134. int len = line->getLength();
  135. line->release();
  136. if (len == 0)
  137. break;
  138. }
  139. // parse body
  140. if (!reader.istEnde())
  141. this->data = answer + reader.getLPosition();
  142. }
  143. const char* Answer::getContentType() const
  144. {
  145. return contentType;
  146. }
  147. const char* Answer::getData() const
  148. {
  149. return data;
  150. }
  151. int Answer::getStatusCode() const
  152. {
  153. return statusNumber;
  154. }
  155. const char* Answer::getStatusText() const
  156. {
  157. return statusText;
  158. }
  159. const char* Answer::getDate() const
  160. {
  161. return date;
  162. }
  163. const char* Answer::getAll() const
  164. {
  165. return all;
  166. }