anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

static_fileserver.cpp (1098B)

      1 /*
      2  * (C) 2003-2022 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 }