1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include "filter_http_file.hpp"
28 #include <boost/thread/mutex.hpp>
35 #include <sys/types.h>
42 namespace mp = metaproxy_1;
43 namespace yf = mp::filter;
45 namespace metaproxy_1 {
47 struct HttpFile::Area {
48 std::string m_url_path_prefix;
49 std::string m_file_root;
51 class HttpFile::Mime {
55 Mime(std::string type);
59 friend class HttpFile;
61 typedef std::list<Area> AreaList;
62 typedef std::map<std::string,Mime> MimeMap;
66 void fetch_uri(mp::Session &session,
67 Z_HTTP_Request *req, mp::Package &package);
68 void fetch_file(mp::Session &session,
70 std::string &fname, mp::Package &package);
71 std::string get_mime_type(std::string &fname);
76 yf::HttpFile::Mime::Mime() {}
78 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
80 yf::HttpFile::HttpFile() : m_p(new Rep)
83 m_p->m_ext_to_map["html"] = Mime("text/html");
84 m_p->m_ext_to_map["htm"] = Mime("text/html");
85 m_p->m_ext_to_map["png"] = Mime("image/png");
86 m_p->m_ext_to_map["txt"] = Mime("text/plain");
87 m_p->m_ext_to_map["text"] = Mime("text/plain");
88 m_p->m_ext_to_map["asc"] = Mime("text/plain");
89 m_p->m_ext_to_map["xml"] = Mime("application/xml");
90 m_p->m_ext_to_map["xsl"] = Mime("application/xml");
94 a.m_url_path_prefix = "/etc";
96 m_p->m_area_list.push_back(a);
100 yf::HttpFile::~HttpFile()
104 std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
106 std::string file_part = fname;
107 std::string::size_type p = fname.find_last_of('/');
109 if (p != std::string::npos)
110 file_part = fname.substr(p+1);
112 p = file_part.find_last_of('.');
113 std::string content_type;
114 if (p != std::string::npos)
116 std::string ext = file_part.substr(p+1);
117 MimeMap::const_iterator it = m_ext_to_map.find(ext);
119 if (it != m_ext_to_map.end())
120 content_type = it->second.m_type;
122 if (content_type.length() == 0)
123 content_type = "application/octet-stream";
127 void yf::HttpFile::Rep::fetch_file(mp::Session &session,
129 std::string &fname, mp::Package &package)
133 FILE *f = fopen(fname.c_str(), "rb");
136 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
137 package.response() = gdu;
140 if (fseek(f, 0L, SEEK_END) == -1)
143 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
144 package.response() = gdu;
151 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
152 package.response() = gdu;
157 Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
159 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
160 hres->content_len = sz;
161 if (hres->content_len > 0)
163 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
164 if (fread(hres->content_buf, hres->content_len, 1, f) != 1)
167 Z_GDU *gdu = o.create_HTTP_Response(session, req, 500);
168 package.response() = gdu;
174 std::string content_type = get_mime_type(fname);
176 z_HTTP_header_add(o, &hres->headers,
177 "Content-Type", content_type.c_str());
178 package.response() = gdu;
181 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
182 Z_HTTP_Request *req, mp::Package &package)
185 std::string::size_type p;
186 std::string path = req->path;
189 if (p != std::string::npos)
190 path = path.erase(p);
193 if (p != std::string::npos)
194 path = path.erase(p);
196 path = mp::util::uri_decode(path);
200 if (p != std::string::npos)
205 AreaList::const_iterator it;
206 for (it = m_area_list.begin(); it != m_area_list.end(); it++)
208 std::string::size_type l = it->m_url_path_prefix.length();
210 if (path.compare(0, l, it->m_url_path_prefix) == 0)
212 std::string fname = it->m_file_root + path.substr(l);
213 package.log("http_file", YLOG_LOG, "%s", fname.c_str());
214 fetch_file(session, req, fname, package);
222 void yf::HttpFile::process(mp::Package &package) const
224 Z_GDU *gdu = package.request().get();
225 if (gdu && gdu->which == Z_GDU_HTTP_Request)
226 m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
231 void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only,
234 for (ptr = ptr->children; ptr; ptr = ptr->next)
236 if (ptr->type != XML_ELEMENT_NODE)
238 if (!strcmp((const char *) ptr->name, "mimetypes"))
240 std::string fname = mp::xml::get_text(ptr);
246 throw mp::filter::FilterException
247 ("Can not open mime types file " + fname);
250 std::vector<std::string> args;
251 while (f.getline(args))
254 for (i = 1; i<args.size(); i++)
255 m_p->m_ext_to_map[args[i]] = args[0];
258 else if (!strcmp((const char *) ptr->name, "area"))
260 xmlNode *a_node = ptr->children;
262 for (; a_node; a_node = a_node->next)
264 if (a_node->type != XML_ELEMENT_NODE)
267 if (mp::xml::is_element_mp(a_node, "documentroot"))
268 a.m_file_root = mp::xml::get_text(a_node);
269 else if (mp::xml::is_element_mp(a_node, "prefix"))
270 a.m_url_path_prefix = mp::xml::get_text(a_node);
272 throw mp::filter::FilterException
274 + std::string((const char *) a_node->name)
278 if (a.m_file_root.length())
280 m_p->m_area_list.push_back(a);
285 throw mp::filter::FilterException
287 + std::string((const char *) ptr->name)
288 + " in virt_db filter");
293 static mp::filter::Base* filter_creator()
295 return new mp::filter::HttpFile;
299 struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
310 * c-file-style: "Stroustrup"
311 * indent-tabs-mode: nil
313 * vim: shiftwidth=4 tabstop=8 expandtab