]>
Commit | Line | Data |
---|---|---|
26cc52cb AR |
1 | /* |
2 | * DEBUG: section 93 ICAP (RFC 3507) Client | |
3 | */ | |
4 | ||
582c2af2 | 5 | #include "squid.h" |
1adcebc3 | 6 | #include "adaptation/Answer.h" |
83c51da9 | 7 | #include "adaptation/icap/Config.h" |
582c2af2 FC |
8 | #include "adaptation/icap/Options.h" |
9 | #include "adaptation/icap/OptXact.h" | |
3d93a84d | 10 | #include "base/TextException.h" |
582c2af2 FC |
11 | #include "comm.h" |
12 | #include "HttpReply.h" | |
3ff65596 | 13 | #include "HttpRequest.h" |
582c2af2 FC |
14 | #include "protos.h" |
15 | #include "SquidTime.h" | |
26cc52cb AR |
16 | |
17 | CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, OptXact); | |
18 | CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, OptXactLauncher); | |
19 | ||
20 | ||
4299f876 | 21 | Adaptation::Icap::OptXact::OptXact(Adaptation::Icap::ServiceRep::Pointer &aService): |
26cc52cb | 22 | AsyncJob("Adaptation::Icap::OptXact"), |
0a1ac29e AR |
23 | Adaptation::Icap::Xaction("Adaptation::Icap::OptXact", aService), |
24 | readAll(false) | |
26cc52cb AR |
25 | { |
26 | } | |
27 | ||
28 | void Adaptation::Icap::OptXact::start() | |
29 | { | |
30 | Adaptation::Icap::Xaction::start(); | |
31 | ||
32 | openConnection(); | |
33 | } | |
34 | ||
35 | void Adaptation::Icap::OptXact::handleCommConnected() | |
36 | { | |
37 | scheduleRead(); | |
38 | ||
39 | MemBuf requestBuf; | |
40 | requestBuf.init(); | |
41 | makeRequest(requestBuf); | |
192378eb | 42 | debugs(93, 9, HERE << "request " << status() << ":\n" << |
26cc52cb | 43 | (requestBuf.terminate(), requestBuf.content())); |
3ff65596 | 44 | icap_tio_start = current_time; |
26cc52cb AR |
45 | scheduleWrite(requestBuf); |
46 | } | |
47 | ||
48 | void Adaptation::Icap::OptXact::makeRequest(MemBuf &buf) | |
49 | { | |
50 | const Adaptation::Service &s = service(); | |
51 | const String uri = s.cfg().uri; | |
52 | buf.Printf("OPTIONS " SQUIDSTRINGPH " ICAP/1.0\r\n", SQUIDSTRINGPRINT(uri)); | |
53 | const String host = s.cfg().host; | |
54 | buf.Printf("Host: " SQUIDSTRINGPH ":%d\r\n", SQUIDSTRINGPRINT(host), s.cfg().port); | |
83c51da9 CT |
55 | if (TheConfig.allow206_enable) |
56 | buf.Printf("Allow: 206\r\n"); | |
26cc52cb | 57 | buf.append(ICAP::crlf, 2); |
3ff65596 AR |
58 | |
59 | // XXX: HttpRequest cannot fully parse ICAP Request-Line | |
b0365bd9 FC |
60 | http_status reqStatus; |
61 | Must(icapRequest->parse(&buf, true, &reqStatus) > 0); | |
26cc52cb AR |
62 | } |
63 | ||
64 | void Adaptation::Icap::OptXact::handleCommWrote(size_t size) | |
65 | { | |
192378eb | 66 | debugs(93, 9, HERE << "finished writing " << size << |
26cc52cb AR |
67 | "-byte request " << status()); |
68 | } | |
69 | ||
70 | // comm module read a portion of the ICAP response for us | |
71 | void Adaptation::Icap::OptXact::handleCommRead(size_t) | |
72 | { | |
c7d51c86 | 73 | if (parseResponse()) { |
0a1ac29e AR |
74 | Must(icapReply != NULL); |
75 | // We read everything if there is no response body. If there is a body, | |
76 | // we cannot parse it because we do not support any opt-body-types, so | |
77 | // we leave readAll false which forces connection closure. | |
78 | readAll = !icapReply->header.getByNameListMember("Encapsulated", | |
5be49f98 | 79 | "opt-body", ',').size(); |
0a1ac29e | 80 | debugs(93, 7, HERE << "readAll=" << readAll); |
3ff65596 AR |
81 | icap_tio_finish = current_time; |
82 | setOutcome(xoOpt); | |
3af10ac0 | 83 | sendAnswer(Answer::Forward(icapReply)); |
26cc52cb AR |
84 | Must(done()); // there should be nothing else to do |
85 | return; | |
86 | } | |
87 | ||
88 | scheduleRead(); | |
89 | } | |
90 | ||
c7d51c86 | 91 | bool Adaptation::Icap::OptXact::parseResponse() |
26cc52cb AR |
92 | { |
93 | debugs(93, 5, HERE << "have " << readBuf.contentSize() << " bytes to parse" << | |
94 | status()); | |
95 | debugs(93, 5, HERE << "\n" << readBuf.content()); | |
96 | ||
c7d51c86 | 97 | HttpReply::Pointer r(new HttpReply); |
26cc52cb AR |
98 | r->protoPrefix = "ICAP/"; // TODO: make an IcapReply class? |
99 | ||
c7d51c86 AR |
100 | if (!parseHttpMsg(r)) // throws on errors |
101 | return false; | |
26cc52cb AR |
102 | |
103 | if (httpHeaderHasConnDir(&r->header, "close")) | |
104 | reuseConnection = false; | |
105 | ||
c7d51c86 AR |
106 | icapReply = r; |
107 | return true; | |
26cc52cb AR |
108 | } |
109 | ||
3ff65596 AR |
110 | void Adaptation::Icap::OptXact::swanSong() |
111 | { | |
112 | Adaptation::Icap::Xaction::swanSong(); | |
113 | } | |
114 | ||
115 | void Adaptation::Icap::OptXact::finalizeLogInfo() | |
116 | { | |
117 | // al.cache.caddr = 0; | |
118 | al.icap.reqMethod = Adaptation::methodOptions; | |
bae917ac CT |
119 | |
120 | if (icapReply && al.icap.bytesRead > icapReply->hdr_sz) | |
121 | al.icap.bodyBytesRead = al.icap.bytesRead - icapReply->hdr_sz; | |
122 | ||
3ff65596 AR |
123 | Adaptation::Icap::Xaction::finalizeLogInfo(); |
124 | } | |
125 | ||
26cc52cb AR |
126 | /* Adaptation::Icap::OptXactLauncher */ |
127 | ||
4299f876 | 128 | Adaptation::Icap::OptXactLauncher::OptXactLauncher(Adaptation::ServicePointer aService): |
26cc52cb | 129 | AsyncJob("Adaptation::Icap::OptXactLauncher"), |
4299f876 | 130 | Adaptation::Icap::Launcher("Adaptation::Icap::OptXactLauncher", aService) |
26cc52cb AR |
131 | { |
132 | } | |
133 | ||
134 | Adaptation::Icap::Xaction *Adaptation::Icap::OptXactLauncher::createXaction() | |
135 | { | |
136 | Adaptation::Icap::ServiceRep::Pointer s = | |
137 | dynamic_cast<Adaptation::Icap::ServiceRep*>(theService.getRaw()); | |
138 | Must(s != NULL); | |
4299f876 | 139 | return new Adaptation::Icap::OptXact(s); |
26cc52cb | 140 | } |