HttpRequest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. : ReferenceCounter()
  10. {
  11. this->path = path;
  12. this->host = host;
  13. this->data = data;
  14. this->contentType = contentType;
  15. this->port = port;
  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. {
  39. char buff[ 2 ];
  40. buff[ 1 ] = 0;
  41. if( httpK.getNachricht( buff, 1 ) )
  42. {
  43. answer += buff;
  44. if( buff[ 0 ] == '\n' )
  45. {
  46. if( length <= 0 )
  47. {
  48. int p = answer.positionVon( "Content-Length: " );
  49. if( p >= 0 )
  50. {
  51. p += 16;
  52. Text *len = answer.getTeilText( p );
  53. len->remove( len->positionVon( "\n" ), len->getLength() );
  54. len->remove( "\r" );
  55. length = *len;
  56. len->release();
  57. }
  58. }
  59. else if( lastn )
  60. { // header der antwort ist vorbei
  61. char *buff = new char[ length + 1 ];
  62. buff[ length ] = 0;
  63. if( length > 0 )
  64. httpK.getNachricht( buff, length );
  65. answer += buff;
  66. delete[] buff;
  67. break;
  68. }
  69. lastn = 1;
  70. }
  71. else if( buff[ 0 ] != '\r' )
  72. lastn = 0;
  73. }
  74. else
  75. break;
  76. } while( httpK.hatNachricht( 1000 ) );
  77. if( answer.getLength() )
  78. return new Answer( answer );
  79. return 0;
  80. }
  81. Answer::Answer( const char *answer )
  82. : ReferenceCounter()
  83. {
  84. all = answer;
  85. TextReader reader( new Text( answer ) );
  86. // parse header
  87. Text *line = reader.leseZeile();
  88. line->remove( "\r\n" );
  89. line->remove( "\n" );
  90. header += *line;
  91. header += "\n";
  92. int trenn = line->positionVon( " " );
  93. Text *tmp = line->getTeilText( 0, trenn );
  94. this->protocol = *tmp;
  95. tmp->release();
  96. line->remove( 0, trenn + 1 );
  97. trenn = line->positionVon( " " );
  98. tmp = line->getTeilText( 0, trenn );
  99. this->statusNumber = *tmp;
  100. tmp->release();
  101. line->remove( 0, trenn + 1 );
  102. this->statusText = *line;
  103. line->release();
  104. while( !reader.istEnde() )
  105. {
  106. Text *line = reader.leseZeile();
  107. line->remove( "\r\n" );
  108. line->remove( "\n" );
  109. if( line->getLength() > 0 )
  110. {
  111. header += *line;
  112. header += "\n";
  113. }
  114. if( line->positionVon( "Date:" ) == 0 )
  115. this->date = line->getText() + 6;
  116. if( line->positionVon( "Content-Type:" ) == 0 )
  117. this->contentType = line->getText() + 14;
  118. int len = line->getLength();
  119. line->release();
  120. if( len == 0 )
  121. break;
  122. }
  123. // parse body
  124. if( !reader.istEnde() )
  125. this->data = answer + reader.getLPosition();
  126. }
  127. const char *Answer::getContentType() const
  128. {
  129. return contentType;
  130. }
  131. const char *Answer::getData() const
  132. {
  133. return data;
  134. }
  135. int Answer::getStatusCode() const
  136. {
  137. return statusNumber;
  138. }
  139. const char *Answer::getStatusText() const
  140. {
  141. return statusText;
  142. }
  143. const char *Answer::getDate() const
  144. {
  145. return date;
  146. }
  147. const char *Answer::getAll() const
  148. {
  149. return all;
  150. }