]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/urn.cc
2 * Copyright (C) 1996-2014 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 (String
&urlpath
);
39 void setUriResFromRequest(HttpRequest
*);
40 bool RequestNeedsMenu(HttpRequest
*r
);
41 void updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
);
42 void createUriResRequest (String
&uri
);
49 HttpRequest::Pointer request
;
50 HttpRequest::Pointer urlres_r
;
55 char reqbuf
[URN_REQBUF_SZ
];
72 static STCB urnHandleReply
;
73 static url_entry
*urnParseReply(const char *inbuf
, const HttpRequestMethod
&);
74 static const char *const crlf
= "\r\n";
75 static QS url_entry_sort
;
77 CBDATA_CLASS_INIT(UrnState
);
85 urnFindMinRtt(url_entry
* urls
, const HttpRequestMethod
& m
, int *rtt_ret
)
89 url_entry
*min_u
= NULL
;
92 debugs(52, 3, "urnFindMinRtt");
95 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
98 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
101 debugs(52, 3, "urnFindMinRtt: Only one URL - return it!");
105 for (i
= 0; i
< urlcnt
; ++i
) {
107 debugs(52, 3, "urnFindMinRtt: " << u
->host
<< " rtt=" << u
->rtt
);
112 if (u
->rtt
> min_rtt
&& min_rtt
!= 0)
123 debugs(52, DBG_IMPORTANT
, "urnFindMinRtt: Returning '" <<
124 (min_u
? min_u
->url
: "NONE") << "' RTT " <<
131 UrnState::getHost (String
&urlpath
)
136 /** FIXME: this appears to be parsing the URL. *very* badly. */
137 /* a proper encapsulated URI/URL type needs to clear this up. */
138 if ((p
=urlpath
.find(':')) != String::npos
) {
139 result
=xstrndup(urlpath
.rawBuf(),p
-1);
141 result
= xstrndup(urlpath
.rawBuf(),urlpath
.size());
147 UrnState::RequestNeedsMenu(HttpRequest
*r
)
149 if (r
->urlpath
.size() < 5)
151 //now we're sure it's long enough
152 return strncasecmp(r
->urlpath
.rawBuf(), "menu.", 5) == 0;
156 UrnState::updateRequestURL(HttpRequest
*r
, char const *newPath
, const size_t newPath_len
)
158 char *new_path
= xstrndup (newPath
, newPath_len
);
159 r
->urlpath
= new_path
;
164 UrnState::createUriResRequest (String
&uri
)
166 LOCAL_ARRAY(char, local_urlres
, 4096);
167 char *host
= getHost (uri
);
168 snprintf(local_urlres
, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSTRINGPH
,
169 host
, SQUIDSTRINGPRINT(uri
));
172 urlres
= xstrdup(local_urlres
);
173 urlres_r
= HttpRequest::CreateFromUrl(urlres
);
177 UrnState::setUriResFromRequest(HttpRequest
*r
)
179 if (RequestNeedsMenu(r
)) {
180 updateRequestURL(r
, r
->urlpath
.rawBuf() + 5, r
->urlpath
.size() - 5 );
181 flags
.force_menu
= true;
184 createUriResRequest (r
->urlpath
);
186 if (urlres_r
== NULL
) {
187 debugs(52, 3, "urnStart: Bad uri-res URL " << urlres
);
188 ErrorState
*err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, r
);
191 errorAppendEntry(entry
, err
);
195 urlres_r
->header
.putStr(HDR_ACCEPT
, "text/plain");
199 UrnState::start(HttpRequest
* r
, StoreEntry
* e
)
201 debugs(52, 3, "urnStart: '" << e
->url() << "'" );
205 entry
->lock("UrnState::start");
206 setUriResFromRequest(r
);
208 if (urlres_r
== NULL
)
211 StoreEntry::getPublic (this, urlres
, Http::METHOD_GET
);
215 UrnState::created(StoreEntry
*newEntry
)
219 if (urlres_e
->isNull()) {
220 urlres_e
= storeCreateEntry(urlres
, urlres
, RequestFlags(), Http::METHOD_GET
);
221 sc
= storeClientListAdd(urlres_e
, this);
222 FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e
, urlres_r
.getRaw());
224 urlres_e
->lock("UrnState::created");
225 sc
= storeClientListAdd(urlres_e
, this);
229 StoreIOBuffer tempBuffer
;
230 tempBuffer
.offset
= reqofs
;
231 tempBuffer
.length
= URN_REQBUF_SZ
;
232 tempBuffer
.data
= reqbuf
;
233 storeClientCopy(sc
, urlres_e
,
240 urnStart(HttpRequest
* r
, StoreEntry
* e
)
242 UrnState
*anUrn
= new UrnState();
247 url_entry_sort(const void *A
, const void *B
)
249 const url_entry
*u1
= (const url_entry
*)A
;
250 const url_entry
*u2
= (const url_entry
*)B
;
252 if (u2
->rtt
== u1
->rtt
)
254 else if (0 == u1
->rtt
)
256 else if (0 == u2
->rtt
)
259 return u1
->rtt
- u2
->rtt
;
263 urnHandleReplyError(UrnState
*urnState
, StoreEntry
*urlres_e
)
265 urlres_e
->unlock("urnHandleReplyError+res");
266 urnState
->entry
->unlock("urnHandleReplyError+prime");
270 /* TODO: use the clientStream support for this */
272 urnHandleReply(void *data
, StoreIOBuffer result
)
274 UrnState
*urnState
= static_cast<UrnState
*>(data
);
275 StoreEntry
*e
= urnState
->entry
;
276 StoreEntry
*urlres_e
= urnState
->urlres_e
;
287 char *buf
= urnState
->reqbuf
;
288 StoreIOBuffer tempBuffer
;
290 debugs(52, 3, "urnHandleReply: Called with size=" << result
.length
<< ".");
292 if (EBIT_TEST(urlres_e
->flags
, ENTRY_ABORTED
) || result
.length
== 0 || result
.flags
.error
) {
293 urnHandleReplyError(urnState
, urlres_e
);
297 /* Update reqofs to point to where in the buffer we'd be */
298 urnState
->reqofs
+= result
.length
;
300 /* Handle reqofs being bigger than normal */
301 if (urnState
->reqofs
>= URN_REQBUF_SZ
) {
302 urnHandleReplyError(urnState
, urlres_e
);
306 /* If we haven't received the entire object (urn), copy more */
307 if (urlres_e
->store_status
== STORE_PENDING
&&
308 urnState
->reqofs
< URN_REQBUF_SZ
) {
309 tempBuffer
.offset
= urnState
->reqofs
;
310 tempBuffer
.length
= URN_REQBUF_SZ
;
311 tempBuffer
.data
= urnState
->reqbuf
+ urnState
->reqofs
;
312 storeClientCopy(urnState
->sc
, urlres_e
,
319 /* we know its STORE_OK */
320 k
= headersEnd(buf
, urnState
->reqofs
);
323 debugs(52, DBG_IMPORTANT
, "urnHandleReply: didn't find end-of-headers for " << e
->url() );
324 urnHandleReplyError(urnState
, urlres_e
);
329 assert(urlres_e
->getReply());
331 rep
->parseCharBuf(buf
, k
);
332 debugs(52, 3, "reply exists, code=" << rep
->sline
.status() << ".");
334 if (rep
->sline
.status() != Http::scOkay
) {
335 debugs(52, 3, "urnHandleReply: failed.");
336 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
337 err
->url
= xstrdup(e
->url());
338 errorAppendEntry(e
, err
);
340 urnHandleReplyError(urnState
, urlres_e
);
349 urls
= urnParseReply(s
, urnState
->request
->method
);
351 for (i
= 0; NULL
!= urls
[i
].url
; ++i
)
354 debugs(53, 3, "urnFindMinRtt: Counted " << i
<< " URLs");
356 if (urls
== NULL
) { /* unkown URN error */
357 debugs(52, 3, "urnTranslateDone: unknown URN " << e
->url());
358 err
= new ErrorState(ERR_URN_RESOLVE
, Http::scNotFound
, urnState
->request
.getRaw());
359 err
->url
= xstrdup(e
->url());
360 errorAppendEntry(e
, err
);
361 urnHandleReplyError(urnState
, urlres_e
);
365 min_u
= urnFindMinRtt(urls
, urnState
->request
->method
, NULL
);
366 qsort(urls
, urlcnt
, sizeof(*urls
), url_entry_sort
);
370 mb
->Printf( "<TITLE>Select URL for %s</TITLE>\n"
371 "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n"
372 "<H2>Select URL for %s</H2>\n"
373 "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e
->url(), e
->url());
375 for (i
= 0; i
< urlcnt
; ++i
) {
377 debugs(52, 3, "URL {" << u
->url
<< "}");
379 "<TR><TD><A HREF=\"%s\">%s</A></TD>", u
->url
, u
->url
);
383 "<TD align=\"right\">%4d <it>ms</it></TD>", u
->rtt
);
385 mb
->Printf("<TD align=\"right\">Unknown</TD>");
388 "<TD>%s</TD></TR>\n", u
->flags
.cached
? " [cached]" : " ");
393 "<HR noshade size=\"1px\">\n"
395 "Generated by %s@%s\n"
397 APP_FULLNAME
, getMyHostname());
399 rep
->setHeaders(Http::scFound
, NULL
, "text/html", mb
->contentSize(), 0, squid_curtime
);
401 if (urnState
->flags
.force_menu
) {
402 debugs(51, 3, "urnHandleReply: forcing menu");
404 rep
->header
.putStr(HDR_LOCATION
, min_u
->url
);
408 /* don't clean or delete mb; rep->body owns it now */
409 e
->replaceHttpReply(rep
);
412 for (i
= 0; i
< urlcnt
; ++i
) {
413 safe_free(urls
[i
].url
);
414 safe_free(urls
[i
].host
);
418 /* mb was absorbed in httpBodySet call, so we must not clean it */
419 storeUnregister(urnState
->sc
, urlres_e
, urnState
);
421 urnHandleReplyError(urnState
, urlres_e
);
425 urnParseReply(const char *inbuf
, const HttpRequestMethod
& m
)
427 char *buf
= xstrdup(inbuf
);
435 debugs(52, 3, "urnParseReply");
436 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
438 for (token
= strtok(buf
, crlf
); token
; token
= strtok(NULL
, crlf
)) {
439 debugs(52, 3, "urnParseReply: got '" << token
<< "'");
444 list
= (url_entry
*)xcalloc(n
+ 1, sizeof(*list
));
445 memcpy(list
, old
, i
* sizeof(*list
));
449 url
= xstrdup(token
);
450 host
= urlHostname(url
);
456 list
[i
].rtt
= netdbHostRtt(host
);
458 if (0 == list
[i
].rtt
) {
459 debugs(52, 3, "urnParseReply: Pinging " << host
);
467 list
[i
].host
= xstrdup(host
);
468 // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked
470 list
[i
].flags
.cached
= storeGetPublic(url
, m
) ? 1 : 0;
474 debugs(52, 3, "urnParseReply: Found " << i
<< " URLs");