From 2bab0500e690612c044b1d9f87a0f03bc1640743 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 13 Dec 2004 20:52:33 +0000 Subject: [PATCH] Using yaz/log.h again. Added config file HTTP access. --- include/yazproxy/proxy.h | 4 +- src/yaz-bw.cpp | 4 +- src/yaz-proxy-config.cpp | 32 ++++++++++++++- src/yaz-proxy-main.cpp | 4 +- src/yaz-proxy.cpp | 99 ++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 124 insertions(+), 19 deletions(-) diff --git a/include/yazproxy/proxy.h b/include/yazproxy/proxy.h index 9ddd94f..ac9b503 100644 --- a/include/yazproxy/proxy.h +++ b/include/yazproxy/proxy.h @@ -1,4 +1,4 @@ -/* $Id: proxy.h,v 1.9 2004-12-03 14:28:18 adam Exp $ +/* $Id: proxy.h,v 1.10 2004-12-13 20:52:33 adam Exp $ Copyright (c) 1998-2004, Index Data. This file is part of the yaz-proxy. @@ -74,6 +74,7 @@ public: const char **cql2rpn, const char **authentication); + const char *check_mime_type(const char *path); int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo); int check_syntax(ODR odr, const char *name, Odr_oid *syntax, Z_RecordComposition *comp, @@ -245,6 +246,7 @@ class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc { Z_APDU *m_s2z_present_apdu; char *m_s2z_stylesheet; char *m_soap_ns; + int file_access(Z_HTTP_Request *hreq); int send_to_srw_client_error(int error, const char *add); int send_to_srw_client_ok(int hits, Z_Records *records, int start); int send_http_response(int code); diff --git a/src/yaz-bw.cpp b/src/yaz-bw.cpp index dea22e0..25f5658 100644 --- a/src/yaz-bw.cpp +++ b/src/yaz-bw.cpp @@ -1,4 +1,4 @@ -/* $Id: yaz-bw.cpp,v 1.5 2004-11-30 21:10:45 adam Exp $ +/* $Id: yaz-bw.cpp,v 1.6 2004-12-13 20:52:33 adam Exp $ Copyright (c) 1998-2004, Index Data. This file is part of the yaz-proxy. @@ -20,7 +20,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA */ #include -#include +#include #include Yaz_bw::Yaz_bw(int sz) diff --git a/src/yaz-proxy-config.cpp b/src/yaz-proxy-config.cpp index 2bc8cfe..903d3dd 100644 --- a/src/yaz-proxy-config.cpp +++ b/src/yaz-proxy-config.cpp @@ -1,4 +1,4 @@ -/* $Id: yaz-proxy-config.cpp,v 1.11 2004-12-03 14:28:18 adam Exp $ +/* $Id: yaz-proxy-config.cpp,v 1.12 2004-12-13 20:52:33 adam Exp $ Copyright (c) 1998-2004, Index Data. This file is part of the yaz-proxy. @@ -20,7 +20,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA */ #include -#include +#include #include "proxyp.h" class Yaz_ProxyConfigP { @@ -451,6 +451,34 @@ int Yaz_ProxyConfigP::check_schema(xmlNodePtr ptr, Z_RecordComposition *comp, } #endif +const char *Yaz_ProxyConfig::check_mime_type(const char *path) +{ + struct { + const char *mask; + const char *type; + } types[] = { + {".xml", "text/xml"}, + {".xsl", "text/xml"}, + {".tkl", "text/xml"}, + {".xsd", "text/xml"}, + {0, "text/plain"}, + {0, 0}, + }; + int i; + size_t plen = strlen (path); + for (i = 0; types[i].type; i++) + if (types[i].mask == 0) + return types[i].type; + else + { + size_t mlen = strlen(types[i].mask); + if (plen > mlen && !memcmp(path+plen-mlen, types[i].mask, mlen)) + return types[i].type; + } + return "application/octet-stream"; +} + + int Yaz_ProxyConfig::check_syntax(ODR odr, const char *name, Odr_oid *syntax, Z_RecordComposition *comp, char **addinfo, diff --git a/src/yaz-proxy-main.cpp b/src/yaz-proxy-main.cpp index c44183e..e64b4c9 100644 --- a/src/yaz-proxy-main.cpp +++ b/src/yaz-proxy-main.cpp @@ -1,4 +1,4 @@ -/* $Id: yaz-proxy-main.cpp,v 1.6 2004-11-30 21:10:45 adam Exp $ +/* $Id: yaz-proxy-main.cpp,v 1.7 2004-12-13 20:52:33 adam Exp $ Copyright (c) 1998-2004, Index Data. This file is part of the yaz-proxy. @@ -34,7 +34,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #endif -#include +#include #include #include diff --git a/src/yaz-proxy.cpp b/src/yaz-proxy.cpp index 6857d0c..935e453 100644 --- a/src/yaz-proxy.cpp +++ b/src/yaz-proxy.cpp @@ -1,4 +1,4 @@ -/* $Id: yaz-proxy.cpp,v 1.13 2004-12-03 14:28:18 adam Exp $ +/* $Id: yaz-proxy.cpp,v 1.14 2004-12-13 20:52:33 adam Exp $ Copyright (c) 1998-2004, Index Data. This file is part of the yaz-proxy. @@ -36,7 +36,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include -#include +#include #include #include "proxyp.h" #include @@ -1560,6 +1560,7 @@ void Yaz_Proxy::recv_GDU(Z_GDU *apdu, int len) reduce = (reduce > nreduce) ? reduce : nreduce; } } + m_http_version = 0; if (reduce) { yaz_log(YLOG_LOG, "%sdelay=%d bw=%d pdu=%d limit-bw=%d limit-pdu=%d", @@ -1882,6 +1883,70 @@ void Yaz_Proxy::srw_get_client(const char *db, const char **backend_db) m_default_target = xstrdup(t); } } + +int Yaz_Proxy::file_access(Z_HTTP_Request *hreq) +{ + yaz_log(YLOG_LOG, "file_access"); + if (strcmp(hreq->method, "GET")) + return 0; + struct stat sbuf; + if (hreq->path[0] != '/') + { + yaz_log(YLOG_WARN, "Path != /"); + return 0; + } + const char *cp = hreq->path; + while (*cp) + { + if (*cp == '/' && strchr("/.", cp[1])) + return 0; + cp++; + } + const char *fname = hreq->path+1; + if (stat(fname, &sbuf)) + { + yaz_log(YLOG_WARN, "stat %s failed", fname); + return 0; + } + if ((sbuf.st_mode & S_IFMT) != S_IFREG) + return 0; + if (sbuf.st_size > (off_t) 1000000) + return 0; + + ODR o = odr_encode(); + Yaz_ProxyConfig *cfg = check_reconfigure(); + const char *ctype = cfg->check_mime_type(fname); + Z_GDU *gdu = z_get_HTTP_Response(o, 200); + Z_HTTP_Response *hres = gdu->u.HTTP_Response; + if (m_http_version) + hres->version = odr_strdup(o, m_http_version); + z_HTTP_header_add(o, &hres->headers, "Content-Type", ctype); + if (m_http_keepalive) + z_HTTP_header_add(o, &hres->headers, "Connection", "Keep-Alive"); + else + timeout(0); + + hres->content_len = sbuf.st_size; + hres->content_buf = (char*) odr_malloc(o, hres->content_len); + FILE *f = fopen(fname, "rb"); + if (f) + { + fread(hres->content_buf, 1, hres->content_len, f); + fclose(f); + } + else + { + return 0; + } + if (m_log_mask & PROXY_LOG_REQ_CLIENT) + { + yaz_log (YLOG_LOG, "%sSending file %s to client", m_session_str, + fname); + } + int len; + send_GDU(gdu, &len); + return 1; +} void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) { @@ -1922,10 +1987,15 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) char *charset = 0; Z_SRW_diagnostic *diagnostic = 0; int num_diagnostic = 0; - if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(), - &charset) == 0 - || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(), - &charset, &diagnostic, &num_diagnostic) == 0) + + if (file_access(hreq)) + { + return; + } + else if (yaz_srw_decode(hreq, &srw_pdu, &soap_package, odr_decode(), + &charset) == 0 + || yaz_sru_decode(hreq, &srw_pdu, &soap_package, odr_decode(), + &charset, &diagnostic, &num_diagnostic) == 0) { m_s2z_odr_init = odr_createmem(ODR_ENCODE); m_s2z_odr_search = odr_createmem(ODR_ENCODE); @@ -2181,10 +2251,7 @@ void Yaz_Proxy::handle_incoming_HTTP(Z_HTTP_Request *hreq) send_to_srw_client_error(4, 0); } } - int len = 0; - Z_GDU *p = z_get_HTTP_Response(odr_encode(), 400); - timeout(0); - send_GDU(p, &len); + send_http_response(400); } void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu) @@ -2216,8 +2283,16 @@ void Yaz_Proxy::handle_incoming_Z_PDU(Z_APDU *apdu) m_client = get_client(apdu, get_cookie(oi), get_proxy(oi)); if (!m_client) { - delete this; - return; + if (m_http_version) + { + send_http_response(404); + return; + } + else + { + delete this; + return; + } } m_client->m_server = this; -- 1.7.10.4