1 /* $Id: filter_http_file.cpp,v 1.6 2006-08-29 10:34:32 marc Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
11 #include "filter_http_file.hpp"
15 #include <boost/thread/mutex.hpp>
22 #include <sys/types.h>
29 namespace mp = metaproxy_1;
30 namespace yf = mp::filter;
32 namespace metaproxy_1 {
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(mp::Session &session,
54 Z_HTTP_Request *req, mp::Package &package);
55 void fetch_file(mp::Session &session,
57 std::string &fname, mp::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(mp::Session &session,
116 std::string &fname, mp::Package &package)
120 FILE *f = fopen(fname.c_str(), "rb");
123 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
124 package.response() = gdu;
127 if (fseek(f, 0L, SEEK_END) == -1)
130 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
131 package.response() = gdu;
138 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
139 package.response() = gdu;
144 Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
146 Z_HTTP_Response *hres = gdu->u.HTTP_Response;
147 hres->content_len = sz;
148 hres->content_buf = (char*) odr_malloc(o, hres->content_len);
149 fread(hres->content_buf, 1, hres->content_len, f);
153 std::string content_type = get_mime_type(fname);
155 z_HTTP_header_add(o, &hres->headers,
156 "Content-Type", content_type.c_str());
157 package.response() = gdu;
160 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
161 Z_HTTP_Request *req, mp::Package &package)
164 std::string path = req->path;
166 // we don't consider ?, # yet..
169 std::string::size_type p = path.find("..");
170 if (p != std::string::npos)
175 AreaList::const_iterator it;
176 for (it = m_area_list.begin(); it != m_area_list.end(); it++)
178 std::string::size_type l = it->m_url_path_prefix.length();
180 if (path.compare(0, l, it->m_url_path_prefix) == 0)
182 std::string fname = it->m_file_root + path.substr(l);
183 std::cout << "fname = " << fname << "\n";
184 fetch_file(session, req, fname, package);
190 Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
191 package.response() = gdu;
194 void yf::HttpFile::process(mp::Package &package) const
196 Z_GDU *gdu = package.request().get();
197 if (gdu && gdu->which == Z_GDU_HTTP_Request)
198 m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
203 void mp::filter::HttpFile::configure(const xmlNode * ptr)
205 for (ptr = ptr->children; ptr; ptr = ptr->next)
207 if (ptr->type != XML_ELEMENT_NODE)
209 if (!strcmp((const char *) ptr->name, "mimetypes"))
211 std::string fname = mp::xml::get_text(ptr);
217 throw mp::filter::FilterException
218 ("Can not open mime types file " + fname);
221 std::vector<std::string> args;
222 while (f.getline(args))
225 for (i = 1; i<args.size(); i++)
226 m_p->m_ext_to_map[args[i]] = args[0];
229 else if (!strcmp((const char *) ptr->name, "area"))
231 xmlNode *a_node = ptr->children;
233 for (; a_node; a_node = a_node->next)
235 if (a_node->type != XML_ELEMENT_NODE)
238 if (mp::xml::is_element_yp2(a_node, "documentroot"))
239 a.m_file_root = mp::xml::get_text(a_node);
240 else if (mp::xml::is_element_yp2(a_node, "prefix"))
241 a.m_url_path_prefix = mp::xml::get_text(a_node);
243 throw mp::filter::FilterException
245 + std::string((const char *) a_node->name)
249 if (a.m_file_root.length())
251 m_p->m_area_list.push_back(a);
256 throw mp::filter::FilterException
258 + std::string((const char *) ptr->name)
259 + " in virt_db filter");
264 static mp::filter::Base* filter_creator()
266 return new mp::filter::HttpFile;
270 struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
281 * indent-tabs-mode: nil
282 * c-file-style: "stroustrup"
284 * vim: shiftwidth=4 tabstop=8 expandtab