]>
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.
34 #include "errorpage.h"
37 #include "HttpReply.h"
38 #include "HttpRequest.h"
39 #include "icmp/net_db.h"
41 #include "mime_header.h"
42 #include "RequestFlags.h"
43 #include "SquidTime.h"
45 #include "StoreClient.h"
50 #define URN_REQBUF_SZ 4096
52 class UrnState
: public StoreClient
56 void created (StoreEntry
*newEntry
);
57 void *operator new (size_t byteCount
);
58 void operator delete (void *address
);
59 void start (HttpRequest
*, StoreEntry
*);
60 char *getHost (String
&urlpath
);
61 void setUriResFromRequest(HttpRequest
*);
62 bool RequestNeedsMenu(HttpRequest
*r
);
63 void updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
);
64 void createUriResRequest (String
&uri
);
71 HttpRequest::Pointer request
;
72 HttpRequest::Pointer urlres_r
;
77 char reqbuf
[URN_REQBUF_SZ
];
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
;
99 CBDATA_TYPE(UrnState
);
101 UrnState::operator new (size_t byteCount
)
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
);
111 UrnState::operator delete (void *address
)
113 UrnState
* tmp
= (UrnState
*)address
;
117 UrnState::~UrnState ()
123 urnFindMinRtt(url_entry
* urls
, const HttpRequestMethod
& m
, int *rtt_ret
)
127 url_entry
*min_u
= NULL
;
130 debugs(52, 3, "urnFindMinRtt");
131 assert(urls
!= NULL
);
133 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
136 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
139 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
143 for (i
= 0; i
< urlcnt
; ++i
) {
145 debugs(52, 3, "urnFindMinRtt: " << u
->host
<< " rtt=" << u
->rtt
);
150 if (u
->rtt
> min_rtt
&& min_rtt
!= 0)
161 debugs(52, DBG_IMPORTANT
, "urnFindMinRtt: Returning '" <<
162 (min_u
? min_u
->url
: "NONE") << "' RTT " <<
169 UrnState::getHost (String
&urlpath
)
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);
179 result
= xstrndup(urlpath
.rawBuf(),urlpath
.size());
185 UrnState::RequestNeedsMenu(HttpRequest
*r
)
187 if (r
->urlpath
.size() < 5)
189 //now we're sure it's long enough
190 return strncasecmp(r
->urlpath
.rawBuf(), "menu.", 5) == 0;
194 UrnState::updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
)
196 char *new_path
= xstrndup (newPath
, newPath_len
);
197 r
->urlpath
= new_path
;
202 UrnState::createUriResRequest (String
&uri
)
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
));
210 urlres
= xstrdup (local_urlres
);
211 urlres_r
= HttpRequest::CreateFromUrl(urlres
);
215 UrnState::setUriResFromRequest(HttpRequest
*r
)
217 if (RequestNeedsMenu(r
)) {
218 updateRequestURL(r
, r
->urlpath
.rawBuf() + 5, r
->urlpath
.size() - 5 );
219 flags
.force_menu
= true;
222 createUriResRequest (r
->urlpath
);
224 if (urlres_r
== NULL
) {
225 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres
);
226 ErrorState
*err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, r
);
229 errorAppendEntry(entry
, err
);
233 urlres_r
->header
.putStr(HDR_ACCEPT
, "text/plain");
237 UrnState::start(HttpRequest
* r
, StoreEntry
* e
)
239 debugs(52, 3, "urnStart: '" << e
->url() << "'" );
244 setUriResFromRequest(r
);
246 if (urlres_r
== NULL
)
249 StoreEntry::getPublic (this, urlres
, Http::METHOD_GET
);
253 UrnState::created(StoreEntry
*newEntry
)
257 if (urlres_e
->isNull()) {
258 urlres_e
= storeCreateEntry(urlres
, urlres
, RequestFlags(), Http::METHOD_GET
);
259 sc
= storeClientListAdd(urlres_e
, this);
260 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e
, urlres_r
.getRaw());
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();
308 /* TODO: use the clientStream support for this */
310 urnHandleReply(void *data
, StoreIOBuffer result
)
312 UrnState
*urnState
= static_cast<UrnState
*>(data
);
313 StoreEntry
*e
= urnState
->entry
;
314 StoreEntry
*urlres_e
= urnState
->urlres_e
;
325 char *buf
= urnState
->reqbuf
;
326 StoreIOBuffer tempBuffer
;
328 debugs(52, 3, "urnHandleReply: Called with size=" << result
.length
<< ".");
330 if (EBIT_TEST(urlres_e
->flags
, ENTRY_ABORTED
) || result
.length
== 0 || result
.flags
.error
) {
331 urnHandleReplyError(urnState
, urlres_e
);
335 /* Update reqofs to point to where in the buffer we'd be */
336 urnState
->reqofs
+= result
.length
;
338 /* Handle reqofs being bigger than normal */
339 if (urnState
->reqofs
>= URN_REQBUF_SZ
) {
340 urnHandleReplyError(urnState
, urlres_e
);
344 /* If we haven't received the entire object (urn), copy more */
345 if (urlres_e
->store_status
== STORE_PENDING
&&
346 urnState
->reqofs
< URN_REQBUF_SZ
) {
347 tempBuffer
.offset
= urnState
->reqofs
;
348 tempBuffer
.length
= URN_REQBUF_SZ
;
349 tempBuffer
.data
= urnState
->reqbuf
+ urnState
->reqofs
;
350 storeClientCopy(urnState
->sc
, urlres_e
,
357 /* we know its STORE_OK */
358 k
= headersEnd(buf
, urnState
->reqofs
);
361 debugs(52, DBG_IMPORTANT
, "urnHandleReply: didn't find end-of-headers for " << e
->url() );
362 urnHandleReplyError(urnState
, urlres_e
);
367 assert(urlres_e
->getReply());
369 rep
->parseCharBuf(buf
, k
);
370 debugs(52, 3, "reply exists, code=" << rep
->sline
.status() << ".");
372 if (rep
->sline
.status() != Http::scOkay
) {
373 debugs(52, 3, "urnHandleReply: failed.");
374 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
375 err
->url
= xstrdup(e
->url());
376 errorAppendEntry(e
, err
);
378 urnHandleReplyError(urnState
, urlres_e
);
387 urls
= urnParseReply(s
, urnState
->request
->method
);
389 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
392 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
394 if (urls
== NULL
) { /* unkown URN error */
395 debugs(52, 3, "urnTranslateDone: unknown URN " << e
->url());
396 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
397 err
->url
= xstrdup(e
->url());
398 errorAppendEntry(e
, err
);
399 urnHandleReplyError(urnState
, urlres_e
);
403 min_u
= urnFindMinRtt(urls
, urnState
->request
->method
, NULL
);
404 qsort(urls
, urlcnt
, sizeof(*urls
), url_entry_sort
);
408 mb
->Printf( "<TITLE>Select URL for %s</TITLE>\n"
409 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
410 "<H2>Select URL for %s</H2>\n"
411 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e
->url(), e
->url());
413 for (i
= 0; i
< urlcnt
; ++i
) {
415 debugs(52, 3, "URL {" << u
->url
<< "}");
417 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u
->url
, u
->url
);
421 "<TD align=\"right\">%4d <it>ms</it></TD>", u
->rtt
);
423 mb
->Printf("<TD align=\"right\">Unknown</TD>");
426 "<TD>%s</TD></TR>\n", u
->flags
.cached
? " [cached]" : " ");
431 "<HR noshade size=\"1px\">\n"
433 "Generated by %s@%s\n"
435 APP_FULLNAME
, getMyHostname());
437 rep
->setHeaders(Http::scMovedTemporarily
, NULL
, "text/html", mb
->contentSize(), 0, squid_curtime
);
439 if (urnState
->flags
.force_menu
) {
440 debugs(51, 3, "urnHandleReply: forcing menu");
442 rep
->header
.putStr(HDR_LOCATION
, min_u
->url
);
446 /* don't clean or delete mb; rep->body owns it now */
447 e
->replaceHttpReply(rep
);
450 for (i
= 0; i
< urlcnt
; ++i
) {
451 safe_free(urls
[i
].url
);
452 safe_free(urls
[i
].host
);
456 /* mb was absorbed in httpBodySet call, so we must not clean it */
457 storeUnregister(urnState
->sc
, urlres_e
, urnState
);
459 urnHandleReplyError(urnState
, urlres_e
);
463 urnParseReply(const char *inbuf
, const HttpRequestMethod
& m
)
465 char *buf
= xstrdup(inbuf
);
473 debugs(52, 3, "urnParseReply");
474 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
476 for (token
= strtok(buf
, crlf
); token
; token
= strtok(NULL
, crlf
)) {
477 debugs(52, 3, "urnParseReply: got '" << token
<< "'");
482 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
483 memcpy(list
, old
, i
* sizeof(*list
));
487 url
= xstrdup(token
);
488 host
= urlHostname(url
);
494 list
[i
].rtt
= netdbHostRtt(host
);
496 if (0 == list
[i
].rtt
) {
497 debugs(52, 3, "urnParseReply: Pinging " << host
);
505 list
[i
].host
= xstrdup(host
);
506 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
508 list
[i
].flags
.cached
= storeGetPublic(url
, m
) ? 1 : 0;
512 debugs(52, 3, "urnParseReply: Found " << i
<< " URLs");