=== added file 'src/InternalServer.cc' --- src/InternalServer.cc 1970-01-01 00:00:00 +0000 +++ src/InternalServer.cc 2010-07-14 13:56:02 +0000 @@ -0,0 +1,97 @@ +/* + * DEBUG: section 76 Internal Squid Object handling + * AUTHOR: Amos Jeffries + * + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + */ +#include "config.h" +#include "InternalServer.h" +#include "MemBuf.h" +#include "Store.h" + +void +InternalServer::readingFromFile(const char *fullpath, const char *uripath, struct stat *sb) +{ + HttpReply *reply = new HttpReply; + + // locate the mime type for this file ... + char *mimetype = mimeGetContentType(path); + if (!mimetype) mimetype = "text/plain"; + + reply->setHeaders(HTTP_OK, "Okay", mimetype, sb->st_size, sb->st_mtime, -2); + entry->replaceHttpReply(reply); + entry->registerAbort(InternalServer::AbortWrapper, this); + + buffer.init(); + expected_remaining = sb->st_size; + file_fd = fopen(fullpath, "r"); +} + +void +InternalServer::ReadWrapper(int fd, void *data) +{ + InternalServer *srv = (InternalServer*)data; + srv->start(); +} + +void +InternalServer::AbortWrapper(void *data) +{ + InternalServer *srv = (InternalServer*)data; + srv->swanSong(); +} + +void +InternalServer::start() +{ + int sz = fread(buffer.content(), sizeof(char), buffer.spaceSize(), fd); + if (sz > 0) { + entry->append(buffer.content(), sz); + expected_remaining -= sz; + } + + if (expected_remaining <= 0) { + // no more file to read. we are done. + swanSong(); + return; + } + + // TODO: handle errors other than EAGAIN and 0 (OK) + + // set read handler on FD again + commSetSelect(fd, COMM_SELECT_READ, InternalServer::ReadWrapper, this, 0); +} + +void +InternalServer::swanSong() +{ + entry->complete(); + entry->unregisterAbort(); + fclose(file_fd); + deleteSelf(); +} === added file 'src/InternalServer.h' --- src/InternalServer.h 1970-01-01 00:00:00 +0000 +++ src/InternalServer.h 2010-07-14 13:56:44 +0000 @@ -0,0 +1,66 @@ + + +/* + * $Id$ + * + * DEBUG: section 76 Internal Squid Object handling + * AUTHOR: Amos Jeffries + * + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + */ +#ifndef _SQUID_INTERNALSERVER_H +#define _SQUID_INTERNALSERVER_H + +#include "base/AsyncJob.h" +#include "MemBuf.h" + +class HttpRequest; +class StoreEntry; + +class InternalServer : public AsyncJob +{ +public: + /// Use the given stat() details about this file + void readingFromFile(const char *fullpath, const char *uripath, struct stat *sb); + + /// read from the open file FD as much as possible. + void start(); + + // FD read callback + static void ReadWrapper(int fd, void *data); + + // Client disconnected Abort handler (for StoreEntry) + static void AbortWrapper(void *data); + +private: + MemBuf buffer; ///< temporary buffer to load the object into. + size_t expect_remaining; ///< counter of how much of the file we are still waiting for. + int file_fd; ///< handle to the file being read. +}; + +#endif /* _SQUID_INTERNALSERVER_H */ === modified file 'src/cache_cf.cc' --- src/cache_cf.cc 2010-07-07 16:41:03 +0000 +++ src/cache_cf.cc 2010-07-12 15:45:07 +0000 @@ -622,6 +622,10 @@ if (Config.errHtmlText == NULL) Config.errHtmlText = xstrdup(null_string); + if (Config.local_file_dir != NULL) { + requirePathnameExists("local_file_dir", Config.local_file_dir); + } + storeConfigure(); snprintf(ThisCache, sizeof(ThisCache), "%s (%s)", === modified file 'src/cf.data.pre' --- src/cf.data.pre 2010-07-06 23:09:44 +0000 +++ src/cf.data.pre 2010-07-12 15:42:36 +0000 @@ -4449,6 +4449,21 @@ your value with 0. DOC_END +NAME: local_file_dir +TYPE: string +LOC: Config.local_file_dir +DEFAULT: none +DOC_START + Specifies the base directory for local files which can be served. + + Combined with the visible_hostname directive + Squid will respond with these files when client browsers request a URL + in the domain of its visible_hostname: + http://${visible_hostname}/ + + To disable, remove from the configuration. +DOC_END + COMMENT_START OPTIONS FOR THE CACHE REGISTRATION SERVICE ----------------------------------------------------------------------------- === modified file 'src/err_type.h' --- src/err_type.h 2009-10-31 11:53:09 +0000 +++ src/err_type.h 2010-07-12 15:20:49 +0000 @@ -55,6 +55,7 @@ ERR_DIR_LISTING, /* Display of remote directory (FTP, Gopher) */ ERR_SQUID_SIGNATURE, /* not really an error */ ERR_SHUTTING_DOWN, + ERR_BLANK, TCP_RESET, ERR_MAX === modified file 'src/forward.cc' --- src/forward.cc 2010-07-13 16:49:48 +0000 +++ src/forward.cc 2010-07-14 13:23:22 +0000 @@ -196,6 +196,14 @@ FwdState::fwdStart(int client_fd, StoreEntry *entry, HttpRequest *request) { /** \note + * Forwarding requests to our own unique machine name indicates it maybe an internal request. ?? + */ + if (internalHostnameIs(request->GetHost())) { + internalStart(request, entry); + return; + } + + /** \note * client_addr == no_addr indicates this is an "internal" request * from peer_digest.c, asn.c, netdb.c, etc and should always * be allowed. yuck, I know. === modified file 'src/internal.cc' --- src/internal.cc 2010-06-24 13:23:42 +0000 +++ src/internal.cc 2010-07-14 13:54:30 +0000 @@ -38,13 +38,17 @@ #include "Store.h" #include "HttpRequest.h" #include "HttpReply.h" +#include "InternalServer.h" #include "MemBuf.h" #include "SquidTime.h" #include "wordlist.h" #include "icmp/net_db.h" -/* called when we "miss" on an internal object; +bool internalLocalFileServer(const char *path, StoreEntry *entry); + +/** called when we "miss" on an internal object; * generate known dynamic objects, + * lookup and load local static files, * return HTTP_NOT_FOUND for others */ void @@ -70,6 +74,9 @@ entry->append(msgbuf, strlen(msgbuf)); entry->complete(); } else { + if (internalLocalFileServer(upath, entry, request)) + return; + debugObj(76, 1, "internalStart: unknown request:\n", request, (ObjPackMethod) & httpRequestPack); err = errorCon(ERR_INVALID_REQ, HTTP_NOT_FOUND, request); @@ -187,3 +194,29 @@ return 0; } + +bool +internalLocalFileCheck(const char *path, StoreEntry *entry, HttpRequest *req) +{ + struct stat sb; + char pathbuf[BUFSIZ]; + assert(path != NULL); + + // locate the file to be sent and pull its details + if (Config.chroot_dir && (geteuid() == 0)) { + snprintf(pathbuf, BUFSIZ, "%s/%s/%s", Config.chroot_dir, Config.local_file_dir, path); + } else { + snprintf(pathbuf, BUFSIZ, "%s/%s", Config.local_file_dir, path); + } + if (stat(pathbuf, &sb) < 0) { + /* No such file. Stop. */ + return false; + } + + // Create an InternalServer to handle reading the file out to client. + InternalServer *is = new InternalServer(); + is->readingFromFile(pathbuf, path, &sb); + is->start(); + + return true; +} === modified file 'src/mime.conf.default' --- src/mime.conf.default 2004-04-03 21:31:21 +0000 +++ src/mime.conf.default 2010-07-12 15:20:49 +0000 @@ -189,6 +189,6 @@ \.xml$ text/xml anthony-text.gif - ascii +download \.xsl$ text/xml anthony-layout.gif - ascii +download \.xyz$ chemical/x-xyz anthony-unknown.gif - image +download - +\/wpad\.dat application/x-ns-proxy-autoconfig anthony-script.gif - ascii +download # the default . text/plain anthony-unknown.gif - image +download +view === modified file 'src/structs.h' --- src/structs.h 2010-07-06 23:09:44 +0000 +++ src/structs.h 2010-07-12 15:43:23 +0000 @@ -519,6 +519,7 @@ int errorLogMissingLanguages; #endif char *errorStylesheet; + char *local_file_dir; struct { int maxtries;