static_processor.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "static_processor.h"
00024 #include "file_reply.h"
00025 #include "error_reply.h"
00026
00027 #include <core/exception.h>
00028 #include <utils/logging/logger.h>
00029
00030 #include <cstring>
00031 #include <cstdlib>
00032 #include <string>
00033 #include <unistd.h>
00034 #include <cerrno>
00035 #include <climits>
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 WebStaticRequestProcessor::WebStaticRequestProcessor(const char *baseurl,
00049 const char *htdocs_dir,
00050 fawkes::Logger *logger)
00051 {
00052 __logger = logger;
00053 __baseurl = strdup(baseurl);
00054 __baseurl_len = strlen(__baseurl);
00055 __htdocs_dir = strdup(htdocs_dir);
00056 __htdocs_dir_len = strlen(__htdocs_dir);
00057
00058 }
00059
00060
00061 WebStaticRequestProcessor::~WebStaticRequestProcessor()
00062 {
00063 free(__baseurl);
00064 free(__htdocs_dir);
00065 }
00066
00067
00068 WebReply *
00069 WebStaticRequestProcessor::process_request(const char *url,
00070 const char *method,
00071 const char *version,
00072 const char *upload_data,
00073 size_t *upload_data_size,
00074 void **session_data)
00075 {
00076 if ( strncmp(__baseurl, url, __baseurl_len) == 0 ) {
00077
00078 std::string file_path = std::string(__htdocs_dir) + std::string(url).substr(__baseurl_len);
00079
00080 char rf[PATH_MAX];
00081 char *realfile = realpath(file_path.c_str(), rf);
00082 if (! realfile ) {
00083 if (errno == ENOENT) {
00084 return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, "File not found");
00085 } else if (errno == EACCES) {
00086 return new WebErrorPageReply(WebReply::HTTP_FORBIDDEN, "Access forbidden");
00087 } else {
00088 char tmp[1024];
00089 strerror_r(errno, tmp, sizeof(tmp));
00090 return new WebErrorPageReply(WebReply::HTTP_INTERNAL_SERVER_ERROR,
00091 std::string("File access failed: ") + tmp);
00092 }
00093 } else {
00094 if (strncmp(realfile, __htdocs_dir, __htdocs_dir_len) == 0) {
00095 try {
00096 DynamicFileWebReply *freply = new DynamicFileWebReply(file_path.c_str());
00097 return freply;
00098 } catch (fawkes::Exception &e) {
00099 __logger->log_error("WebStaticReqProc", "Cannot fulfill request for file %s,"
00100 " exception follows", url);
00101 __logger->log_error("WebStaticReqProc", e);
00102 return new WebErrorPageReply(WebReply::HTTP_INTERNAL_SERVER_ERROR,
00103 *(e.begin()));
00104 }
00105 } else {
00106
00107 return new WebErrorPageReply(WebReply::HTTP_FORBIDDEN,
00108 "Access forbidden, breakout detected.");
00109 }
00110 }
00111 } else {
00112
00113 __logger->log_error("WebStaticReqProc", "Called for invalid base url "
00114 "(url: %s, baseurl: %s)", url, __baseurl);
00115 return NULL;
00116 }
00117 }