Fawkes API  Fawkes Development Version
file_reply.cpp
1 
2 /***************************************************************************
3  * file_reply.cpp - Web request file reply
4  *
5  * Created: Thu Oct 23 14:00:17 2008
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <webview/file_reply.h>
24 
25 #include <core/exceptions/system.h>
26 #include <utils/system/filetype.h>
27 
28 #include <cerrno>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 
32 namespace fawkes {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 /** @class DynamicFileWebReply <webview/file_reply.h>
38  * Dynamic raw file transfer reply.
39  * This dynamic file transfer reply transmits the given file with a mime type
40  * determined with libmagic.
41  * @author Tim Niemueller
42  */
43 
44 /** Constructor.
45  * @param filename path and name of the file to transmit
46  */
48  : DynamicWebReply(WebReply::HTTP_OK)
49 {
50  if (access(filename, R_OK) != 0 || ((__file = fopen(filename, "r")) == NULL)) {
51  throw fawkes::CouldNotOpenFileException(filename, errno);
52  }
53 
54  struct stat sbuf;
55  fstat(fileno(__file), &sbuf);
56 
57  if ( S_ISDIR(sbuf.st_mode) ) {
58  throw fawkes::Exception("Cannot send directory\n");
59  }
60  __size = sbuf.st_size;
61 
62  add_header("Content-type", fawkes::mimetype_file(filename));
63 }
64 
65 /** Destructor. */
67 {
68  fclose(__file);
69 }
70 
71 size_t
73 {
74  return __size;
75 }
76 
77 size_t
78 DynamicFileWebReply::next_chunk(size_t pos, char *buffer, size_t buf_max_size)
79 {
80  if ( (fseek(__file, pos, SEEK_SET) == -1) || feof(__file) ) {
81  return (size_t)-1;
82  }
83  return fread(buffer, 1, buf_max_size, __file);
84 }
85 
86 } // end namespace fawkes
DynamicFileWebReply(const char *filename)
Constructor.
Definition: file_reply.cpp:47
File could not be opened.
Definition: system.h:53
virtual size_t size()
Total size of the web reply.
Definition: file_reply.cpp:72
Fawkes library namespace.
void add_header(std::string header, std::string content)
Add a HTTP header.
Definition: reply.cpp:72
virtual ~DynamicFileWebReply()
Destructor.
Definition: file_reply.cpp:66
std::string mimetype_file(const char *filename)
Get mime-type of file.
Definition: filetype.cpp:74
Dynamic web reply.
Definition: reply.h:115
Base class for exceptions in Fawkes.
Definition: exception.h:36
Basic web reply.
Definition: reply.h:34
virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size)
Get data of next chunk.
Definition: file_reply.cpp:78