HttpRequest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\n";
  20. message += "Host: ";
  21. message += host;
  22. message += "\nContent-Type: ";
  23. message += contentType;
  24. message += "\nContent-Length: ";
  25. message += data.getLength();
  26. message += "\n\n";
  27. message += data;
  28. message += "\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( 0 ) );
  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. TextReader reader( new Text( answer ) );
  58. // parse header
  59. Text *line = reader.leseZeile();
  60. line->remove( "\r\n" );
  61. line->remove( "\n" );
  62. header += *line;
  63. header += "\n";
  64. int trenn = line->positionVon( " " );
  65. Text *tmp = line->getTeilText( 0, trenn );
  66. this->protocol = *tmp;
  67. tmp->release();
  68. line->remove( 0, trenn + 1 );
  69. trenn = line->positionVon( " " );
  70. tmp = line->getTeilText( 0, trenn );
  71. this->statusNumber = *tmp;
  72. tmp->release();
  73. line->remove( 0, trenn + 1 );
  74. this->statusText = *line;
  75. line->release();
  76. while( !reader.istEnde() )
  77. {
  78. Text *line = reader.leseZeile();
  79. line->remove( "\r\n" );
  80. line->remove( "\n" );
  81. if( line->getLength() > 0 )
  82. {
  83. header += *line;
  84. header += "\n";
  85. }
  86. if( line->positionVon( "Date:" ) == 0 )
  87. this->date = line->getText() + 6;
  88. if( line->positionVon( "Content-Type:" ) == 0 )
  89. this->contentType = line->getText() + 14;
  90. int len = line->getLength();
  91. line->release();
  92. if( len == 0 )
  93. break;
  94. }
  95. // parse body
  96. if( !reader.istEnde() )
  97. this->data = answer + reader.getLPosition();
  98. ref = 1;
  99. }
  100. const char *Answer::getContentType() const
  101. {
  102. return contentType;
  103. }
  104. const char *Answer::getData() const
  105. {
  106. return data;
  107. }
  108. int Answer::getStatusCode() const
  109. {
  110. return statusNumber;
  111. }
  112. const char *Answer::getStatusText() const
  113. {
  114. return statusText;
  115. }
  116. const char *Answer::getDate() const
  117. {
  118. return date;
  119. }
  120. Answer *Answer::getThis()
  121. {
  122. ref++;
  123. return this;
  124. }
  125. Answer *Answer::release()
  126. {
  127. if( !--ref )
  128. delete this;
  129. return 0;
  130. }