HttpRequest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. return new Answer( answer );
  77. }
  78. PostRequest *PostRequest::getThis()
  79. {
  80. ref++;
  81. return this;
  82. }
  83. PostRequest *PostRequest::release()
  84. {
  85. if( !--ref )
  86. delete this;
  87. return 0;
  88. }
  89. Answer::Answer( const char *answer )
  90. {
  91. all = answer;
  92. TextReader reader( new Text( answer ) );
  93. // parse header
  94. Text *line = reader.leseZeile();
  95. line->remove( "\r\n" );
  96. line->remove( "\n" );
  97. header += *line;
  98. header += "\n";
  99. int trenn = line->positionVon( " " );
  100. Text *tmp = line->getTeilText( 0, trenn );
  101. this->protocol = *tmp;
  102. tmp->release();
  103. line->remove( 0, trenn + 1 );
  104. trenn = line->positionVon( " " );
  105. tmp = line->getTeilText( 0, trenn );
  106. this->statusNumber = *tmp;
  107. tmp->release();
  108. line->remove( 0, trenn + 1 );
  109. this->statusText = *line;
  110. line->release();
  111. while( !reader.istEnde() )
  112. {
  113. Text *line = reader.leseZeile();
  114. line->remove( "\r\n" );
  115. line->remove( "\n" );
  116. if( line->getLength() > 0 )
  117. {
  118. header += *line;
  119. header += "\n";
  120. }
  121. if( line->positionVon( "Date:" ) == 0 )
  122. this->date = line->getText() + 6;
  123. if( line->positionVon( "Content-Type:" ) == 0 )
  124. this->contentType = line->getText() + 14;
  125. int len = line->getLength();
  126. line->release();
  127. if( len == 0 )
  128. break;
  129. }
  130. // parse body
  131. if( !reader.istEnde() )
  132. this->data = answer + reader.getLPosition();
  133. ref = 1;
  134. }
  135. const char *Answer::getContentType() const
  136. {
  137. return contentType;
  138. }
  139. const char *Answer::getData() const
  140. {
  141. return data;
  142. }
  143. int Answer::getStatusCode() const
  144. {
  145. return statusNumber;
  146. }
  147. const char *Answer::getStatusText() const
  148. {
  149. return statusText;
  150. }
  151. const char *Answer::getDate() const
  152. {
  153. return date;
  154. }
  155. const char *Answer::getAll() const
  156. {
  157. return all;
  158. }
  159. Answer *Answer::getThis()
  160. {
  161. ref++;
  162. return this;
  163. }
  164. Answer *Answer::release()
  165. {
  166. if( !--ref )
  167. delete this;
  168. return 0;
  169. }