From de1a7f80f2df879c0b9276fa3dfedb7c06b833cc Mon Sep 17 00:00:00 2001 From: Anonymous275 Date: Tue, 17 Mar 2020 23:44:58 +0200 Subject: [PATCH] Native HTTP get request implementation --- CMakeLists.txt | 2 +- http.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 10 +++++---- 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 http.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f4a5221..e3c8a6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,4 +4,4 @@ project(BeamNG-MP-Launcher) set(CMAKE_CXX_STANDARD 14) -add_executable(BeamNG-MP-Launcher main.cpp enet.h proxy.cpp Security.cpp) +add_executable(BeamNG-MP-Launcher main.cpp enet.h proxy.cpp Security.cpp http.cpp) diff --git a/http.cpp b/http.cpp new file mode 100644 index 0000000..c629998 --- /dev/null +++ b/http.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +std::string HTTP_REQUEST(){ + + WSADATA wsaData; + SOCKET Socket; + SOCKADDR_IN SockAddr; + int lineCount=0; + int rowCount=0; + struct hostent *host; + std::locale local; + char buffer[10000]; + int i = 0 ; + int nDataLength; + + std::string website_HTML; + + std::string url = "s1.yourthought.co.uk"; + + std::string get_http = "GET /servers-info HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n"; + + if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0){ + std::cout << "WSAStartup failed.\n"; + system("pause"); + //return 1; + } + + Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); + host = gethostbyname(url.c_str()); + + SockAddr.sin_port=htons(3599); //PORT + SockAddr.sin_family=AF_INET; + SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr); + + if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){ + std::cout << "Could not connect"; + //system("pause"); + //return 1; + } + + send(Socket,get_http.c_str(), strlen(get_http.c_str()),0 ); + + while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){ + int i = 0; + while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){ + + website_HTML+=buffer[i]; + i += 1; + } + } + + closesocket(Socket); + WSACleanup(); + + return website_HTML; +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index 853ee77..5df09c1 100644 --- a/main.cpp +++ b/main.cpp @@ -6,23 +6,25 @@ #include #include - std::vector Check(); void Exit(const std::string& Msg){ std::cout << Msg << std::endl; - std::cout << "Press Enter to Continue..."; + std::cout << "Press Enter to continue . . ."; std::cin.ignore(); exit(-1); } void ProxyStart(); +std::string HTTP_REQUEST(); int main() { //Security std::vector Data = Check(); - std::cout << "You own BeamNG on this machine!" << std::endl; - std::cout << Data.at(1) << "\\BeamNG.drive.exe" << std::endl; + std::cout << Data.at(1) << "\\BeamNG.drive.exe"; + std::cout << "\nHTTP TEST :\n\n"; + + std::cout << HTTP_REQUEST(); /// Update, Mods ect...