]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/Host.cc
Set Squid IDs for known header names to speedup comparisons.
[thirdparty/squid.git] / src / adaptation / ecap / Host.cc
1 /*
2 * DEBUG: section 93 eCAP Interface
3 */
4 #include "squid.h"
5 #include <libecap/adapter/service.h>
6 #include <libecap/common/names.h>
7 #include <libecap/common/registry.h>
8 #include "base/TextException.h"
9 #include "adaptation/ecap/ServiceRep.h"
10 #include "adaptation/ecap/Host.h"
11 #include "adaptation/ecap/MessageRep.h"
12 #include "HttpRequest.h"
13 #include "HttpReply.h"
14
15 const libecap::Name Adaptation::Ecap::protocolInternal("internal", libecap::Name::NextId());
16 const libecap::Name Adaptation::Ecap::protocolCacheObj("cache_object", libecap::Name::NextId());
17 const libecap::Name Adaptation::Ecap::protocolIcp("ICP", libecap::Name::NextId());
18 #if USE_HTCP
19 const libecap::Name Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
20 #endif
21
22 /// the host application (i.e., Squid) wrapper registered with libecap
23 static libecap::shared_ptr<Adaptation::Ecap::Host> TheHost;
24
25 Adaptation::Ecap::Host::Host()
26 {
27 // assign our host-specific IDs to well-known names
28 // this code can run only once
29
30 libecap::headerContentLength.assignHostId(HDR_CONTENT_LENGTH);
31 libecap::headerTransferEncoding.assignHostId(HDR_TRANSFER_ENCODING);
32 libecap::headerReferer.assignHostId(HDR_REFERER);
33 libecap::headerVia.assignHostId(HDR_VIA);
34 // TODO: libecap::headerXClientIp.assignHostId(HDR_X_CLIENT_IP);
35 // TODO: libecap::headerXServerIp.assignHostId(HDR_X_SERVER_IP);
36
37 libecap::protocolHttp.assignHostId(PROTO_HTTP);
38 libecap::protocolHttps.assignHostId(PROTO_HTTPS);
39 libecap::protocolFtp.assignHostId(PROTO_FTP);
40 libecap::protocolGopher.assignHostId(PROTO_GOPHER);
41 libecap::protocolWais.assignHostId(PROTO_WAIS);
42 libecap::protocolUrn.assignHostId(PROTO_URN);
43 libecap::protocolWhois.assignHostId(PROTO_WHOIS);
44 protocolInternal.assignHostId(PROTO_INTERNAL);
45 protocolCacheObj.assignHostId(PROTO_CACHEOBJ);
46 protocolIcp.assignHostId(PROTO_ICP);
47 #if USE_HTCP
48 protocolHtcp.assignHostId(PROTO_HTCP);
49 #endif
50 }
51
52 std::string
53 Adaptation::Ecap::Host::uri() const
54 {
55 return "ecap://squid-cache.org/ecap/hosts/squid";
56 }
57
58 void
59 Adaptation::Ecap::Host::describe(std::ostream &os) const
60 {
61 os << PACKAGE_NAME << " v" << PACKAGE_VERSION;
62 }
63
64 void
65 Adaptation::Ecap::Host::noteService(const libecap::weak_ptr<libecap::adapter::Service> &weak)
66 {
67 Must(!weak.expired());
68 RegisterAdapterService(weak.lock());
69 }
70
71 static int
72 SquidLogLevel(libecap::LogVerbosity lv)
73 {
74 if (lv.critical())
75 return DBG_CRITICAL; // is it a good idea to ignore other flags?
76
77 if (lv.large())
78 return DBG_DATA; // is it a good idea to ignore other flags?
79
80 if (lv.application())
81 return DBG_IMPORTANT; // is it a good idea to ignore other flags?
82
83 return 2 + 2*lv.debugging() + 3*lv.operation() + 2*lv.xaction();
84 }
85
86 std::ostream *
87 Adaptation::Ecap::Host::openDebug(libecap::LogVerbosity lv)
88 {
89 const int squidLevel = SquidLogLevel(lv);
90 const int squidSection = 93; // XXX: this should be a global constant
91 // XXX: Debug.h should provide this to us
92 if ((Debug::level = squidLevel) <= Debug::Levels[squidSection])
93 return &Debug::getDebugOut();
94 else
95 return NULL;
96 }
97
98 void
99 Adaptation::Ecap::Host::closeDebug(std::ostream *debug)
100 {
101 if (debug)
102 Debug::finishDebug();
103 }
104
105
106 Adaptation::Ecap::Host::MessagePtr
107 Adaptation::Ecap::Host::newRequest() const
108 {
109 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpRequest));
110 }
111
112 Adaptation::Ecap::Host::MessagePtr
113 Adaptation::Ecap::Host::newResponse() const
114 {
115 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpReply));
116 }
117
118 void
119 Adaptation::Ecap::Host::Register()
120 {
121 if (!TheHost) {
122 TheHost.reset(new Adaptation::Ecap::Host);
123 libecap::RegisterHost(TheHost);
124 }
125 }