Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / src / filter_http_file.cpp
index c5cca41..500cdc1 100644 (file)
@@ -1,16 +1,30 @@
-/* $Id: filter_http_file.cpp,v 1.8 2007-01-25 14:05:54 adam Exp $
-   Copyright (c) 2005-2007, Index Data.
+/* This file is part of Metaproxy.
+   Copyright (C) Index Data
 
-   See the LICENSE file for details
- */
+Metaproxy is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
 
 #include "config.hpp"
-#include "filter.hpp"
-#include "package.hpp"
-#include "util.hpp"
+#include <metaproxy/filter.hpp>
+#include <metaproxy/package.hpp>
+#include <metaproxy/util.hpp>
 #include "filter_http_file.hpp"
+#include <sys/stat.h>
 
 #include <yaz/zgdu.h>
+#include <yaz/log.h>
 
 #include <boost/thread/mutex.hpp>
 
@@ -34,6 +48,9 @@ namespace metaproxy_1 {
         struct HttpFile::Area {
             std::string m_url_path_prefix;
             std::string m_file_root;
+            bool m_raw;
+            bool m_passthru;
+            Area();
         };
         class HttpFile::Mime {
             friend class Rep;
@@ -54,12 +71,17 @@ namespace metaproxy_1 {
                            Z_HTTP_Request *req, mp::Package &package);
             void fetch_file(mp::Session &session,
                             Z_HTTP_Request *req,
-                            std::string &fname, mp::Package &package);
+                            std::string &fname, mp::Package &package,
+                            bool raw, bool passthru);
             std::string get_mime_type(std::string &fname);
         };
     }
 }
 
+yf::HttpFile::Area::Area() : m_raw(false), m_passthru(false)
+{
+}
+
 yf::HttpFile::Mime::Mime() {}
 
 yf::HttpFile::Mime::Mime(std::string type) : m_type(type) {}
@@ -92,7 +114,7 @@ std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
 {
     std::string file_part = fname;
     std::string::size_type p = fname.find_last_of('/');
-    
+
     if (p != std::string::npos)
         file_part = fname.substr(p+1);
 
@@ -113,10 +135,40 @@ std::string yf::HttpFile::Rep::get_mime_type(std::string &fname)
 
 void yf::HttpFile::Rep::fetch_file(mp::Session &session,
                                    Z_HTTP_Request *req,
-                                   std::string &fname, mp::Package &package)
+                                   std::string &fname, mp::Package &package,
+                                   bool raw, bool passthru)
 {
-    mp::odr o;
-    
+    mp::odr o(ODR_ENCODE);
+
+    if (strcmp(req->method, "GET"))
+    {
+        if (passthru)
+        {
+            package.move();
+        }
+        else
+        {
+            Z_GDU *gdu = o.create_HTTP_Response(session, req, 405);
+            package.response() = gdu;
+        }
+        return;
+    }
+
+    struct stat st;
+    if (stat(fname.c_str(), &st) == -1 || (st.st_mode & S_IFMT) != S_IFREG)
+    {
+        if (passthru)
+        {
+            package.move();
+        }
+        else
+        {
+            Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
+            package.response() = gdu;
+        }
+        return;
+    }
+
     FILE *f = fopen(fname.c_str(), "rb");
     if (!f)
     {
@@ -140,20 +192,40 @@ void yf::HttpFile::Rep::fetch_file(mp::Session &session,
         return;
     }
     rewind(f);
-
-    Z_GDU *gdu = o.create_HTTP_Response(session, req, 200);
-
-    Z_HTTP_Response *hres = gdu->u.HTTP_Response;
-    hres->content_len = sz;
-    hres->content_buf = (char*) odr_malloc(o, hres->content_len);
-    fread(hres->content_buf, 1, hres->content_len, f);
-
+    char *fbuf = (char*) odr_malloc(o, sz);
+    if (sz > 0)
+    {
+        if (fread(fbuf, sz, 1, f) != 1)
+        {
+            Z_GDU *gdu = o.create_HTTP_Response(session, req, 500);
+            package.response() = gdu;
+            fclose(f);
+            return;
+        }
+    }
     fclose(f);
-    
-    std::string content_type = get_mime_type(fname);
 
-    z_HTTP_header_add(o, &hres->headers,
+    Z_GDU *gdu = 0;
+    mp::odr decode(ODR_DECODE);
+    if (raw)
+    {
+        odr_setbuf(decode, (char *) fbuf, sz, 0);
+        int r = z_GDU(decode, &gdu, 0, 0);
+        if (!r)
+        {
+            gdu = o.create_HTTP_Response(session, req, 500);
+        }
+    }
+    else
+    {
+        gdu = o.create_HTTP_Response(session, req, 200);
+        Z_HTTP_Response *hres = gdu->u.HTTP_Response;
+        hres->content_len = sz;
+        hres->content_buf = fbuf;
+        std::string content_type = get_mime_type(fname);
+        z_HTTP_header_add(o, &hres->headers,
                       "Content-Type", content_type.c_str());
+    }
     package.response() = gdu;
 }
 
@@ -161,12 +233,21 @@ void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
                                   Z_HTTP_Request *req, mp::Package &package)
 {
     bool sane = true;
+    std::string::size_type p;
     std::string path = req->path;
 
-    // we don't consider ?, # yet..
+    p = path.find("#");
+    if (p != std::string::npos)
+        path = path.erase(p);
+
+    p = path.find("?");
+    if (p != std::string::npos)
+        path = path.erase(p);
+
+    path = mp::util::uri_decode(path);
 
     // we don't allow ..
-    std::string::size_type p = path.find("..");
+    p = path.find("..");
     if (p != std::string::npos)
         sane = false;
 
@@ -180,17 +261,16 @@ void yf::HttpFile::Rep::fetch_uri(mp::Session &session,
             if (path.compare(0, l, it->m_url_path_prefix) == 0)
             {
                 std::string fname = it->m_file_root + path.substr(l);
-                std::cout << "fname = " << fname << "\n";
-                fetch_file(session, req, fname, package);
+                package.log("http_file", YLOG_LOG, "%s", fname.c_str());
+                fetch_file(session, req, fname, package, it->m_raw,
+                    it->m_passthru);
                 return;
             }
         }
     }
-    mp::odr o;
-    Z_GDU *gdu = o.create_HTTP_Response(session, req, 404);
-    package.response() = gdu;
+    package.move();
 }
