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