Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * file_reply.cpp - Web request file reply 00004 * 00005 * Created: Thu Oct 23 14:00:17 2008 00006 * Copyright 2006-2009 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 #include <webview/file_reply.h> 00024 00025 #include <core/exceptions/system.h> 00026 #include <utils/system/filetype.h> 00027 00028 #include <cerrno> 00029 #include <sys/stat.h> 00030 00031 namespace fawkes { 00032 #if 0 /* just to make Emacs auto-indent happy */ 00033 } 00034 #endif 00035 00036 /** @class DynamicFileWebReply <webview/file_reply.h> 00037 * Dynamic raw file transfer reply. 00038 * This dynamic file transfer reply transmits the given file with a mime type 00039 * determined with libmagic. 00040 * @author Tim Niemueller 00041 */ 00042 00043 /** Constructor. 00044 * @param filename path and name of the file to transmit 00045 */ 00046 DynamicFileWebReply::DynamicFileWebReply(const char *filename) 00047 : DynamicWebReply(WebReply::HTTP_OK) 00048 { 00049 if (access(filename, R_OK) != 0 || ((__file = fopen(filename, "r")) == NULL)) { 00050 throw fawkes::CouldNotOpenFileException(filename, errno); 00051 } 00052 00053 struct stat sbuf; 00054 fstat(fileno(__file), &sbuf); 00055 00056 if ( S_ISDIR(sbuf.st_mode) ) { 00057 throw fawkes::Exception("Cannot send directory\n"); 00058 } 00059 __size = sbuf.st_size; 00060 00061 add_header("Content-type", fawkes::mimetype_file(filename)); 00062 } 00063 00064 /** Destructor. */ 00065 DynamicFileWebReply::~DynamicFileWebReply() 00066 { 00067 fclose(__file); 00068 } 00069 00070 size_t 00071 DynamicFileWebReply::size() 00072 { 00073 return __size; 00074 } 00075 00076 size_t 00077 DynamicFileWebReply::next_chunk(size_t pos, char *buffer, size_t buf_max_size) 00078 { 00079 if ( (fseek(__file, pos, SEEK_SET) == -1) || feof(__file) ) { 00080 return (size_t)-1; 00081 } 00082 return fread(buffer, 1, buf_max_size, __file); 00083 } 00084 00085 } // end namespace fawkes