]>
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.
36 #include "squid-old.h"
37 #include "errorpage.h"
38 #include "StoreClient.h"
40 #include "HttpReply.h"
41 #include "HttpRequest.h"
44 #include "SquidTime.h"
45 #include "icmp/net_db.h"
47 #define URN_REQBUF_SZ 4096
49 class UrnState
: public StoreClient
53 void created (StoreEntry
*newEntry
);
54 void *operator new (size_t byteCount
);
55 void operator delete (void *address
);
56 void start (HttpRequest
*, StoreEntry
*);
57 char *getHost (String
&urlpath
);
58 void setUriResFromRequest(HttpRequest
*);
59 bool RequestNeedsMenu(HttpRequest
*r
);
60 void updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
);
61 void createUriResRequest (String
&uri
);
70 HttpRequest
*urlres_r
;
73 unsigned int force_menu
:1;
75 char reqbuf
[URN_REQBUF_SZ
];
92 static STCB urnHandleReply
;
93 static url_entry
*urnParseReply(const char *inbuf
, const HttpRequestMethod
&);
94 static const char *const crlf
= "\r\n";
95 static QS url_entry_sort
;
97 CBDATA_TYPE(UrnState
);
99 UrnState::operator new (size_t byteCount
)
101 /* derived classes with different sizes must implement their own new */
102 assert (byteCount
== sizeof (UrnState
));
103 CBDATA_INIT_TYPE(UrnState
);
104 return cbdataAlloc(UrnState
);
109 UrnState::operator delete (void *address
)
111 UrnState
* tmp
= (UrnState
*)address
;
115 UrnState::~UrnState ()
121 urnFindMinRtt(url_entry
* urls
, const HttpRequestMethod
& m
, int *rtt_ret
)
125 url_entry
*min_u
= NULL
;
128 debugs(52, 3, "urnFindMinRtt");
129 assert(urls
!= NULL
);
131 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
134 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
137 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
141 for (i
= 0; i
< urlcnt
; ++i
) {
143 debugs(52, 3, "urnFindMinRtt: " << u
->host
<< " rtt=" << u
->rtt
);
148 if (u
->rtt
> min_rtt
&& min_rtt
!= 0)
159 debugs(52, 1, "urnFindMinRtt: Returning '" <<
160 (min_u
? min_u
->url
: "NONE") << "' RTT " <<
167 UrnState::getHost (String
&urlpath
)
172 /** FIXME: this appears to be parsing the URL. *very* badly. */
173 /* a proper encapsulated URI/URL type needs to clear this up. */
174 if ((p
=urlpath
.find(':')) != String::npos
) {
175 result
=xstrndup(urlpath
.rawBuf(),p
-1);
177 result
= xstrndup(urlpath
.rawBuf(),urlpath
.size());
183 UrnState::RequestNeedsMenu(HttpRequest
*r
)
185 if (r
->urlpath
.size() < 5)
187 //now we're sure it's long enough
188 return strncasecmp(r
->urlpath
.rawBuf(), "menu.", 5) == 0;
192 UrnState::updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
)
194 char *new_path
= xstrndup (newPath
, newPath_len
);
195 r
->urlpath
= new_path
;
200 UrnState::createUriResRequest (String
&uri
)
202 LOCAL_ARRAY(char, local_urlres
, 4096);
203 char *host
= getHost (uri
);
204 snprintf(local_urlres
, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSTRINGPH
,
205 host
, SQUIDSTRINGPRINT(uri
));
208 urlres
= xstrdup (local_urlres
);
209 urlres_r
= HttpRequest::CreateFromUrl(urlres
);
213 UrnState::setUriResFromRequest(HttpRequest
*r
)
215 if (RequestNeedsMenu(r
)) {
216 updateRequestURL(r
, r
->urlpath
.rawBuf() + 5, r
->urlpath
.size() - 5 );
217 flags
.force_menu
= 1;
220 createUriResRequest (r
->urlpath
);
222 if (urlres_r
== NULL
) {
223 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres
);
224 ErrorState
*err
= new ErrorState(ERR_URN_RESOLVE
, HTTP_NOT_FOUND
, r
);
227 errorAppendEntry(entry
, err
);
231 HTTPMSGLOCK(urlres_r
);
232 urlres_r
->header
.putStr(HDR_ACCEPT
, "text/plain");
236 UrnState::start(HttpRequest
* r
, StoreEntry
* e
)
238 debugs(52, 3, "urnStart: '" << e
->url() << "'" );
240 request
= HTTPMSGLOCK(r
);
243 setUriResFromRequest(r
);
245 if (urlres_r
== NULL
)
248 StoreEntry::getPublic (this, urlres
, METHOD_GET
);
252 UrnState::created(StoreEntry
*newEntry
)
256 if (urlres_e
->isNull()) {
257 urlres_e
= storeCreateEntry(urlres
, urlres
, request_flags(), METHOD_GET
);
258 sc
= storeClientListAdd(urlres_e
, this);
259 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e
, urlres_r
);
263 sc
= storeClientListAdd(urlres_e
, this);
267 StoreIOBuffer tempBuffer
;
268 tempBuffer
.offset
= reqofs
;
269 tempBuffer
.length
= URN_REQBUF_SZ
;
270 tempBuffer
.data
= reqbuf
;
271 storeClientCopy(sc
, urlres_e
,
278 urnStart(HttpRequest
* r
, StoreEntry
* e
)
280 UrnState
*anUrn
= new UrnState();
285 url_entry_sort(const void *A
, const void *B
)
287 const url_entry
*u1
= (const url_entry
*)A
;
288 const url_entry
*u2
= (const url_entry
*)B
;
290 if (u2
->rtt
== u1
->rtt
)
292 else if (0 == u1
->rtt
)
294 else if (0 == u2
->rtt
)
297 return u1
->rtt
- u2
->rtt
;
301 urnHandleReplyError(UrnState
*urnState
, StoreEntry
*urlres_e
)
304 urnState
->entry
->unlock();
305 HTTPMSGUNLOCK(urnState
->request
);
306 HTTPMSGUNLOCK(urnState
->urlres_r
);
310 /* TODO: use the clientStream support for this */
312 urnHandleReply(void *data
, StoreIOBuffer result
)
314 UrnState
*urnState
= static_cast<UrnState
*>(data
);
315 StoreEntry
*e
= urnState
->entry
;
316 StoreEntry
*urlres_e
= urnState
->urlres_e
;
327 char *buf
= urnState
->reqbuf
;
328 StoreIOBuffer tempBuffer
;
330 debugs(52, 3, "urnHandleReply: Called with size=" << result
.length
<< ".");
332 if (EBIT_TEST(urlres_e
->flags
, ENTRY_ABORTED
) || result
.length
== 0 || result
.flags
.error
) {
333 urnHandleReplyError(urnState
, urlres_e
);
337 /* Update reqofs to point to where in the buffer we'd be */
338 urnState
->reqofs
+= result
.length
;
340 /* Handle reqofs being bigger than normal */
341 if (urnState
->reqofs
>= URN_REQBUF_SZ
) {
342 urnHandleReplyError(urnState
, urlres_e
);
346 /* If we haven't received the entire object (urn), copy more */
347 if (urlres_e
->store_status
== STORE_PENDING
&&
348 urnState
->reqofs
< URN_REQBUF_SZ
) {
349 tempBuffer
.offset
= urnState
->reqofs
;
350 tempBuffer
.length
= URN_REQBUF_SZ
;
351 tempBuffer
.data
= urnState
->reqbuf
+ urnState
->reqofs
;
352 storeClientCopy(urnState
->sc
, urlres_e
,
359 /* we know its STORE_OK */
360 k
= headersEnd(buf
, urnState
->reqofs
);
363 debugs(52, 1, "urnHandleReply: didn't find end-of-headers for " << e
->url() );
364 urnHandleReplyError(urnState
, urlres_e
);
369 assert(urlres_e
->getReply());
371 rep
->parseCharBuf(buf
, k
);
372 debugs(52, 3, "reply exists, code=" << rep
->sline
.status
<< ".");
374 if (rep
->sline
.status
!= HTTP_OK
) {
375 debugs(52, 3, "urnHandleReply: failed.");
376 err
= new ErrorState(ERR_URN_RESOLVE
, HTTP_NOT_FOUND
, urnState
->request
);
377 err
->url
= xstrdup(e
->url());
378 errorAppendEntry(e
, err
);
380 urnHandleReplyError(urnState
, urlres_e
);
389 urls
= urnParseReply(s
, urnState
->request
->method
);
391 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
394 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
396 if (urls
== NULL
) { /* unkown URN error */
397 debugs(52, 3, "urnTranslateDone: unknown URN " << e
->url() );
398 err
= new ErrorState(ERR_URN_RESOLVE
, HTTP_NOT_FOUND
, urnState
->request
);
399 err
->url
= xstrdup(e
->url());
400 errorAppendEntry(e
, err
);
401 urnHandleReplyError(urnState
, urlres_e
);
405 min_u
= urnFindMinRtt(urls
, urnState
->request
->method
, NULL
);
406 qsort(urls
, urlcnt
, sizeof(*urls
), url_entry_sort
);
410 mb
->Printf( "<TITLE>Select URL for %s</TITLE>\n"
411 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
412 "<H2>Select URL for %s</H2>\n"
413 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e
->url(), e
->url());
415 for (i
= 0; i
< urlcnt
; ++i
) {
417 debugs(52, 3, "URL {" << u
->url
<< "}");
419 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u
->url
, u
->url
);
423 "<TD align=\"right\">%4d <it>ms</it></TD>", u
->rtt
);
425 mb
->Printf("<TD align=\"right\">Unknown</TD>");
428 "<TD>%s</TD></TR>\n", u
->flags
.cached
? " [cached]" : " ");
433 "<HR noshade size=\"1px\">\n"
435 "Generated by %s@%s\n"
437 APP_FULLNAME
, getMyHostname());
439 rep
->setHeaders(HTTP_MOVED_TEMPORARILY
, NULL
, "text/html", mb
->contentSize(), 0, squid_curtime
);
441 if (urnState
->flags
.force_menu
) {
442 debugs(51, 3, "urnHandleReply: forcing menu");
444 rep
->header
.putStr(HDR_LOCATION
, min_u
->url
);
448 /* don't clean or delete mb; rep->body owns it now */
449 e
->replaceHttpReply(rep
);
452 for (i
= 0; i
< urlcnt
; ++i
) {
453 safe_free(urls
[i
].url
);
454 safe_free(urls
[i
].host
);
458 /* mb was absorbed in httpBodySet call, so we must not clean it */
459 storeUnregister(urnState
->sc
, urlres_e
, urnState
);
461 urnHandleReplyError(urnState
, urlres_e
);
465 urnParseReply(const char *inbuf
, const HttpRequestMethod
& m
)
467 char *buf
= xstrdup(inbuf
);
475 debugs(52, 3, "urnParseReply");
476 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
478 for (token
= strtok(buf
, crlf
); token
; token
= strtok(NULL
, crlf
)) {
479 debugs(52, 3, "urnParseReply: got '" << token
<< "'");
484 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
485 memcpy(list
, old
, i
* sizeof(*list
));
489 url
= xstrdup(token
);
490 host
= urlHostname(url
);
496 list
[i
].rtt
= netdbHostRtt(host
);
498 if (0 == list
[i
].rtt
) {
499 debugs(52, 3, "urnParseReply: Pinging " << host
);
507 list
[i
].host
= xstrdup(host
);
508 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
510 list
[i
].flags
.cached
= storeGetPublic(url
, m
) ? 1 : 0;
514 debugs(52, 3, "urnParseReply: Found " << i
<< " URLs");