Browse Source

Http request beschleuniegt

Kolja Strohm 5 years ago
parent
commit
6661e29c84
1 changed files with 54 additions and 0 deletions
  1. 54 0
      Network/HttpRequest.cpp

+ 54 - 0
Network/HttpRequest.cpp

@@ -1,5 +1,7 @@
 #include "HttpRequest.h"
 #include "HttpRequest.h"
 #include "Klient.h"
 #include "Klient.h"
+#include <Zeit.h>
+#include <iostream>
 
 
 using namespace Framework;
 using namespace Framework;
 using namespace Network;
 using namespace Network;
@@ -18,6 +20,8 @@ PostRequest::PostRequest( const char *path, const char *host, const char *data,
 
 
 Answer *PostRequest::execute() const
 Answer *PostRequest::execute() const
 {
 {
+    ZeitMesser zm;
+    zm.messungStart();
     Text message = "POST ";
     Text message = "POST ";
     message += path;
     message += path;
     message += " HTTP/1.1\r\n";
     message += " HTTP/1.1\r\n";
@@ -29,19 +33,69 @@ Answer *PostRequest::execute() const
     message += data.getLength();
     message += data.getLength();
     message += "\r\n\r\n";
     message += "\r\n\r\n";
     message += data;
     message += data;
+    zm.messungEnde();
+    std::cout << "http request string erstellen: " << zm.getSekunden() << "\n";
+    zm.messungStart();
     Klient httpK;
     Klient httpK;
     if( !httpK.verbinde( port, host ) )
     if( !httpK.verbinde( port, host ) )
         return 0;
         return 0;
+    zm.messungEnde();
+    std::cout << "verbindung erstellen: " << zm.getSekunden() << "\n";
+    zm.messungStart();
     httpK.sende( message, message.getLength() );
     httpK.sende( message, message.getLength() );
+    zm.messungEnde();
+    std::cout << "request senden: " << zm.getSekunden() << "\n";
+    zm.messungStart();
+    int length = -1;
+    bool lastn = 0;
     Text answer;
     Text answer;
     do {
     do {
         char buff[ 2 ];
         char buff[ 2 ];
         buff[ 1 ] = 0;
         buff[ 1 ] = 0;
         if( httpK.getNachricht( buff, 1 ) )
         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;
             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
         else
             break;
             break;
     } while( httpK.hatNachricht( 1000 ) );
     } while( httpK.hatNachricht( 1000 ) );
+    zm.messungEnde();
+    std::cout << "nachricht empfangen: " << zm.getSekunden() << "\n";
     return new Answer( answer );
     return new Answer( answer );
 }
 }