HttpRequest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 += "; charset=latin-1\r\nContent-Length: ";
  25. message += data.getLength();
  26. message += "\r\n\r\n";
  27. message += data;
  28. Klient httpK;
  29. if( !httpK.verbinde( port, host ) )
  30. return 0;
  31. httpK.sende( message, message.getLength() );
  32. Text answer;
  33. do {
  34. char buff[ 2 ];
  35. buff[ 1 ] = 0;
  36. if( httpK.getNachricht( buff, 1 ) )
  37. answer += buff;
  38. else
  39. break;
  40. } while( httpK.hatNachricht( 1000 ) );
  41. return new Answer( answer );
  42. }
  43. PostRequest *PostRequest::getThis()
  44. {
  45. ref++;
  46. return this;
  47. }
  48. PostRequest *PostRequest::release()
  49. {
  50. if( !--ref )
  51. delete this;
  52. return 0;
  53. }
  54. Answer::Answer( const char *answer )
  55. {
  56. all = answer;
  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. const char *Answer::getAll() const
  121. {
  122. return all;
  123. }
  124. Answer *Answer::getThis()
  125. {
  126. ref++;
  127. return this;
  128. }
  129. Answer *Answer::release()
  130. {
  131. if( !--ref )
  132. delete this;
  133. return 0;
  134. }