]> git.ipfire.org Git - thirdparty/squid.git/blame - src/urn.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / urn.cc
CommitLineData
85491f8d 1
2/*
262a0e14 3 * $Id$
ebe14c5d 4 *
6f6f0853 5 * DEBUG: section 52 URN Parsing
85491f8d 6 * AUTHOR: Kostas Anagnostakis
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
85491f8d 10 *
2b6662ba 11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
85491f8d 19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
26ac0430 24 *
85491f8d 25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
26ac0430 29 *
85491f8d 30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
85491f8d 34 */
35
f7f3304a 36#include "squid-old.h"
aa839030 37#include "errorpage.h"
c8be6d7b 38#include "StoreClient.h"
e6ccf245 39#include "Store.h"
528b2c61 40#include "HttpReply.h"
41#include "HttpRequest.h"
0eb49b6d 42#include "MemBuf.h"
b6b6f466 43#include "forward.h"
985c86bc 44#include "SquidTime.h"
9b5c4a9a 45#include "icmp/net_db.h"
85491f8d 46
add2192d 47#define URN_REQBUF_SZ 4096
48
62e76326 49class UrnState : public StoreClient
50{
51
e6ccf245 52public:
3b13a8fd 53 void created (StoreEntry *newEntry);
06096e82 54 void *operator new (size_t byteCount);
e6ccf245 55 void operator delete (void *address);
190154cf 56 void start (HttpRequest *, StoreEntry *);
30abd221 57 char *getHost (String &urlpath);
190154cf 58 void setUriResFromRequest(HttpRequest *);
59 bool RequestNeedsMenu(HttpRequest *r);
17bc3f22 60 void updateRequestURL(HttpRequest *r, char const *newPath, const size_t newPath_len);
30abd221 61 void createUriResRequest (String &uri);
e6ccf245 62
63 virtual ~UrnState();
62e76326 64
65
164f7660 66 StoreEntry *entry;
06d2839d 67 store_client *sc;
164f7660 68 StoreEntry *urlres_e;
190154cf 69 HttpRequest *request;
70 HttpRequest *urlres_r;
62e76326 71
26ac0430 72 struct {
3d0ac046
HN
73 unsigned int force_menu:1;
74 } flags;
add2192d 75 char reqbuf[URN_REQBUF_SZ];
76 int reqofs;
62e76326 77
e6ccf245 78private:
79 char *urlres;
80};
85491f8d 81
26ac0430 82typedef struct {
9ce5e3e6 83 char *url;
84 char *host;
85 int rtt;
62e76326 86
26ac0430 87 struct {
62e76326 88 int cached;
2fadd50d
HN
89 } flags;
90} url_entry;
9ce5e3e6 91
92static STCB urnHandleReply;
60745f24 93static url_entry *urnParseReply(const char *inbuf, const HttpRequestMethod&);
9ce5e3e6 94static const char *const crlf = "\r\n";
95static QS url_entry_sort;
96
e6ccf245 97CBDATA_TYPE(UrnState);
98void *
06096e82 99UrnState::operator new (size_t byteCount)
e6ccf245 100{
62e76326 101 /* derived classes with different sizes must implement their own new */
e6ccf245 102 assert (byteCount == sizeof (UrnState));
103 CBDATA_INIT_TYPE(UrnState);
104 return cbdataAlloc(UrnState);
62e76326 105
e6ccf245 106}
107
108void
109UrnState::operator delete (void *address)
110{
1f1ae50a 111 UrnState * tmp = (UrnState *)address;
112 cbdataFree (tmp);
e6ccf245 113}
114
115UrnState::~UrnState ()
116{
117 safe_free(urlres);
118}
119
48ebcb22 120static url_entry *
60745f24 121urnFindMinRtt(url_entry * urls, const HttpRequestMethod& m, int *rtt_ret)
85491f8d 122{
23d92c64 123 int min_rtt = 0;
9ce5e3e6 124 url_entry *u = NULL;
125 url_entry *min_u = NULL;
126 int i;
1caf595b 127 int urlcnt = 0;
bf8fe701 128 debugs(52, 3, "urnFindMinRtt");
23d92c64 129 assert(urls != NULL);
62e76326 130
00141c96 131 for (i = 0; NULL != urls[i].url; i++)
62e76326 132 urlcnt++;
133
bf8fe701 134 debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs");
62e76326 135
9ce5e3e6 136 if (1 == urlcnt) {
bf8fe701 137 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
62e76326 138 return urls;
1caf595b 139 }
62e76326 140
00141c96 141 for (i = 0; i < urlcnt; i++) {
62e76326 142 u = &urls[i];
bf8fe701 143 debugs(52, 3, "urnFindMinRtt: " << u->host << " rtt=" << u->rtt);
62e76326 144
145 if (u->rtt == 0)
146 continue;
147
148 if (u->rtt > min_rtt && min_rtt != 0)
149 continue;
150
151 min_rtt = u->rtt;
152
153 min_u = u;
23d92c64 154 }
62e76326 155
23d92c64 156 if (rtt_ret)
62e76326 157 *rtt_ret = min_rtt;
158
bf8fe701 159 debugs(52, 1, "urnFindMinRtt: Returning '" <<
26ac0430
AJ
160 (min_u ? min_u->url : "NONE") << "' RTT " <<
161 min_rtt );
62e76326 162
9ce5e3e6 163 return min_u;
85491f8d 164}
165
e6ccf245 166char *
30abd221 167UrnState::getHost (String &urlpath)
85491f8d 168{
e6ccf245 169 char * result;
b4f2886c 170 size_t p;
62e76326 171
b4f2886c
FC
172 /** FIXME: this appears to be parsing the URL. *very* badly. */
173 /* a proper encapsulated URI/URL type needs to clear this up. */
2c1fd837 174 if ((p=urlpath.find(':')) != String::npos) {
b4f2886c 175 result=xstrndup(urlpath.rawBuf(),p-1);
fa040df2 176 } else {
b4f2886c 177 result = xstrndup(urlpath.rawBuf(),urlpath.size());
fa040df2 178 }
e6ccf245 179 return result;
180}
181
182bool
190154cf 183UrnState::RequestNeedsMenu(HttpRequest *r)
e6ccf245 184{
17bc3f22
FC
185 if (r->urlpath.size() < 5)
186 return false;
187 //now we're sure it's long enough
188 return strncasecmp(r->urlpath.rawBuf(), "menu.", 5) == 0;
e6ccf245 189}
190
191void
17bc3f22 192UrnState::updateRequestURL(HttpRequest *r, char const *newPath, const size_t newPath_len)
e6ccf245 193{
17bc3f22 194 char *new_path = xstrndup (newPath, newPath_len);
62e76326 195 r->urlpath = new_path;
196 xfree(new_path);
e6ccf245 197}
198
199void
30abd221 200UrnState::createUriResRequest (String &uri)
e6ccf245 201{
202 LOCAL_ARRAY(char, local_urlres, 4096);
203 char *host = getHost (uri);
2c1fd837 204 snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSTRINGPH,
af6a12ee 205 host, SQUIDSTRINGPRINT(uri));
e6ccf245 206 safe_free (host);
207 safe_free (urlres);
208 urlres = xstrdup (local_urlres);
c21ad0f5 209 urlres_r = HttpRequest::CreateFromUrl(urlres);
e6ccf245 210}
211
212void
190154cf 213UrnState::setUriResFromRequest(HttpRequest *r)
e6ccf245 214{
215 if (RequestNeedsMenu(r)) {
17bc3f22 216 updateRequestURL(r, r->urlpath.rawBuf() + 5, r->urlpath.size() - 5 );
62e76326 217 flags.force_menu = 1;
e6ccf245 218 }
62e76326 219
e6ccf245 220 createUriResRequest (r->urlpath);
62e76326 221
0adbab7c 222 if (urlres_r == NULL) {
bf8fe701 223 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres);
913524f0 224 ErrorState *err = new ErrorState(ERR_URN_RESOLVE, HTTP_NOT_FOUND, r);
62e76326 225 err->url = urlres;
226 urlres = NULL;
227 errorAppendEntry(entry, err);
228 return;
0adbab7c 229 }
62e76326 230
6dd9f4bd 231 HTTPMSGLOCK(urlres_r);
a9925b40 232 urlres_r->header.putStr(HDR_ACCEPT, "text/plain");
e6ccf245 233}
234
235void
190154cf 236UrnState::start(HttpRequest * r, StoreEntry * e)
e6ccf245 237{
bf8fe701 238 debugs(52, 3, "urnStart: '" << e->url() << "'" );
e6ccf245 239 entry = e;
6dd9f4bd 240 request = HTTPMSGLOCK(r);
34266cde 241
3d0ac046 242 entry->lock();
e6ccf245 243 setUriResFromRequest(r);
62e76326 244
e6ccf245 245 if (urlres_r == NULL)
62e76326 246 return;
247
3b13a8fd 248 StoreEntry::getPublic (this, urlres, METHOD_GET);
e6ccf245 249}
250
251void
3b13a8fd 252UrnState::created(StoreEntry *newEntry)
e6ccf245 253{
254 urlres_e = newEntry;
62e76326 255
e6ccf245 256 if (urlres_e->isNull()) {
62e76326 257 urlres_e = storeCreateEntry(urlres, urlres, request_flags(), METHOD_GET);
258 sc = storeClientListAdd(urlres_e, this);
e83cc785 259 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e, urlres_r);
cf26e54c 260 } else {
34266cde 261
3d0ac046 262 urlres_e->lock();
62e76326 263 sc = storeClientListAdd(urlres_e, this);
85491f8d 264 }
62e76326 265
e6ccf245 266 reqofs = 0;
528b2c61 267 StoreIOBuffer tempBuffer;
e6ccf245 268 tempBuffer.offset = reqofs;
c8be6d7b 269 tempBuffer.length = URN_REQBUF_SZ;
e6ccf245 270 tempBuffer.data = reqbuf;
271 storeClientCopy(sc, urlres_e,
62e76326 272 tempBuffer,
273 urnHandleReply,
274 this);
e6ccf245 275}
276
277void
190154cf 278urnStart(HttpRequest * r, StoreEntry * e)
e6ccf245 279{
280 UrnState *anUrn = new UrnState();
281 anUrn->start (r, e);
85491f8d 282}
283
9ce5e3e6 284static int
285url_entry_sort(const void *A, const void *B)
286{
e6ccf245 287 const url_entry *u1 = (const url_entry *)A;
288 const url_entry *u2 = (const url_entry *)B;
62e76326 289
9ce5e3e6 290 if (u2->rtt == u1->rtt)
62e76326 291 return 0;
9ce5e3e6 292 else if (0 == u1->rtt)
62e76326 293 return 1;
9ce5e3e6 294 else if (0 == u2->rtt)
62e76326 295 return -1;
9ce5e3e6 296 else
62e76326 297 return u1->rtt - u2->rtt;
9ce5e3e6 298}
299
bb9edbb2
AJ
300static void
301urnHandleReplyError(UrnState *urnState, StoreEntry *urlres_e)
302{
303 urlres_e->unlock();
304 urnState->entry->unlock();
305 HTTPMSGUNLOCK(urnState->request);
306 HTTPMSGUNLOCK(urnState->urlres_r);
307 delete urnState;
308}
309
528b2c61 310/* TODO: use the clientStream support for this */
85491f8d 311static void
c8be6d7b 312urnHandleReply(void *data, StoreIOBuffer result)
85491f8d 313{
e6ccf245 314 UrnState *urnState = static_cast<UrnState *>(data);
cf26e54c 315 StoreEntry *e = urnState->entry;
316 StoreEntry *urlres_e = urnState->urlres_e;
23d92c64 317 char *s = NULL;
2334c194 318 size_t k;
cb69b4c7 319 HttpReply *rep;
9ce5e3e6 320 url_entry *urls;
00141c96 321 url_entry *u;
9ce5e3e6 322 url_entry *min_u;
032785bf 323 MemBuf *mb = NULL;
cf26e54c 324 ErrorState *err;
9ce5e3e6 325 int i;
326 int urlcnt = 0;
add2192d 327 char *buf = urnState->reqbuf;
c8be6d7b 328 StoreIOBuffer tempBuffer;
cf26e54c 329
58c0b17d 330 debugs(52, 3, "urnHandleReply: Called with size=" << result.length << ".");
62e76326 331
58c0b17d 332 if (EBIT_TEST(urlres_e->flags, ENTRY_ABORTED) || result.length == 0 || result.flags.error) {
bb9edbb2
AJ
333 urnHandleReplyError(urnState, urlres_e);
334 return;
85491f8d 335 }
62e76326 336
add2192d 337 /* Update reqofs to point to where in the buffer we'd be */
c8be6d7b 338 urnState->reqofs += result.length;
add2192d 339
340 /* Handle reqofs being bigger than normal */
341 if (urnState->reqofs >= URN_REQBUF_SZ) {
bb9edbb2
AJ
342 urnHandleReplyError(urnState, urlres_e);
343 return;
add2192d 344 }
62e76326 345
add2192d 346 /* If we haven't received the entire object (urn), copy more */
347 if (urlres_e->store_status == STORE_PENDING &&
62e76326 348 urnState->reqofs < URN_REQBUF_SZ) {
349 tempBuffer.offset = urnState->reqofs;
350 tempBuffer.length = URN_REQBUF_SZ;
351 tempBuffer.data = urnState->reqbuf + urnState->reqofs;
352 storeClientCopy(urnState->sc, urlres_e,
353 tempBuffer,
354 urnHandleReply,
355 urnState);
356 return;
23d92c64 357 }
62e76326 358
23d92c64 359 /* we know its STORE_OK */
add2192d 360 k = headersEnd(buf, urnState->reqofs);
62e76326 361
2334c194 362 if (0 == k) {
bf8fe701 363 debugs(52, 1, "urnHandleReply: didn't find end-of-headers for " << e->url() );
bb9edbb2
AJ
364 urnHandleReplyError(urnState, urlres_e);
365 return;
85491f8d 366 }
62e76326 367
2334c194 368 s = buf + k;
528b2c61 369 assert(urlres_e->getReply());
06a5ae20 370 rep = new HttpReply;
59eed7dc 371 rep->parseCharBuf(buf, k);
bf8fe701 372 debugs(52, 3, "reply exists, code=" << rep->sline.status << ".");
62e76326 373
528b2c61 374 if (rep->sline.status != HTTP_OK) {
bf8fe701 375 debugs(52, 3, "urnHandleReply: failed.");
913524f0 376 err = new ErrorState(ERR_URN_RESOLVE, HTTP_NOT_FOUND, urnState->request);
3900307b 377 err->url = xstrdup(e->url());
62e76326 378 errorAppendEntry(e, err);
06a5ae20 379 delete rep;
bb9edbb2
AJ
380 urnHandleReplyError(urnState, urlres_e);
381 return;
85491f8d 382 }
62e76326 383
06a5ae20 384 delete rep;
62e76326 385
b6a2f15e 386 while (xisspace(*s))
62e76326 387 s++;
388
9ce5e3e6 389 urls = urnParseReply(s, urnState->request->method);
62e76326 390
00141c96 391 for (i = 0; NULL != urls[i].url; i++)
62e76326 392 urlcnt++;
393
bf8fe701 394 debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs");
62e76326 395
164f7660 396 if (urls == NULL) { /* unkown URN error */
bf8fe701 397 debugs(52, 3, "urnTranslateDone: unknown URN " << e->url() );
913524f0 398 err = new ErrorState(ERR_URN_RESOLVE, HTTP_NOT_FOUND, urnState->request);
3900307b 399 err->url = xstrdup(e->url());
62e76326 400 errorAppendEntry(e, err);
bb9edbb2
AJ
401 urnHandleReplyError(urnState, urlres_e);
402 return;
164f7660 403 }
62e76326 404
9ce5e3e6 405 min_u = urnFindMinRtt(urls, urnState->request->method, NULL);
406 qsort(urls, urlcnt, sizeof(*urls), url_entry_sort);
3900307b 407 e->buffer();
032785bf 408 mb = new MemBuf;
2fe7eff9 409 mb->init();
410 mb->Printf( "<TITLE>Select URL for %s</TITLE>\n"
411 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
412 "<H2>Select URL for %s</H2>\n"
3900307b 413 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e->url(), e->url());
62e76326 414
9ce5e3e6 415 for (i = 0; i < urlcnt; i++) {
62e76326 416 u = &urls[i];
bf8fe701 417 debugs(52, 3, "URL {" << u->url << "}");
2fe7eff9 418 mb->Printf(
419 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url);
62e76326 420
421 if (urls[i].rtt > 0)
2fe7eff9 422 mb->Printf(
423 "<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt);
62e76326 424 else
2fe7eff9 425 mb->Printf("<TD align=\"right\">Unknown</TD>");
62e76326 426
2fe7eff9 427 mb->Printf(
428 "<TD>%s</TD></TR>\n", u->flags.cached ? " [cached]" : " ");
cf26e54c 429 }
62e76326 430
2fe7eff9 431 mb->Printf(
432 "</TABLE>"
433 "<HR noshade size=\"1px\">\n"
434 "<ADDRESS>\n"
435 "Generated by %s@%s\n"
436 "</ADDRESS>\n",
7dbca7a4 437 APP_FULLNAME, getMyHostname());
06a5ae20 438 rep = new HttpReply;
11992b6f 439 rep->setHeaders(HTTP_MOVED_TEMPORARILY, NULL, "text/html", mb->contentSize(), 0, squid_curtime);
62e76326 440
b515fc11 441 if (urnState->flags.force_menu) {
bf8fe701 442 debugs(51, 3, "urnHandleReply: forcing menu");
9ce5e3e6 443 } else if (min_u) {
a9925b40 444 rep->header.putStr(HDR_LOCATION, min_u->url);
cb69b4c7 445 }
62e76326 446
0521f8be 447 rep->body.setMb(mb);
032785bf 448 /* don't clean or delete mb; rep->body owns it now */
db237875 449 e->replaceHttpReply(rep);
528b2c61 450 e->complete();
62e76326 451
9ce5e3e6 452 for (i = 0; i < urlcnt; i++) {
62e76326 453 safe_free(urls[i].url);
454 safe_free(urls[i].host);
9ce5e3e6 455 }
62e76326 456
9ce5e3e6 457 safe_free(urls);
96a5be3d 458 /* mb was absorbed in httpBodySet call, so we must not clean it */
06d2839d 459 storeUnregister(urnState->sc, urlres_e, urnState);
62e76326 460
bb9edbb2 461 urnHandleReplyError(urnState, urlres_e);
85491f8d 462}
463
9ce5e3e6 464static url_entry *
60745f24 465urnParseReply(const char *inbuf, const HttpRequestMethod& m)
85491f8d 466{
23d92c64 467 char *buf = xstrdup(inbuf);
468 char *token;
9ce5e3e6 469 char *url;
470 char *host;
9ce5e3e6 471 url_entry *list;
472 url_entry *old;
473 int n = 32;
474 int i = 0;
bf8fe701 475 debugs(52, 3, "urnParseReply");
e6ccf245 476 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
62e76326 477
23d92c64 478 for (token = strtok(buf, crlf); token; token = strtok(NULL, crlf)) {
bf8fe701 479 debugs(52, 3, "urnParseReply: got '" << token << "'");
62e76326 480
481 if (i == n) {
482 old = list;
483 n <<= 2;
484 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
41d00cd3 485 memcpy(list, old, i * sizeof(*list));
62e76326 486 safe_free(old);
487 }
488
489 url = xstrdup(token);
490 host = urlHostname(url);
491
492 if (NULL == host)
493 continue;
494
9b5c4a9a
AJ
495#if USE_ICMP
496 list[i].rtt = netdbHostRtt(host);
62e76326 497
9b5c4a9a 498 if (0 == list[i].rtt) {
bf8fe701 499 debugs(52, 3, "urnParseReply: Pinging " << host);
62e76326 500 netdbPingSite(host);
501 }
9b5c4a9a
AJ
502#else
503 list[i].rtt = 0;
504#endif
62e76326 505
506 list[i].url = url;
507 list[i].host = xstrdup(host);
5bd484b5
AR
508 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
509 // ones.
62e76326 510 list[i].flags.cached = storeGetPublic(url, m) ? 1 : 0;
511 i++;
85491f8d 512 }
62e76326 513
bf8fe701 514 debugs(52, 3, "urnParseReply: Found " << i << " URLs");
9ce5e3e6 515 return list;
85491f8d 516}