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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/* ------------------------------------------------------------ */
/*
HTTrack Website Copier, Offline Browser for Windows and Unix
Copyright (C) 1998-2016 Xavier Roche and other contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Please visit our Website: http://www.httrack.com
*/
/* ------------------------------------------------------------ */
/* File: ProxyTrack, httrack cache-based proxy */
/* Author: Xavier Roche */
/* ------------------------------------------------------------ */
#ifndef WEBHTTRACK_PROXYTRACK
#define WEBHTTRACK_PROXYTRACK
/* Version */
#define PROXYTRACK_VERSION "0.5"
/* Store manager */
#include "../minizip/mztools.h"
#include "store.h"
#include <sys/stat.h>
#ifndef HTS_DO_NOT_USE_FTIME
#ifdef _WIN32
#include <sys/utime.h>
#else
#include <utime.h>
#endif
#include <sys/timeb.h>
#else
#include <utime.h>
#endif
#ifndef _WIN32
#include <pthread.h>
#endif
#include <stdarg.h>
/* generic */
int proxytrack_main(char *proxyAddr, int proxyPort, char *icpAddr, int icpPort,
PT_Indexes index);
/* Spaces: CR,LF,TAB,FF */
#define is_space(c) ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11) || ((c)=='\'') )
#define is_realspace(c) ( ((c)==' ') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11) )
#define is_taborspace(c) ( ((c)==' ') || ((c)==9) )
#define is_quote(c) ( ((c)=='\"') || ((c)=='\'') )
#define is_retorsep(c) ( ((c)==10) || ((c)==13) || ((c)==9) )
/* Static definitions */
HTS_UNUSED static void proxytrack_print_log(const char *severity, const char *format, ...) {
if (severity != NULL) {
const int error = errno;
FILE *const fp = stderr;
va_list args;
fprintf(fp, " * %s: ", severity);
va_start(args, format);
(void) vfprintf(fp, format, args);
va_end(args);
fputs("\n", fp);
fflush(fp);
errno = error;
}
}
#define CRITICAL "critical"
#define WARNING "warning"
#define LOG "log"
#if defined(_DEBUG) || defined(DEBUG)
#define DEBUG "debug"
#else
#define DEBUG NULL
#endif
/* Header for generated pages */
#define PROXYTRACK_COMMENT_HEADER \
"<!-- Generated by ProxyTrack " PROXYTRACK_VERSION " -->\r\n" \
"<!-- This is an add-on for HTTrack " HTTRACK_VERSIONID " -->\r\n"
/* See IE "feature" (MSKB Q294807) */
#define DISABLE_IE_FRIENDLY_HTTP_ERROR_MESSAGES \
"<!-- Start Disable IE Friendly HTTP Error Messages -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- _-._.--._._-._.--._._-._.--._._-._.--._._-._.--._. -->\r\n" \
"<!-- End Disable IE Friendly HTTP Error Messages -->\r\n"
HTS_UNUSED static const char *gethomedir(void) {
const char *home = getenv("HOME");
if (home)
return home;
else
return ".";
}
HTS_UNUSED static int linput(FILE * fp, char *s, int max) {
int c;
int j = 0;
do {
c = fgetc(fp);
if (c != EOF) {
switch (c) {
case 13:
break; // sauter CR
case 10:
c = -1;
break;
case 0:
case 9:
case 12:
break; // sauter ces caractères
default:
s[j++] = (char) c;
break;
}
}
} while((c != -1) && (c != EOF) && (j < (max - 1)));
s[j] = '\0';
return j;
}
HTS_UNUSED static int link_has_authority(const char *lien) {
const char *a = lien;
if (isalpha((const unsigned char) *a)) {
// Skip scheme?
while(isalpha((const unsigned char) *a))
a++;
if (*a == ':')
a++;
else
return 0;
}
if (strncmp(a, "//", 2) == 0)
return 1;
return 0;
}
HTS_UNUSED static const char *jump_protocol(const char *source) {
int p;
// scheme
// "Comparisons of scheme names MUST be case-insensitive" (RFC2616)
if ((p = strfield(source, "http:")))
source += p;
else if ((p = strfield(source, "ftp:")))
source += p;
else if ((p = strfield(source, "https:")))
source += p;
else if ((p = strfield(source, "file:")))
source += p;
// net_path
if (strncmp(source, "//", 2) == 0)
source += 2;
return source;
}
HTS_UNUSED static const char *strrchr_limit(const char *s, char c, const char *limit) {
if (limit == NULL) {
char *p = strrchr(s, c);
return p ? (p + 1) : NULL;
} else {
char *a = NULL, *p;
for(;;) {
p = strchr((a) ? a : s, c);
if ((p >= limit) || (p == NULL))
return a;
a = p + 1;
}
}
}
HTS_UNUSED static const char *jump_protocol_and_auth(const char *source) {
const char *a, *trytofind;
if (strcmp(source, "file://") == 0)
return source;
a = jump_protocol(source);
trytofind = strrchr_limit(a, '@', strchr(a, '/'));
return (trytofind != NULL) ? trytofind : a;
}
#ifndef min
#define min(a,b) ((a)>(b)?(b):(a))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
HTS_UNUSED static int linput_trim(FILE * fp, char *s, int max) {
int rlen = 0;
char *const ls = (char *) malloc(max + 1);
s[0] = '\0';
if (ls) {
char *a;
// lire ligne
rlen = linput(fp, ls, max);
if (rlen) {
// sauter espaces et tabs en fin
while((rlen > 0) && is_realspace(ls[max(rlen - 1, 0)]))
ls[--rlen] = '\0';
// sauter espaces en début
a = ls;
while((rlen > 0) && ((*a == ' ') || (*a == '\t'))) {
a++;
rlen--;
}
if (rlen > 0) {
memcpy(s, a, rlen); // can copy \0 chars
s[rlen] = '\0';
}
}
//
free(ls);
}
return rlen;
}
#ifndef S_ISREG
#define S_ISREG(m) ((m) & _S_IFREG)
#endif
HTS_UNUSED static int fexist(char *s) {
struct stat st;
memset(&st, 0, sizeof(st));
if (stat(s, &st) == 0) {
if (S_ISREG(st.st_mode)) {
return 1;
}
}
return 0;
}
/* convertir une chaine en temps */
HTS_UNUSED static void set_lowcase(char *s) {
int i;
for(i = 0; i < (int) strlen(s); i++)
if ((s[i] >= 'A') && (s[i] <= 'Z'))
s[i] += ('a' - 'A');
}
HTS_UNUSED static struct tm *convert_time_rfc822(struct tm *result, const char *s) {
char months[] = "jan feb mar apr may jun jul aug sep oct nov dec";
char str[256];
char *a;
/* */
int result_mm = -1;
int result_dd = -1;
int result_n1 = -1;
int result_n2 = -1;
int result_n3 = -1;
int result_n4 = -1;
/* */
if ((int) strlen(s) > 200)
return NULL;
strcpy(str, s);
set_lowcase(str);
/* éliminer :,- */
while((a = strchr(str, '-')))
*a = ' ';
while((a = strchr(str, ':')))
*a = ' ';
while((a = strchr(str, ',')))
*a = ' ';
/* tokeniser */
a = str;
while(*a) {
char *first, *last;
char tok[256];
/* découper mot */
while(*a == ' ')
a++; /* sauter espaces */
first = a;
while((*a) && (*a != ' '))
a++;
last = a;
tok[0] = '\0';
if (first != last) {
char *pos;
strncat(tok, first, (int) (last - first));
/* analyser */
if ((pos = strstr(months, tok))) { /* month always in letters */
result_mm = ((int) (pos - months)) / 4;
} else {
int number;
if (sscanf(tok, "%d", &number) == 1) { /* number token */
if (result_dd < 0) /* day always first number */
result_dd = number;
else if (result_n1 < 0)
result_n1 = number;
else if (result_n2 < 0)
result_n2 = number;
else if (result_n3 < 0)
result_n3 = number;
else if (result_n4 < 0)
result_n4 = number;
} /* sinon, bruit de fond(+1GMT for exampel) */
}
}
}
if ((result_n1 >= 0) && (result_mm >= 0) && (result_dd >= 0)
&& (result_n2 >= 0) && (result_n3 >= 0) && (result_n4 >= 0)) {
if (result_n4 >= 1000) { /* Sun Nov 6 08:49:37 1994 */
result->tm_year = result_n4 - 1900;
result->tm_hour = result_n1;
result->tm_min = result_n2;
result->tm_sec = max(result_n3, 0);
} else { /* Sun, 06 Nov 1994 08:49:37 GMT or Sunday, 06-Nov-94 08:49:37 GMT */
result->tm_hour = result_n2;
result->tm_min = result_n3;
result->tm_sec = max(result_n4, 0);
if (result_n1 <= 50) /* 00 means 2000 */
result->tm_year = result_n1 + 100;
else if (result_n1 < 1000) /* 99 means 1999 */
result->tm_year = result_n1;
else /* 2000 */
result->tm_year = result_n1 - 1900;
}
result->tm_isdst = 0; /* assume GMT */
result->tm_yday = -1; /* don't know */
result->tm_wday = -1; /* don't know */
result->tm_mon = result_mm;
result->tm_mday = result_dd;
return result;
}
return NULL;
}
HTS_UNUSED static struct tm PT_GetTime(time_t t) {
struct tm tmbuf;
#ifdef _WIN32
struct tm *tm = gmtime(&t);
#else
struct tm *tm = gmtime_r(&t, &tmbuf);
#endif
if (tm != NULL)
return *tm;
else {
memset(&tmbuf, 0, sizeof(tmbuf));
return tmbuf;
}
}
HTS_UNUSED static int set_filetime(const char *file, struct tm *tm_time) {
struct utimbuf tim;
#ifndef HTS_DO_NOT_USE_FTIME
struct timeb B;
memset(&B, 0, sizeof(B));
B.timezone = 0;
ftime(&B);
tim.actime = tim.modtime = mktime(tm_time) - B.timezone * 60;
#else
// bogus time (GMT/local)..
tim.actime = tim.modtime = mktime(tm_time);
#endif
return utime(file, &tim);
}
HTS_UNUSED static int set_filetime_time_t(const char *file, time_t t) {
if (t != (time_t) 0 && t != (time_t) - 1) {
struct tm tm = PT_GetTime(t);
return set_filetime(file, &tm);
}
return -1;
}
HTS_UNUSED static int set_filetime_rfc822(const char *file, const char *date) {
struct tm buffer;
struct tm *tm_s = convert_time_rfc822(&buffer, date);
if (tm_s) {
return set_filetime(file, tm_s);
} else
return -1;
}
#endif
|