1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 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 fread(hres->content_buf, 1, hres->content_len, f);
165 std::string content_type = get_mime_type(fname);
167 z_HTTP_header_add(o, &hres->headers,
168 "Content-Type", content_type.c_str());
169 package.response() = gdu;
172 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
173 Z_HTTP_Request *req, mp::Package &package)
176 std::string path = req->path;
178 // we don't consider ?, # yet..
181 std::string::size_type p = path.find("..");
182 if (p != std::string::npos)
187 AreaList::const_iterator it;
188 for (it = m_area_list.begin(); it != m_area_list.end(); it++)
190 std::string::size_type l = it->m_url_path_prefix.length();
192 if (path.compare(0, l, it->m_url_path_prefix) == 0)
194 std::string fname = it->m_file_root + path.substr(l);
195 std::cout << "fname = " << fname << "\n";
196 fetch_file(session, req, fname, package);
202 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
203 package.response() = gdu;
206 void yf::HttpFile::process(mp::Package &package) const
208 Z_GDU *gdu = package.request().get();
209 if (gdu && gdu->which == Z_GDU_HTTP_Request)
210 m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
215 void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only)
217 for (ptr = ptr->children; ptr; ptr = ptr->next)
219 if (ptr->type != XML_ELEMENT_NODE)
221 if (!strcmp((const char *) ptr->name, "mimetypes"))
223 std::string fname = mp::xml::get_text(ptr);
229 throw mp::filter::FilterException
230 ("Can not open mime types file " + fname);
233 std::vector<std::string> args;
234 while (f.getline(args))
237 for (i = 1; i<args.size(); i++)
238 m_p->m_ext_to_map[args[i]] = args[0];
241 else if (!strcmp((const char *) ptr->name, "area"))
243 xmlNode *a_node = ptr->children;
245 for (; a_node; a_node = a_node->next)
247 if (a_node->type != XML_ELEMENT_NODE)
250 if (mp::xml::is_element_mp(a_node, "documentroot"))
251 a.m_file_root = mp::xml::get_text(a_node);
252 else if (mp::xml::is_element_mp(a_node, "prefix"))
253 a.m_url_path_prefix = mp::xml::get_text(a_node);
255 throw mp::filter::FilterException
257 + std::string((const char *) a_node->name)
261 if (a.m_file_root.length())
263 m_p->m_area_list.push_back(a);
268 throw mp::filter::FilterException
270 + std::string((const char *) ptr->name)
271 + " in virt_db filter");
276 static mp::filter::Base* filter_creator()
278 return new mp::filter::HttpFile;
282 struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
293 * indent-tabs-mode: nil
294 * c-file-style: "stroustrup"
296 * vim: shiftwidth=4 tabstop=8 expandtab