]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/urn.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 52 URN Parsing */
13 #include "errorpage.h"
16 #include "HttpReply.h"
17 #include "HttpRequest.h"
18 #include "icmp/net_db.h"
20 #include "mime_header.h"
21 #include "RequestFlags.h"
22 #include "SquidTime.h"
24 #include "StoreClient.h"
29 #define URN_REQBUF_SZ 4096
31 class UrnState
: public StoreClient
33 CBDATA_CLASS(UrnState
);
36 void created (StoreEntry
*newEntry
);
37 void start (HttpRequest
*, StoreEntry
*);
38 char *getHost(const SBuf
&urlpath
);
39 void setUriResFromRequest(HttpRequest
*);
46 HttpRequest::Pointer request
;
47 HttpRequest::Pointer urlres_r
;
52 char reqbuf
[URN_REQBUF_SZ
];
69 static STCB urnHandleReply
;
70 static url_entry
*urnParseReply(const char *inbuf
, const HttpRequestMethod
&);
71 static const char *const crlf
= "\r\n";
73 CBDATA_CLASS_INIT(UrnState
);
81 urnFindMinRtt(url_entry
* urls
, const HttpRequestMethod
&, int *rtt_ret
)
85 url_entry
*min_u
= NULL
;
88 debugs(52, 3, "urnFindMinRtt");
91 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
94 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
97 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
101 for (i
= 0; i
< urlcnt
; ++i
) {
103 debugs(52, 3, "urnFindMinRtt: " << u
->host
<< " rtt=" << u
->rtt
);
108 if (u
->rtt
> min_rtt
&& min_rtt
!= 0)
119 debugs(52, DBG_IMPORTANT
, "urnFindMinRtt: Returning '" <<
120 (min_u
? min_u
->url
: "NONE") << "' RTT " <<
127 UrnState::getHost(const SBuf
&urlpath
)
129 /** FIXME: this appears to be parsing the URL. *very* badly. */
130 /* a proper encapsulated URI/URL type needs to clear this up. */
132 if ((p
= urlpath
.find(':')) != SBuf::npos
)
133 return SBufToCstring(urlpath
.substr(0, p
-1));
135 return SBufToCstring(urlpath
);
139 UrnState::setUriResFromRequest(HttpRequest
*r
)
141 static const SBuf
menu(".menu");
142 if (r
->url
.path().startsWith(menu
)) {
143 r
->url
.path(r
->url
.path().substr(5)); // strip prefix "menu."
144 flags
.force_menu
= true;
147 SBuf uri
= r
->url
.path();
148 LOCAL_ARRAY(char, local_urlres
, 4096);
149 char *host
= getHost(uri
);
150 snprintf(local_urlres
, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSBUFPH
, host
, SQUIDSBUFPRINT(uri
));
153 urlres
= xstrdup(local_urlres
);
154 urlres_r
= HttpRequest::CreateFromUrl(urlres
);
156 if (urlres_r
== NULL
) {
157 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres
);
158 ErrorState
*err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, r
);
161 errorAppendEntry(entry
, err
);
165 urlres_r
->header
.putStr(Http::HdrType::ACCEPT
, "text/plain");
169 UrnState::start(HttpRequest
* r
, StoreEntry
* e
)
171 debugs(52, 3, "urnStart: '" << e
->url() << "'" );
175 entry
->lock("UrnState::start");
176 setUriResFromRequest(r
);
178 if (urlres_r
== NULL
)
181 StoreEntry::getPublic (this, urlres
, Http::METHOD_GET
);
185 UrnState::created(StoreEntry
*newEntry
)
189 if (urlres_e
->isNull()) {
190 urlres_e
= storeCreateEntry(urlres
, urlres
, RequestFlags(), Http::METHOD_GET
);
191 sc
= storeClientListAdd(urlres_e
, this);
192 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e
, urlres_r
.getRaw());
194 urlres_e
->lock("UrnState::created");
195 sc
= storeClientListAdd(urlres_e
, this);
199 StoreIOBuffer tempBuffer
;
200 tempBuffer
.offset
= reqofs
;
201 tempBuffer
.length
= URN_REQBUF_SZ
;
202 tempBuffer
.data
= reqbuf
;
203 storeClientCopy(sc
, urlres_e
,
210 urnStart(HttpRequest
* r
, StoreEntry
* e
)
212 UrnState
*anUrn
= new UrnState();
217 url_entry_sort(const void *A
, const void *B
)
219 const url_entry
*u1
= (const url_entry
*)A
;
220 const url_entry
*u2
= (const url_entry
*)B
;
222 if (u2
->rtt
== u1
->rtt
)
224 else if (0 == u1
->rtt
)
226 else if (0 == u2
->rtt
)
229 return u1
->rtt
- u2
->rtt
;
233 urnHandleReplyError(UrnState
*urnState
, StoreEntry
*urlres_e
)
235 urlres_e
->unlock("urnHandleReplyError+res");
236 urnState
->entry
->unlock("urnHandleReplyError+prime");
240 /* TODO: use the clientStream support for this */
242 urnHandleReply(void *data
, StoreIOBuffer result
)
244 UrnState
*urnState
= static_cast<UrnState
*>(data
);
245 StoreEntry
*e
= urnState
->entry
;
246 StoreEntry
*urlres_e
= urnState
->urlres_e
;
257 char *buf
= urnState
->reqbuf
;
258 StoreIOBuffer tempBuffer
;
260 debugs(52, 3, "urnHandleReply: Called with size=" << result
.length
<< ".");
262 if (EBIT_TEST(urlres_e
->flags
, ENTRY_ABORTED
) || result
.length
== 0 || result
.flags
.error
) {
263 urnHandleReplyError(urnState
, urlres_e
);
267 /* Update reqofs to point to where in the buffer we'd be */
268 urnState
->reqofs
+= result
.length
;
270 /* Handle reqofs being bigger than normal */
271 if (urnState
->reqofs
>= URN_REQBUF_SZ
) {
272 urnHandleReplyError(urnState
, urlres_e
);
276 /* If we haven't received the entire object (urn), copy more */
277 if (urlres_e
->store_status
== STORE_PENDING
&&
278 urnState
->reqofs
< URN_REQBUF_SZ
) {
279 tempBuffer
.offset
= urnState
->reqofs
;
280 tempBuffer
.length
= URN_REQBUF_SZ
;
281 tempBuffer
.data
= urnState
->reqbuf
+ urnState
->reqofs
;
282 storeClientCopy(urnState
->sc
, urlres_e
,
289 /* we know its STORE_OK */
290 k
= headersEnd(buf
, urnState
->reqofs
);
293 debugs(52, DBG_IMPORTANT
, "urnHandleReply: didn't find end-of-headers for " << e
->url() );
294 urnHandleReplyError(urnState
, urlres_e
);
299 assert(urlres_e
->getReply());
301 rep
->parseCharBuf(buf
, k
);
302 debugs(52, 3, "reply exists, code=" << rep
->sline
.status() << ".");
304 if (rep
->sline
.status() != Http::scOkay
) {
305 debugs(52, 3, "urnHandleReply: failed.");
306 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
307 err
->url
= xstrdup(e
->url());
308 errorAppendEntry(e
, err
);
310 urnHandleReplyError(urnState
, urlres_e
);
319 urls
= urnParseReply(s
, urnState
->request
->method
);
321 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
324 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
326 if (urls
== NULL
) { /* unkown URN error */
327 debugs(52, 3, "urnTranslateDone: unknown URN " << e
->url());
328 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
329 err
->url
= xstrdup(e
->url());
330 errorAppendEntry(e
, err
);
331 urnHandleReplyError(urnState
, urlres_e
);
335 min_u
= urnFindMinRtt(urls
, urnState
->request
->method
, NULL
);
336 qsort(urls
, urlcnt
, sizeof(*urls
), url_entry_sort
);
340 mb
->appendf( "<TITLE>Select URL for %s</TITLE>\n"
341 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
342 "<H2>Select URL for %s</H2>\n"
343 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e
->url(), e
->url());
345 for (i
= 0; i
< urlcnt
; ++i
) {
347 debugs(52, 3, "URL {" << u
->url
<< "}");
349 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u
->url
, u
->url
);
353 "<TD align=\"right\">%4d <it>ms</it></TD>", u
->rtt
);
355 mb
->appendf("<TD align=\"right\">Unknown</TD>");
357 mb
->appendf("<TD>%s</TD></TR>\n", u
->flags
.cached
? " [cached]" : " ");
362 "<HR noshade size=\"1px\">\n"
364 "Generated by %s@%s\n"
366 APP_FULLNAME
, getMyHostname());
368 rep
->setHeaders(Http::scFound
, NULL
, "text/html", mb
->contentSize(), 0, squid_curtime
);
370 if (urnState
->flags
.force_menu
) {
371 debugs(51, 3, "urnHandleReply: forcing menu");
373 rep
->header
.putStr(Http::HdrType::LOCATION
, min_u
->url
);
377 /* don't clean or delete mb; rep->body owns it now */
378 e
->replaceHttpReply(rep
);
381 for (i
= 0; i
< urlcnt
; ++i
) {
382 safe_free(urls
[i
].url
);
383 safe_free(urls
[i
].host
);
387 /* mb was absorbed in httpBodySet call, so we must not clean it */
388 storeUnregister(urnState
->sc
, urlres_e
, urnState
);
390 urnHandleReplyError(urnState
, urlres_e
);
394 urnParseReply(const char *inbuf
, const HttpRequestMethod
& m
)
396 char *buf
= xstrdup(inbuf
);
404 debugs(52, 3, "urnParseReply");
405 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
407 for (token
= strtok(buf
, crlf
); token
; token
= strtok(NULL
, crlf
)) {
408 debugs(52, 3, "urnParseReply: got '" << token
<< "'");
413 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
414 memcpy(list
, old
, i
* sizeof(*list
));
418 url
= xstrdup(token
);
419 host
= urlHostname(url
);
425 list
[i
].rtt
= netdbHostRtt(host
);
427 if (0 == list
[i
].rtt
) {
428 debugs(52, 3, "urnParseReply: Pinging " << host
);
436 list
[i
].host
= xstrdup(host
);
437 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
439 list
[i
].flags
.cached
= storeGetPublic(url
, m
) ? 1 : 0;
443 debugs(52, 3, "urnParseReply: Found " << i
<< " URLs");