|
@@ -0,0 +1,124 @@
|
|
|
+#include "HttpRequest.h"
|
|
|
+#include "Klient.h"
|
|
|
+
|
|
|
+using namespace Framework;
|
|
|
+using namespace Network;
|
|
|
+using namespace HTTP;
|
|
|
+
|
|
|
+
|
|
|
+PostRequest::PostRequest( const char *path, const char *host, const char *data, const char *contentType, unsigned short port )
|
|
|
+{
|
|
|
+ this->path = path;
|
|
|
+ this->host = host;
|
|
|
+ this->data = data;
|
|
|
+ this->contentType = contentType;
|
|
|
+ this->port = port;
|
|
|
+ ref = 1;
|
|
|
+}
|
|
|
+
|
|
|
+Answer *PostRequest::execute() const
|
|
|
+{
|
|
|
+ Text message = "POST ";
|
|
|
+ message += path;
|
|
|
+ message += " HTTP/1.1\n";
|
|
|
+ message += "Host: ";
|
|
|
+ message += host;
|
|
|
+ message += "\nContent-Type: ";
|
|
|
+ message += contentType;
|
|
|
+ message += "\nContent-Length: ";
|
|
|
+ message += data.getLength();
|
|
|
+ message += "\n\n";
|
|
|
+ message += data;
|
|
|
+ message += "\n";
|
|
|
+ Klient httpK;
|
|
|
+ if( !httpK.verbinde( port, host ) )
|
|
|
+ return 0;
|
|
|
+ httpK.sende( message, message.getLength() );
|
|
|
+ Text answer;
|
|
|
+ do {
|
|
|
+ char buff[ 2 ];
|
|
|
+ buff[ 1 ] = 0;
|
|
|
+ httpK.getNachricht( buff, 1 );
|
|
|
+ answer += buff;
|
|
|
+ } while( httpK.hatNachricht( 0 ) );
|
|
|
+ return new Answer( answer );
|
|
|
+}
|
|
|
+
|
|
|
+PostRequest *PostRequest::getThis()
|
|
|
+{
|
|
|
+ ref++;
|
|
|
+ return this;
|
|
|
+}
|
|
|
+
|
|
|
+PostRequest *PostRequest::release()
|
|
|
+{
|
|
|
+ if( !--ref )
|
|
|
+ delete this;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+Answer::Answer( const char *answer )
|
|
|
+{
|
|
|
+ TextReader reader( new Text( answer ) );
|
|
|
+ // parse header
|
|
|
+ Text *line = reader.leseZeile();
|
|
|
+ header += *line;
|
|
|
+ header += "\n";
|
|
|
+ int trenn = line->positionVon( " " );
|
|
|
+ Text *tmp = line->getTeilText( 0, trenn );
|
|
|
+ this->protocol = *tmp;
|
|
|
+ tmp->release();
|
|
|
+ line->remove( 0, trenn + 1 );
|
|
|
+ trenn = line->positionVon( " " );
|
|
|
+ tmp = line->getTeilText( 0, trenn );
|
|
|
+ this->statusNumber = *tmp;
|
|
|
+ tmp->release();
|
|
|
+ line->remove( 0, trenn + 1 );
|
|
|
+ this->statusText = *line;
|
|
|
+ line->release();
|
|
|
+ while( !reader.istEnde() )
|
|
|
+ {
|
|
|
+ Text *line = reader.leseZeile();
|
|
|
+ if( line->getLength() > 0 )
|
|
|
+ {
|
|
|
+ header += *line;
|
|
|
+ header += "\n";
|
|
|
+ }
|
|
|
+ if( line->positionVon( "Data:" ) == 0 )
|
|
|
+ this->date = line->getText() + 6;
|
|
|
+ if( line->positionVon( "Content-Type:" ) == 0 )
|
|
|
+ this->contentType = line->getText() + 14;
|
|
|
+ int len = line->getLength();
|
|
|
+ line->release();
|
|
|
+ if( len == 0 )
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // parse body
|
|
|
+ if( !reader.istEnde() )
|
|
|
+ this->data = answer + reader.getLPosition();
|
|
|
+ ref = 1;
|
|
|
+}
|
|
|
+
|
|
|
+const char *Answer::getContentType() const
|
|
|
+{
|
|
|
+ return contentType;
|
|
|
+}
|
|
|
+
|
|
|
+const char *Answer::getData() const
|
|
|
+{
|
|
|
+ return data;
|
|
|
+}
|
|
|
+
|
|
|
+Answer *Answer::getThis()
|
|
|
+{
|
|
|
+ ref++;
|
|
|
+ return this;
|
|
|
+}
|
|
|
+
|
|
|
+Answer *Answer::release()
|
|
|
+{
|
|
|
+ if( !--ref )
|
|
|
+ delete this;
|
|
|
+ return 0;
|
|
|
+}
|