webpage/main.c

96 lines
1.9 KiB
C
Raw Normal View History

2023-12-30 20:33:23 +01:00
#include <stddef.h>
2023-12-29 14:13:05 +01:00
#include <stdio.h>
2023-12-30 14:30:29 +01:00
#include <stdlib.h>
2023-12-30 14:09:04 +01:00
#include <string.h>
2023-12-30 18:01:32 +01:00
#include <curl/curl.h>
2023-12-30 20:33:23 +01:00
#include <assert.h>
2023-12-30 14:09:04 +01:00
2023-12-30 20:33:23 +01:00
#define DEFAULT_PAGE_LEN 16
2023-12-30 18:01:32 +01:00
int pageChunkLen = DEFAULT_PAGE_LEN;
char** pageChunk;
2023-12-30 14:09:04 +01:00
2023-12-30 14:30:29 +01:00
typedef struct UserConfig {
int port;
2023-12-30 18:01:32 +01:00
char* addr;
2023-12-30 14:30:29 +01:00
} UserConfig;
2023-12-30 18:01:32 +01:00
2023-12-30 14:30:29 +01:00
UserConfig config = {
.port = 443,
2023-12-30 18:01:32 +01:00
.addr = "https://github.com/CaptainBoulbi/webpage",
2023-12-30 14:30:29 +01:00
};
2023-12-30 18:01:32 +01:00
void getUserConfig(int argc, char* argv[]){
for (int i=1; i<argc; i++){
if (strcmp(argv[i], "-p") == 0){
if (argc <= (i+1)){
perror("ERROR: Port not defined by user");
exit(1);
}
config.port = atoi(argv[++i]);
} else {
config.addr = argv[i];
}
2023-12-30 14:09:04 +01:00
}
2023-12-30 18:01:32 +01:00
}
2023-12-30 14:09:04 +01:00
2023-12-30 20:33:23 +01:00
size_t save_chunk(char* buffer, size_t itemsize, size_t nitems, void* ignore){
2023-12-30 18:01:32 +01:00
size_t bytes = itemsize * nitems;
static int nbchuck = 0;
2023-12-30 14:09:04 +01:00
2023-12-30 18:01:32 +01:00
if (nbchuck >= pageChunkLen){
2023-12-30 20:33:23 +01:00
pageChunkLen *= 1.5;
pageChunk = realloc(pageChunk, sizeof(*pageChunk) * pageChunkLen);
printf("len = %d\n", pageChunkLen);
2023-12-30 18:01:32 +01:00
if (pageChunk == NULL){
perror("ERROR: Buy more ram.\n");
exit(1);
}
2023-12-30 14:09:04 +01:00
}
2023-12-30 20:33:23 +01:00
pageChunk[nbchuck] = malloc(bytes);
2023-12-30 18:01:32 +01:00
pageChunk[nbchuck][bytes] = '\0';
2023-12-30 20:33:23 +01:00
strncpy(pageChunk[nbchuck], buffer, bytes);
2023-12-30 18:01:32 +01:00
nbchuck++;
return bytes;
}
2023-12-30 14:09:04 +01:00
2023-12-30 18:01:32 +01:00
void getPage(void){
CURL* curl = curl_easy_init();
if (curl == NULL){
perror("ERROR: curl init failed.\n");
exit(1);
2023-12-30 14:09:04 +01:00
}
2023-12-30 18:01:32 +01:00
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){
2023-12-30 20:33:23 +01:00
fprintf(stderr, "CURL ERROR: %s\n", curl_easy_strerror(result));
2023-12-30 14:09:04 +01:00
}
2023-12-30 18:01:32 +01:00
curl_easy_cleanup(curl);
2023-12-30 14:09:04 +01:00
}
2023-12-29 14:13:05 +01:00
2023-12-30 18:01:32 +01:00
void printPage(void){
2023-12-30 20:33:23 +01:00
for (int i=0; i<pageChunkLen && pageChunk[i] != NULL; i++){
2023-12-30 18:01:32 +01:00
printf("%s", pageChunk[i]);
2023-12-30 14:30:29 +01:00
}
}
int main(int argc, char* argv[]){
2023-12-30 18:01:32 +01:00
pageChunk = malloc(sizeof(*pageChunk) * DEFAULT_PAGE_LEN);
2023-12-30 14:30:29 +01:00
getUserConfig(argc, argv);
2023-12-30 14:09:04 +01:00
getPage();
2023-12-30 18:01:32 +01:00
printPage();
2023-12-29 14:13:05 +01:00
return 0;
}