/* during a file download we first check to see if there is a language specific file available. If there is then use that, otherwise just open the specified file */ int web_open(const char *fname, int flags, mode_t mode) { char *p = NULL; char *lang = lang_tdb_current(); int fd; if (lang) { if (asprintf(&p, "lang/%s/%s", lang, fname) != -1) { fd = sys_open(p, flags, mode); free(p); if (fd != -1) { return fd; } } } /* fall through to default name */ return sys_open(fname, flags, mode); }
/*************************************************************************** handle a file download ***************************************************************************/ static void cgi_download(char *file) { SMB_STRUCT_STAT st; char buf[1024]; int fd, l, i; char *p; char *lang; /* sanitise the filename */ for (i=0;file[i];i++) { if (!isalnum((int)file[i]) && !strchr_m("/.-_", file[i])) { cgi_setup_error("404 File Not Found","", "Illegal character in filename"); } } if (sys_stat(file, &st) != 0) { cgi_setup_error("404 File Not Found","", "The requested file was not found"); } if (S_ISDIR(st.st_mode)) { snprintf(buf, sizeof(buf), "%s/index.html", file); if (!file_exist(buf, &st) || !S_ISREG(st.st_mode)) { cgi_setup_error("404 File Not Found","", "The requested file was not found"); } } else if (S_ISREG(st.st_mode)) { snprintf(buf, sizeof(buf), "%s", file); } else { cgi_setup_error("404 File Not Found","", "The requested file was not found"); } fd = web_open(buf,O_RDONLY,0); if (fd == -1) { cgi_setup_error("404 File Not Found","", "The requested file was not found"); } printf("HTTP/1.0 200 OK\r\n"); if ((p=strrchr_m(buf, '.'))) { if (strcmp(p,".gif")==0) { printf("Content-Type: image/gif\r\n"); } else if (strcmp(p,".jpg")==0) { printf("Content-Type: image/jpeg\r\n"); } else if (strcmp(p,".png")==0) { printf("Content-Type: image/png\r\n"); } else if (strcmp(p,".css")==0) { printf("Content-Type: text/css\r\n"); } else if (strcmp(p,".txt")==0) { printf("Content-Type: text/plain\r\n"); } else { printf("Content-Type: text/html\r\n"); } } printf("Expires: %s\r\n", http_timestring(time(NULL)+EXPIRY_TIME)); lang = lang_tdb_current(); if (lang) { printf("Content-Language: %s\r\n", lang); } printf("Content-Length: %d\r\n\r\n", (int)st.st_size); while ((l=read(fd,buf,sizeof(buf)))>0) { if (fwrite(buf, 1, l, stdout) != l) { break; } } close(fd); exit(0); }