-                         
+
 void yf::HttpFile::process(mp::Package &package) const
 {
     Z_GDU *gdu = package.request().get();
@@ -200,7 +280,8 @@ void yf::HttpFile::process(mp::Package &package) const
         package.move();
 }
 
-void mp::filter::HttpFile::configure(const xmlNode * ptr)
+void mp::filter::HttpFile::configure(const xmlNode * ptr, bool test_only,
+                                     const char *path)
 {
     for (ptr = ptr->children; ptr; ptr = ptr->next)
     {
@@ -217,7 +298,7 @@ void mp::filter::HttpFile::configure(const xmlNode * ptr)
                 throw mp::filter::FilterException
                     ("Can not open mime types file " + fname);
             }
-            
+
             std::vector<std::string> args;
             while (f.getline(args))
             {
@@ -230,18 +311,23 @@ void mp::filter::HttpFile::configure(const xmlNode * ptr)
         {
             xmlNode *a_node = ptr->children;
             Area a;
+
             for (; a_node; a_node = a_node->next)
             {
                 if (a_node->type != XML_ELEMENT_NODE)
                     continue;
-                
+
                 if (mp::xml::is_element_mp(a_node, "documentroot"))
                     a.m_file_root = mp::xml::get_text(a_node);
                 else if (mp::xml::is_element_mp(a_node, "prefix"))
                     a.m_url_path_prefix = mp::xml::get_text(a_node);
+                else if (mp::xml::is_element_mp(a_node, "raw"))
+                    a.m_raw = mp::xml::get_bool(a_node, false);
+                else if (mp::xml::is_element_mp(a_node, "passthru"))
+                    a.m_passthru = mp::xml::get_bool(a_node, false);
                 else
                     throw mp::filter::FilterException
-                        ("Bad element " 
+                        ("Bad element "
                          + std::string((const char *) a_node->name)
                          + " in area section"
                             );
@@ -254,7 +340,7 @@ void mp::filter::HttpFile::configure(const xmlNode * ptr)
         else
         {
             throw mp::filter::FilterException
-                ("Bad element " 
+                ("Bad element "
                  + std::string((const char *) ptr->name)
                  + " in virt_db filter");
         }
@@ -278,8 +364,9 @@ extern "C" {
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
- * c-file-style: "stroustrup"
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+