1 /* $Id: filter_http_file.cpp,v 1.2 2006-01-25 11:28:23 adam Exp $
2 Copyright (c) 2005, Index Data.
10 #include "package.hpp"
12 #include <boost/thread/mutex.hpp>
15 #include "filter_http_file.hpp"
23 #include <sys/types.h>
30 namespace yf = yp2::filter;
34 struct HttpFile::Area {
35 std::string m_url_path_prefix;
36 std::string m_file_root;
38 class HttpFile::Mime {
42 Mime(std::string type);
46 friend class HttpFile;
48 typedef std::list<Area> AreaList;
49 typedef std::map<std::string,Mime> MimeMap;
53 void fetch_uri(yp2::Session &session,
54 Z_HTTP_Request *req, yp2::Package &package);
55 void fetch_file(yp2::Session &session,
57 std::string &fname, yp2::Package &package);
58 std::string get_mime_type(std::string &fname);
63 yf::HttpFile::Mime::Mime() {}
65 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
67 yf::HttpFile::HttpFile() : m_p(new Rep)
70 m_p->m_ext_to_map["html"] = Mime("text/html");
71 m_p->m_ext_to_map["htm"] = Mime("text/html");
72 m_p->m_ext_to_map["png"] = Mime("image/png");
73 m_p->m_ext_to_map["txt"] = Mime("text/plain");
74 m_p->m_ext_to_map["text"] = Mime("text/plain");
75 m_p->m_ext_to_map["asc"] = Mime("text/plain");
76 m_p->m_ext_to_map["xml"] = Mime("application/xml");
77 m_p->m_ext_to_map["xsl"] = Mime("application/xml");
81 a.m_url_path_prefix = "/etc";
83 m_p->m_area_list.push_back(a);
87 yf::HttpFile::~HttpFile()
91 std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
93 std::string file_part = fname;
94 std::string::size_type p = fname.find_last_of('/');
96 if (p != std::string::npos)
97 file_part = fname.substr(p+1);
99 p = file_part.find_last_of('.');
100 std::string content_type;
101 if (p != std::string::npos)
103 std::string ext = file_part.substr(p+1);
104 MimeMap::const_iterator it = m_ext_to_map.find(ext);
106 if (it != m_ext_to_map.end())
107 content_type = it->second.m_type;
109 if (content_type.length() == 0)
110 content_type = "application/octet-stream";
114 void yf::HttpFile::Rep::fetch_file(yp2::Session &session,
116 std::string &fname, yp2::Package &package)
121 if (stat(fname.c_str(), &sbuf))
123 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
124 package.response() = gdu;
127 if ((sbuf.st_mode & S_IFMT) != S_IFREG)
129 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
130 package.response() = gdu;
133 if (sbuf.st_size > (off_t) 1000000)
135 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
136 package.response() = gdu;
140 FILE *f = fopen(fname.c_str(), "rb");
143 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
144 package.response() = gdu;
147 Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
149 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
150 hres->content_len = sbuf.st_size;
151 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
152 fread(hres->content_buf, 1, hres->content_len, f);
156 std::string content_type = get_mime_type(fname);
158 z_HTTP_header_add(o, &hres->headers,
159 "Content-Type", content_type.c_str());
160 package.response() = gdu;
163 void yf::HttpFile::Rep::fetch_uri(yp2::Session &session,
164 Z_HTTP_Request *req, yp2::Package &package)
167 std::string path = req->path;
169 // we don't consider ?, # yet..
172 std::string::size_type p = path.find("..");
173 if (p != std::string::npos)
178 AreaList::const_iterator it;
179 for (it = m_area_list.begin(); it != m_area_list.end(); it++)
181 std::string::size_type l = it->m_url_path_prefix.length();
183 if (path.compare(0, l, it->m_url_path_prefix) == 0)
185 std::string fname = it->m_file_root + path.substr(l);
186 std::cout << "fname = " << fname << "\n";
187 fetch_file(session, req, fname, package);
193 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
194 package.response() = gdu;
197 void yf::HttpFile::process(yp2::Package &package) const
199 Z_GDU *gdu = package.request().get();
200 if (gdu && gdu->which == Z_GDU_HTTP_Request)
201 m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
206 void yp2::filter::HttpFile::configure(const xmlNode * ptr)
208 for (ptr = ptr->children; ptr; ptr = ptr->next)
210 if (ptr->type != XML_ELEMENT_NODE)
212 if (!strcmp((const char *) ptr->name, "mimetypes"))
214 std::string fname = yp2::xml::get_text(ptr);
220 throw yp2::filter::FilterException
221 ("Can not open mime types file " + fname);
224 std::vector<std::string> args;
225 while (f.getline(args))
228 for (i = 1; i<args.size(); i++)
229 m_p->m_ext_to_map[args[i]] = args[0];
232 else if (!strcmp((const char *) ptr->name, "area"))
234 xmlNode *a_node = ptr->children;
236 for (; a_node; a_node = a_node->next)
238 if (a_node->type != XML_ELEMENT_NODE)
241 if (yp2::xml::is_element_yp2(a_node, "documentroot"))
242 a.m_file_root = yp2::xml::get_text(a_node);
243 else if (yp2::xml::is_element_yp2(a_node, "prefix"))
244 a.m_url_path_prefix = yp2::xml::get_text(a_node);
246 throw yp2::filter::FilterException
248 + std::string((const char *) a_node->name)
252 if (a.m_file_root.length())
254 m_p->m_area_list.push_back(a);
259 throw yp2::filter::FilterException
261 + std::string((const char *) ptr->name)
262 + " in virt_db filter");
267 static yp2::filter::Base* filter_creator()
269 return new yp2::filter::HttpFile;
273 struct yp2_filter_struct yp2_filter_http_file = {
284 * indent-tabs-mode: nil
285 * c-file-style: "stroustrup"
287 * vim: shiftwidth=4 tabstop=8 expandtab