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