forked from tsoding/crepl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetlib.h
More file actions
165 lines (132 loc) · 4.95 KB
/
netlib.h
File metadata and controls
165 lines (132 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Check if the path is a URL
int is_url(const char* path) {
return (strncmp(path, "http://", 7) == 0 ||
strncmp(path, "https://", 8) == 0);
}
// Struct to store downloaded data
typedef struct {
char *data;
size_t size;
} MemoryBuffer;
// Callback function for libcurl to write data to memory
static size_t write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t total_size = size * nmemb;
MemoryBuffer *buffer = (MemoryBuffer *)userp;
// Reallocate buffer to accommodate new data
char *new_data = realloc(buffer->data, buffer->size + total_size + 1);
if (!new_data) {
fprintf(stderr, "ERROR: Memory reallocation failed (%zu + %zu bytes)\n",
buffer->size, total_size);
return 0; // Return 0 to indicate failure to libcurl
}
buffer->data = new_data;
memcpy(buffer->data + buffer->size, contents, total_size);
buffer->size += total_size;
buffer->data[buffer->size] = '\0'; // Always null-terminate
return total_size;
}
// Initialize memory buffer
static int init_memory_buffer(MemoryBuffer *buffer) {
buffer->data = malloc(1); // Start with minimal allocation
if (!buffer->data) {
fprintf(stderr, "ERROR: Initial memory allocation failed\n");
return -1;
}
buffer->data[0] = '\0';
buffer->size = 0;
return 0;
}
// Download content from URL using libcurl
char* download_from_url(const char* url) {
CURL *curl = NULL;
CURLcode res;
MemoryBuffer buffer = {0};
char *result = NULL;
// Validate input
if (!url || strlen(url) == 0) {
fprintf(stderr, "ERROR: Invalid URL provided\n");
return NULL;
}
printf("Downloading from: %s\n", url);
// Initialize memory buffer
if (init_memory_buffer(&buffer) != 0) {
return NULL;
}
// Initialize libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "ERROR: Failed to initialize CURL\n");
goto cleanup;
}
// Configure curl options
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buffer);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "malcrepl/1.0");
// Security: Ignore SSL certificate verification (for self-signed certs)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
// Performance and reliability options
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); // Fail on HTTP errors (4xx, 5xx)
// Optional: Enable for debugging
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// Execute the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "ERROR: Download failed: %s\n", curl_easy_strerror(res));
// Provide more specific error messages for common cases
if (res == CURLE_COULDNT_CONNECT) {
fprintf(stderr, "Hint: Check if the server is running and accessible\n");
} else if (res == CURLE_SSL_CONNECT_ERROR) {
fprintf(stderr, "Hint: SSL/TLS connection issue - check certificate configuration\n");
} else if (res == CURLE_OPERATION_TIMEDOUT) {
fprintf(stderr, "Hint: Connection timed out - check network connectivity\n");
}
goto cleanup;
}
// Verify we actually got data
if (buffer.size == 0) {
fprintf(stderr, "ERROR: Empty response received from server\n");
goto cleanup;
}
printf("Downloaded %zu bytes successfully\n", buffer.size);
// Return the data (transfer ownership to caller)
result = buffer.data;
buffer.data = NULL; // Prevent double-free in cleanup
cleanup:
// Cleanup libcurl resources
if (curl) {
curl_easy_cleanup(curl);
}
curl_global_cleanup();
// Cleanup buffer if not transferred to result
if (buffer.data) {
free(buffer.data);
}
return result;
}
// Optional: Function to get HTTP status code (for debugging)
int get_http_status(const char* url) {
CURL *curl;
CURLcode res;
long http_code = 0;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L); // HEAD request
res = curl_easy_perform(curl);
if (res == CURLE_OK) {
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return (int)http_code;
}