瀏覽代碼

Http request beschleuniegt

Kolja Strohm 5 年之前
父節點
當前提交
6661e29c84
共有 1 個文件被更改,包括 54 次插入0 次删除
  1. 54 0
      Network/HttpRequest.cpp

+ 54 - 0
Network/HttpRequest.cpp

@@ -1,5 +1,7 @@
 #include "HttpRequest.h"
 #include "Klient.h"
+#include <Zeit.h>
+#include <iostream>
 
 using namespace Framework;
 using namespace Network;
@@ -18,6 +20,8 @@ PostRequest::PostRequest( const char *path, const char *host, const char *data,
 
 Answer *PostRequest::execute() const
 {
+    ZeitMesser zm;
+    zm.messungStart();
     Text message = "POST ";
     message += path;
     message += " HTTP/1.1\r\n";
@@ -29,19 +33,69 @@ Answer *PostRequest::execute() const
     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 );
 }