1 /* $Id: filter_http_file.cpp,v 1.10 2008-02-20 15:07:51 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "package.hpp"
26 #include "filter_http_file.hpp"
30 #include <boost/thread/mutex.hpp>
37 #include <sys/types.h>
44 namespace mp = metaproxy_1;
45 namespace yf = mp::filter;
47 namespace metaproxy_1 {
49 struct HttpFile::Area {
50 std::string m_url_path_prefix;
51 std::string m_file_root;
53 class HttpFile::Mime {
57 Mime(std::string type);
61 friend class HttpFile;
63 typedef std::list<Area> AreaList;
64 typedef std::map<std::string,Mime> MimeMap;
68 void fetch_uri(mp::Session &session,
69 Z_HTTP_Request *req, mp::Package &package);
70 void fetch_file(mp::Session &session,
72 std::string &fname, mp::Package &package);
73 std::string get_mime_type(std::string &fname);
78 yf::HttpFile::Mime::Mime() {}
80 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
82 yf::HttpFile::HttpFile() : m_p(new Rep)
85 m_p->m_ext_to_map["html"] = Mime("text/html");
86 m_p->m_ext_to_map["htm"] = Mime("text/html");
87 m_p->m_ext_to_map["png"] = Mime("image/png");
88 m_p->m_ext_to_map["txt"] = Mime("text/plain");
89 m_p->m_ext_to_map["text"] = Mime("text/plain");
90 m_p->m_ext_to_map["asc"] = Mime("text/plain");
91 m_p->m_ext_to_map["xml"] = Mime("application/xml");
92 m_p->m_ext_to_map["xsl"] = Mime("application/xml");
96 a.m_url_path_prefix = "/etc";
98 m_p->m_area_list.push_back(a);
102 yf::HttpFile::~HttpFile()
106 std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
108 std::string file_part = fname;
109 std::string::size_type p = fname.find_last_of('/');
111 if (p != std::string::npos)
112 file_part = fname.substr(p+1);
114 p = file_part.find_last_of('.');
115 std::string content_type;
116 if (p != std::string::npos)
118 std::string ext = file_part.substr(p+1);
119 MimeMap::const_iterator it = m_ext_to_map.find(ext);
121 if (it != m_ext_to_map.end())
122 content_type = it->second.m_type;
124 if (content_type.length() == 0)
125 content_type = "application/octet-stream";
129 void yf::HttpFile::Rep::fetch_file(mp::Session &session,
131 std::string &fname, mp::Package &package)
135 FILE *f = fopen(fname.c_str(), "rb");
138 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
139 package.response() = gdu;
142 if (fseek(f, 0L, SEEK_END) == -1)
145 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
146 package.response() = gdu;
153 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
154 package.response() = gdu;
159 Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
161 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
162 hres->content_len = sz;
163 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
164 fread(hres->content_buf, 1, hres->content_len, f);
168 std::string content_type = get_mime_type(fname);
170 z_HTTP_header_add(o, &hres->headers,
171 "Content-Type", content_type.c_str());
172 package.response() = gdu;
175 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
176 Z_HTTP_Request *req, mp::Package &package)
179 std::string path = req->path;
181 // we don't consider ?, # yet..
184 std::string::size_type p = path.find("..");
185 if (p != std::string::npos)
190 AreaList::const_iterator it;
191 for (it = m_area_list.begin(); it != m_area_list.end(); it++)
193 std::string::size_type l = it->m_url_path_prefix.length();
195 if (path.compare(0, l, it->m_url_path_prefix) == 0)
197 std::string fname = it->m_file_root + path.substr(l);
198 std::cout << "fname = " << fname << "\n";
199 fetch_file(session, req, fname, package);
205 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
206 package.response() = gdu;
209 void yf::HttpFile::process(mp::Package &package) const
211 Z_GDU *gdu = package.request().get();
212 if (gdu && gdu->which == Z_GDU_HTTP_Request)
213 m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
218 void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only)
220 for (ptr = ptr->children; ptr; ptr = ptr->next)
222 if (ptr->type != XML_ELEMENT_NODE)
224 if (!strcmp((const char *) ptr->name, "mimetypes"))
226 std::string fname = mp::xml::get_text(ptr);
232 throw mp::filter::FilterException
233 ("Can not open mime types file " + fname);
236 std::vector<std::string> args;
237 while (f.getline(args))
240 for (i = 1; i<args.size(); i++)
241 m_p->m_ext_to_map[args[i]] = args[0];
244 else if (!strcmp((const char *) ptr->name, "area"))
246 xmlNode *a_node = ptr->children;
248 for (; a_node; a_node = a_node->next)
250 if (a_node->type != XML_ELEMENT_NODE)
253 if (mp::xml::is_element_mp(a_node, "documentroot"))
254 a.m_file_root = mp::xml::get_text(a_node);
255 else if (mp::xml::is_element_mp(a_node, "prefix"))
256 a.m_url_path_prefix = mp::xml::get_text(a_node);
258 throw mp::filter::FilterException
260 + std::string((const char *) a_node->name)
264 if (a.m_file_root.length())
266 m_p->m_area_list.push_back(a);
271 throw mp::filter::FilterException
273 + std::string((const char *) ptr->name)
274 + " in virt_db filter");
279 static mp::filter::Base* filter_creator()
281 return new mp::filter::HttpFile;
285 struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
296 * indent-tabs-mode: nil
297 * c-file-style: "stroustrup"
299 * vim: shiftwidth=4 tabstop=8 expandtab