]> git.ipfire.org Git - thirdparty/squid.git/blame - src/urn.cc
Adding a comment that tests/testUfs will fail when 'heap' is the only
[thirdparty/squid.git] / src / urn.cc
CommitLineData
85491f8d 1
2/*
3900307b 3 * $Id: urn.cc,v 1.104 2007/04/21 07:14:15 wessels Exp $
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.
24 *
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.
29 *
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
36#include "squid.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"
85491f8d 45
add2192d 46#define URN_REQBUF_SZ 4096
47
62e76326 48class UrnState : public StoreClient
49{
50
e6ccf245 51public:
3b13a8fd 52 void created (StoreEntry *newEntry);
06096e82 53 void *operator new (size_t byteCount);
e6ccf245 54 void operator delete (void *address);
190154cf 55 void start (HttpRequest *, StoreEntry *);
e6ccf245 56 char *getHost (String &urlpath);
190154cf 57 void setUriResFromRequest(HttpRequest *);
58 bool RequestNeedsMenu(HttpRequest *r);
59 void updateRequestURL(HttpRequest *r, char const *newPath);
e6ccf245 60 void createUriResRequest (String &uri);
61
62 virtual ~UrnState();
62e76326 63
64
164f7660 65 StoreEntry *entry;
06d2839d 66 store_client *sc;
164f7660 67 StoreEntry *urlres_e;
190154cf 68 HttpRequest *request;
69 HttpRequest *urlres_r;
62e76326 70
71 struct
72 {
73
74unsigned int force_menu:
75 1;
76 }
77
78 flags;
add2192d 79 char reqbuf[URN_REQBUF_SZ];
80 int reqofs;
62e76326 81
e6ccf245 82private:
83 char *urlres;
84};
85491f8d 85
62e76326 86typedef struct
87{
9ce5e3e6 88 char *url;
89 char *host;
90 int rtt;
62e76326 91
92 struct
93 {
94 int cached;
95 }
96
97 flags;
98}
99
100url_entry;
9ce5e3e6 101
102static STCB urnHandleReply;
103static url_entry *urnParseReply(const char *inbuf, method_t);
104static const char *const crlf = "\r\n";
105static QS url_entry_sort;
106
e6ccf245 107CBDATA_TYPE(UrnState);
108void *
06096e82 109UrnState::operator new (size_t byteCount)
e6ccf245 110{
62e76326 111 /* derived classes with different sizes must implement their own new */
e6ccf245 112 assert (byteCount == sizeof (UrnState));
113 CBDATA_INIT_TYPE(UrnState);
114 return cbdataAlloc(UrnState);
62e76326 115
e6ccf245 116}
117
118void
119UrnState::operator delete (void *address)
120{
1f1ae50a 121 UrnState * tmp = (UrnState *)address;
122 cbdataFree (tmp);
e6ccf245 123}
124
125UrnState::~UrnState ()
126{
127 safe_free(urlres);
128}
129
48ebcb22 130static url_entry *
9ce5e3e6 131urnFindMinRtt(url_entry * urls, method_t m, int *rtt_ret)
85491f8d 132{
23d92c64 133 int min_rtt = 0;
9ce5e3e6 134 url_entry *u = NULL;
135 url_entry *min_u = NULL;
136 int i;
1caf595b 137 int urlcnt = 0;
6f6f0853 138 debug(52, 3) ("urnFindMinRtt\n");
23d92c64 139 assert(urls != NULL);
62e76326 140
00141c96 141 for (i = 0; NULL != urls[i].url; i++)
62e76326 142 urlcnt++;
143
94934dbe 144 debug(53, 3) ("urnFindMinRtt: Counted %d URLs\n", i);
62e76326 145
9ce5e3e6 146 if (1 == urlcnt) {
62e76326 147 debug(52, 3) ("urnFindMinRtt: Only one URL - return it!\n");
148 return urls;
1caf595b 149 }
62e76326 150
00141c96 151 for (i = 0; i < urlcnt; i++) {
62e76326 152 u = &urls[i];
153 debug(52, 3) ("urnFindMinRtt: %s rtt=%d\n", u->host, u->rtt);
154
155 if (u->rtt == 0)
156 continue;
157
158 if (u->rtt > min_rtt && min_rtt != 0)
159 continue;
160
161 min_rtt = u->rtt;
162
163 min_u = u;
23d92c64 164 }
62e76326 165
23d92c64 166 if (rtt_ret)
62e76326 167 *rtt_ret = min_rtt;
168
6f6f0853 169 debug(52, 1) ("urnFindMinRtt: Returning '%s' RTT %d\n",
62e76326 170 min_u ? min_u->url : "NONE",
171 min_rtt);
172
9ce5e3e6 173 return min_u;
85491f8d 174}
175
e6ccf245 176char *
177UrnState::getHost (String &urlpath)
85491f8d 178{
e6ccf245 179 char * result;
180 char const *t;
62e76326 181
650c4b88 182 if ((t = urlpath.pos(':')) != NULL) {
183 urlpath.set(t, '\0');
62e76326 184 result = xstrdup(urlpath.buf());
650c4b88 185 urlpath.set(t, ':');
fa040df2 186 } else {
62e76326 187 result = xstrdup(urlpath.buf());
fa040df2 188 }
62e76326 189
e6ccf245 190 return result;
191}
192
193bool
190154cf 194UrnState::RequestNeedsMenu(HttpRequest *r)
e6ccf245 195{
528b2c61 196 return strncasecmp(r->urlpath.buf(), "menu.", 5) == 0;
e6ccf245 197}
198
199void
190154cf 200UrnState::updateRequestURL(HttpRequest *r, char const *newPath)
e6ccf245 201{
62e76326 202 char *new_path = xstrdup (newPath);
203 r->urlpath = new_path;
204 xfree(new_path);
e6ccf245 205}
206
207void
208UrnState::createUriResRequest (String &uri)
209{
210 LOCAL_ARRAY(char, local_urlres, 4096);
211 char *host = getHost (uri);
528b2c61 212 snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?urn:%s", host, uri.buf());
e6ccf245 213 safe_free (host);
214 safe_free (urlres);
215 urlres = xstrdup (local_urlres);
c21ad0f5 216 urlres_r = HttpRequest::CreateFromUrl(urlres);
e6ccf245 217}
218
219void
190154cf 220UrnState::setUriResFromRequest(HttpRequest *r)
e6ccf245 221{
222 if (RequestNeedsMenu(r)) {
62e76326 223 updateRequestURL(r, r->urlpath.buf() + 5);
224 flags.force_menu = 1;
e6ccf245 225 }
62e76326 226
e6ccf245 227 createUriResRequest (r->urlpath);
62e76326 228
0adbab7c 229 if (urlres_r == NULL) {
62e76326 230 debug(52, 3) ("urnStart: Bad uri-res URL %s\n", urlres);
2cc81f1f 231 ErrorState *err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND, r);
62e76326 232 err->url = urlres;
233 urlres = NULL;
234 errorAppendEntry(entry, err);
235 return;
0adbab7c 236 }
62e76326 237
6dd9f4bd 238 HTTPMSGLOCK(urlres_r);
a9925b40 239 urlres_r->header.putStr(HDR_ACCEPT, "text/plain");
e6ccf245 240}
241
242void
190154cf 243UrnState::start(HttpRequest * r, StoreEntry * e)
e6ccf245 244{
3900307b 245 debug(52, 3) ("urnStart: '%s'\n", e->url());
e6ccf245 246 entry = e;
6dd9f4bd 247 request = HTTPMSGLOCK(r);
34266cde 248
249 entry->lock()
250
251 ;
e6ccf245 252 setUriResFromRequest(r);
62e76326 253
e6ccf245 254 if (urlres_r == NULL)
62e76326 255 return;
256
3b13a8fd 257 StoreEntry::getPublic (this, urlres, METHOD_GET);
e6ccf245 258}
259
260void
3b13a8fd 261UrnState::created(StoreEntry *newEntry)
e6ccf245 262{
263 urlres_e = newEntry;
62e76326 264
e6ccf245 265 if (urlres_e->isNull()) {
62e76326 266 urlres_e = storeCreateEntry(urlres, urlres, request_flags(), METHOD_GET);
267 sc = storeClientListAdd(urlres_e, this);
b6b6f466 268 FwdState::fwdStart(-1, urlres_e, urlres_r);
cf26e54c 269 } else {
34266cde 270
271 urlres_e->lock()
272
273 ;
62e76326 274 sc = storeClientListAdd(urlres_e, this);
85491f8d 275 }
62e76326 276
e6ccf245 277 reqofs = 0;
528b2c61 278 StoreIOBuffer tempBuffer;
e6ccf245 279 tempBuffer.offset = reqofs;
c8be6d7b 280 tempBuffer.length = URN_REQBUF_SZ;
e6ccf245 281 tempBuffer.data = reqbuf;
282 storeClientCopy(sc, urlres_e,
62e76326 283 tempBuffer,
284 urnHandleReply,
285 this);
e6ccf245 286}
287
288void
190154cf 289urnStart(HttpRequest * r, StoreEntry * e)
e6ccf245 290{
291 UrnState *anUrn = new UrnState();
292 anUrn->start (r, e);
85491f8d 293}
294
9ce5e3e6 295static int
296url_entry_sort(const void *A, const void *B)
297{
e6ccf245 298 const url_entry *u1 = (const url_entry *)A;
299 const url_entry *u2 = (const url_entry *)B;
62e76326 300
9ce5e3e6 301 if (u2->rtt == u1->rtt)
62e76326 302 return 0;
9ce5e3e6 303 else if (0 == u1->rtt)
62e76326 304 return 1;
9ce5e3e6 305 else if (0 == u2->rtt)
62e76326 306 return -1;
9ce5e3e6 307 else
62e76326 308 return u1->rtt - u2->rtt;
9ce5e3e6 309}
310
528b2c61 311/* TODO: use the clientStream support for this */
85491f8d 312static void
c8be6d7b 313urnHandleReply(void *data, StoreIOBuffer result)
85491f8d 314{
e6ccf245 315 UrnState *urnState = static_cast<UrnState *>(data);
cf26e54c 316 StoreEntry *e = urnState->entry;
317 StoreEntry *urlres_e = urnState->urlres_e;
23d92c64 318 char *s = NULL;
2334c194 319 size_t k;
cb69b4c7 320 HttpReply *rep;
9ce5e3e6 321 url_entry *urls;
00141c96 322 url_entry *u;
9ce5e3e6 323 url_entry *min_u;
032785bf 324 MemBuf *mb = NULL;
cf26e54c 325 ErrorState *err;
9ce5e3e6 326 int i;
327 int urlcnt = 0;
add2192d 328 char *buf = urnState->reqbuf;
c8be6d7b 329 StoreIOBuffer tempBuffer;
cf26e54c 330
c4b7a5a9 331 debug(52, 3) ("urnHandleReply: Called with size=%u.\n", (unsigned int)result.length);
62e76326 332
450e0c10 333 /* Can't be lower because of the goto's */
334 HttpVersion version(1, 0);
335
b7fe0ab0 336 if (EBIT_TEST(urlres_e->flags, ENTRY_ABORTED)) {
62e76326 337 goto error;
85491f8d 338 }
62e76326 339
c8be6d7b 340 if (result.length == 0) {
62e76326 341 goto error;
c8be6d7b 342 } else if (result.flags.error < 0) {
62e76326 343 goto error;
85491f8d 344 }
62e76326 345
add2192d 346 /* Update reqofs to point to where in the buffer we'd be */
c8be6d7b 347 urnState->reqofs += result.length;
add2192d 348
349 /* Handle reqofs being bigger than normal */
350 if (urnState->reqofs >= URN_REQBUF_SZ) {
62e76326 351 goto error;
add2192d 352 }
62e76326 353
add2192d 354 /* If we haven't received the entire object (urn), copy more */
355 if (urlres_e->store_status == STORE_PENDING &&
62e76326 356 urnState->reqofs < URN_REQBUF_SZ) {
357 tempBuffer.offset = urnState->reqofs;
358 tempBuffer.length = URN_REQBUF_SZ;
359 tempBuffer.data = urnState->reqbuf + urnState->reqofs;
360 storeClientCopy(urnState->sc, urlres_e,
361 tempBuffer,
362 urnHandleReply,
363 urnState);
364 return;
23d92c64 365 }
62e76326 366
23d92c64 367 /* we know its STORE_OK */
add2192d 368 k = headersEnd(buf, urnState->reqofs);
62e76326 369
2334c194 370 if (0 == k) {
62e76326 371 debug(52, 1) ("urnHandleReply: didn't find end-of-headers for %s\n",
3900307b 372 e->url());
62e76326 373 goto error;
85491f8d 374 }
62e76326 375
2334c194 376 s = buf + k;
528b2c61 377 assert(urlres_e->getReply());
06a5ae20 378 rep = new HttpReply;
59eed7dc 379 rep->parseCharBuf(buf, k);
4a56ee8d 380 debug(52, 3) ("reply exists, code=%d.\n", rep->sline.status);
62e76326 381
528b2c61 382 if (rep->sline.status != HTTP_OK) {
62e76326 383 debug(52, 3) ("urnHandleReply: failed.\n");
2cc81f1f 384 err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND, urnState->request);
3900307b 385 err->url = xstrdup(e->url());
62e76326 386 errorAppendEntry(e, err);
06a5ae20 387 delete rep;
62e76326 388 goto error;
85491f8d 389 }
62e76326 390
06a5ae20 391 delete rep;
62e76326 392
b6a2f15e 393 while (xisspace(*s))
62e76326 394 s++;
395
9ce5e3e6 396 urls = urnParseReply(s, urnState->request->method);
62e76326 397
00141c96 398 for (i = 0; NULL != urls[i].url; i++)
62e76326 399 urlcnt++;
400
94934dbe 401 debug(53, 3) ("urnFindMinRtt: Counted %d URLs\n", i);
62e76326 402
164f7660 403 if (urls == NULL) { /* unkown URN error */
3900307b 404 debug(52, 3) ("urnTranslateDone: unknown URN %s\n", e->url());
2cc81f1f 405 err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND, urnState->request);
3900307b 406 err->url = xstrdup(e->url());
62e76326 407 errorAppendEntry(e, err);
408 goto error;
164f7660 409 }
62e76326 410
9ce5e3e6 411 min_u = urnFindMinRtt(urls, urnState->request->method, NULL);
412 qsort(urls, urlcnt, sizeof(*urls), url_entry_sort);
3900307b 413 e->buffer();
032785bf 414 mb = new MemBuf;
2fe7eff9 415 mb->init();
416 mb->Printf( "<TITLE>Select URL for %s</TITLE>\n"
417 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
418 "<H2>Select URL for %s</H2>\n"
3900307b 419 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e->url(), e->url());
62e76326 420
9ce5e3e6 421 for (i = 0; i < urlcnt; i++) {
62e76326 422 u = &urls[i];
423 debug(52, 3) ("URL {%s}\n", u->url);
2fe7eff9 424 mb->Printf(
425 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url);
62e76326 426
427 if (urls[i].rtt > 0)
2fe7eff9 428 mb->Printf(
429 "<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt);
62e76326 430 else
2fe7eff9 431 mb->Printf("<TD align=\"right\">Unknown</TD>");
62e76326 432
2fe7eff9 433 mb->Printf(
434 "<TD>%s</TD></TR>\n", u->flags.cached ? " [cached]" : " ");
cf26e54c 435 }
62e76326 436
2fe7eff9 437 mb->Printf(
438 "</TABLE>"
439 "<HR noshade size=\"1px\">\n"
440 "<ADDRESS>\n"
441 "Generated by %s@%s\n"
442 "</ADDRESS>\n",
443 full_appname_string, getMyHostname());
06a5ae20 444 rep = new HttpReply;
445 rep->setHeaders(version, HTTP_MOVED_TEMPORARILY, NULL,
446 "text/html", mb->contentSize(), 0, squid_curtime);
62e76326 447
b515fc11 448 if (urnState->flags.force_menu) {
62e76326 449 debug(51, 3) ("urnHandleReply: forcing menu\n");
9ce5e3e6 450 } else if (min_u) {
a9925b40 451 rep->header.putStr(HDR_LOCATION, min_u->url);
cb69b4c7 452 }
62e76326 453
032785bf 454 httpBodySet(&rep->body, mb);
455 /* don't clean or delete mb; rep->body owns it now */
db237875 456 e->replaceHttpReply(rep);
528b2c61 457 e->complete();
62e76326 458
9ce5e3e6 459 for (i = 0; i < urlcnt; i++) {
62e76326 460 safe_free(urls[i].url);
461 safe_free(urls[i].host);
9ce5e3e6 462 }
62e76326 463
9ce5e3e6 464 safe_free(urls);
96a5be3d 465 /* mb was absorbed in httpBodySet call, so we must not clean it */
06d2839d 466 storeUnregister(urnState->sc, urlres_e, urnState);
62e76326 467
468error:
97b5e68f 469 urlres_e->unlock();
470 urnState->entry->unlock();
6dd9f4bd 471 HTTPMSGUNLOCK(urnState->request);
472 HTTPMSGUNLOCK(urnState->urlres_r);
e6ccf245 473 delete urnState;
85491f8d 474}
475
9ce5e3e6 476static url_entry *
477urnParseReply(const char *inbuf, method_t m)
85491f8d 478{
23d92c64 479 char *buf = xstrdup(inbuf);
480 char *token;
9ce5e3e6 481 char *url;
482 char *host;
9ce5e3e6 483 int rtt;
484 url_entry *list;
485 url_entry *old;
486 int n = 32;
487 int i = 0;
6f6f0853 488 debug(52, 3) ("urnParseReply\n");
e6ccf245 489 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
62e76326 490
23d92c64 491 for (token = strtok(buf, crlf); token; token = strtok(NULL, crlf)) {
62e76326 492 debug(52, 3) ("urnParseReply: got '%s'\n", token);
493
494 if (i == n) {
495 old = list;
496 n <<= 2;
497 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
498 xmemcpy(list, old, i * sizeof(*list));
499 safe_free(old);
500 }
501
502 url = xstrdup(token);
503 host = urlHostname(url);
504
505 if (NULL == host)
506 continue;
507
508 rtt = netdbHostRtt(host);
509
510 if (0 == rtt) {
511 debug(52, 3) ("urnParseReply: Pinging %s\n", host);
512 netdbPingSite(host);
513 }
514
515 list[i].url = url;
516 list[i].host = xstrdup(host);
517 list[i].rtt = rtt;
518 list[i].flags.cached = storeGetPublic(url, m) ? 1 : 0;
519 i++;
85491f8d 520 }
62e76326 521
94934dbe 522 debug(52, 3) ("urnParseReply: Found %d URLs\n", i);
9ce5e3e6 523 return list;
85491f8d 524}