Updated footer comment
[metaproxy-moved-to-github.git] / src / filter_http_file.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include "filter.hpp"
21 #include "package.hpp"
22 #include "util.hpp"
23 #include "filter_http_file.hpp"
24
25 #include <yaz/zgdu.h>
26
27 #include <boost/thread/mutex.hpp>
28
29 #include <list>
30 #include <map>
31 #include <iostream>
32
33 #if HAVE_SYS_TYPES_H
34 #include <sys/types.h>
35 #endif
36
37 #if HAVE_SYS_STAT_H
38 #include <sys/stat.h>
39 #endif
40
41 namespace mp = metaproxy_1;
42 namespace yf = mp::filter;
43
44 namespace metaproxy_1 {
45     namespace filter {
46         struct HttpFile::Area {
47             std::string m_url_path_prefix;
48             std::string m_file_root;
49         };
50         class HttpFile::Mime {
51             friend class Rep;
52             std::string m_type;
53         public:
54             Mime(std::string type);
55             Mime();
56         };
57         class HttpFile::Rep {
58             friend class HttpFile;
59
60             typedef std::list<Area> AreaList;
61             typedef std::map<std::string,Mime> MimeMap;
62
63             MimeMap m_ext_to_map;
64             AreaList m_area_list;
65             void fetch_uri(mp::Session &session,
66                            Z_HTTP_Request *req, mp::Package &package);
67             void fetch_file(mp::Session &session,
68                             Z_HTTP_Request *req,
69                             std::string &fname, mp::Package &package);
70             std::string get_mime_type(std::string &fname);
71         };
72     }
73 }
74
75 yf::HttpFile::Mime::Mime() {}
76
77 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
78
79 yf::HttpFile::HttpFile() : m_p(new Rep)
80 {
81 #if 0
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");
90 #endif
91 #if 0
92     Area a;
93     a.m_url_path_prefix = "/etc";
94     a.m_file_root = ".";
95     m_p->m_area_list.push_back(a);
96 #endif
97 }
98
99 yf::HttpFile::~HttpFile()
100 {
101 }
102
103 std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
104 {
105     std::string file_part = fname;
106     std::string::size_type p = fname.find_last_of('/');
107     
108     if (p != std::string::npos)
109         file_part = fname.substr(p+1);
110
111     p = file_part.find_last_of('.');
112     std::string content_type;
113     if (p != std::string::npos)
114     {
115         std::string ext = file_part.substr(p+1);
116         MimeMap::const_iterator it = m_ext_to_map.find(ext);
117
118         if (it != m_ext_to_map.end())
119             content_type = it->second.m_type;
120     }
121     if (content_type.length() == 0)
122         content_type = "application/octet-stream";
123     return content_type;
124 }
125
126 void yf::HttpFile::Rep::fetch_file(mp::Session &session,
127                                    Z_HTTP_Request *req,
128                                    std::string &fname, mp::Package &package)
129 {
130     mp::odr o;
131     
132     FILE *f = fopen(fname.c_str(), "rb");
133     if (!f)
134     {
135         Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
136         package.response() = gdu;
137         return;
138     }
139     if (fseek(f, 0L, SEEK_END) == -1)
140     {
141         fclose(f);
142         Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
143         package.response() = gdu;
144         return;
145     }
146     long sz = ftell(f);
147     if (sz > 1000000L)
148     {
149         fclose(f);
150         Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
151         package.response() = gdu;
152         return;
153     }
154     rewind(f);
155
156     Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
157
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);
162
163     fclose(f);
164     
165     std::string content_type = get_mime_type(fname);
166
167     z_HTTP_header_add(o, &hres->headers,
168                       "Content-Type", content_type.c_str());
169     package.response() = gdu;
170 }
171
172 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
173                                   Z_HTTP_Request *req, mp::Package &package)
174 {
175     bool sane = true;
176     std::string path = req->path;
177
178     // we don't consider ?, # yet..
179
180     // we don't allow ..
181     std::string::size_type p = path.find("..");
182     if (p != std::string::npos)
183         sane = false;
184
185     if (sane)
186     {
187         AreaList::const_iterator it;
188         for (it = m_area_list.begin(); it != m_area_list.end(); it++)
189         {
190             std::string::size_type l = it->m_url_path_prefix.length();
191
192             if (path.compare(0, l, it->m_url_path_prefix) == 0)
193             {
194                 std::string fname = it->m_file_root + path.substr(l);
195                 std::cout << "fname = " << fname << "\n";
196                 fetch_file(session, req, fname, package);
197                 return;
198             }
199         }
200     }
201     mp::odr o;
202     Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
203     package.response() = gdu;
204 }
205                          
206 void yf::HttpFile::process(mp::Package &package) const
207 {
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);
211     else
212         package.move();
213 }
214
215 void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only)
216 {
217     for (ptr = ptr->children; ptr; ptr = ptr->next)
218     {
219         if (ptr->type != XML_ELEMENT_NODE)
220             continue;
221         if (!strcmp((const char *) ptr->name, "mimetypes"))
222         {
223             std::string fname = mp::xml::get_text(ptr);
224
225             mp::PlainFile f;
226
227             if (!f.open(fname))
228             {
229                 throw mp::filter::FilterException
230                     ("Can not open mime types file " + fname);
231             }
232             
233             std::vector<std::string> args;
234             while (f.getline(args))
235             {
236                 size_t i;
237                 for (i = 1; i<args.size(); i++)
238                     m_p->m_ext_to_map[args[i]] = args[0];
239             }
240         }
241         else if (!strcmp((const char *) ptr->name, "area"))
242         {
243             xmlNode *a_node = ptr->children;
244             Area a;
245             for (; a_node; a_node = a_node->next)
246             {
247                 if (a_node->type != XML_ELEMENT_NODE)
248                     continue;
249                 
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);
254                 else
255                     throw mp::filter::FilterException
256                         ("Bad element " 
257                          + std::string((const char *) a_node->name)
258                          + " in area section"
259                             );
260             }
261             if (a.m_file_root.length())
262             {
263                 m_p->m_area_list.push_back(a);
264             }
265         }
266         else
267         {
268             throw mp::filter::FilterException
269                 ("Bad element " 
270                  + std::string((const char *) ptr->name)
271                  + " in virt_db filter");
272         }
273     }
274 }
275
276 static mp::filter::Base* filter_creator()
277 {
278     return new mp::filter::HttpFile;
279 }
280
281 extern "C" {
282     struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
283         0,
284         "http_file",
285         filter_creator
286     };
287 }
288
289
290 /*
291  * Local variables:
292  * c-basic-offset: 4
293  * c-file-style: "Stroustrup"
294  * indent-tabs-mode: nil
295  * End:
296  * vim: shiftwidth=4 tabstop=8 expandtab
297  */
298