]> git.ipfire.org Git - thirdparty/squid.git/blame - src/urn.cc
Bootstrapped
[thirdparty/squid.git] / src / urn.cc
CommitLineData
85491f8d 1
2/*
34266cde 3 * $Id: urn.cc,v 1.101 2006/05/19 17:19:10 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"
c8be6d7b 37#include "StoreClient.h"
e6ccf245 38#include "Store.h"
528b2c61 39#include "HttpReply.h"
40#include "HttpRequest.h"
0eb49b6d 41#include "MemBuf.h"
b6b6f466 42#include "forward.h"
985c86bc 43#include "SquidTime.h"
85491f8d 44
add2192d 45#define URN_REQBUF_SZ 4096
46
62e76326 47class UrnState : public StoreClient
48{
49
e6ccf245 50public:
3b13a8fd 51 void created (StoreEntry *newEntry);
06096e82 52 void *operator new (size_t byteCount);
e6ccf245 53 void operator delete (void *address);
190154cf 54 void start (HttpRequest *, StoreEntry *);
e6ccf245 55 char *getHost (String &urlpath);
190154cf 56 void setUriResFromRequest(HttpRequest *);
57 bool RequestNeedsMenu(HttpRequest *r);
58 void updateRequestURL(HttpRequest *r, char const *newPath);
e6ccf245 59 void createUriResRequest (String &uri);
60
61 virtual ~UrnState();
62e76326 62
63
164f7660 64 StoreEntry *entry;
06d2839d 65 store_client *sc;
164f7660 66 StoreEntry *urlres_e;
190154cf 67 HttpRequest *request;
68 HttpRequest *urlres_r;
62e76326 69
70 struct
71 {
72
73unsigned int force_menu:
74 1;
75 }
76
77 flags;
add2192d 78 char reqbuf[URN_REQBUF_SZ];
79 int reqofs;
62e76326 80
e6ccf245 81private:
82 char *urlres;
83};
85491f8d 84
62e76326 85typedef struct
86{
9ce5e3e6 87 char *url;
88 char *host;
89 int rtt;
62e76326 90
91 struct
92 {
93 int cached;
94 }
95
96 flags;
97}
98
99url_entry;
9ce5e3e6 100
101static STCB urnHandleReply;
102static url_entry *urnParseReply(const char *inbuf, method_t);
103static const char *const crlf = "\r\n";
104static QS url_entry_sort;
105
e6ccf245 106CBDATA_TYPE(UrnState);
107void *
06096e82 108UrnState::operator new (size_t byteCount)
e6ccf245 109{
62e76326 110 /* derived classes with different sizes must implement their own new */
e6ccf245 111 assert (byteCount == sizeof (UrnState));
112 CBDATA_INIT_TYPE(UrnState);
113 return cbdataAlloc(UrnState);
62e76326 114
e6ccf245 115}
116
117void
118UrnState::operator delete (void *address)
119{
1f1ae50a 120 UrnState * tmp = (UrnState *)address;
121 cbdataFree (tmp);
e6ccf245 122}
123
124UrnState::~UrnState ()
125{
126 safe_free(urlres);
127}
128
48ebcb22 129static url_entry *
9ce5e3e6 130urnFindMinRtt(url_entry * urls, method_t m, int *rtt_ret)
85491f8d 131{
23d92c64 132 int min_rtt = 0;
9ce5e3e6 133 url_entry *u = NULL;
134 url_entry *min_u = NULL;
135 int i;
1caf595b 136 int urlcnt = 0;
6f6f0853 137 debug(52, 3) ("urnFindMinRtt\n");
23d92c64 138 assert(urls != NULL);
62e76326 139
00141c96 140 for (i = 0; NULL != urls[i].url; i++)
62e76326 141 urlcnt++;
142
94934dbe 143 debug(53, 3) ("urnFindMinRtt: Counted %d URLs\n", i);
62e76326 144
9ce5e3e6 145 if (1 == urlcnt) {
62e76326 146 debug(52, 3) ("urnFindMinRtt: Only one URL - return it!\n");
147 return urls;
1caf595b 148 }
62e76326 149
00141c96 150 for (i = 0; i < urlcnt; i++) {
62e76326 151 u = &urls[i];
152 debug(52, 3) ("urnFindMinRtt: %s rtt=%d\n", u->host, u->rtt);
153
154 if (u->rtt == 0)
155 continue;
156
157 if (u->rtt > min_rtt && min_rtt != 0)
158 continue;
159
160 min_rtt = u->rtt;
161
162 min_u = u;
23d92c64 163 }
62e76326 164
23d92c64 165 if (rtt_ret)
62e76326 166 *rtt_ret = min_rtt;
167
6f6f0853 168 debug(52, 1) ("urnFindMinRtt: Returning '%s' RTT %d\n",
62e76326 169 min_u ? min_u->url : "NONE",
170 min_rtt);
171
9ce5e3e6 172 return min_u;
85491f8d 173}
174
e6ccf245 175char *
176UrnState::getHost (String &urlpath)
85491f8d 177{
e6ccf245 178 char * result;
179 char const *t;
62e76326 180
650c4b88 181 if ((t = urlpath.pos(':')) != NULL) {
182 urlpath.set(t, '\0');
62e76326 183 result = xstrdup(urlpath.buf());
650c4b88 184 urlpath.set(t, ':');
fa040df2 185 } else {
62e76326 186 result = xstrdup(urlpath.buf());
fa040df2 187 }
62e76326 188
e6ccf245 189 return result;
190}
191
192bool
190154cf 193UrnState::RequestNeedsMenu(HttpRequest *r)
e6ccf245 194{
528b2c61 195 return strncasecmp(r->urlpath.buf(), "menu.", 5) == 0;
e6ccf245 196}
197
198void
190154cf 199UrnState::updateRequestURL(HttpRequest *r, char const *newPath)
e6ccf245 200{
62e76326 201 char *new_path = xstrdup (newPath);
202 r->urlpath = new_path;
203 xfree(new_path);
e6ccf245 204}
205
206void
207UrnState::createUriResRequest (String &uri)
208{
209 LOCAL_ARRAY(char, local_urlres, 4096);
210 char *host = getHost (uri);
528b2c61 211 snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?urn:%s", host, uri.buf());
e6ccf245 212 safe_free (host);
213 safe_free (urlres);
214 urlres = xstrdup (local_urlres);
c21ad0f5 215 urlres_r = HttpRequest::CreateFromUrl(urlres);
e6ccf245 216}
217
218void
190154cf 219UrnState::setUriResFromRequest(HttpRequest *r)
e6ccf245 220{
221 if (RequestNeedsMenu(r)) {
62e76326 222 updateRequestURL(r, r->urlpath.buf() + 5);
223 flags.force_menu = 1;
e6ccf245 224 }
62e76326 225
e6ccf245 226 createUriResRequest (r->urlpath);
62e76326 227
0adbab7c 228 if (urlres_r == NULL) {
62e76326 229 debug(52, 3) ("urnStart: Bad uri-res URL %s\n", urlres);
230 ErrorState *err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND);
231 err->url = urlres;
6dd9f4bd 232 err->request = HTTPMSGLOCK(r);
62e76326 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{
245 debug(52, 3) ("urnStart: '%s'\n", storeUrl(e));
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",
372 storeUrl(e));
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");
384 err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND);
6dd9f4bd 385 err->request = HTTPMSGLOCK(urnState->request);
62e76326 386 err->url = xstrdup(storeUrl(e));
387 errorAppendEntry(e, err);
06a5ae20 388 delete rep;
62e76326 389 goto error;
85491f8d 390 }
62e76326 391
06a5ae20 392 delete rep;
62e76326 393
b6a2f15e 394 while (xisspace(*s))
62e76326 395 s++;
396
9ce5e3e6 397 urls = urnParseReply(s, urnState->request->method);
62e76326 398
00141c96 399 for (i = 0; NULL != urls[i].url; i++)
62e76326 400 urlcnt++;
401
94934dbe 402 debug(53, 3) ("urnFindMinRtt: Counted %d URLs\n", i);
62e76326 403
164f7660 404 if (urls == NULL) { /* unkown URN error */
62e76326 405 debug(52, 3) ("urnTranslateDone: unknown URN %s\n", storeUrl(e));
406 err = errorCon(ERR_URN_RESOLVE, HTTP_NOT_FOUND);
6dd9f4bd 407 err->request = HTTPMSGLOCK(urnState->request);
62e76326 408 err->url = xstrdup(storeUrl(e));
409 errorAppendEntry(e, err);
410 goto error;
164f7660 411 }
62e76326 412
9ce5e3e6 413 min_u = urnFindMinRtt(urls, urnState->request->method, NULL);
414 qsort(urls, urlcnt, sizeof(*urls), url_entry_sort);
cf26e54c 415 storeBuffer(e);
032785bf 416 mb = new MemBuf;
2fe7eff9 417 mb->init();
418 mb->Printf( "<TITLE>Select URL for %s</TITLE>\n"
419 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
420 "<H2>Select URL for %s</H2>\n"
421 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", storeUrl(e), storeUrl(e));
62e76326 422
9ce5e3e6 423 for (i = 0; i < urlcnt; i++) {
62e76326 424 u = &urls[i];
425 debug(52, 3) ("URL {%s}\n", u->url);
2fe7eff9 426 mb->Printf(
427 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url);
62e76326 428
429 if (urls[i].rtt > 0)
2fe7eff9 430 mb->Printf(
431 "<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt);
62e76326 432 else
2fe7eff9 433 mb->Printf("<TD align=\"right\">Unknown</TD>");
62e76326 434
2fe7eff9 435 mb->Printf(
436 "<TD>%s</TD></TR>\n", u->flags.cached ? " [cached]" : " ");
cf26e54c 437 }
62e76326 438
2fe7eff9 439 mb->Printf(
440 "</TABLE>"
441 "<HR noshade size=\"1px\">\n"
442 "<ADDRESS>\n"
443 "Generated by %s@%s\n"
444 "</ADDRESS>\n",
445 full_appname_string, getMyHostname());
06a5ae20 446 rep = new HttpReply;
447 rep->setHeaders(version, HTTP_MOVED_TEMPORARILY, NULL,
448 "text/html", mb->contentSize(), 0, squid_curtime);
62e76326 449
b515fc11 450 if (urnState->flags.force_menu) {
62e76326 451 debug(51, 3) ("urnHandleReply: forcing menu\n");
9ce5e3e6 452 } else if (min_u) {
a9925b40 453 rep->header.putStr(HDR_LOCATION, min_u->url);
cb69b4c7 454 }
62e76326 455
032785bf 456 httpBodySet(&rep->body, mb);
457 /* don't clean or delete mb; rep->body owns it now */
db237875 458 e->replaceHttpReply(rep);
528b2c61 459 e->complete();
62e76326 460
9ce5e3e6 461 for (i = 0; i < urlcnt; i++) {
62e76326 462 safe_free(urls[i].url);
463 safe_free(urls[i].host);
9ce5e3e6 464 }
62e76326 465
9ce5e3e6 466 safe_free(urls);
96a5be3d 467 /* mb was absorbed in httpBodySet call, so we must not clean it */
06d2839d 468 storeUnregister(urnState->sc, urlres_e, urnState);
62e76326 469
470error:
97b5e68f 471 urlres_e->unlock();
472 urnState->entry->unlock();
6dd9f4bd 473 HTTPMSGUNLOCK(urnState->request);
474 HTTPMSGUNLOCK(urnState->urlres_r);
e6ccf245 475 delete urnState;
85491f8d 476}
477
9ce5e3e6 478static url_entry *
479urnParseReply(const char *inbuf, method_t m)
85491f8d 480{
23d92c64 481 char *buf = xstrdup(inbuf);
482 char *token;
9ce5e3e6 483 char *url;
484 char *host;
9ce5e3e6 485 int rtt;
486 url_entry *list;
487 url_entry *old;
488 int n = 32;
489 int i = 0;
6f6f0853 490 debug(52, 3) ("urnParseReply\n");
e6ccf245 491 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
62e76326 492
23d92c64 493 for (token = strtok(buf, crlf); token; token = strtok(NULL, crlf)) {
62e76326 494 debug(52, 3) ("urnParseReply: got '%s'\n", token);
495
496 if (i == n) {
497 old = list;
498 n <<= 2;
499 list = (url_entry *)xcalloc(n + 1, sizeof(*list));
500 xmemcpy(list, old, i * sizeof(*list));
501 safe_free(old);
502 }
503
504 url = xstrdup(token);
505 host = urlHostname(url);
506
507 if (NULL == host)
508 continue;
509
510 rtt = netdbHostRtt(host);
511
512 if (0 == rtt) {
513 debug(52, 3) ("urnParseReply: Pinging %s\n", host);
514 netdbPingSite(host);
515 }
516
517 list[i].url = url;
518 list[i].host = xstrdup(host);
519 list[i].rtt = rtt;
520 list[i].flags.cached = storeGetPublic(url, m) ? 1 : 0;
521 i++;
85491f8d 522 }
62e76326 523
94934dbe 524 debug(52, 3) ("urnParseReply: Found %d URLs\n", i);
9ce5e3e6 525 return list;
85491f8d 526}