1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 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
21 #include "package.hpp"
23 #include "filter_http_file.hpp"
27 #include <boost/thread/mutex.hpp>
34 #include <sys/types.h>
41 namespace mp = metaproxy_1;
42 namespace yf = mp::filter;
44 namespace metaproxy_1 {
46 struct HttpFile::Area {
47 std::string m_url_path_prefix;
48 std::string m_file_root;
50 class HttpFile::Mime {
54 Mime(std::string type);
58 friend class HttpFile;
60 typedef std::list<Area> AreaList;
61 typedef std::map<std::string,Mime> MimeMap;
65 void fetch_uri(mp::Session &session,
66 Z_HTTP_Request *req, mp::Package &package);
67 void fetch_file(mp::Session &session,
69 std::string &fname, mp::Package &package);
70 std::string get_mime_type(std::string &fname);
75 yf::HttpFile::Mime::Mime() {}
77 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
79 yf::HttpFile::HttpFile() : m_p(new Rep)
82 m_p->m_ext_to_map["html"] = Mime("text/html");
83 m_p->m_ext_to_map["htm"] = Mime("text/html");
84 m_p->m_ext_to_map["png"] = Mime("image/png");
85 m_p->m_ext_to_map["txt"] = Mime("text/plain");
86 m_p->m_ext_to_map["text"] = Mime("text/plain");
87 m_p->m_ext_to_map["asc"] = Mime("text/plain");
88 m_p->m_ext_to_map["xml"] = Mime("application/xml");
89 m_p->m_ext_to_map["xsl"] = Mime("application/xml");
93 a.m_url_path_prefix = "/etc";
95 m_p->m_area_list.push_back(a);
99 yf::HttpFile::~HttpFile()
103 std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
105 std::string file_part = fname;
106 std::string::size_type p = fname.find_last_of('/');
108 if (p != std::string::npos)
109 file_part = fname.substr(p+1);
111 p = file_part.find_last_of('.');
112 std::string content_type;
113 if (p != std::string::npos)
115 std::string ext = file_part.substr(p+1);
116 MimeMap::const_iterator it = m_ext_to_map.find(ext);
118 if (it != m_ext_to_map.end())
119 content_type = it->second.m_type;
121 if (content_type.length() == 0)
122 content_type = "application/octet-stream";
126 void yf::HttpFile::Rep::fetch_file(mp::Session &session,
128 std::string &fname, mp::Package &package)
132 FILE *f = fopen(fname.c_str(), "rb");
135 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
136 package.response() = gdu;
139 if (fseek(f, 0L, SEEK_END) == -1)
142 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
143 package.response() = gdu;
150 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
151 package.response() = gdu;
156 Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
158 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
159 hres->content_len = sz;
160 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
161 if (fread(hres->content_buf, hres->content_len, 1, f) != 1)
164 Z_GDU *gdu = o.create_HTTP_Response(session, req, 500);
165 package.response() = gdu;
171 std::string content_type = get_mime_type(fname);
173 z_HTTP_header_add(o, &hres->headers,
174 "Content-Type", content_type.c_str());
175 package.response() = gdu;
178 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
179 Z_HTTP_Request *req, mp::Package &package)
182 std::string path = req->path;
184 // we don't consider ?, # yet..
187 std::string::size_type p = path.find("..");
188 if (p != std::string::npos)
193 AreaList::const_iterator it;
194 for (it = m_area_list.begin(); it != m_area_list.end(); it++)
196 std::string::size_type l = it->m_url_path_prefix.length();
198 if (path.compare(0, l, it->m_url_path_prefix) == 0)
200 std::string fname = it->m_file_root + path.substr(l);
201 std::cout << "fname = " << fname << "\n";
202 fetch_file(session, req, fname, package);
208 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
209 package.response() = gdu;
212 void yf::HttpFile::process(mp::Package &package) const
214 Z_GDU *gdu = package.request().get();
215 if (gdu && gdu->which == Z_GDU_HTTP_Request)
216 m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
221 void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only)
223 for (ptr = ptr->children; ptr; ptr = ptr->next)
225 if (ptr->type != XML_ELEMENT_NODE)
227 if (!strcmp((const char *) ptr->name, "mimetypes"))
229 std::string fname = mp::xml::get_text(ptr);
235 throw mp::filter::FilterException
236 ("Can not open mime types file " + fname);
239 std::vector<std::string> args;
240 while (f.getline(args))
243 for (i = 1; i<args.size(); i++)
244 m_p->m_ext_to_map[args[i]] = args[0];
247 else if (!strcmp((const char *) ptr->name, "area"))
249 xmlNode *a_node = ptr->children;
251 for (; a_node; a_node = a_node->next)
253 if (a_node->type != XML_ELEMENT_NODE)
256 if (mp::xml::is_element_mp(a_node, "documentroot"))
257 a.m_file_root = mp::xml::get_text(a_node);
258 else if (mp::xml::is_element_mp(a_node, "prefix"))
259 a.m_url_path_prefix = mp::xml::get_text(a_node);
261 throw mp::filter::FilterException
263 + std::string((const char *) a_node->name)
267 if (a.m_file_root.length())
269 m_p->m_area_list.push_back(a);
274 throw mp::filter::FilterException
276 + std::string((const char *) ptr->name)
277 + " in virt_db filter");
282 static mp::filter::Base* filter_creator()
284 return new mp::filter::HttpFile;
288 struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
299 * c-file-style: "Stroustrup"
300 * indent-tabs-mode: nil
302 * vim: shiftwidth=4 tabstop=8 expandtab