Anope IRC Services  Version 2.0
static_fileserver.cpp
Go to the documentation of this file.
1 /*
2  * (C) 2003-2014 Anope Team
3  * Contact us at team@anope.org
4  *
5  * Please read COPYING and README for further details.
6  */
7 
8 #include "webcpanel.h"
9 #include <fstream>
10 #include <errno.h>
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 
16 StaticFileServer::StaticFileServer(const Anope::string &f_n, const Anope::string &u, const Anope::string &c_t) : HTTPPage(u, c_t), file_name(f_n)
17 {
18 }
19 
20 bool StaticFileServer::OnRequest(HTTPProvider *server, const Anope::string &page_name, HTTPClient *client, HTTPMessage &message, HTTPReply &reply)
21 {
22  int fd = open((template_base + "/" + this->file_name).c_str(), O_RDONLY);
23  if (fd < 0)
24  {
25  Log(LOG_NORMAL, "httpd") << "Error serving file " << page_name << " (" << (template_base + "/" + this->file_name) << "): " << strerror(errno);
26 
27  client->SendError(HTTP_PAGE_NOT_FOUND, "Page not found");
28  return true;
29  }
30 
31  reply.content_type = this->GetContentType();
32  reply.headers["Cache-Control"] = "public";
33 
34  int i;
35  char buffer[BUFSIZE];
36  while ((i = read(fd, buffer, sizeof(buffer))) > 0)
37  reply.Write(buffer, i);
38 
39  close(fd);
40  return true;
41 }
42 
Anope::string file_name
Anope::string content_type
Definition: httpd.h:17
bool OnRequest(HTTPProvider *, const Anope::string &, HTTPClient *, HTTPMessage &, HTTPReply &) anope_override
Anope::string template_base
Definition: webcpanel.cpp:11
#define BUFSIZE
Definition: services.h:18
#define read
Definition: socket.h:8
std::map< Anope::string, Anope::string > headers
Definition: httpd.h:18
const Anope::string & GetContentType() const
Definition: httpd.h:98
void Write(const Anope::string &message)
Definition: httpd.h:62
Definition: logger.h:53
Definition: httpd.h:88
StaticFileServer(const Anope::string &f_n, const Anope::string &u, const Anope::string &c_t)
virtual void SendError(HTTPError err, const Anope::string &msg)=0