From 194299d5227862df6098f492e3555516fb3cca79 Mon Sep 17 00:00:00 2001 From: _N3m0 Date: Sat, 30 Dec 2023 18:01:32 +0100 Subject: [PATCH] get small page with curl --- .gitignore | 1 + Makefile | 4 +- main.c | 122 ++++++++++++++++++++++++++--------------------------- 3 files changed, 63 insertions(+), 64 deletions(-) diff --git a/.gitignore b/.gitignore index be7aca5..38e025a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ webpage +out diff --git a/Makefile b/Makefile index fcfddf3..484deed 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SRC=main.c BIN=webpage -LIB=ssl -FLAGS=-Wall -Wextra +LIB=curl +FLAGS=-Wall -Wextra -Og -g all: $(BIN) diff --git a/main.c b/main.c index 1dcbc00..a44c016 100644 --- a/main.c +++ b/main.c @@ -1,73 +1,23 @@ #include #include -#include -#include -#include #include +#include -char page[1024]; +#define DEFAULT_PAGE_LEN 4 + +int pageChunkLen = DEFAULT_PAGE_LEN; +char** pageChunk; typedef struct UserConfig { int port; - int addr; + char* addr; } UserConfig; + UserConfig config = { .port = 443, - .addr = 0x08080808, + .addr = "https://github.com/CaptainBoulbi/webpage", }; -void getPage(void){ - int sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd == -1){ - perror("ERROR: Could not create socket.\n"); - exit(-1); - } - - struct sockaddr_in addr = { - .sin_family = AF_INET, - .sin_port = htons(config.port), - .sin_addr = htonl(config.addr), - }; - - if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(addr)) == -1){ - perror("ERROR: Could not connect.\n"); - exit(-1); - } - - SSL_CTX* ctx = SSL_CTX_new(TLS_method()); - if (ctx == NULL){ - perror("ERROR: Could not create SSL_CTX object.\n"); - exit(-1); - } - SSL* ssl = SSL_new(ctx); - if (ssl == NULL){ - perror("ERROR: Could not create SSL structure.\n"); - exit(-1); - } - if (SSL_set_fd(ssl, sockfd) != 1){ - perror("ERROR: Could not connect SSL object file descriptor.\n"); - exit(-1); - } - if (SSL_connect(ssl) != 1){ - perror("ERROR: Could not initiate TLS/SSL connection.\n"); - exit(-1); - } - - char* request = "GET /\r\n\r\n"; - if (SSL_write(ssl, request, strlen(request)) <= 0){ - perror("ERROR: Could not send request.\n"); - exit(-1); - } - char buffer[1024] = {0}; - int readbyte = SSL_read(ssl, buffer, 1023); - if (readbyte <= 0){ - perror("ERROR: Could not read from connection.\n"); - exit(-1); - } - - strcpy(page, buffer); -} - void getUserConfig(int argc, char* argv[]){ for (int i=1; i= pageChunkLen){ + pageChunkLen = (nbchuck+1) * 1.5; + pageChunk = realloc(pageChunk, sizeof(*pageChunk)*pageChunkLen); + if (pageChunk == NULL){ + perror("ERROR: Buy more ram.\n"); + exit(1); + } + } + pageChunk[nbchuck] = malloc(bytes); + + strncpy(pageChunk[nbchuck], buffer, bytes); + pageChunk[nbchuck][bytes] = '\0'; + nbchuck++; + + return bytes; +} + +void getPage(void){ + CURL* curl = curl_easy_init(); + if (curl == NULL){ + perror("ERROR: curl init failed.\n"); + exit(1); + } + + curl_easy_setopt(curl, CURLOPT_URL, config.addr); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_chunk); + + CURLcode result = curl_easy_perform(curl); + if (result != CURLE_OK){ + fprintf(stderr, "ERROR: %s\n", curl_easy_strerror(result)); + } + + curl_easy_cleanup(curl); +} + +void printPage(void){ + int i=0; + for (i=0; i