]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/urn.cc
2 * DEBUG: section 52 URN Parsing
3 * AUTHOR: Kostas Anagnostakis
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 #include "errorpage.h"
38 #include "HttpReply.h"
39 #include "HttpRequest.h"
40 #include "icmp/net_db.h"
42 #include "mime_header.h"
43 #include "RequestFlags.h"
44 #include "SquidTime.h"
46 #include "StoreClient.h"
51 #define URN_REQBUF_SZ 4096
53 class UrnState
: public StoreClient
57 void created (StoreEntry
*newEntry
);
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
);
70 HttpRequest::Pointer request
;
71 HttpRequest::Pointer urlres_r
;
76 char reqbuf
[URN_REQBUF_SZ
];
82 CBDATA_CLASS2(UrnState
);
95 static STCB urnHandleReply
;
96 static url_entry
*urnParseReply(const char *inbuf
, const HttpRequestMethod
&);
97 static const char *const crlf
= "\r\n";
98 static QS url_entry_sort
;
100 CBDATA_CLASS_INIT(UrnState
);
102 UrnState::~UrnState()
108 urnFindMinRtt(url_entry
* urls
, const HttpRequestMethod
& m
, int *rtt_ret
)
112 url_entry
*min_u
= NULL
;
115 debugs(52, 3, "urnFindMinRtt");
116 assert(urls
!= NULL
);
118 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
121 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
124 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
128 for (i
= 0; i
< urlcnt
; ++i
) {
130 debugs(52, 3, "urnFindMinRtt: " << u
->host
<< " rtt=" << u
->rtt
);
135 if (u
->rtt
> min_rtt
&& min_rtt
!= 0)
146 debugs(52, DBG_IMPORTANT
, "urnFindMinRtt: Returning '" <<
147 (min_u
? min_u
->url
: "NONE") << "' RTT " <<
154 UrnState::getHost (String
&urlpath
)
159 /** FIXME: this appears to be parsing the URL. *very* badly. */
160 /* a proper encapsulated URI/URL type needs to clear this up. */
161 if ((p
=urlpath
.find(':')) != String::npos
) {
162 result
=xstrndup(urlpath
.rawBuf(),p
-1);
164 result
= xstrndup(urlpath
.rawBuf(),urlpath
.size());
170 UrnState::RequestNeedsMenu(HttpRequest
*r
)
172 if (r
->urlpath
.size() < 5)
174 //now we're sure it's long enough
175 return strncasecmp(r
->urlpath
.rawBuf(), "menu.", 5) == 0;
179 UrnState::updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
)
181 char *new_path
= xstrndup (newPath
, newPath_len
);
182 r
->urlpath
= new_path
;
187 UrnState::createUriResRequest (String
&uri
)
189 LOCAL_ARRAY(char, local_urlres
, 4096);
190 char *host
= getHost (uri
);
191 snprintf(local_urlres
, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSTRINGPH
,
192 host
, SQUIDSTRINGPRINT(uri
));
195 urlres
= xstrdup(local_urlres
);
196 urlres_r
= HttpRequest::CreateFromUrl(urlres
);
200 UrnState::setUriResFromRequest(HttpRequest
*r
)
202 if (RequestNeedsMenu(r
)) {
203 updateRequestURL(r
, r
->urlpath
.rawBuf() + 5, r
->urlpath
.size() - 5 );
204 flags
.force_menu
= true;
207 createUriResRequest (r
->urlpath
);
209 if (urlres_r
== NULL
) {
210 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres
);
211 ErrorState
*err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, r
);
214 errorAppendEntry(entry
, err
);
218 urlres_r
->header
.putStr(HDR_ACCEPT
, "text/plain");
222 UrnState::start(HttpRequest
* r
, StoreEntry
* e
)
224 debugs(52, 3, "urnStart: '" << e
->url() << "'" );
228 entry
->lock("UrnState::start");
229 setUriResFromRequest(r
);
231 if (urlres_r
== NULL
)
234 StoreEntry::getPublic (this, urlres
, Http::METHOD_GET
);
238 UrnState::created(StoreEntry
*newEntry
)
242 if (urlres_e
->isNull()) {
243 urlres_e
= storeCreateEntry(urlres
, urlres
, RequestFlags(), Http::METHOD_GET
);
244 sc
= storeClientListAdd(urlres_e
, this);
245 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e
, urlres_r
.getRaw());
247 urlres_e
->lock("UrnState::created");
248 sc
= storeClientListAdd(urlres_e
, this);
252 StoreIOBuffer tempBuffer
;
253 tempBuffer
.offset
= reqofs
;
254 tempBuffer
.length
= URN_REQBUF_SZ
;
255 tempBuffer
.data
= reqbuf
;
256 storeClientCopy(sc
, urlres_e
,
263 urnStart(HttpRequest
* r
, StoreEntry
* e
)
265 UrnState
*anUrn
= new UrnState();
270 url_entry_sort(const void *A
, const void *B
)
272 const url_entry
*u1
= (const url_entry
*)A
;
273 const url_entry
*u2
= (const url_entry
*)B
;
275 if (u2
->rtt
== u1
->rtt
)
277 else if (0 == u1
->rtt
)
279 else if (0 == u2
->rtt
)
282 return u1
->rtt
- u2
->rtt
;
286 urnHandleReplyError(UrnState
*urnState
, StoreEntry
*urlres_e
)
288 urlres_e
->unlock("urnHandleReplyError+res");
289 urnState
->entry
->unlock("urnHandleReplyError+prime");
293 /* TODO: use the clientStream support for this */
295 urnHandleReply(void *data
, StoreIOBuffer result
)
297 UrnState
*urnState
= static_cast<UrnState
*>(data
);
298 StoreEntry
*e
= urnState
->entry
;
299 StoreEntry
*urlres_e
= urnState
->urlres_e
;
310 char *buf
= urnState
->reqbuf
;
311 StoreIOBuffer tempBuffer
;
313 debugs(52, 3, "urnHandleReply: Called with size=" << result
.length
<< ".");
315 if (EBIT_TEST(urlres_e
->flags
, ENTRY_ABORTED
) || result
.length
== 0 || result
.flags
.error
) {
316 urnHandleReplyError(urnState
, urlres_e
);
320 /* Update reqofs to point to where in the buffer we'd be */
321 urnState
->reqofs
+= result
.length
;
323 /* Handle reqofs being bigger than normal */
324 if (urnState
->reqofs
>= URN_REQBUF_SZ
) {
325 urnHandleReplyError(urnState
, urlres_e
);
329 /* If we haven't received the entire object (urn), copy more */
330 if (urlres_e
->store_status
== STORE_PENDING
&&
331 urnState
->reqofs
< URN_REQBUF_SZ
) {
332 tempBuffer
.offset
= urnState
->reqofs
;
333 tempBuffer
.length
= URN_REQBUF_SZ
;
334 tempBuffer
.data
= urnState
->reqbuf
+ urnState
->reqofs
;
335 storeClientCopy(urnState
->sc
, urlres_e
,
342 /* we know its STORE_OK */
343 k
= headersEnd(buf
, urnState
->reqofs
);
346 debugs(52, DBG_IMPORTANT
, "urnHandleReply: didn't find end-of-headers for " << e
->url() );
347 urnHandleReplyError(urnState
, urlres_e
);
352 assert(urlres_e
->getReply());
354 rep
->parseCharBuf(buf
, k
);
355 debugs(52, 3, "reply exists, code=" << rep
->sline
.status() << ".");
357 if (rep
->sline
.status() != Http::scOkay
) {
358 debugs(52, 3, "urnHandleReply: failed.");
359 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
360 err
->url
= xstrdup(e
->url());
361 errorAppendEntry(e
, err
);
363 urnHandleReplyError(urnState
, urlres_e
);
372 urls
= urnParseReply(s
, urnState
->request
->method
);
374 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
377 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
379 if (urls
== NULL
) { /* unkown URN error */
380 debugs(52, 3, "urnTranslateDone: unknown URN " << e
->url());
381 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
382 err
->url
= xstrdup(e
->url());
383 errorAppendEntry(e
, err
);
384 urnHandleReplyError(urnState
, urlres_e
);
388 min_u
= urnFindMinRtt(urls
, urnState
->request
->method
, NULL
);
389 qsort(urls
, urlcnt
, sizeof(*urls
), url_entry_sort
);
393 mb
->Printf( "<TITLE>Select URL for %s</TITLE>\n"
394 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
395 "<H2>Select URL for %s</H2>\n"
396 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e
->url(), e
->url());
398 for (i
= 0; i
< urlcnt
; ++i
) {
400 debugs(52, 3, "URL {" << u
->url
<< "}");
402 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u
->url
, u
->url
);
406 "<TD align=\"right\">%4d <it>ms</it></TD>", u
->rtt
);
408 mb
->Printf("<TD align=\"right\">Unknown</TD>");
411 "<TD>%s</TD></TR>\n", u
->flags
.cached
? " [cached]" : " ");
416 "<HR noshade size=\"1px\">\n"
418 "Generated by %s@%s\n"
420 APP_FULLNAME
, getMyHostname());
422 rep
->setHeaders(Http::scFound
, NULL
, "text/html", mb
->contentSize(), 0, squid_curtime
);
424 if (urnState
->flags
.force_menu
) {
425 debugs(51, 3, "urnHandleReply: forcing menu");
427 rep
->header
.putStr(HDR_LOCATION
, min_u
->url
);
431 /* don't clean or delete mb; rep->body owns it now */
432 e
->replaceHttpReply(rep
);
435 for (i
= 0; i
< urlcnt
; ++i
) {
436 safe_free(urls
[i
].url
);
437 safe_free(urls
[i
].host
);
441 /* mb was absorbed in httpBodySet call, so we must not clean it */
442 storeUnregister(urnState
->sc
, urlres_e
, urnState
);
444 urnHandleReplyError(urnState
, urlres_e
);
448 urnParseReply(const char *inbuf
, const HttpRequestMethod
& m
)
450 char *buf
= xstrdup(inbuf
);
458 debugs(52, 3, "urnParseReply");
459 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
461 for (token
= strtok(buf
, crlf
); token
; token
= strtok(NULL
, crlf
)) {
462 debugs(52, 3, "urnParseReply: got '" << token
<< "'");
467 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
468 memcpy(list
, old
, i
* sizeof(*list
));
472 url
= xstrdup(token
);
473 host
= urlHostname(url
);
479 list
[i
].rtt
= netdbHostRtt(host
);
481 if (0 == list
[i
].rtt
) {
482 debugs(52, 3, "urnParseReply: Pinging " << host
);
490 list
[i
].host
= xstrdup(host
);
491 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
493 list
[i
].flags
.cached
= storeGetPublic(url
, m
) ? 1 : 0;
497 debugs(52, 3, "urnParseReply: Found " << i
<< " URLs");