]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/urn.cc
5 * DEBUG: section 52 URN Parsing
6 * AUTHOR: Kostas Anagnostakis
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
37 #include "errorpage.h"
40 #include "HttpReply.h"
41 #include "HttpRequest.h"
42 #include "icmp/net_db.h"
44 #include "mime_header.h"
46 #include "SquidTime.h"
48 #include "StoreClient.h"
52 #define URN_REQBUF_SZ 4096
54 class UrnState
: public StoreClient
58 void created (StoreEntry
*newEntry
);
59 void *operator new (size_t byteCount
);
60 void operator delete (void *address
);
61 void start (HttpRequest
*, StoreEntry
*);
62 char *getHost (String
&urlpath
);
63 void setUriResFromRequest(HttpRequest
*);
64 bool RequestNeedsMenu(HttpRequest
*r
);
65 void updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
);
66 void createUriResRequest (String
&uri
);
74 HttpRequest
*urlres_r
;
77 unsigned int force_menu
:1;
79 char reqbuf
[URN_REQBUF_SZ
];
96 static STCB urnHandleReply
;
97 static url_entry
*urnParseReply(const char *inbuf
, const HttpRequestMethod
&);
98 static const char *const crlf
= "\r\n";
99 static QS url_entry_sort
;
101 CBDATA_TYPE(UrnState
);
103 UrnState::operator new (size_t byteCount
)
105 /* derived classes with different sizes must implement their own new */
106 assert (byteCount
== sizeof (UrnState
));
107 CBDATA_INIT_TYPE(UrnState
);
108 return cbdataAlloc(UrnState
);
113 UrnState::operator delete (void *address
)
115 UrnState
* tmp
= (UrnState
*)address
;
119 UrnState::~UrnState ()
125 urnFindMinRtt(url_entry
* urls
, const HttpRequestMethod
& m
, int *rtt_ret
)
129 url_entry
*min_u
= NULL
;
132 debugs(52, 3, "urnFindMinRtt");
133 assert(urls
!= NULL
);
135 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
138 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
141 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
145 for (i
= 0; i
< urlcnt
; ++i
) {
147 debugs(52, 3, "urnFindMinRtt: " << u
->host
<< " rtt=" << u
->rtt
);
152 if (u
->rtt
> min_rtt
&& min_rtt
!= 0)
163 debugs(52, DBG_IMPORTANT
, "urnFindMinRtt: Returning '" <<
164 (min_u
? min_u
->url
: "NONE") << "' RTT " <<
171 UrnState::getHost (String
&urlpath
)
176 /** FIXME: this appears to be parsing the URL. *very* badly. */
177 /* a proper encapsulated URI/URL type needs to clear this up. */
178 if ((p
=urlpath
.find(':')) != String::npos
) {
179 result
=xstrndup(urlpath
.rawBuf(),p
-1);
181 result
= xstrndup(urlpath
.rawBuf(),urlpath
.size());
187 UrnState::RequestNeedsMenu(HttpRequest
*r
)
189 if (r
->urlpath
.size() < 5)
191 //now we're sure it's long enough
192 return strncasecmp(r
->urlpath
.rawBuf(), "menu.", 5) == 0;
196 UrnState::updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
)
198 char *new_path
= xstrndup (newPath
, newPath_len
);
199 r
->urlpath
= new_path
;
204 UrnState::createUriResRequest (String
&uri
)
206 LOCAL_ARRAY(char, local_urlres
, 4096);
207 char *host
= getHost (uri
);
208 snprintf(local_urlres
, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSTRINGPH
,
209 host
, SQUIDSTRINGPRINT(uri
));
212 urlres
= xstrdup (local_urlres
);
213 urlres_r
= HttpRequest::CreateFromUrl(urlres
);
217 UrnState::setUriResFromRequest(HttpRequest
*r
)
219 if (RequestNeedsMenu(r
)) {
220 updateRequestURL(r
, r
->urlpath
.rawBuf() + 5, r
->urlpath
.size() - 5 );
221 flags
.force_menu
= 1;
224 createUriResRequest (r
->urlpath
);
226 if (urlres_r
== NULL
) {
227 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres
);
228 ErrorState
*err
= new ErrorState(ERR_URN_RESOLVE
, HTTP_NOT_FOUND
, r
);
231 errorAppendEntry(entry
, err
);
235 HTTPMSGLOCK(urlres_r
);
236 urlres_r
->header
.putStr(HDR_ACCEPT
, "text/plain");
240 UrnState::start(HttpRequest
* r
, StoreEntry
* e
)
242 debugs(52, 3, "urnStart: '" << e
->url() << "'" );
244 request
= HTTPMSGLOCK(r
);
247 setUriResFromRequest(r
);
249 if (urlres_r
== NULL
)
252 StoreEntry::getPublic (this, urlres
, METHOD_GET
);
256 UrnState::created(StoreEntry
*newEntry
)
260 if (urlres_e
->isNull()) {
261 urlres_e
= storeCreateEntry(urlres
, urlres
, request_flags(), METHOD_GET
);
262 sc
= storeClientListAdd(urlres_e
, this);
263 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e
, urlres_r
);
267 sc
= storeClientListAdd(urlres_e
, this);
271 StoreIOBuffer tempBuffer
;
272 tempBuffer
.offset
= reqofs
;
273 tempBuffer
.length
= URN_REQBUF_SZ
;
274 tempBuffer
.data
= reqbuf
;
275 storeClientCopy(sc
, urlres_e
,
282 urnStart(HttpRequest
* r
, StoreEntry
* e
)
284 UrnState
*anUrn
= new UrnState();
289 url_entry_sort(const void *A
, const void *B
)
291 const url_entry
*u1
= (const url_entry
*)A
;
292 const url_entry
*u2
= (const url_entry
*)B
;
294 if (u2
->rtt
== u1
->rtt
)
296 else if (0 == u1
->rtt
)
298 else if (0 == u2
->rtt
)
301 return u1
->rtt
- u2
->rtt
;
305 urnHandleReplyError(UrnState
*urnState
, StoreEntry
*urlres_e
)
308 urnState
->entry
->unlock();
309 HTTPMSGUNLOCK(urnState
->request
);
310 HTTPMSGUNLOCK(urnState
->urlres_r
);
314 /* TODO: use the clientStream support for this */
316 urnHandleReply(void *data
, StoreIOBuffer result
)
318 UrnState
*urnState
= static_cast<UrnState
*>(data
);
319 StoreEntry
*e
= urnState
->entry
;
320 StoreEntry
*urlres_e
= urnState
->urlres_e
;
331 char *buf
= urnState
->reqbuf
;
332 StoreIOBuffer tempBuffer
;
334 debugs(52, 3, "urnHandleReply: Called with size=" << result
.length
<< ".");
336 if (EBIT_TEST(urlres_e
->flags
, ENTRY_ABORTED
) || result
.length
== 0 || result
.flags
.error
) {
337 urnHandleReplyError(urnState
, urlres_e
);
341 /* Update reqofs to point to where in the buffer we'd be */
342 urnState
->reqofs
+= result
.length
;
344 /* Handle reqofs being bigger than normal */
345 if (urnState
->reqofs
>= URN_REQBUF_SZ
) {
346 urnHandleReplyError(urnState
, urlres_e
);
350 /* If we haven't received the entire object (urn), copy more */
351 if (urlres_e
->store_status
== STORE_PENDING
&&
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
,
363 /* we know its STORE_OK */
364 k
= headersEnd(buf
, urnState
->reqofs
);
367 debugs(52, DBG_IMPORTANT
, "urnHandleReply: didn't find end-of-headers for " << e
->url() );
368 urnHandleReplyError(urnState
, urlres_e
);
373 assert(urlres_e
->getReply());
375 rep
->parseCharBuf(buf
, k
);
376 debugs(52, 3, "reply exists, code=" << rep
->sline
.status
<< ".");
378 if (rep
->sline
.status
!= HTTP_OK
) {
379 debugs(52, 3, "urnHandleReply: failed.");
380 err
= new ErrorState(ERR_URN_RESOLVE
, HTTP_NOT_FOUND
, urnState
->request
);
381 err
->url
= xstrdup(e
->url());
382 errorAppendEntry(e
, err
);
384 urnHandleReplyError(urnState
, urlres_e
);
393 urls
= urnParseReply(s
, urnState
->request
->method
);
395 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
398 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
400 if (urls
== NULL
) { /* unkown URN error */
401 debugs(52, 3, "urnTranslateDone: unknown URN " << e
->url() );
402 err
= new ErrorState(ERR_URN_RESOLVE
, HTTP_NOT_FOUND
, urnState
->request
);
403 err
->url
= xstrdup(e
->url());
404 errorAppendEntry(e
, err
);
405 urnHandleReplyError(urnState
, urlres_e
);
409 min_u
= urnFindMinRtt(urls
, urnState
->request
->method
, NULL
);
410 qsort(urls
, urlcnt
, sizeof(*urls
), url_entry_sort
);
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"
417 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e
->url(), e
->url());
419 for (i
= 0; i
< urlcnt
; ++i
) {
421 debugs(52, 3, "URL {" << u
->url
<< "}");
423 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u
->url
, u
->url
);
427 "<TD align=\"right\">%4d <it>ms</it></TD>", u
->rtt
);
429 mb
->Printf("<TD align=\"right\">Unknown</TD>");
432 "<TD>%s</TD></TR>\n", u
->flags
.cached
? " [cached]" : " ");
437 "<HR noshade size=\"1px\">\n"
439 "Generated by %s@%s\n"
441 APP_FULLNAME
, getMyHostname());
443 rep
->setHeaders(HTTP_MOVED_TEMPORARILY
, NULL
, "text/html", mb
->contentSize(), 0, squid_curtime
);
445 if (urnState
->flags
.force_menu
) {
446 debugs(51, 3, "urnHandleReply: forcing menu");
448 rep
->header
.putStr(HDR_LOCATION
, min_u
->url
);
452 /* don't clean or delete mb; rep->body owns it now */
453 e
->replaceHttpReply(rep
);
456 for (i
= 0; i
< urlcnt
; ++i
) {
457 safe_free(urls
[i
].url
);
458 safe_free(urls
[i
].host
);
462 /* mb was absorbed in httpBodySet call, so we must not clean it */
463 storeUnregister(urnState
->sc
, urlres_e
, urnState
);
465 urnHandleReplyError(urnState
, urlres_e
);
469 urnParseReply(const char *inbuf
, const HttpRequestMethod
& m
)
471 char *buf
= xstrdup(inbuf
);
479 debugs(52, 3, "urnParseReply");
480 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
482 for (token
= strtok(buf
, crlf
); token
; token
= strtok(NULL
, crlf
)) {
483 debugs(52, 3, "urnParseReply: got '" << token
<< "'");
488 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
489 memcpy(list
, old
, i
* sizeof(*list
));
493 url
= xstrdup(token
);
494 host
= urlHostname(url
);
500 list
[i
].rtt
= netdbHostRtt(host
);
502 if (0 == list
[i
].rtt
) {
503 debugs(52, 3, "urnParseReply: Pinging " << host
);
511 list
[i
].host
= xstrdup(host
);
512 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
514 list
[i
].flags
.cached
= storeGetPublic(url
, m
) ? 1 : 0;
518 debugs(52, 3, "urnParseReply: Found " << i
<< " URLs");