Browse Source

Http Post Request hinzugefügt

Kolja Strohm 5 years ago
parent
commit
61aaa2997f

+ 2 - 0
Network Linux.vcxproj

@@ -51,11 +51,13 @@
     <IntDir>$(ProjectDir)obj\$(Platform)\release\</IntDir>
   </PropertyGroup>
   <ItemGroup>
+    <ClCompile Include="Network\HttpRequest.cpp" />
     <ClCompile Include="Network\Klient.cpp" />
     <ClCompile Include="Network\Network.cpp" />
     <ClCompile Include="Network\Server.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="Network\HttpRequest.h" />
     <ClInclude Include="Network\Klient.h" />
     <ClInclude Include="Network\Network.h" />
     <ClInclude Include="Network\Server.h" />

+ 6 - 0
Network Linux.vcxproj.filters

@@ -18,6 +18,9 @@
     <ClCompile Include="Network\Klient.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="Network\HttpRequest.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Network\Server.h">
@@ -29,5 +32,8 @@
     <ClInclude Include="Network\Klient.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="Network\HttpRequest.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 124 - 0
Network/HttpRequest.cpp

@@ -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;
+}

+ 50 - 0
Network/HttpRequest.h

@@ -0,0 +1,50 @@
+#pragma once
+
+#include <Text.h>
+
+namespace Network
+{
+    namespace HTTP
+    {
+        class Answer
+        {
+        private:
+            Framework::Text protocol;
+            int statusNumber;
+            Framework::Text statusText;
+            Framework::Text date;
+            Framework::Text contentType;
+            Framework::Text header;
+            Framework::Text data;
+            int ref;
+
+        public:
+            Answer( const char *answer );
+
+            const char *getContentType() const;
+            const char *getData() const;
+
+            Answer *getThis();
+            Answer *release();
+        };
+
+        class PostRequest
+        {
+        private:
+            Framework::Text path;
+            Framework::Text host;
+            Framework::Text contentType;
+            Framework::Text data;
+            unsigned short port;
+            int ref;
+
+        public:
+            PostRequest( const char *path, const char *host, const char *data, const char *contentType, unsigned short port );
+
+            Answer *execute() const;
+
+            PostRequest *getThis();
+            PostRequest *release();
+        };
+    }
+}

+ 8 - 0
Network/Klient.cpp

@@ -3,6 +3,7 @@
 #include "Klient.h"
 #ifndef WIN32
 #include <string.h>
+#include <netdb.h>
 #endif
 #include <Key.h>
 #include <Datei.h>
@@ -73,6 +74,13 @@ bool Klient::verbinde( unsigned short port, const char *ip ) // verbindet mit Se
 		closesocket( sock );
 	sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
 	long sIp = inet_addr( ip ); // ip addresse
+    if( sIp == INADDR_NONE )
+    {
+        struct hostent* pHostInfo = gethostbyname( ip );
+        if( pHostInfo == 0 )
+            return 0;
+        sIp = *(long*)pHostInfo->h_addr_list[ 0 ];
+    }
 	memcpy( (char*)&server.sin_addr, &sIp, sizeof( sIp ) );
 	server.sin_port = htons( port ); // port
 	if( connect( sock, ( struct sockaddr* )&server, sizeof( server ) ) < 0 ) // verbinden

+ 2 - 0
Network/Network.vcxproj

@@ -176,11 +176,13 @@ copy "..\x64\Release\Network.dll" "..\..\..\Spiele Platform\SMP\Fertig\x64\netwo
     </CustomBuildStep>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClInclude Include="HttpRequest.h" />
     <ClInclude Include="Klient.h" />
     <ClInclude Include="Network.h" />
     <ClInclude Include="Server.h" />
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="HttpRequest.cpp" />
     <ClCompile Include="Klient.cpp" />
     <ClCompile Include="Network.cpp" />
     <ClCompile Include="Server.cpp" />

+ 6 - 0
Network/Network.vcxproj.filters

@@ -24,6 +24,9 @@
     <ClInclude Include="Klient.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="HttpRequest.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Server.cpp">
@@ -35,5 +38,8 @@
     <ClCompile Include="Network.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="HttpRequest.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 27 - 2
build.bat

@@ -1,6 +1,31 @@
+:DebugWin64
+SET RETURN=DebugWin64
+SET NEXT=ReleaseWin64
 "D:\Visual Studio 2017\MSBuild\15.0\Bin\MSBuild.exe" "Network/Network.vcxproj" /p:configuration=debug /p:platform=x64
+if errorlevel 1 GOTO Error
+:ReleaseWin64
+SET RETURN=ReleaseWin64
+SET NEXT=ReleaseWin32
 "D:\Visual Studio 2017\MSBuild\15.0\Bin\MSBuild.exe" "Network/Network.vcxproj" /p:configuration=release /p:platform=x64
+if errorlevel 1 GOTO Error
+:ReleaseWin32
+SET RETURN=ReleaseWin32
+SET NEXT=DebugLinux64
 "D:\Visual Studio 2017\MSBuild\15.0\Bin\MSBuild.exe" "Network/Network.vcxproj" /p:configuration=release /p:platform=win32
-
+if errorlevel 1 GOTO Error
+:DebugLinux64
+SET RETURN=DebugLinux64
+SET NEXT=ReleaseLinux64
 "D:\Visual Studio 2017\MSBuild\15.0\Bin\MSBuild.exe" "Network Linux.vcxproj" /t:rebuild /p:configuration=debug /p:platform=x64
-"D:\Visual Studio 2017\MSBuild\15.0\Bin\MSBuild.exe" "Network Linux.vcxproj" /t:rebuild /p:configuration=release /p:platform=x64
+if errorlevel 1 GOTO Error
+:ReleaseLinux64
+SET RETURN=ReleaseLinux64
+SET NEXT=End
+"D:\Visual Studio 2017\MSBuild\15.0\Bin\MSBuild.exe" "Network Linux.vcxproj" /t:rebuild /p:configuration=release /p:platform=x64
+if errorlevel 1 GOTO Error
+GOTO End
+:Error
+SET /p redo=Nochmal versuchen?(j/n):
+IF /I '%redo%' equ 'j' GOTO %RETURN%
+GOTO %NEXT%
+:End