HttpRequest.cpp 4.4 KB

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