Fawkes API  Fawkes Development Version
static_processor.cpp
1 
2 /***************************************************************************
3  * static_processor.cpp - Web request processor for static files
4  *
5  * Created: Mon Oct 13 23:41:24 2008
6  * Copyright 2006-2008 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 "static_processor.h"
24 #include <webview/file_reply.h>
25 #include <webview/error_reply.h>
26 
27 #include <core/exception.h>
28 #include <logging/logger.h>
29 
30 #include <cstring>
31 #include <cstdlib>
32 #include <string>
33 #include <unistd.h>
34 #include <cerrno>
35 #include <climits>
36 
37 using namespace fawkes;
38 
39 /** @class WebviewStaticRequestProcessor "static_processor.h"
40  * Static file web processor.
41  * This processor provides access to static files.
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor.
46  * @param baseurl Base URL where the static processor is mounted
47  * @param htdocs_dir directory in the file system where to look for static files
48  * @param logger logger
49  */
51  const char *htdocs_dir,
52  fawkes::Logger *logger)
53 {
54  __logger = logger;
55  __baseurl = strdup(baseurl);
56  __baseurl_len = strlen(__baseurl);
57  __htdocs_dir = strdup(htdocs_dir);
58  __htdocs_dir_len = strlen(__htdocs_dir);
59 
60 }
61 
62 /** Destructor. */
64 {
65  free(__baseurl);
66  free(__htdocs_dir);
67 }
68 
69 
70 WebReply *
72  const char *method,
73  const char *version,
74  const char *upload_data,
75  size_t *upload_data_size,
76  void **session_data)
77 {
78  if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
79  // It is in our URL prefix range
80  std::string file_path = std::string(__htdocs_dir) + std::string(url).substr(__baseurl_len);
81 
82  char rf[PATH_MAX];
83  char *realfile = realpath(file_path.c_str(), rf);
84  if (! realfile ) {
85  if (errno == ENOENT) {
86  return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, "File not found");
87  } else if (errno == EACCES) {
88  return new WebErrorPageReply(WebReply::HTTP_FORBIDDEN, "Access forbidden");
89  } else {
90  char tmp[1024];
91  strerror_r(errno, tmp, sizeof(tmp));
93  "File access failed: %s", tmp);
94  }
95  } else {
96  if (strncmp(realfile, __htdocs_dir, __htdocs_dir_len) == 0) {
97  try {
98  DynamicFileWebReply *freply = new DynamicFileWebReply(file_path.c_str());
99  return freply;
100  } catch (fawkes::Exception &e) {
101  __logger->log_error("WebStaticReqProc",
102  "Cannot fulfill request for file %s,"
103  " exception follows", url);
104  __logger->log_error("WebStaticReqProc", e);
106  *(e.begin()));
107  }
108  } else {
109  // Someone tries to trick us to give away files we don't want to give
111  "Access forbidden, breakout detected.");
112  }
113  }
114  } else {
115  // wrong base url, why the heck are we called!?
116  __logger->log_error("WebStaticReqProc", "Called for invalid base url "
117  "(url: %s, baseurl: %s)", url, __baseurl);
118  return NULL;
119  }
120 }
Dynamic raw file transfer reply.
Definition: file_reply.h:34
Fawkes library namespace.
Base class for exceptions in Fawkes.
Definition: exception.h:36
iterator begin()
Get iterator for messages.
Definition: exception.cpp:678
Basic web reply.
Definition: reply.h:34
virtual ~WebviewStaticRequestProcessor()
Destructor.
virtual fawkes::WebReply * process_request(const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **session_data)
Process a request.
Static error page reply.
Definition: error_reply.h:33
WebviewStaticRequestProcessor(const char *baseurl, const char *htdocs_dir, fawkes::Logger *logger)
Constructor.
Interface for logging.
Definition: logger.h:34