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