HttpRequest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 )
  9. {
  10. this->path = path;
  11. this->host = host;
  12. this->data = data;
  13. this->contentType = contentType;
  14. this->port = port;
  15. ref = 1;
  16. }
  17. Answer *PostRequest::execute() const
  18. {
  19. ZeitMesser zm;
  20. zm.messungStart();
  21. Text message = "POST ";
  22. message += path;
  23. message += " HTTP/1.1\r\n";
  24. message += "Host: ";
  25. message += host;
  26. message += "\r\nContent-Type: ";
  27. message += contentType;
  28. message += "; charset=latin-1\r\nContent-Length: ";
  29. message += data.getLength();
  30. message += "\r\n\r\n";
  31. message += data;
  32. zm.messungEnde();
  33. std::cout << "http request string erstellen: " << zm.getSekunden() << "\n";
  34. zm.messungStart();
  35. Klient httpK;
  36. if( !httpK.verbinde( port, host ) )
  37. return 0;
  38. zm.messungEnde();
  39. std::cout << "verbindung erstellen: " << zm.getSekunden() << "\n";
  40. zm.messungStart();
  41. httpK.sende( message, message.getLength() );
  42. zm.messungEnde();
  43. std::cout << "request senden: " << zm.getSekunden() << "\n";
  44. zm.messungStart();
  45. int length = -1;
  46. bool lastn = 0;
  47. Text answer;
  48. do {
  49. char buff[ 2 ];
  50. buff[ 1 ] = 0;
  51. if( httpK.getNachricht( buff, 1 ) )
  52. {
  53. if( !answer.getText() || !answer.getText()[ 0 ] )
  54. {
  55. zm.messungEnde();
  56. std::cout << "auf antwort warten: " << zm.getSekunden() << "\n";
  57. zm.messungStart();
  58. }
  59. answer += buff;
  60. if( buff[ 0 ] == '\n' )
  61. {
  62. if( length <= 0 )
  63. {
  64. int p = answer.positionVon( "Content-Length: " );
  65. if( p >= 0 )
  66. {
  67. p += 16;
  68. Text *len = answer.getTeilText( p );
  69. len->remove( len->positionVon( "\n" ), len->getLength() );
  70. len->remove( "\r" );
  71. length = *len;
  72. len->release();
  73. }
  74. }
  75. else if( lastn )
  76. { // header der antwort ist vorbei
  77. char *buff = new char[ length + 1 ];
  78. buff[ length ] = 0;
  79. if( length > 0 )
  80. httpK.getNachricht( buff, length );
  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( httpK.hatNachricht( 1000 ) );
  93. zm.messungEnde();
  94. std::cout << "nachricht empfangen: " << zm.getSekunden() << "\n";
  95. return new Answer( answer );
  96. }
  97. PostRequest *PostRequest::getThis()
  98. {
  99. ref++;
  100. return this;
  101. }
  102. PostRequest *PostRequest::release()
  103. {
  104. if( !--ref )
  105. delete this;
  106. return 0;
  107. }
  108. Answer::Answer( const char *answer )
  109. {
  110. all = answer;
  111. TextReader reader( new Text( answer ) );
  112. // parse header
  113. Text *line = reader.leseZeile();
  114. line->remove( "\r\n" );
  115. line->remove( "\n" );
  116. header += *line;
  117. header += "\n";
  118. int trenn = line->positionVon( " " );
  119. Text *tmp = line->getTeilText( 0, trenn );
  120. this->protocol = *tmp;
  121. tmp->release();
  122. line->remove( 0, trenn + 1 );
  123. trenn = line->positionVon( " " );
  124. tmp = line->getTeilText( 0, trenn );
  125. this->statusNumber = *tmp;
  126. tmp->release();
  127. line->remove( 0, trenn + 1 );
  128. this->statusText = *line;
  129. line->release();
  130. while( !reader.istEnde() )
  131. {
  132. Text *line = reader.leseZeile();
  133. line->remove( "\r\n" );
  134. line->remove( "\n" );
  135. if( line->getLength() > 0 )
  136. {
  137. header += *line;
  138. header += "\n";
  139. }
  140. if( line->positionVon( "Date:" ) == 0 )
  141. this->date = line->getText() + 6;
  142. if( line->positionVon( "Content-Type:" ) == 0 )
  143. this->contentType = line->getText() + 14;
  144. int len = line->getLength();
  145. line->release();
  146. if( len == 0 )
  147. break;
  148. }
  149. // parse body
  150. if( !reader.istEnde() )
  151. this->data = answer + reader.getLPosition();
  152. ref = 1;
  153. }
  154. const char *Answer::getContentType() const
  155. {
  156. return contentType;
  157. }
  158. const char *Answer::getData() const
  159. {
  160. return data;
  161. }
  162. int Answer::getStatusCode() const
  163. {
  164. return statusNumber;
  165. }
  166. const char *Answer::getStatusText() const
  167. {
  168. return statusText;
  169. }
  170. const char *Answer::getDate() const
  171. {
  172. return date;
  173. }
  174. const char *Answer::getAll() const
  175. {
  176. return all;
  177. }
  178. Answer *Answer::getThis()
  179. {
  180. ref++;
  181. return this;
  182. }
  183. Answer *Answer::release()
  184. {
  185. if( !--ref )
  186. delete this;
  187. return 0;
  188. }