--------------------- PatchSet 1885 Date: 2005/09/27 15:22:39 Author: dwsquid Branch: squid3-icap Tag: (none) Log: These are for ICAP config and directive parsing Members: src/ICAPConfig.cc:1.1->1.1.2.1 src/ICAPConfig.h:1.1->1.1.2.1 --- /dev/null Wed Feb 14 13:33:00 2007 +++ squid3/src/ICAPConfig.cc Wed Feb 14 13:35:13 2007 @@ -0,0 +1,311 @@ + +/* + * $Id: ICAPConfig.cc,v 1.1.2.1 2005/09/27 15:22:39 dwsquid Exp $ + * + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * This program 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 of the License, or + * (at your option) any later version. + * + * This program 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + * + * Copyright (c) 2003, Robert Collins + */ + +#include "squid.h" + +#include "ConfigParser.h" +#include "ACL.h" +#include "Store.h" +#include "List.h" +#include "ICAPConfig.h" + +ICAPConfig TheICAPConfig; + +icap_service_t +IcapService::ServiceType() +{ + if (!strcmp(type_name, "reqmod_precache")) { + return SERVICE_REQMOD_PRECACHE; + } else if (!strcmp(type_name, "reqmod_postcache")) { + return SERVICE_REQMOD_POSTCACHE; + } else if (!strcmp(type_name, "respmod_precache")) { + return SERVICE_RESPMOD_PRECACHE; + } else if (!strcmp(type_name, "respmod_postcache")) { + return SERVICE_RESPMOD_POSTCACHE; + } else { + return SERVICE_MAX; + } +}; + +void +IcapService::parseConfigLine() +{ + + ConfigParser::ParseString(&key); + ConfigParser::ParseString(&type_name); + ConfigParser::ParseUShort(&bypass); + ConfigParser::ParseString(&uri); +}; + +int +IcapService::prepare() +{ + char *s, *e, *t; + unsigned int len; + int uport; + + parseConfigLine(); + debug(3, 5) ("IcapService::prepare (line %d): %s %s %d\n", config_lineno, key, type_name, bypass); + + type = ServiceType(); + if (type >= SERVICE_MAX) { + return 0; + } + + if ((type == SERVICE_REQMOD_PRECACHE) || (type == SERVICE_REQMOD_POSTCACHE)) { + method = ICAP_METHOD_REQMOD; + } else if ((type == SERVICE_RESPMOD_PRECACHE) || (type == SERVICE_RESPMOD_POSTCACHE)) { + method = ICAP_METHOD_RESPMOD; + } + debug(3, 5) ("IcapService::prepare (line %d): type=%d\n", config_lineno, type); + + if (strncmp(uri, "icap://", 7) != 0) { + debug(3, 0) ("IcapService::prepare (line %d): wrong uri: %s\n", config_lineno, uri); + return 0; + } + s = uri + 7; + + if ((e = strchr(s, ':')) != NULL) { + uport = 1; + } else if ((e = strchr(s, '/')) != NULL) { + uport = 0; + } else { + return 0; + } + len = e - s; + hostname = xstrndup(s, len + 1); + hostname[len] = '\0'; + s = e; + if (uport) { + s++; + if ((e = strchr(s, '/')) != NULL) { + port = strtoul(s, &t, 0) % 65536; + if (t != e) { + return 0; + } + s = e; + if (s[0] != '/') { + return 0; + } + } + } else { + struct servent *serv = getservbyname("icap", "tcp"); + if (serv) { + port = htons(serv->s_port); + } else { + port = 1344; + } + } + + s++; + e = strchr(s, '\0'); + len = e - s; + if (len > 1024) { + debug(3, 0) ("icap_service_process (line %d): long resource name (>1024), probably wrong\n", config_lineno); + } + resource = xstrndup(s, len + 1); + resource[len] = 0; + if ((bypass != 0) && (bypass != 1)) { + return 0; + } + return 1; +}; + + +int +IcapClass::prepare() +{ + int found = 0; + List **Tail; + List *service_iter; + wordlist *iter; + + ConfigParser::ParseString(&key); + ConfigParser::ParseWordList(&service_names); + + for (iter = service_names; iter; iter = iter->next) { + for (service_iter = TheICAPConfig.services; service_iter; service_iter = service_iter->next) { + if (!strcmp(service_iter->element.key, iter->key)) { + for (Tail = &services; *Tail; Tail = &((*Tail)->next)) + + ; + List * q = new List(service_iter->element); + *(Tail) = q; + found = 1; + } + } + } + + return found; +}; + +int +IcapAccess::prepare() +{ + int found = 0; + + ConfigParser::ParseString(&key); + + List *data = TheICAPConfig.classes; + while (data != NULL) { + if (!strcmp(data->element.key, key)) { + aclass =& data->element; + found = 1; + break; + } + data = data->next; + } + + if (found) { + aclParseAccessLine(&(this->access)); + } + + return found; +}; + +void +ICAPConfig::parseIcapService() +{ + List **Tail; + IcapService *S = new IcapService(); + if (S->prepare()) { + for (Tail = &services; *Tail; Tail = &((*Tail)->next)) + + ; + List * q = new List(*S); + *(Tail) = q; + } else { + delete S; + } + return; +}; + +void +ICAPConfig::freeIcapService() +{ + delete services; +}; + +void +ICAPConfig::dumpIcapService(StoreEntry *entry, const char *name) +{ + List *data = services; + while (data != NULL) { + storeAppendPrintf(entry, "%s %s %s %d %s\n", name, data->element.key, data->element.type_name, data->element.bypass, data->element.uri); + data = data->next; + } +}; + +void +ICAPConfig::parseIcapClass() +{ + List **Tail; + + IcapClass *C = new IcapClass(); + if (C->prepare()) { + for (Tail = &classes; *Tail; Tail = &((*Tail)->next)) + + ; + List * q = new List(*C); + *(Tail) = q; + } else { + delete C; + } +}; + + +void +ICAPConfig::freeIcapClass() +{ + delete classes; +}; + +void +ICAPConfig::dumpIcapClass(StoreEntry *entry, const char *name) +{ + List *data = classes; + while (data != NULL) { + storeAppendPrintf(entry, "%s %s\n", name, data->element.key); + data = data->next; + } +}; + +void +ICAPConfig::parseIcapAccess() +{ + List **Tail; + + IcapAccess *A = new IcapAccess(); + if (A->prepare()) { + for (Tail = &accesses; *Tail; Tail = &((*Tail)->next)) + + ; + List * q = new List(*A); + *(Tail) = q; + + } else { + delete A; + } + return; +}; + +void +ICAPConfig::freeIcapAccess() +{ + delete accesses; +}; + +void +ICAPConfig::dumpIcapAccess(StoreEntry *entry, const char *name) +{ + LOCAL_ARRAY(char, nom, 64); + if (!accesses) { + storeAppendPrintf(entry, "%s 0\n", name); + return; + } + + List *data = accesses; + while (data != NULL) { + snprintf(nom, 64, "%s %s", name, data->element.key); + dump_acl_access(entry, nom, data->element.access); + data = data->next; + } +}; + +ICAPConfig::~ICAPConfig() +{ + delete classes; + delete services; + delete accesses; +}; --- /dev/null Wed Feb 14 13:33:00 2007 +++ squid3/src/ICAPConfig.h Wed Feb 14 13:35:13 2007 @@ -0,0 +1,151 @@ + +/* + * $Id: ICAPConfig.h,v 1.1.2.1 2005/09/27 15:22:39 dwsquid Exp $ + * + * + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * This program 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 of the License, or + * (at your option) any later version. + * + * This program 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + * + * Copyright (c) 2003, Robert Collins + */ + +#ifndef SQUID_ICAPCONFIG_H +#define SQUID_ICAPCONFIG_H + +#include "List.h" + +class acl_access; +class ICAPConfig; + +typedef enum { + SERVICE_REQMOD_PRECACHE, + SERVICE_REQMOD_POSTCACHE, + SERVICE_RESPMOD_PRECACHE, + SERVICE_RESPMOD_POSTCACHE, + SERVICE_MAX +} icap_service_t; + +typedef enum { + ICAP_METHOD_NONE, + ICAP_METHOD_OPTION, + ICAP_METHOD_REQMOD, + ICAP_METHOD_RESPMOD +} icap_method_t; + +class IcapService +{ + +public: + char *key; + char *uri; + char *type_name; + char *hostname; + unsigned short port; + char * resource; + unsigned short bypass; + unsigned short unreachable; + int preview; + icap_service_t type; + icap_method_t method; + + IcapService() : key(NULL), uri(NULL), type_name(NULL) { + type = SERVICE_MAX; + }; + icap_service_t ServiceType(); + void parseConfigLine(); + int prepare(); + +}; + + +class IcapClass +{ + +public: + char *key; + wordlist *service_names; + + List *services; + + IcapClass() : key(NULL), service_names(NULL), services(NULL) {}; + int prepare(); +}; + + +class IcapAccess +{ + +public: + char *key; + IcapClass *aclass; + acl_access *access; + + IcapAccess() + { + key = NULL; + aclass = NULL; + access = NULL; + }; + int prepare(); + +}; + +class ICAPConfig +{ + +public: + + int onoff; + int preview_enable; + int preview_size; + int check_interval; + int send_client_ip; + int auth_user; + char *auth_scheme; + + List *services; + List *classes; + List *accesses; + + ICAPConfig() : services(NULL), classes(NULL), accesses(NULL) {}; + ~ICAPConfig(); + + void parseIcapService(void); + void freeIcapService(void); + void dumpIcapService(StoreEntry *, const char *); + + void parseIcapClass(void); + void freeIcapClass(void); + void dumpIcapClass(StoreEntry *, const char *); + + void parseIcapAccess(void); + void freeIcapAccess(void); + void dumpIcapAccess(StoreEntry *, const char *); + +}; + +#endif /* SQUID_ICAPCONFIG_H */