HttpRequest.cpp 3.1 KB

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