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