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