]>
Commit | Line | Data |
---|---|---|
85491f8d | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 The Squid Software Foundation and contributors |
e25c139f | 3 | * |
bbc27441 AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
85491f8d | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 52 URN Parsing */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
d2a6dcba | 12 | #include "AccessLogEntry.h" |
819be284 | 13 | #include "acl/FilledChecklist.h" |
bd10977c | 14 | #include "base/TextException.h" |
bda078fe | 15 | #include "cbdata.h" |
aa839030 | 16 | #include "errorpage.h" |
eb13c21e | 17 | #include "FwdState.h" |
582c2af2 | 18 | #include "globals.h" |
e87137f1 FC |
19 | #include "HttpReply.h" |
20 | #include "HttpRequest.h" | |
9b5c4a9a | 21 | #include "icmp/net_db.h" |
e87137f1 | 22 | #include "MemBuf.h" |
b6149797 | 23 | #include "mime_header.h" |
f206b652 | 24 | #include "RequestFlags.h" |
e87137f1 FC |
25 | #include "Store.h" |
26 | #include "StoreClient.h" | |
8d03bdb4 | 27 | #include "tools.h" |
5eb529cb | 28 | #include "urn.h" |
85491f8d | 29 | |
62e76326 | 30 | class UrnState : public StoreClient |
31 | { | |
5c2f68b7 | 32 | CBDATA_CLASS(UrnState); |
62e76326 | 33 | |
e6ccf245 | 34 | public: |
d2a6dcba EB |
35 | explicit UrnState(const AccessLogEntry::Pointer &anAle): ale(anAle) {} |
36 | ||
190154cf | 37 | void start (HttpRequest *, StoreEntry *); |
190154cf | 38 | void setUriResFromRequest(HttpRequest *); |
e6ccf245 | 39 | |
337b9aa4 | 40 | ~UrnState() override; |
62e76326 | 41 | |
d2a6dcba EB |
42 | StoreEntry *entry = nullptr; |
43 | store_client *sc = nullptr; | |
44 | StoreEntry *urlres_e = nullptr; | |
8a70cdbb AJ |
45 | HttpRequest::Pointer request; |
46 | HttpRequest::Pointer urlres_r; | |
7e6eabbc | 47 | AccessLogEntry::Pointer ale; ///< details of the requesting transaction |
62e76326 | 48 | |
122a6e3c AR |
49 | /// for receiving a URN resolver reply body from Store and interpreting it |
50 | Store::ParsingBuffer parsingBuffer; | |
62e76326 | 51 | |
e6ccf245 | 52 | private: |
819be284 | 53 | /* StoreClient API */ |
337b9aa4 AR |
54 | LogTags *loggingTags() const override { return ale ? &ale->cache.code : nullptr; } |
55 | void fillChecklist(ACLFilledChecklist &) const override; | |
819be284 | 56 | |
d2a6dcba | 57 | char *urlres = nullptr; |
e6ccf245 | 58 | }; |
85491f8d | 59 | |
26ac0430 | 60 | typedef struct { |
9ce5e3e6 | 61 | char *url; |
62 | char *host; | |
63 | int rtt; | |
62e76326 | 64 | |
26ac0430 | 65 | struct { |
62e76326 | 66 | int cached; |
2fadd50d HN |
67 | } flags; |
68 | } url_entry; | |
9ce5e3e6 | 69 | |
70 | static STCB urnHandleReply; | |
122a6e3c | 71 | static url_entry *urnParseReply(const SBuf &, const HttpRequestMethod &); |
9ce5e3e6 | 72 | static const char *const crlf = "\r\n"; |
9ce5e3e6 | 73 | |
bda078fe | 74 | CBDATA_CLASS_INIT(UrnState); |
62e76326 | 75 | |
bda078fe | 76 | UrnState::~UrnState() |
e6ccf245 | 77 | { |
bd10977c EB |
78 | SWALLOW_EXCEPTIONS({ |
79 | if (urlres_e) { | |
80 | if (sc) | |
81 | storeUnregister(sc, urlres_e, this); | |
82 | urlres_e->unlock("~UrnState+res"); | |
83 | } | |
84 | ||
85 | if (entry) | |
86 | entry->unlock("~UrnState+prime"); | |
87 | ||
88 | safe_free(urlres); | |
89 | }); | |
e6ccf245 | 90 | } |
91 | ||
48ebcb22 | 92 | static url_entry * |
ced8def3 | 93 | urnFindMinRtt(url_entry * urls, const HttpRequestMethod &, int *rtt_ret) |
85491f8d | 94 | { |
23d92c64 | 95 | int min_rtt = 0; |
aee3523a AR |
96 | url_entry *u = nullptr; |
97 | url_entry *min_u = nullptr; | |
9ce5e3e6 | 98 | int i; |
1caf595b | 99 | int urlcnt = 0; |
bf8fe701 | 100 | debugs(52, 3, "urnFindMinRtt"); |
aee3523a | 101 | assert(urls != nullptr); |
62e76326 | 102 | |
aee3523a | 103 | for (i = 0; nullptr != urls[i].url; ++i) |
14942edd | 104 | ++urlcnt; |
62e76326 | 105 | |
bf8fe701 | 106 | debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs"); |
62e76326 | 107 | |
9ce5e3e6 | 108 | if (1 == urlcnt) { |
bf8fe701 | 109 | debugs(52, 3, "urnFindMinRtt: Only one URL - return it!"); |
62e76326 | 110 | return urls; |
1caf595b | 111 | } |
62e76326 | 112 | |
14942edd | 113 | for (i = 0; i < urlcnt; ++i) { |
62e76326 | 114 | u = &urls[i]; |
bf8fe701 | 115 | debugs(52, 3, "urnFindMinRtt: " << u->host << " rtt=" << u->rtt); |
62e76326 | 116 | |
117 | if (u->rtt == 0) | |
118 | continue; | |
119 | ||
120 | if (u->rtt > min_rtt && min_rtt != 0) | |
121 | continue; | |
122 | ||
123 | min_rtt = u->rtt; | |
124 | ||
125 | min_u = u; | |
23d92c64 | 126 | } |
62e76326 | 127 | |
23d92c64 | 128 | if (rtt_ret) |
62e76326 | 129 | *rtt_ret = min_rtt; |
130 | ||
e0236918 | 131 | debugs(52, DBG_IMPORTANT, "urnFindMinRtt: Returning '" << |
26ac0430 AJ |
132 | (min_u ? min_u->url : "NONE") << "' RTT " << |
133 | min_rtt ); | |
62e76326 | 134 | |
9ce5e3e6 | 135 | return min_u; |
85491f8d | 136 | } |
137 | ||
e6ccf245 | 138 | void |
51b5dcf5 | 139 | UrnState::setUriResFromRequest(HttpRequest *r) |
e6ccf245 | 140 | { |
6c880a16 AJ |
141 | const auto &query = r->url.absolute(); |
142 | const auto host = r->url.host(); | |
c8ab5ec6 | 143 | // TODO: use class AnyP::Uri instead of generating a string and re-parsing |
e6ccf245 | 144 | LOCAL_ARRAY(char, local_urlres, 4096); |
6c880a16 | 145 | snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?" SQUIDSBUFPH, host, SQUIDSBUFPRINT(query)); |
86c63190 | 146 | safe_free(urlres); |
6c880a16 | 147 | urlres_r = HttpRequest::FromUrlXXX(local_urlres, r->masterXaction); |
62e76326 | 148 | |
8babada0 AJ |
149 | if (!urlres_r) { |
150 | debugs(52, 3, "Bad uri-res URL " << local_urlres); | |
7e6eabbc | 151 | const auto err = new ErrorState(ERR_URN_RESOLVE, Http::scNotFound, r, ale); |
8babada0 | 152 | err->url = xstrdup(local_urlres); |
62e76326 | 153 | errorAppendEntry(entry, err); |
154 | return; | |
0adbab7c | 155 | } |
62e76326 | 156 | |
8babada0 | 157 | urlres = xstrdup(local_urlres); |
789217a2 | 158 | urlres_r->header.putStr(Http::HdrType::ACCEPT, "text/plain"); |
e6ccf245 | 159 | } |
160 | ||
161 | void | |
190154cf | 162 | UrnState::start(HttpRequest * r, StoreEntry * e) |
e6ccf245 | 163 | { |
bf8fe701 | 164 | debugs(52, 3, "urnStart: '" << e->url() << "'" ); |
e6ccf245 | 165 | entry = e; |
b248c2a3 | 166 | request = r; |
34266cde | 167 | |
1bfe9ade | 168 | entry->lock("UrnState::start"); |
e6ccf245 | 169 | setUriResFromRequest(r); |
62e76326 | 170 | |
aee3523a | 171 | if (urlres_r == nullptr) |
62e76326 | 172 | return; |
173 | ||
7976fed3 | 174 | auto urlEntry = storeGetPublic(urlres, Http::METHOD_GET); |
e6ccf245 | 175 | |
7976fed3 | 176 | if (!urlEntry || (urlEntry->hittingRequiresCollapsing() && !startCollapsingOn(*urlEntry, false))) { |
c2a7cefd | 177 | urlres_e = storeCreateEntry(urlres, urlres, RequestFlags(), Http::METHOD_GET); |
62e76326 | 178 | sc = storeClientListAdd(urlres_e, this); |
d2a6dcba | 179 | FwdState::Start(Comm::ConnectionPointer(), urlres_e, urlres_r.getRaw(), ale); |
7976fed3 | 180 | if (urlEntry) { |
d868b138 | 181 | urlEntry->abandon(__func__); |
7976fed3 EB |
182 | urlEntry = nullptr; |
183 | } | |
cf26e54c | 184 | } else { |
7976fed3 | 185 | urlres_e = urlEntry; |
d868b138 | 186 | urlres_e->lock(__func__); |
62e76326 | 187 | sc = storeClientListAdd(urlres_e, this); |
85491f8d | 188 | } |
62e76326 | 189 | |
e6ccf245 | 190 | storeClientCopy(sc, urlres_e, |
122a6e3c | 191 | parsingBuffer.makeInitialSpace(), |
62e76326 | 192 | urnHandleReply, |
193 | this); | |
e6ccf245 | 194 | } |
195 | ||
7976fed3 EB |
196 | void |
197 | UrnState::fillChecklist(ACLFilledChecklist &checklist) const | |
198 | { | |
199 | checklist.setRequest(request.getRaw()); | |
200 | checklist.al = ale; | |
201 | } | |
202 | ||
e6ccf245 | 203 | void |
d2a6dcba | 204 | urnStart(HttpRequest *r, StoreEntry *e, const AccessLogEntryPointer &ale) |
e6ccf245 | 205 | { |
d2a6dcba | 206 | const auto anUrn = new UrnState(ale); |
e6ccf245 | 207 | anUrn->start (r, e); |
85491f8d | 208 | } |
209 | ||
9ce5e3e6 | 210 | static int |
211 | url_entry_sort(const void *A, const void *B) | |
212 | { | |
e6ccf245 | 213 | const url_entry *u1 = (const url_entry *)A; |
214 | const url_entry *u2 = (const url_entry *)B; | |
62e76326 | 215 | |
9ce5e3e6 | 216 | if (u2->rtt == u1->rtt) |
62e76326 | 217 | return 0; |
9ce5e3e6 | 218 | else if (0 == u1->rtt) |
62e76326 | 219 | return 1; |
9ce5e3e6 | 220 | else if (0 == u2->rtt) |
62e76326 | 221 | return -1; |
9ce5e3e6 | 222 | else |
62e76326 | 223 | return u1->rtt - u2->rtt; |
9ce5e3e6 | 224 | } |
225 | ||
528b2c61 | 226 | /* TODO: use the clientStream support for this */ |
85491f8d | 227 | static void |
c8be6d7b | 228 | urnHandleReply(void *data, StoreIOBuffer result) |
85491f8d | 229 | { |
e6ccf245 | 230 | UrnState *urnState = static_cast<UrnState *>(data); |
cf26e54c | 231 | StoreEntry *e = urnState->entry; |
232 | StoreEntry *urlres_e = urnState->urlres_e; | |
9ce5e3e6 | 233 | url_entry *urls; |
00141c96 | 234 | url_entry *u; |
9ce5e3e6 | 235 | url_entry *min_u; |
cf26e54c | 236 | ErrorState *err; |
9ce5e3e6 | 237 | int i; |
238 | int urlcnt = 0; | |
cf26e54c | 239 | |
122a6e3c | 240 | debugs(52, 3, result << " with " << *e); |
62e76326 | 241 | |
682d5190 EB |
242 | if (EBIT_TEST(urlres_e->flags, ENTRY_ABORTED) || result.flags.error) { |
243 | delete urnState; | |
bb9edbb2 | 244 | return; |
85491f8d | 245 | } |
62e76326 | 246 | |
d2b604ec AR |
247 | if (!e->isAccepting()) { |
248 | debugs(52, 3, "terminating due to bad " << *e); | |
249 | delete urnState; | |
250 | return; | |
251 | } | |
252 | ||
122a6e3c | 253 | urnState->parsingBuffer.appended(result.data, result.length); |
62e76326 | 254 | |
add2192d | 255 | /* If we haven't received the entire object (urn), copy more */ |
122a6e3c AR |
256 | if (!urnState->sc->atEof()) { |
257 | const auto bufferedBytes = urnState->parsingBuffer.contentSize(); | |
258 | const auto remainingSpace = urnState->parsingBuffer.space().positionAt(bufferedBytes); | |
259 | ||
260 | if (!remainingSpace.length) { | |
261 | debugs(52, 3, "ran out of buffer space after " << bufferedBytes << " bytes"); | |
262 | // TODO: Here and in other error cases, send ERR_URN_RESOLVE to client. | |
263 | delete urnState; | |
264 | return; | |
265 | } | |
266 | ||
62e76326 | 267 | storeClientCopy(urnState->sc, urlres_e, |
122a6e3c | 268 | remainingSpace, |
62e76326 | 269 | urnHandleReply, |
270 | urnState); | |
271 | return; | |
23d92c64 | 272 | } |
62e76326 | 273 | |
122a6e3c AR |
274 | const auto &peerReply = urlres_e->mem().baseReply(); |
275 | debugs(52, 3, "got reply, code=" << peerReply.sline.status()); | |
276 | if (peerReply.sline.status() != Http::scOkay) { | |
bf8fe701 | 277 | debugs(52, 3, "urnHandleReply: failed."); |
7e6eabbc | 278 | err = new ErrorState(ERR_URN_RESOLVE, Http::scNotFound, urnState->request.getRaw(), urnState->ale); |
3900307b | 279 | err->url = xstrdup(e->url()); |
62e76326 | 280 | errorAppendEntry(e, err); |
682d5190 | 281 | delete urnState; |
bb9edbb2 | 282 | return; |
85491f8d | 283 | } |
62e76326 | 284 | |
122a6e3c | 285 | urls = urnParseReply(urnState->parsingBuffer.toSBuf(), urnState->request->method); |
62e76326 | 286 | |
892ee3d0 | 287 | if (!urls) { /* unknown URN error */ |
8a70cdbb | 288 | debugs(52, 3, "urnTranslateDone: unknown URN " << e->url()); |
7e6eabbc | 289 | err = new ErrorState(ERR_URN_RESOLVE, Http::scNotFound, urnState->request.getRaw(), urnState->ale); |
3900307b | 290 | err->url = xstrdup(e->url()); |
62e76326 | 291 | errorAppendEntry(e, err); |
682d5190 | 292 | delete urnState; |
bb9edbb2 | 293 | return; |
164f7660 | 294 | } |
62e76326 | 295 | |
892ee3d0 AJ |
296 | for (i = 0; urls[i].url; ++i) |
297 | ++urlcnt; | |
298 | ||
299 | debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs"); | |
300 | ||
aee3523a | 301 | min_u = urnFindMinRtt(urls, urnState->request->method, nullptr); |
9ce5e3e6 | 302 | qsort(urls, urlcnt, sizeof(*urls), url_entry_sort); |
3900307b | 303 | e->buffer(); |
7e6eabbc CT |
304 | SBuf body; |
305 | SBuf *mb = &body; // diff reduction hack; TODO: Remove | |
4391cd15 | 306 | mb->appendf( "<TITLE>Select URL for %s</TITLE>\n" |
f680026f SM |
307 | "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n" |
308 | "<H2>Select URL for %s</H2>\n" | |
309 | "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e->url(), e->url()); | |
62e76326 | 310 | |
14942edd | 311 | for (i = 0; i < urlcnt; ++i) { |
62e76326 | 312 | u = &urls[i]; |
bf8fe701 | 313 | debugs(52, 3, "URL {" << u->url << "}"); |
4391cd15 | 314 | mb->appendf( |
2fe7eff9 | 315 | "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url); |
62e76326 | 316 | |
317 | if (urls[i].rtt > 0) | |
4391cd15 | 318 | mb->appendf( |
2fe7eff9 | 319 | "<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt); |
62e76326 | 320 | else |
4391cd15 | 321 | mb->appendf("<TD align=\"right\">Unknown</TD>"); |
62e76326 | 322 | |
4391cd15 | 323 | mb->appendf("<TD>%s</TD></TR>\n", u->flags.cached ? " [cached]" : " "); |
cf26e54c | 324 | } |
62e76326 | 325 | |
4391cd15 | 326 | mb->appendf( |
2fe7eff9 | 327 | "</TABLE>" |
328 | "<HR noshade size=\"1px\">\n" | |
329 | "<ADDRESS>\n" | |
330 | "Generated by %s@%s\n" | |
331 | "</ADDRESS>\n", | |
c81de627 | 332 | visible_appname_string, getMyHostname()); |
122a6e3c | 333 | const auto rep = new HttpReply; |
aee3523a | 334 | rep->setHeaders(Http::scFound, nullptr, "text/html", mb->length(), 0, squid_curtime); |
62e76326 | 335 | |
6c880a16 | 336 | if (min_u) { |
789217a2 | 337 | rep->header.putStr(Http::HdrType::LOCATION, min_u->url); |
cb69b4c7 | 338 | } |
62e76326 | 339 | |
7e6eabbc | 340 | rep->body.set(body); |
db237875 | 341 | e->replaceHttpReply(rep); |
528b2c61 | 342 | e->complete(); |
62e76326 | 343 | |
14942edd | 344 | for (i = 0; i < urlcnt; ++i) { |
62e76326 | 345 | safe_free(urls[i].url); |
346 | safe_free(urls[i].host); | |
9ce5e3e6 | 347 | } |
62e76326 | 348 | |
9ce5e3e6 | 349 | safe_free(urls); |
62e76326 | 350 | |
682d5190 | 351 | delete urnState; |
85491f8d | 352 | } |
353 | ||
9ce5e3e6 | 354 | static url_entry * |
122a6e3c | 355 | urnParseReply(const SBuf &inBuf, const HttpRequestMethod &m) |
85491f8d | 356 | { |
23d92c64 | 357 | char *token; |
9ce5e3e6 | 358 | url_entry *list; |
359 | url_entry *old; | |
360 | int n = 32; | |
361 | int i = 0; | |
bf8fe701 | 362 | debugs(52, 3, "urnParseReply"); |
e6ccf245 | 363 | list = (url_entry *)xcalloc(n + 1, sizeof(*list)); |
62e76326 | 364 | |
122a6e3c AR |
365 | // XXX: Switch to tokenizer-based parsing. |
366 | const auto allocated = SBufToCstring(inBuf); | |
367 | ||
368 | auto buf = allocated; | |
369 | while (xisspace(*buf)) | |
370 | ++buf; | |
371 | ||
aee3523a | 372 | for (token = strtok(buf, crlf); token; token = strtok(nullptr, crlf)) { |
bf8fe701 | 373 | debugs(52, 3, "urnParseReply: got '" << token << "'"); |
62e76326 | 374 | |
375 | if (i == n) { | |
376 | old = list; | |
377 | n <<= 2; | |
378 | list = (url_entry *)xcalloc(n + 1, sizeof(*list)); | |
41d00cd3 | 379 | memcpy(list, old, i * sizeof(*list)); |
62e76326 | 380 | safe_free(old); |
381 | } | |
382 | ||
1ac1d4d3 AJ |
383 | AnyP::Uri uri; |
384 | if (!uri.parse(m, SBuf(token)) || !*uri.host()) | |
62e76326 | 385 | continue; |
386 | ||
9b5c4a9a | 387 | #if USE_ICMP |
1ac1d4d3 | 388 | list[i].rtt = netdbHostRtt(uri.host()); |
62e76326 | 389 | |
9b5c4a9a | 390 | if (0 == list[i].rtt) { |
1ac1d4d3 AJ |
391 | debugs(52, 3, "Pinging " << uri.host()); |
392 | netdbPingSite(uri.host()); | |
62e76326 | 393 | } |
9b5c4a9a AJ |
394 | #else |
395 | list[i].rtt = 0; | |
396 | #endif | |
62e76326 | 397 | |
1ac1d4d3 AJ |
398 | list[i].url = xstrdup(uri.absolute().c_str()); |
399 | list[i].host = xstrdup(uri.host()); | |
5bd484b5 AR |
400 | // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked |
401 | // ones. | |
8babada0 | 402 | list[i].flags.cached = storeGetPublic(list[i].url, m) ? 1 : 0; |
14942edd | 403 | ++i; |
85491f8d | 404 | } |
62e76326 | 405 | |
bf8fe701 | 406 | debugs(52, 3, "urnParseReply: Found " << i << " URLs"); |
122a6e3c | 407 | xfree(allocated); |
9ce5e3e6 | 408 | return list; |
85491f8d | 409 | } |
f53969cc | 410 |