#include "HttpRequest.h" #include "Klient.h" #include #include 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 { ZeitMesser zm; zm.messungStart(); Text message = "POST "; message += path; message += " HTTP/1.1\r\n"; message += "Host: "; message += host; message += "\r\nContent-Type: "; message += contentType; message += "; charset=latin-1\r\nContent-Length: "; message += data.getLength(); message += "\r\n\r\n"; message += data; zm.messungEnde(); std::cout << "http request string erstellen: " << zm.getSekunden() << "\n"; zm.messungStart(); Klient httpK; if( !httpK.verbinde( port, host ) ) return 0; zm.messungEnde(); std::cout << "verbindung erstellen: " << zm.getSekunden() << "\n"; zm.messungStart(); httpK.sende( message, message.getLength() ); zm.messungEnde(); std::cout << "request senden: " << zm.getSekunden() << "\n"; zm.messungStart(); int length = -1; bool lastn = 0; Text answer; do { char buff[ 2 ]; buff[ 1 ] = 0; if( httpK.getNachricht( buff, 1 ) ) { if( !answer.getText() || !answer.getText()[ 0 ] ) { zm.messungEnde(); std::cout << "auf antwort warten: " << zm.getSekunden() << "\n"; zm.messungStart(); } answer += buff; if( buff[ 0 ] == '\n' ) { if( length <= 0 ) { int p = answer.positionVon( "Content-Length: " ); if( p >= 0 ) { p += 16; Text *len = answer.getTeilText( p ); len->remove( len->positionVon( "\n" ), len->getLength() ); len->remove( "\r" ); length = *len; len->release(); } } else if( lastn ) { // header der antwort ist vorbei char *buff = new char[ length + 1 ]; buff[ length ] = 0; if( length > 0 ) httpK.getNachricht( buff, length ); answer += buff; delete[] buff; break; } lastn = 1; } else if( buff[ 0 ] != '\r' ) lastn = 0; } else break; } while( httpK.hatNachricht( 1000 ) ); zm.messungEnde(); std::cout << "nachricht empfangen: " << zm.getSekunden() << "\n"; return new Answer( answer ); } PostRequest *PostRequest::getThis() { ref++; return this; } PostRequest *PostRequest::release() { if( !--ref ) delete this; return 0; } Answer::Answer( const char *answer ) { all = answer; TextReader reader( new Text( answer ) ); // parse header Text *line = reader.leseZeile(); line->remove( "\r\n" ); line->remove( "\n" ); 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(); line->remove( "\r\n" ); line->remove( "\n" ); if( line->getLength() > 0 ) { header += *line; header += "\n"; } if( line->positionVon( "Date:" ) == 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; } int Answer::getStatusCode() const { return statusNumber; } const char *Answer::getStatusText() const { return statusText; } const char *Answer::getDate() const { return date; } const char *Answer::getAll() const { return all; } Answer *Answer::getThis() { ref++; return this; } Answer *Answer::release() { if( !--ref ) delete this; return 0; }