some work on getting an http response from google.com

This commit is contained in:
2025-09-15 13:21:34 -05:00
parent 53b493299b
commit de04372cb0
4 changed files with 64 additions and 21 deletions

3
.gitignore vendored
View File

@@ -2,3 +2,6 @@ compile_commands.json
bin/**
obj/**
.cache/**
*.rad
*.idb
*.pdb

View File

@@ -1,14 +1 @@
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /r "C:\Program Files\Microsoft Visual Studio" %%G IN (*vcvars64.bat) DO (
SET "_file=%%G"
SET "_comp=!_file:\VC\=!"
IF NOT "!_comp!" == "!_file!" "!_file!"
)
FOR /r "C:\Program Files (x86)\Microsoft Visual Studio" %%G IN (*vcvars64.bat) DO (
SET "_file=%%G"
SET "_comp=!_file:\VC\=!"
IF NOT "!_comp!" == "!_file!" "!_file!"
)
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

View File

@@ -6,19 +6,36 @@
#include <cstdio>
#endif
#ifdef _WIN32
typedef SOCKET Socket;
#endif
#define DEBUG
typedef unsigned char byte;
#ifdef _WIN32
typedef SOCKET Socket;
bool init_socket_lib() {
// TODO: Should do some sort of smart version upgrade via data
// See https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-wsadata
WSADATA data;
if (WSAStartup((2 << 8) | 2, &data) != 0) {
printf("init_socket_lib failed: %d\n", WSAGetLastError());
WSACleanup();
return false;
}
return true;
}
#endif
extern bool socket_lib_initialized = init_socket_lib();
namespace tcp {
Socket make_socket();
bool connect(Socket& s, const char* hostname, int port);
bool connect(Socket &s, const char* hostname, int port);
bool send_transmit_stop(Socket &s);
bool send_read_stop(Socket &s);
bool disconnect(Socket &s);
bool send_data(Socket& s, byte* buf, int len);
bool get_data(Socket& s, byte* out_buf, int buf_size, int& out_num_written);
bool send_data(Socket &s, byte* buf, int len);
bool get_data(Socket &s, byte* out_buf, int buf_size, int& out_num_written);
#ifdef _WIN32
@@ -56,6 +73,8 @@ bool connect(Socket& s, const char *hostname, int port) {
if (code == SOCKET_ERROR) {
closesocket(s);
s = INVALID_SOCKET;
} else {
break;
}
ptr = ptr->ai_next;
} while (ptr);
@@ -65,12 +84,32 @@ bool connect(Socket& s, const char *hostname, int port) {
if (s == INVALID_SOCKET) {
printf("Unable to connect to %s:%d\n", hostname, port);
WSACleanup();
return 1;
return false;
}
return true;
}
bool send_transmit_stop(Socket &s) {
int code = shutdown(s, SD_SEND);
if (code == SOCKET_ERROR) {
printf("send_transmit_stop failed: %d\n", WSAGetLastError());
closesocket(s);
return false;
}
return true;
}
bool send_read_stop(Socket &s) {
int code = shutdown(s, SD_RECEIVE);
if (code == SOCKET_ERROR) {
printf("send_read_stop failed: %d\n", WSAGetLastError());
closesocket(s);
return false;
}
return true;
}
bool disconnect(Socket &s) {
int code = shutdown(s, SD_BOTH);
if (code == SOCKET_ERROR) {

View File

@@ -2,6 +2,20 @@
using namespace tcp;
const int buf_size = 1024*1024;
static byte buf[buf_size];
const char *basic_get_request = "GET / HTTP/1.1\nHOST: google.com\nUser-Agent:\ncoupon scraper\nAccept: */*";
int main() {
if (!socket_lib_initialized) return -1;
Socket s = make_socket();
if (!connect(s, "google.com", 80)) return -1;
if (!send_data(s, (byte*)basic_get_request, strlen(basic_get_request))) return -1;
int n_read;
if (!send_transmit_stop(s)) return -1;
if (!get_data(s, buf, buf_size, n_read)) return -1;
buf[n_read] = '\0';
printf("%s", (char*)buf);
}