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
|
/*
** Multiformat handling
**
** History:
** March 94 AL Separated from HTFile.c because
** multiformat handling would be a mess in VMS.
**
**
*/
#include "HTMulti.h"
#include "HTFile.h"
#include "HTList.h"
PRIVATE HTList * welcome_names = NULL; /* Welcome.html, index.html etc. */
/*
** Set default file name for welcome page on each directory.
*/
PUBLIC void HTAddWelcome ARGS1(char *, name)
{
if (name) {
char * mycopy = NULL;
StrAllocCopy(mycopy,name);
if (!welcome_names)
welcome_names = HTList_new();
HTList_addObject(welcome_names, (void*)mycopy);
}
}
#ifdef GOT_READ_DIR
/* PRIVATE multi_match()
**
** Check if actual filename (split in parts) fulfills
** the requirements.
*/
PRIVATE BOOL multi_match ARGS4(char **, required,
int, m,
char **, actual,
int, n)
{
int c;
int i,j;
#ifdef VMS
for(c=0; c<m && c<n && !strcasecomp(required[c], actual[c]); c++);
#else /* not VMS */
for(c=0; c<m && c<n && !strcmp(required[c], actual[c]); c++);
#endif /* not VMS */
if (!c) return NO; /* Names differ rigth from start */
for(i=c; i<m; i++) {
BOOL found = NO;
for(j=c; j<n; j++) {
#ifdef VMS
if (!strcasecomp(required[i], actual[j])) {
#else /* not VMS */
if (!strcmp(required[i], actual[j])) {
#endif /* not VMS */
found = YES;
break;
}
}
if (!found) return NO;
}
return YES;
}
/*
** Get multi-match possibilities for a given file
** ----------------------------------------------
** On entry:
** path absolute path to one file in a directory,
** may end in .multi.
** On exit:
** returns a list of ContentDesription structures
** describing the mathing files.
**
*/
PRIVATE HTList * dir_matches ARGS1(char *, path)
{
static char * required[MAX_SUFF+1];
static char * actual[MAX_SUFF+1];
int m,n;
char * dirname = NULL;
char * basename = NULL;
int baselen;
char * multi = NULL;
DIR * dp;
STRUCT_DIRENT * dirbuf;
HTList * matches = NULL;
if (!path) return NULL;
StrAllocCopy(dirname, path);
basename = (strrchr(dirname, '/'));
if (!basename)
goto dir_match_failed;
*basename++ = 0;
multi = strrchr(basename, MULTI_SUFFIX[0]);
if (multi && !strcasecomp(multi, MULTI_SUFFIX))
*multi = 0;
baselen = strlen(basename);
m = HTSplitFilename(basename, required);
dp = opendir(dirname);
if (!dp) {
CTRACE(stderr,"Warning..... Can't open directory %s\n",
dirname);
goto dir_match_failed;
}
matches = HTList_new();
while ((dirbuf = readdir(dp))) {
if (!dirbuf->d_ino) continue; /* Not in use */
if (!strcmp(dirbuf->d_name,".") ||
!strcmp(dirbuf->d_name,"..") ||
!strcmp(dirbuf->d_name, HT_DIR_ENABLE_FILE))
continue;
/* Use of direct->namlen is only valid in BSD'ish system */
/* Thanks to chip@chinacat.unicom.com (Chip Rosenthal) */
/* if ((int)(dirbuf->d_namlen) >= baselen) { */
if ((int) strlen(dirbuf->d_name) >= baselen) {
n = HTSplitFilename(dirbuf->d_name, actual);
if (multi_match(required, m, actual, n)) {
HTContentDescription * cd;
cd = HTGetContentDescription(actual, n);
if (cd) {
if (cd->content_type) {
cd->filename = (char*)malloc(strlen(dirname) + 2 +
strlen(dirbuf->d_name));
if (!cd->filename) outofmem(__FILE__, "dir_matches");
sprintf(cd->filename, "%s/%s",
dirname, dirbuf->d_name);
HTList_addObject(matches, (void*)cd);
}
else free(cd);
}
}
}
}
closedir(dp);
dir_match_failed:
free(dirname);
return matches;
}
/*
** Get the best match for a given file
** -----------------------------------
** On entry:
** req->conversions accepted content-types
** req->encodings accepted content-transfer-encodings
** req->languages accepted content-languages
** path absolute pathname of the filename for
** which the match is desired.
** On exit:
** returns a newly allocated absolute filepath.
*/
PRIVATE char * HTGetBest ARGS2(HTRequest *, req,
char *, path)
{
HTList * matches;
HTList * cur;
HTContentDescription * cd;
HTContentDescription * best = NULL;
char * best_path = NULL;
if (!path || !*path) return NO;
matches = dir_matches(path);
if (!matches) {
CTRACE(stderr, "No matches.. for \"%s\"\n", path);
return NO;
}
/* BEGIN DEBUG */
cur = matches;
CTRACE(stderr, "Multi....... Possibilities for \"%s\"\n", path);
CTRACE(stderr, "\nCONTENT-TYPE LANGUAGE ENCODING QUALITY FILE\n");
while ((cd = (HTContentDescription*)HTList_nextObject(cur))) {
CTRACE(stderr, "%s\t%s\t%s\t %.5f %s\n",
cd->content_type ?HTAtom_name(cd->content_type) :"-\t",
cd->content_language?HTAtom_name(cd->content_language):"-",
cd->content_encoding?HTAtom_name(cd->content_encoding):"-",
cd->quality,
cd->filename ?cd->filename :"-");
}
CTRACE(stderr, "\n");
/* END DEBUG */
/*
** Finally get best that is readable
*/
if (HTRank(matches, req->conversions, req->languages, req->encodings)) {
cur = matches;
while ((best = (HTContentDescription*)HTList_nextObject(cur))) {
if (best && best->filename) {
if (access(best->filename, R_OK) != -1) {
StrAllocCopy(best_path, best->filename);
break;
}
else CTRACE(stderr, "Bad News.... \"%s\" is not readable\n",
best->filename);
}
}
} /* Select best */
cur = matches;
while ((cd = (HTContentDescription*)HTList_nextObject(cur))) {
if (cd->filename) free(cd->filename);
free(cd);
}
HTList_delete(matches);
return best_path;
}
PRIVATE int welcome_value ARGS1(char *, name)
{
HTList * cur = welcome_names;
char * welcome;
int v = 0;
while ((welcome = (char*)HTList_nextObject(cur))) {
v++;
if (!strcmp(welcome,name)) return v;
}
return 0;
}
PRIVATE char * get_best_welcome ARGS1(char *, path)
{
char * best_welcome = NULL;
int best_value = 0;
DIR * dp;
STRUCT_DIRENT * dirbuf;
char * last = strrchr(path, '/');
if (!welcome_names) {
HTAddWelcome("Welcome.html");
HTAddWelcome("welcome.html");
#if 0
HTAddWelcome("Index.html");
#endif
HTAddWelcome("index.html");
}
if (last && last!=path) *last = 0;
dp = opendir(path);
if (last && last!=path) *last='/';
if (!dp) {
CTRACE(stderr, "Warning..... Can't open directory %s\n",path);
return NULL;
}
while ((dirbuf = readdir(dp))) {
if (!dirbuf->d_ino ||
!strcmp(dirbuf->d_name,".") ||
!strcmp(dirbuf->d_name,"..") ||
!strcmp(dirbuf->d_name, HT_DIR_ENABLE_FILE))
continue;
else {
int v = welcome_value(dirbuf->d_name);
if (v > best_value) {
best_value = v;
StrAllocCopy(best_welcome, dirbuf->d_name);
}
}
}
closedir(dp);
if (best_welcome) {
char * welcome = (char*)malloc(strlen(path) + strlen(best_welcome)+2);
if (!welcome) outofmem(__FILE__, "get_best_welcome");
sprintf(welcome, "%s%s%s", path, last ? "" : "/", best_welcome);
free(best_welcome);
CTRACE(stderr,"Welcome..... \"%s\"\n",welcome);
return welcome;
}
return NULL;
}
#endif /* GOT_READ_DIR */
/*
** Do multiformat handling
** -----------------------
** On entry:
** req->conversions accepted content-types
** req->encodings accepted content-transfer-encodings
** req->languages accepted content-languages
** path absolute pathname of the filename for
** which the match is desired.
** stat_info pointer to result space.
**
** On exit:
** returns a newly allocated absolute filepath of the best
** match, or NULL if no match.
** stat_info will contain inode information as
** returned by stat().
*/
PUBLIC char * HTMulti ARGS3(HTRequest *, req,
char *, path,
struct stat *, stat_info)
{
char * new_path = NULL;
int stat_status = -1;
int len;
if (!req || !path || !*path || !stat_info)
return NULL;
#ifdef GOT_READ_DIR
len = strlen(path);
if (path[len-1] == '/') { /* Find welcome page */
new_path = get_best_welcome(path);
if (new_path) path = new_path;
}
else{
char * multi = strrchr(path, MULTI_SUFFIX[0]);
if (multi && !strcasecomp(multi, MULTI_SUFFIX)) {
CTRACE(stderr, "Multi....... by %s suffix\n", MULTI_SUFFIX);
if (!(new_path = HTGetBest(req, path))) {
CTRACE(stderr, "Multi....... failed -- giving up\n");
return NULL;
}
path = new_path;
}
else {
stat_status = HTStat(path, stat_info);
if (stat_status == -1) {
CTRACE(stderr,
"AutoMulti... because can't stat \"%s\" (errno %d)\n",
path, errno);
if (!(new_path = HTGetBest(req, path))) {
CTRACE(stderr, "AutoMulti... failed -- giving up\n");
return NULL;
}
path = new_path;
}
}
}
#endif /* GOT_READ_DIR */
if (stat_status == -1)
stat_status = HTStat(path, stat_info);
if (stat_status == -1) {
CTRACE(stderr, "Stat fails.. on \"%s\" -- giving up (errno %d)\n",
path, errno);
return NULL;
}
else {
if (!new_path) {
StrAllocCopy(new_path, path);
return new_path;
}
else return path;
}
}
|