-
Notifications
You must be signed in to change notification settings - Fork 1
/
wget.cpp
executable file
·272 lines (235 loc) · 6.27 KB
/
wget.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***
wget.cpp
As an alternative to the sockets code, here are functions that act as wrappers
to Wget. Can provide cookies, so might be easier to adapt to certain applications.
Also permits HTTPS, proxies, and does not require linking in any external sockets
libraries.
Also gives FetchURI(), which uses Wget on native builds and calls the appropriate
libcodehappy function for WASM builds.
Copyright (c) 2010-2024 Chris Street
***/
#include "libcodehappy.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define PURL '\"'
static bool __rinit = false;
char* load_file_mem(const char* fname, u32* filelen_out) {
FILE* f;
char* w;
u32 len = filelen(fname);
if (!is_null(filelen_out))
*filelen_out = len;
if (len <= 0)
return nullptr;
f = fopen(fname, "rb");
if (is_null(f))
return nullptr;
w = new char [len + 1];
fread(w, 1, len, f);
fclose(f);
w[len] = '\000';
return w;
}
char* temp_file_name(const char* extension) {
char* name;
if (!__rinit) {
srand(time(NULL));
__rinit = true;
}
name = new char [32];
forever {
sprintf(name, "tmp%04d%04d%s", rand() % 10000, rand() % 10000, ((extension == nullptr) ? "" : extension));
if (!FileExists(name))
break;
}
return name;
}
char* temp_file_name(const char* prefix, const char* extension) {
char* name;
if (!__rinit) {
srand(time(NULL));
__rinit = true;
}
int ialloc = strlen(prefix) + strlen(extension) + 10;
ialloc = std::max(256, ialloc);
name = new char [ialloc];
forever {
sprintf(name, "%s%04d%04d%s", prefix, rand() % 10000, rand() % 10000, ((extension == nullptr) ? "" : extension));
if (!FileExists(name))
break;
}
return name;
}
static void DoublePercent(const char *u, char *d) {
int e;
int f;
e = 0;
f = 0;
while (u[e]) {
d[f] = u[e];
f++;
if (u[e] == '%') {
d[f] = u[e];
f++;
}
e++;
}
d[f] = '\000';
}
#ifdef CODEHAPPY_WINDOWS
#define SILENT " >nul"
#define BATCH_XTN ".bat"
static char default_wget_loc[] = "C:\\wget\\wget.exe";
#else
#define SILENT " >/dev/null"
#define BATCH_XTN ".sh"
static char default_wget_loc[] = "wget";
#endif
static const char* user_wget_loc = NULL;
const char* get_wget_location(void) {
if (not_null(user_wget_loc))
return(user_wget_loc);
return (default_wget_loc);
}
void set_wget_location(const char* loc) {
user_wget_loc = loc;
}
char* WgetFileCookies(const char *url, bool timeout, bool silent, char *cookies, bool again, u32* filelen_out, bool check_cert) {
/* made much more robust in the face of network connections that may occasionally drop -- CS, 20100609 */
char *durl;
char *cmd;
char* fname;
char* bname;
char* sname;
bool tryagain;
int att;
const int max_attempts = 3;
FILE *f;
u32 flen;
unsigned char *status;
char* ret;
std::string c_cert = (check_cert ? "" : "--no-check-certificate");
att = 0;
durl = new char [strlen(url) * 2 + 1];
NOT_NULL_OR_RETURN(durl, durl);
cmd = new char [strlen(url) * 2 + 1024];
if (is_null(cmd)) {
delete durl;
return nullptr;
}
do {
#ifdef CODEHAPPY_WINDOWS
DoublePercent(url, durl);
#else
strcpy(durl, url);
#endif
fname = temp_file_name(nullptr);
bname = temp_file_name(BATCH_XTN);
sname = temp_file_name(nullptr);
#define WGET_CH_USER_AGENT "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0"
#ifdef CODEHAPPY_WINDOWS
f = fopen(bname, "w");
fprintf(f,
"%s --user-agent=\"" WGET_CH_USER_AGENT "\" %s %s%s %s %s -O %s %c%s%c 1>> %s 2>&1\n",
get_wget_location(),
c_cert.c_str(),
(cookies != NULL) ? "--load-cookies " : "",
(cookies != NULL) ? cookies : "",
timeout ? "-T5" : "",
silent ? "-q" : "",
fname,
PURL,
durl,
PURL,
sname);
fclose(f);
sprintf(cmd, "%s%s", bname, silent ? SILENT : "");
system(cmd);
remove(bname);
#else
sprintf(cmd,
"%s --user-agent=\"" WGET_CH_USER_AGENT "\" %s %s%s %s %s -O %s %c%s%c > %s%s",
get_wget_location(),
c_cert.c_str(),
(cookies != NULL) ? "--load-cookies " : "",
(cookies != NULL) ? cookies : "",
timeout ? "-T5" : "",
silent ? "-q" : "",
fname,
PURL,
durl,
PURL,
sname,
silent ? " 2>/dev/null" : "");
system(cmd);
#endif
delete [] bname;
tryagain = again;
// read the status
flen = filelen(sname);
f = fopen(sname, "rb");
NOT_NULL_OR_RETURN(f, nullptr);
status = new unsigned char[flen + 1];
fread(status, 1, flen, f);
status[flen] = 0;
fclose(f);
if (status && strstr((char *)status, "failed: Unknown host")) {
// we have lost the connection. Wait 10 seconds and then try again.
sleep(10);
tryagain = true;
} else {
tryagain = false;
}
if (status)
delete [] status;
remove(sname);
delete [] sname;
if (!tryagain)
ret = load_file_mem(fname, filelen_out);
remove(fname);
delete [] fname;
att++;
} while (tryagain && att < max_attempts);
delete durl;
delete cmd;
return(ret);
}
char* WgetFile(const char *url, bool timeout, bool silent, bool again, u32* flen, bool check_cert) {
return WgetFileCookies(url, timeout, silent, NULL, again, flen, check_cert);
}
/* A function that works on both native and WASM builds. This will fetch the
requested URI into a RamFile. */
RamFile* FetchURI(const char* URI, bool check_cert) {
RamFile* ret;
#ifdef CODEHAPPY_NATIVE
// Native builds use Wget.
u32 flen;
char* buf = WgetFile(URI, true, true, false, &flen, check_cert);
NOT_NULL_OR_RETURN(buf, nullptr);
ret = new RamFile;
ret->open_static(buf, flen, RAMFILE_DEFAULT);
#else
// WebAssembly builds use codehappy_URI_fetch().
ret = codehappy_URI_fetch(URI);
#endif
return ret;
}
RamFile* FetchURI(const std::string& URI, bool check_cert) {
return FetchURI(URI.c_str(), check_cert);
}
bool FetchURIToFile(const char* URI, const char* out_path, bool check_cert) {
RamFile* rf = FetchURI(URI, check_cert);
if (is_null(rf) || is_null(rf->buffer()))
return false;
rf->write_to_file(out_path);
delete rf;
return true;
}
bool FetchURIToFile(const std::string& URI, const std::string& out_path, bool check_cert) {
return FetchURIToFile(URI.c_str(), out_path.c_str(), check_cert);
}
/*** end wget.cpp ***/