Work on raw fetch in http_file
[metaproxy-moved-to-github.git] / src / filter_http_file.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 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 <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include "filter_http_file.hpp"
24
25 #include <yaz/zgdu.h>
26 #include <yaz/log.h>
27
28 #include <boost/thread/mutex.hpp>
29
30 #include <list>
31 #include <map>
32 #include <iostream>
33
34 #if HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #if HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41
42 namespace mp = metaproxy_1;
43 namespace yf = mp::filter;
44
45 namespace metaproxy_1 {
46     namespace filter {
47         struct HttpFile::Area {
48             std::string m_url_path_prefix;
49             std::string m_file_root;
50             bool m_raw;
51             Area();
52         };
53         class HttpFile::Mime {
54             friend class Rep;
55             std::string m_type;
56         public:
57             Mime(std::string type);
58             Mime();
59         };
60         class HttpFile::Rep {
61             friend class HttpFile;
62
63             typedef std::list<Area> AreaList;
64             typedef std::map<std::string,Mime> MimeMap;
65
66             MimeMap m_ext_to_map;
67             AreaList m_area_list;
68             void fetch_uri(mp::Session &session,
69                            Z_HTTP_Request *req, mp::Package &package);
70             void fetch_file(mp::Session &session,
71                             Z_HTTP_Request *req,
72                             std::string &fname, mp::Package &package,
73                             bool raw);
74             std::string get_mime_type(std::string &fname);
75         };
76     }
77 }
78
79 yf::HttpFile::Area::Area() : m_raw(false)
80 {
81 }
82
83 yf::HttpFile::Mime::Mime() {}
84
85 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
86
87 yf::HttpFile::HttpFile() : m_p(new Rep)
88 {
89 #if 0
90     m_p->m_ext_to_map["html"] = Mime("text/html");
91     m_p->m_ext_to_map["htm"] = Mime("text/html");
92     m_p->m_ext_to_map["png"] = Mime("image/png");
93     m_p->m_ext_to_map["txt"] = Mime("text/plain");
94     m_p->m_ext_to_map["text"] = Mime("text/plain");
95     m_p->m_ext_to_map["asc"] = Mime("text/plain");
96     m_p->m_ext_to_map["xml"] = Mime("application/xml");
97     m_p->m_ext_to_map["xsl"] = Mime("application/xml");
98 #endif
99 #if 0
100     Area a;
101     a.m_url_path_prefix = "/etc";
102     a.m_file_root = ".";
103     m_p->m_area_list.push_back(a);
104 #endif
105 }
106
107 yf::HttpFile::~HttpFile()
108 {
109 }
110
111 std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
112 {
113     std::string file_part = fname;
114     std::string::size_type p = fname.find_last_of('/');
115
116     if (p != std::string::npos)
117         file_part = fname.substr(p+1);
118
119     p = file_part.find_last_of('.');
120     std::string content_type;
121     if (p != std::string::npos)
122     {
123         std::string ext = file_part.substr(p+1);
124         MimeMap::const_iterator it = m_ext_to_map.find(ext);
125
126         if (it != m_ext_to_map.end())
127             content_type = it->second.m_type;
128     }
129     if (content_type.length() == 0)
130         content_type = "application/octet-stream";
131     return content_type;
132 }
133
134 void yf::HttpFile::Rep::fetch_file(mp::Session &session,
135                                    Z_HTTP_Request *req,
136                                    std::string &fname, mp::Package &package,
137                                    bool raw)
138 {
139     mp::odr o(ODR_ENCODE);
140
141     FILE *f = fopen(fname.c_str(), "rb");
142     if (!f)
143     {
144         Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
145         package.response() = gdu;
146         return;
147     }
148     if (fseek(f, 0L, SEEK_END) == -1)
149     {
150         fclose(f);
151         Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
152         package.response() = gdu;
153         return;
154     }
155     long sz = ftell(f);
156     if (sz > 1000000L)
157     {
158         fclose(f);
159         Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
160         package.response() = gdu;
161         return;
162     }
163     rewind(f);
164     char *fbuf = (char*) odr_malloc(o, sz);
165     if (sz > 0)
166     {
167         if (fread(fbuf, sz, 1, f) != 1)
168         {
169             Z_GDU *gdu = o.create_HTTP_Response(session, req, 500);
170             package.response() = gdu;
171             fclose(f);
172             return;
173         }
174     }
175     fclose(f);
176
177     Z_GDU *gdu = 0;
178     if (raw)
179     {
180         odr_setbuf(o, (char *) fbuf, sz, 0);
181         int r = z_GDU(o, &gdu, 0, 0);
182         if (!r)
183         {
184             Z_GDU *gdu = o.create_HTTP_Response(session, req, 500);
185             package.response() = gdu;
186             fclose(f);
187             return;
188         }
189     }
190     else
191     {
192         gdu = o.create_HTTP_Response(session, req, 200);
193         Z_HTTP_Response *hres = gdu->u.HTTP_Response;
194         hres->content_len = sz;
195         hres->content_buf = fbuf;
196         std::string content_type = get_mime_type(fname);
197         z_HTTP_header_add(o, &hres->headers,
198                       "Content-Type", content_type.c_str());
199     }
200     package.response() = gdu;
201 }
202
203 void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
204                                   Z_HTTP_Request *req, mp::Package &package)
205 {
206     bool sane = true;
207     std::string::size_type p;
208     std::string path = req->path;
209
210     p = path.find("#");
211     if (p != std::string::npos)
212         path = path.erase(p);
213
214     p = path.find("?");
215     if (p != std::string::npos)
216         path = path.erase(p);
217
218     path = mp::util::uri_decode(path);
219
220     // we don't allow ..
221     p = path.find("..");
222     if (p != std::string::npos)
223         sane = false;
224
225     if (sane)
226     {
227         AreaList::const_iterator it;
228         for (it = m_area_list.begin(); it != m_area_list.end(); it++)
229         {
230             std::string::size_type l = it->m_url_path_prefix.length();
231
232             if (path.compare(0, l, it->m_url_path_prefix) == 0)
233             {
234                 std::string fname = it->m_file_root + path.substr(l);
235                 package.log("http_file", YLOG_LOG, "%s", fname.c_str());
236                 fetch_file(session, req, fname, package, it->m_raw);
237                 return;
238             }
239         }
240     }
241     package.move();
242 }
243
244 void yf::HttpFile::process(mp::Package &package) const
245 {
246     Z_GDU *gdu = package.request().get();
247     if (gdu && gdu->which == Z_GDU_HTTP_Request)
248         m_p->fetch_uri(package.session(), gdu->u.HTTP_Request, package);
249     else
250         package.move();
251 }
252
253 void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only,
254                                      const char *path)
255 {
256     for (ptr = ptr->children; ptr; ptr = ptr->next)
257     {
258         if (ptr->type != XML_ELEMENT_NODE)
259             continue;
260         if (!strcmp((const char *) ptr->name, "mimetypes"))
261         {
262             std::string fname = mp::xml::get_text(ptr);
263
264             mp::PlainFile f;
265
266             if (!f.open(fname))
267             {
268                 throw mp::filter::FilterException
269                     ("Can not open mime types file " + fname);
270             }
271
272             std::vector<std::string> args;
273             while (f.getline(args))
274             {
275                 size_t i;
276                 for (i = 1; i<args.size(); i++)
277                     m_p->m_ext_to_map[args[i]] = args[0];
278             }
279         }
280         else if (!strcmp((const char *) ptr->name, "area"))
281         {
282             xmlNode *a_node = ptr->children;
283             Area a;
284
285             for (; a_node; a_node = a_node->next)
286             {
287                 if (a_node->type != XML_ELEMENT_NODE)
288                     continue;
289
290                 if (mp::xml::is_element_mp(a_node, "documentroot"))
291                     a.m_file_root = mp::xml::get_text(a_node);
292                 else if (mp::xml::is_element_mp(a_node, "prefix"))
293                     a.m_url_path_prefix = mp::xml::get_text(a_node);
294                 else if (mp::xml::is_element_mp(a_node, "raw"))
295                     a.m_raw = mp::xml::get_bool(a_node, false);
296                 else
297                     throw mp::filter::FilterException
298                         ("Bad element "
299                          + std::string((const char *) a_node->name)
300                          + " in area section"
301                             );
302             }
303             if (a.m_file_root.length())
304             {
305                 m_p->m_area_list.push_back(a);
306             }
307         }
308         else
309         {
310             throw mp::filter::FilterException
311                 ("Bad element "
312                  + std::string((const char *) ptr->name)
313                  + " in virt_db filter");
314         }
315     }
316 }
317
318 static mp::filter::Base* filter_creator()
319 {
320     return new mp::filter::HttpFile;
321 }
322
323 extern "C" {
324     struct metaproxy_1_filter_struct metaproxy_1_filter_http_file = {
325         0,
326         "http_file",
327         filter_creator
328     };
329 }
330
331
332 /*
333  * Local variables:
334  * c-basic-offset: 4
335  * c-file-style: "Stroustrup"
336  * indent-tabs-mode: nil
337  * End:
338  * vim: shiftwidth=4 tabstop=8 expandtab
339  */
340