]> git.ipfire.org Git - thirdparty/squid.git/blame - src/urn.cc
Fix SQUID_YESNO 'syntax error near unexpected token' (#2117)
[thirdparty/squid.git] / src / urn.cc
CommitLineData
85491f8d 1/*
1f7b830e 2 * Copyright (C) 1996-2025 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 30class UrnState : public StoreClient
31{
5c2f68b7 32 CBDATA_CLASS(UrnState);
62e76326 33
e6ccf245 34public:
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 52private:
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 60typedef 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
70static STCB urnHandleReply;
122a6e3c 71static url_entry *urnParseReply(const SBuf &, const HttpRequestMethod &);
9ce5e3e6 72static const char *const crlf = "\r\n";
9ce5e3e6 73
bda078fe 74CBDATA_CLASS_INIT(UrnState);
62e76326 75
bda078fe 76UrnState::~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 92static url_entry *
ced8def3 93urnFindMinRtt(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 138void
51b5dcf5 139UrnState::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
161void
190154cf 162UrnState::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
196void
197UrnState::fillChecklist(ACLFilledChecklist &checklist) const
198{
199 checklist.setRequest(request.getRaw());
200 checklist.al = ale;
201}
202
e6ccf245 203void
d2a6dcba 204urnStart(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 210static int
211url_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 227static void
c8be6d7b 228urnHandleReply(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
ee989932
AR
285 // XXX: Missing reply freshness checks (e.g., calling refreshCheckHTTP()).
286
122a6e3c 287 urls = urnParseReply(urnState->parsingBuffer.toSBuf(), urnState->request->method);
62e76326 288
892ee3d0 289 if (!urls) { /* unknown URN error */
8a70cdbb 290 debugs(52, 3, "urnTranslateDone: unknown URN " << e->url());
7e6eabbc 291 err = new ErrorState(ERR_URN_RESOLVE, Http::scNotFound, urnState->request.getRaw(), urnState->ale);
3900307b 292 err->url = xstrdup(e->url());
62e76326 293 errorAppendEntry(e, err);
682d5190 294 delete urnState;
bb9edbb2 295 return;
164f7660 296 }
62e76326 297
892ee3d0
AJ
298 for (i = 0; urls[i].url; ++i)
299 ++urlcnt;
300
301 debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs");
302
aee3523a 303 min_u = urnFindMinRtt(urls, urnState->request->method, nullptr);
9ce5e3e6 304 qsort(urls, urlcnt, sizeof(*urls), url_entry_sort);
3900307b 305 e->buffer();
7e6eabbc
CT
306 SBuf body;
307 SBuf *mb = &body; // diff reduction hack; TODO: Remove
4391cd15 308 mb->appendf( "<TITLE>Select URL for %s</TITLE>\n"
f680026f
SM
309 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
310 "<H2>Select URL for %s</H2>\n"
311 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e->url(), e->url());
62e76326 312
14942edd 313 for (i = 0; i < urlcnt; ++i) {
62e76326 314 u = &urls[i];
bf8fe701 315 debugs(52, 3, "URL {" << u->url << "}");
4391cd15 316 mb->appendf(
2fe7eff9 317 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url);
62e76326 318
319 if (urls[i].rtt > 0)
4391cd15 320 mb->appendf(
2fe7eff9 321 "<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt);
62e76326 322 else
4391cd15 323 mb->appendf("<TD align=\"right\">Unknown</TD>");
62e76326 324
4391cd15 325 mb->appendf("<TD>%s</TD></TR>\n", u->flags.cached ? " [cached]" : " ");
cf26e54c 326 }
62e76326 327
4391cd15 328 mb->appendf(
2fe7eff9 329 "</TABLE>"
330 "<HR noshade size=\"1px\">\n"
331 "<ADDRESS>\n"
332 "Generated by %s@%s\n"
333 "</ADDRESS>\n",
c81de627 334 visible_appname_string, getMyHostname());
122a6e3c 335 const auto rep = new HttpReply;
aee3523a 336 rep->setHeaders(Http::scFound, nullptr, "text/html", mb->length(), 0, squid_curtime);
62e76326 337
6c880a16 338 if (min_u) {
789217a2 339 rep->header.putStr(Http::HdrType::LOCATION, min_u->url);
cb69b4c7 340 }
62e76326 341
7e6eabbc 342 rep->body.set(body);
db237875 343 e->replaceHttpReply(rep);
528b2c61 344 e->complete();
62e76326 345
14942edd 346 for (i = 0; i < urlcnt; ++i) {
62e76326 347 safe_free(urls[i].url);
348 safe_free(urls[i].host);
9ce5e3e6 349 }
62e76326 350
9ce5e3e6 351 safe_free(urls);
62e76326 352
682d5190 353 delete urnState;
85491f8d 354}
355
9ce5e3e6 356static url_entry *
122a6e3c 357urnParseReply(const SBuf &inBuf, const HttpRequestMethod &m)
85491f8d 358{
23d92c64 359 char *token;
9ce5e3e6 360 url_entry *list;
361 url_entry *old;
362 int n = 32;
363 int i = 0;
bf8fe701 364 debugs(52, 3, "urnParseReply");
e6ccf245 365 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
62e76326 366
122a6e3c
AR
367 // XXX: Switch to tokenizer-based parsing.
368 const auto allocated = SBufToCstring(inBuf);
369
370 auto buf = allocated;
371 while (xisspace(*buf))
372 ++buf;
373
aee3523a 374 for (token = strtok(buf, crlf); token; token = strtok(nullptr, crlf)) {
bf8fe701 375 debugs(52, 3, "urnParseReply: got '" << token << "'");
62e76326 376
377 if (i == n) {
378 old = list;
379 n <<= 2;
380 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
41d00cd3 381 memcpy(list, old, i * sizeof(*list));
62e76326 382 safe_free(old);
383 }
384
1ac1d4d3
AJ
385 AnyP::Uri uri;
386 if (!uri.parse(m, SBuf(token)) || !*uri.host())
62e76326 387 continue;
388
9b5c4a9a 389#if USE_ICMP
1ac1d4d3 390 list[i].rtt = netdbHostRtt(uri.host());
62e76326 391
9b5c4a9a 392 if (0 == list[i].rtt) {
1ac1d4d3
AJ
393 debugs(52, 3, "Pinging " << uri.host());
394 netdbPingSite(uri.host());
62e76326 395 }
9b5c4a9a
AJ
396#else
397 list[i].rtt = 0;
398#endif
62e76326 399
1ac1d4d3
AJ
400 list[i].url = xstrdup(uri.absolute().c_str());
401 list[i].host = xstrdup(uri.host());
5bd484b5
AR
402 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
403 // ones.
8babada0 404 list[i].flags.cached = storeGetPublic(list[i].url, m) ? 1 : 0;
14942edd 405 ++i;
85491f8d 406 }
62e76326 407
bf8fe701 408 debugs(52, 3, "urnParseReply: Found " << i << " URLs");
122a6e3c 409 xfree(allocated);
9ce5e3e6 410 return list;
85491f8d 411}
f53969cc 412