Extract HTTP request functionality.

This commit is contained in:
Bartosz Taudul 2020-09-12 12:46:00 +02:00
parent 5e43ad84b9
commit 80e0941520
5 changed files with 82 additions and 12 deletions

View File

@ -156,6 +156,7 @@
<ClCompile Include="..\..\..\zstd\zstd_ldm.c" />
<ClCompile Include="..\..\..\zstd\zstd_opt.c" />
<ClCompile Include="..\..\libs\gl3w\GL\gl3w.c" />
<ClCompile Include="..\..\src\HttpRequest.cpp" />
<ClCompile Include="..\..\src\imgui_freetype.cpp" />
<ClCompile Include="..\..\src\imgui_impl_glfw.cpp" />
<ClCompile Include="..\..\src\imgui_impl_opengl3.cpp" />
@ -260,6 +261,7 @@
<ClInclude Include="..\..\src\Arimo.hpp" />
<ClInclude Include="..\..\src\Cousine.hpp" />
<ClInclude Include="..\..\src\FontAwesomeSolid.hpp" />
<ClInclude Include="..\..\src\HttpRequest.hpp" />
<ClInclude Include="..\..\src\icon.hpp" />
<ClInclude Include="..\..\src\imgui_freetype.h" />
<ClInclude Include="..\..\src\imgui_impl_glfw.h" />

View File

@ -207,6 +207,9 @@
<ClCompile Include="..\..\..\server\TracyMouse.cpp">
<Filter>server</Filter>
</ClCompile>
<ClCompile Include="..\..\src\HttpRequest.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -503,6 +506,9 @@
<ClInclude Include="..\..\src\NativeWindow.hpp">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\src\HttpRequest.hpp">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="DebugVis.natvis" />

View File

@ -0,0 +1,56 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "../common/TracySocket.hpp"
#include "../server/TracyVersion.hpp"
#include "HttpRequest.hpp"
static constexpr char CRLF[2] = { '\r', '\n' };
void HttpRequest( const char* server, const char* resource, int port, std::function<void(int, char*)> cb )
{
tracy::Socket sock;
if( !sock.ConnectBlocking( server, port ) ) return;
char request[4096];
const auto len = sprintf( request, "GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: Tracy Profiler %i.%i.%i\r\nConnection: close\r\nCache-Control: no-cache, no-store, must-revalidate\r\n\r\n", resource, server, tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch );
sock.Send( request, len );
char response[4096];
const auto sz = sock.ReadUpTo( response, 4096, 15 );
if( sz < 13 ) return;
if( memcmp( response, "HTTP/1.1 200", 12 ) != 0 ) return;
auto hdr = response + 13;
int contentLength = 0;
for(;;)
{
while( memcmp( hdr, CRLF, 2 ) != 0 ) hdr++;
hdr += 2;
if( memcmp( hdr, "Content-Length: ", 16 ) == 0 )
{
hdr += 16;
contentLength = atoi( hdr );
break;
}
}
assert( contentLength != 0 );
for(;;)
{
while( memcmp( hdr, CRLF, 2 ) != 0 ) hdr++;
hdr += 2;
if( memcmp( hdr, CRLF, 2 ) == 0 )
{
hdr += 2;
break;
}
hdr += 2;
}
const auto hdrSize = hdr - response;
const auto partSize = sz - hdrSize;
char* data = new char[contentLength];
memcpy( data, hdr, partSize );
auto remaining = contentLength - partSize;
if( remaining > 0 ) sock.Read( data + partSize, remaining, 15 );
cb( contentLength, data );
}

View File

@ -0,0 +1,8 @@
#ifndef __HTTPREQUEST_HPP__
#define __HTTPREQUEST_HPP__
#include <functional>
void HttpRequest( const char* server, const char* resource, int port, std::function<void(int, char*)> cb );
#endif

View File

@ -51,6 +51,7 @@
#include "icon.hpp"
#include "ResolvService.hpp"
#include "NativeWindow.hpp"
#include "HttpRequest.hpp"
static void glfw_error_callback(int error, const char* description)
{
@ -227,18 +228,15 @@ int main( int argc, char** argv )
mainThread = std::this_thread::get_id();
updateThread = std::thread( [] {
tracy::Socket sock;
if( !sock.ConnectBlocking( "51.89.23.220", 8099 ) ) return;
char request[1024];
const auto len = sprintf( request, "GET /tracy/version HTTP/1.1\r\nHost: 51.89.23.220\r\nUser-Agent: Tracy Profiler %i.%i.%i\r\nConnection: close\r\nCache-Control: no-cache, no-store, must-revalidate\r\n\r\n", tracy::Version::Major, tracy::Version::Minor, tracy::Version::Patch );
sock.Send( request, len );
char response[1024];
const auto sz = sock.ReadUpTo( response, 1024, 15 );
if( sz < 13 ) return;
if( memcmp( response, "HTTP/1.1 200", 12 ) != 0 ) return;
uint32_t ver;
memcpy( &ver, response + sz - 4, 4 );
RunOnMainThread( [ver] { updateVersion = ver; } );
HttpRequest( "51.89.23.220", "/tracy/version", 8099, [] ( int size, char* data ) {
if( size == 4 )
{
uint32_t ver;
memcpy( &ver, data, 4 );
RunOnMainThread( [ver] { updateVersion = ver; } );
}
delete[] data;
} );
} );
// Setup window