HttpRequest.cpp 4.3 KB

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