]>
Commit | Line | Data |
---|---|---|
85491f8d | 1 | |
2 | /* | |
262a0e14 | 3 | * $Id$ |
ebe14c5d | 4 | * |
6f6f0853 | 5 | * DEBUG: section 52 URN Parsing |
85491f8d | 6 | * AUTHOR: Kostas Anagnostakis |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 9 | * ---------------------------------------------------------- |
85491f8d | 10 | * |
2b6662ba | 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. | |
85491f8d | 19 | * |
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. | |
26ac0430 | 24 | * |
85491f8d | 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. | |
26ac0430 | 29 | * |
85491f8d | 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 | |
cbdec147 | 32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 33 | * |
85491f8d | 34 | */ |
35 | ||
582c2af2 | 36 | #include "squid.h" |
aa839030 | 37 | #include "errorpage.h" |
c8be6d7b | 38 | #include "StoreClient.h" |
e6ccf245 | 39 | #include "Store.h" |
528b2c61 | 40 | #include "HttpReply.h" |
41 | #include "HttpRequest.h" | |
0eb49b6d | 42 | #include "MemBuf.h" |
b6b6f466 | 43 | #include "forward.h" |
582c2af2 | 44 | #include "globals.h" |
985c86bc | 45 | #include "SquidTime.h" |
9b5c4a9a | 46 | #include "icmp/net_db.h" |
b6149797 | 47 | #include "mime_header.h" |
582c2af2 | 48 | #include "protos.h" |
b1bd952a | 49 | #include "URL.h" |
85491f8d | 50 | |
add2192d | 51 | #define URN_REQBUF_SZ 4096 |
52 | ||
62e76326 | 53 | class UrnState : public StoreClient |
54 | { | |
55 | ||
e6ccf245 | 56 | public: |
3b13a8fd | 57 | void created (StoreEntry *newEntry); |
06096e82 | 58 | void *operator new (size_t byteCount); |
e6ccf245 | 59 | void operator delete (void *address); |
190154cf | 60 | void start (HttpRequest *, StoreEntry *); |
30abd221 | 61 | char *getHost (String &urlpath); |
190154cf | 62 | void setUriResFromRequest(HttpRequest *); |
63 | bool RequestNeedsMenu(HttpRequest *r); | |
17bc3f22 | 64 | void updateRequestURL(HttpRequest *r, char const *newPath, const size_t newPath_len); |
30abd221 | 65 | void createUriResRequest (String &uri); |
e6ccf245 | 66 | |
67 | virtual ~UrnState(); | |
62e76326 | 68 | |
164f7660 | 69 | StoreEntry *entry; |
06d2839d | 70 | store_client *sc; |
164f7660 | 71 | StoreEntry *urlres_e; |
190154cf | 72 | HttpRequest *request; |
73 | HttpRequest *urlres_r; | |
62e76326 | 74 | |
26ac0430 | 75 | struct { |
3d0ac046 HN |
76 | unsigned int force_menu:1; |
77 | } flags; | |
add2192d | 78 | char reqbuf[URN_REQBUF_SZ]; |
79 | int reqofs; | |
62e76326 | 80 | |
e6ccf245 | 81 | private: |
82 | char *urlres; | |
83 | }; | |
85491f8d | 84 | |
26ac0430 | 85 | typedef struct { |
9ce5e3e6 | 86 | char *url; |
87 | char *host; | |
88 | int rtt; | |
62e76326 | 89 | |
26ac0430 | 90 | struct { |
62e76326 | 91 | int cached; |
2fadd50d HN |
92 | } flags; |
93 | } url_entry; | |
9ce5e3e6 | 94 | |
95 | static STCB urnHandleReply; | |
60745f24 | 96 | static url_entry *urnParseReply(const char *inbuf, const HttpRequestMethod&); |
9ce5e3e6 | 97 | static const char *const crlf = "\r\n"; |
98 | static QS url_entry_sort; | |
99 | ||
e6ccf245 | 100 | CBDATA_TYPE(UrnState); |
101 | void * | |
06096e82 | 102 | UrnState::operator new (size_t byteCount) |
e6ccf245 | 103 | { |
62e76326 | 104 | /* derived classes with different sizes must implement their own new */ |
e6ccf245 | 105 | assert (byteCount == sizeof (UrnState)); |
106 | CBDATA_INIT_TYPE(UrnState); | |
107 | return cbdataAlloc(UrnState); | |
62e76326 | 108 | |
e6ccf245 | 109 | } |
110 | ||
111 | void | |
112 | UrnState::operator delete (void *address) | |
113 | { | |
1f1ae50a | 114 | UrnState * tmp = (UrnState *)address; |
115 | cbdataFree (tmp); | |
e6ccf245 | 116 | } |
117 | ||
118 | UrnState::~UrnState () | |
119 | { | |
120 | safe_free(urlres); | |
121 | } | |
122 | ||
48ebcb22 | 123 | static url_entry * |
60745f24 | 124 | urnFindMinRtt(url_entry * urls, const HttpRequestMethod& m, int *rtt_ret) |
85491f8d | 125 | { |
23d92c64 | 126 | int min_rtt = 0; |
9ce5e3e6 | 127 | url_entry *u = NULL; |
128 | url_entry *min_u = NULL; | |
129 | int i; | |
1caf595b | 130 | int urlcnt = 0; |
bf8fe701 | 131 | debugs(52, 3, "urnFindMinRtt"); |
23d92c64 | 132 | assert(urls != NULL); |
62e76326 | 133 | |
14942edd FC |
134 | for (i = 0; NULL != urls[i].url; ++i) |
135 | ++urlcnt; | |
62e76326 | 136 | |
bf8fe701 | 137 | debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs"); |
62e76326 | 138 | |
9ce5e3e6 | 139 | if (1 == urlcnt) { |
bf8fe701 | 140 | debugs(52, 3, "urnFindMinRtt: Only one URL - return it!"); |
62e76326 | 141 | return urls; |
1caf595b | 142 | } |
62e76326 | 143 | |
14942edd | 144 | for (i = 0; i < urlcnt; ++i) { |
62e76326 | 145 | u = &urls[i]; |
bf8fe701 | 146 | debugs(52, 3, "urnFindMinRtt: " << u->host << " rtt=" << u->rtt); |
62e76326 | 147 | |
148 | if (u->rtt == 0) | |
149 | continue; | |
150 | ||
151 | if (u->rtt > min_rtt && min_rtt != 0) | |
152 | continue; | |
153 | ||
154 | min_rtt = u->rtt; | |
155 | ||
156 | min_u = u; | |
23d92c64 | 157 | } |
62e76326 | 158 | |
23d92c64 | 159 | if (rtt_ret) |
62e76326 | 160 | *rtt_ret = min_rtt; |
161 | ||
e0236918 | 162 | debugs(52, DBG_IMPORTANT, "urnFindMinRtt: Returning '" << |
26ac0430 AJ |
163 | (min_u ? min_u->url : "NONE") << "' RTT " << |
164 | min_rtt ); | |
62e76326 | 165 | |
9ce5e3e6 | 166 | return min_u; |
85491f8d | 167 | } |
168 | ||
e6ccf245 | 169 | char * |
30abd221 | 170 | UrnState::getHost (String &urlpath) |
85491f8d | 171 | { |
e6ccf245 | 172 | char * result; |
b4f2886c | 173 | size_t p; |
62e76326 | 174 | |
b4f2886c FC |
175 | /** FIXME: this appears to be parsing the URL. *very* badly. */ |
176 | /* a proper encapsulated URI/URL type needs to clear this up. */ | |
2c1fd837 | 177 | if ((p=urlpath.find(':')) != String::npos) { |
b4f2886c | 178 | result=xstrndup(urlpath.rawBuf(),p-1); |
fa040df2 | 179 | } else { |
b4f2886c | 180 | result = xstrndup(urlpath.rawBuf(),urlpath.size()); |
fa040df2 | 181 | } |
e6ccf245 | 182 | return result; |
183 | } | |
184 | ||
185 | bool | |
190154cf | 186 | UrnState::RequestNeedsMenu(HttpRequest *r) |
e6ccf245 | 187 | { |
17bc3f22 FC |
188 | if (r->urlpath.size() < 5) |
189 | return false; | |
190 | //now we're sure it's long enough | |
191 | return strncasecmp(r->urlpath.rawBuf(), "menu.", 5) == 0; | |
e6ccf245 | 192 | } |
193 | ||
194 | void | |
17bc3f22 | 195 | UrnState::updateRequestURL(HttpRequest *r, char const *newPath, const size_t newPath_len) |
e6ccf245 | 196 | { |
17bc3f22 | 197 | char *new_path = xstrndup (newPath, newPath_len); |
62e76326 | 198 | r->urlpath = new_path; |
199 | xfree(new_path); | |
e6ccf245 | 200 | } |
201 | ||
202 | void | |
30abd221 | 203 | UrnState::createUriResRequest (String &uri) |
e6ccf245 | 204 | { |
205 | LOCAL_ARRAY(char, local_urlres, 4096); | |
206 | char *host = getHost (uri); | |
2c1fd837 | 207 | snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?urn:" SQUIDSTRINGPH, |
af6a12ee | 208 | host, SQUIDSTRINGPRINT(uri)); |
e6ccf245 | 209 | safe_free (host); |
210 | safe_free (urlres); | |
211 | urlres = xstrdup (local_urlres); | |
c21ad0f5 | 212 | urlres_r = HttpRequest::CreateFromUrl(urlres); |
e6ccf245 | 213 | } |
214 | ||
215 | void | |
190154cf | 216 | UrnState::setUriResFromRequest(HttpRequest *r) |
e6ccf245 | 217 | { |
218 | if (RequestNeedsMenu(r)) { | |
17bc3f22 | 219 | updateRequestURL(r, r->urlpath.rawBuf() + 5, r->urlpath.size() - 5 ); |
62e76326 | 220 | flags.force_menu = 1; |
e6ccf245 | 221 | } |
62e76326 | 222 | |
e6ccf245 | 223 | createUriResRequest (r->urlpath); |
62e76326 | 224 | |
0adbab7c | 225 | if (urlres_r == NULL) { |
bf8fe701 | 226 | debugs(52, 3, "urnStart: Bad uri-res URL " << urlres); |
913524f0 | 227 | ErrorState *err = new ErrorState(ERR_URN_RESOLVE, HTTP_NOT_FOUND, r); |
62e76326 | 228 | err->url = urlres; |
229 | urlres = NULL; | |
230 | errorAppendEntry(entry, err); | |
231 | return; | |
0adbab7c | 232 | } |
62e76326 | 233 | |
6dd9f4bd | 234 | HTTPMSGLOCK(urlres_r); |
a9925b40 | 235 | urlres_r->header.putStr(HDR_ACCEPT, "text/plain"); |
e6ccf245 | 236 | } |
237 | ||
238 | void | |
190154cf | 239 | UrnState::start(HttpRequest * r, StoreEntry * e) |
e6ccf245 | 240 | { |
bf8fe701 | 241 | debugs(52, 3, "urnStart: '" << e->url() << "'" ); |
e6ccf245 | 242 | entry = e; |
6dd9f4bd | 243 | request = HTTPMSGLOCK(r); |
34266cde | 244 | |
3d0ac046 | 245 | entry->lock(); |
e6ccf245 | 246 | setUriResFromRequest(r); |
62e76326 | 247 | |
e6ccf245 | 248 | if (urlres_r == NULL) |
62e76326 | 249 | return; |
250 | ||
3b13a8fd | 251 | StoreEntry::getPublic (this, urlres, METHOD_GET); |
e6ccf245 | 252 | } |
253 | ||
254 | void | |
3b13a8fd | 255 | UrnState::created(StoreEntry *newEntry) |
e6ccf245 | 256 | { |
257 | urlres_e = newEntry; | |
62e76326 | 258 | |
e6ccf245 | 259 | if (urlres_e->isNull()) { |
62e76326 | 260 | urlres_e = storeCreateEntry(urlres, urlres, request_flags(), METHOD_GET); |
261 | sc = storeClientListAdd(urlres_e, this); | |
e83cc785 | 262 | FwdState::fwdStart(Comm::ConnectionPointer(), urlres_e, urlres_r); |
cf26e54c | 263 | } else { |
34266cde | 264 | |
3d0ac046 | 265 | urlres_e->lock(); |
62e76326 | 266 | sc = storeClientListAdd(urlres_e, this); |
85491f8d | 267 | } |
62e76326 | 268 | |
e6ccf245 | 269 | reqofs = 0; |
528b2c61 | 270 | StoreIOBuffer tempBuffer; |
e6ccf245 | 271 | tempBuffer.offset = reqofs; |
c8be6d7b | 272 | tempBuffer.length = URN_REQBUF_SZ; |
e6ccf245 | 273 | tempBuffer.data = reqbuf; |
274 | storeClientCopy(sc, urlres_e, | |
62e76326 | 275 | tempBuffer, |
276 | urnHandleReply, | |
277 | this); | |
e6ccf245 | 278 | } |
279 | ||
280 | void | |
190154cf | 281 | urnStart(HttpRequest * r, StoreEntry * e) |
e6ccf245 | 282 | { |
283 | UrnState *anUrn = new UrnState(); | |
284 | anUrn->start (r, e); | |
85491f8d | 285 | } |
286 | ||
9ce5e3e6 | 287 | static int |
288 | url_entry_sort(const void *A, const void *B) | |
289 | { | |
e6ccf245 | 290 | const url_entry *u1 = (const url_entry *)A; |
291 | const url_entry *u2 = (const url_entry *)B; | |
62e76326 | 292 | |
9ce5e3e6 | 293 | if (u2->rtt == u1->rtt) |
62e76326 | 294 | return 0; |
9ce5e3e6 | 295 | else if (0 == u1->rtt) |
62e76326 | 296 | return 1; |
9ce5e3e6 | 297 | else if (0 == u2->rtt) |
62e76326 | 298 | return -1; |
9ce5e3e6 | 299 | else |
62e76326 | 300 | return u1->rtt - u2->rtt; |
9ce5e3e6 | 301 | } |
302 | ||
bb9edbb2 AJ |
303 | static void |
304 | urnHandleReplyError(UrnState *urnState, StoreEntry *urlres_e) | |
305 | { | |
306 | urlres_e->unlock(); | |
307 | urnState->entry->unlock(); | |
308 | HTTPMSGUNLOCK(urnState->request); | |
309 | HTTPMSGUNLOCK(urnState->urlres_r); | |
310 | delete urnState; | |
311 | } | |
312 | ||
528b2c61 | 313 | /* TODO: use the clientStream support for this */ |
85491f8d | 314 | static void |
c8be6d7b | 315 | urnHandleReply(void *data, StoreIOBuffer result) |
85491f8d | 316 | { |
e6ccf245 | 317 | UrnState *urnState = static_cast<UrnState *>(data); |
cf26e54c | 318 | StoreEntry *e = urnState->entry; |
319 | StoreEntry *urlres_e = urnState->urlres_e; | |
23d92c64 | 320 | char *s = NULL; |
2334c194 | 321 | size_t k; |
cb69b4c7 | 322 | HttpReply *rep; |
9ce5e3e6 | 323 | url_entry *urls; |
00141c96 | 324 | url_entry *u; |
9ce5e3e6 | 325 | url_entry *min_u; |
032785bf | 326 | MemBuf *mb = NULL; |
cf26e54c | 327 | ErrorState *err; |
9ce5e3e6 | 328 | int i; |
329 | int urlcnt = 0; | |
add2192d | 330 | char *buf = urnState->reqbuf; |
c8be6d7b | 331 | StoreIOBuffer tempBuffer; |
cf26e54c | 332 | |
58c0b17d | 333 | debugs(52, 3, "urnHandleReply: Called with size=" << result.length << "."); |
62e76326 | 334 | |
58c0b17d | 335 | if (EBIT_TEST(urlres_e->flags, ENTRY_ABORTED) || result.length == 0 || result.flags.error) { |
bb9edbb2 AJ |
336 | urnHandleReplyError(urnState, urlres_e); |
337 | return; | |
85491f8d | 338 | } |
62e76326 | 339 | |
add2192d | 340 | /* Update reqofs to point to where in the buffer we'd be */ |
c8be6d7b | 341 | urnState->reqofs += result.length; |
add2192d | 342 | |
343 | /* Handle reqofs being bigger than normal */ | |
344 | if (urnState->reqofs >= URN_REQBUF_SZ) { | |
bb9edbb2 AJ |
345 | urnHandleReplyError(urnState, urlres_e); |
346 | return; | |
add2192d | 347 | } |
62e76326 | 348 | |
add2192d | 349 | /* If we haven't received the entire object (urn), copy more */ |
350 | if (urlres_e->store_status == STORE_PENDING && | |
62e76326 | 351 | urnState->reqofs < URN_REQBUF_SZ) { |
352 | tempBuffer.offset = urnState->reqofs; | |
353 | tempBuffer.length = URN_REQBUF_SZ; | |
354 | tempBuffer.data = urnState->reqbuf + urnState->reqofs; | |
355 | storeClientCopy(urnState->sc, urlres_e, | |
356 | tempBuffer, | |
357 | urnHandleReply, | |
358 | urnState); | |
359 | return; | |
23d92c64 | 360 | } |
62e76326 | 361 | |
23d92c64 | 362 | /* we know its STORE_OK */ |
add2192d | 363 | k = headersEnd(buf, urnState->reqofs); |
62e76326 | 364 | |
2334c194 | 365 | if (0 == k) { |
e0236918 | 366 | debugs(52, DBG_IMPORTANT, "urnHandleReply: didn't find end-of-headers for " << e->url() ); |
bb9edbb2 AJ |
367 | urnHandleReplyError(urnState, urlres_e); |
368 | return; | |
85491f8d | 369 | } |
62e76326 | 370 | |
2334c194 | 371 | s = buf + k; |
528b2c61 | 372 | assert(urlres_e->getReply()); |
06a5ae20 | 373 | rep = new HttpReply; |
59eed7dc | 374 | rep->parseCharBuf(buf, k); |
bf8fe701 | 375 | debugs(52, 3, "reply exists, code=" << rep->sline.status << "."); |
62e76326 | 376 | |
528b2c61 | 377 | if (rep->sline.status != HTTP_OK) { |
bf8fe701 | 378 | debugs(52, 3, "urnHandleReply: failed."); |
913524f0 | 379 | err = new ErrorState(ERR_URN_RESOLVE, HTTP_NOT_FOUND, urnState->request); |
3900307b | 380 | err->url = xstrdup(e->url()); |
62e76326 | 381 | errorAppendEntry(e, err); |
06a5ae20 | 382 | delete rep; |
bb9edbb2 AJ |
383 | urnHandleReplyError(urnState, urlres_e); |
384 | return; | |
85491f8d | 385 | } |
62e76326 | 386 | |
06a5ae20 | 387 | delete rep; |
62e76326 | 388 | |
b6a2f15e | 389 | while (xisspace(*s)) |
14942edd | 390 | ++s; |
62e76326 | 391 | |
9ce5e3e6 | 392 | urls = urnParseReply(s, urnState->request->method); |
62e76326 | 393 | |
14942edd FC |
394 | for (i = 0; NULL != urls[i].url; ++i) |
395 | ++urlcnt; | |
62e76326 | 396 | |
bf8fe701 | 397 | debugs(53, 3, "urnFindMinRtt: Counted " << i << " URLs"); |
62e76326 | 398 | |
164f7660 | 399 | if (urls == NULL) { /* unkown URN error */ |
bf8fe701 | 400 | debugs(52, 3, "urnTranslateDone: unknown URN " << e->url() ); |
913524f0 | 401 | err = new ErrorState(ERR_URN_RESOLVE, HTTP_NOT_FOUND, urnState->request); |
3900307b | 402 | err->url = xstrdup(e->url()); |
62e76326 | 403 | errorAppendEntry(e, err); |
bb9edbb2 AJ |
404 | urnHandleReplyError(urnState, urlres_e); |
405 | return; | |
164f7660 | 406 | } |
62e76326 | 407 | |
9ce5e3e6 | 408 | min_u = urnFindMinRtt(urls, urnState->request->method, NULL); |
409 | qsort(urls, urlcnt, sizeof(*urls), url_entry_sort); | |
3900307b | 410 | e->buffer(); |
032785bf | 411 | mb = new MemBuf; |
2fe7eff9 | 412 | mb->init(); |
413 | mb->Printf( "<TITLE>Select URL for %s</TITLE>\n" | |
414 | "<STYLE type=\"text/css\"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}--></STYLE>\n" | |
415 | "<H2>Select URL for %s</H2>\n" | |
3900307b | 416 | "<TABLE BORDER=\"0\" WIDTH=\"100%%\">\n", e->url(), e->url()); |
62e76326 | 417 | |
14942edd | 418 | for (i = 0; i < urlcnt; ++i) { |
62e76326 | 419 | u = &urls[i]; |
bf8fe701 | 420 | debugs(52, 3, "URL {" << u->url << "}"); |
2fe7eff9 | 421 | mb->Printf( |
422 | "<TR><TD><A HREF=\"%s\">%s</A></TD>", u->url, u->url); | |
62e76326 | 423 | |
424 | if (urls[i].rtt > 0) | |
2fe7eff9 | 425 | mb->Printf( |
426 | "<TD align=\"right\">%4d <it>ms</it></TD>", u->rtt); | |
62e76326 | 427 | else |
2fe7eff9 | 428 | mb->Printf("<TD align=\"right\">Unknown</TD>"); |
62e76326 | 429 | |
2fe7eff9 | 430 | mb->Printf( |
431 | "<TD>%s</TD></TR>\n", u->flags.cached ? " [cached]" : " "); | |
cf26e54c | 432 | } |
62e76326 | 433 | |
2fe7eff9 | 434 | mb->Printf( |
435 | "</TABLE>" | |
436 | "<HR noshade size=\"1px\">\n" | |
437 | "<ADDRESS>\n" | |
438 | "Generated by %s@%s\n" | |
439 | "</ADDRESS>\n", | |
7dbca7a4 | 440 | APP_FULLNAME, getMyHostname()); |
06a5ae20 | 441 | rep = new HttpReply; |
11992b6f | 442 | rep->setHeaders(HTTP_MOVED_TEMPORARILY, NULL, "text/html", mb->contentSize(), 0, squid_curtime); |
62e76326 | 443 | |
b515fc11 | 444 | if (urnState->flags.force_menu) { |
bf8fe701 | 445 | debugs(51, 3, "urnHandleReply: forcing menu"); |
9ce5e3e6 | 446 | } else if (min_u) { |
a9925b40 | 447 | rep->header.putStr(HDR_LOCATION, min_u->url); |
cb69b4c7 | 448 | } |
62e76326 | 449 | |
0521f8be | 450 | rep->body.setMb(mb); |
032785bf | 451 | /* don't clean or delete mb; rep->body owns it now */ |
db237875 | 452 | e->replaceHttpReply(rep); |
528b2c61 | 453 | e->complete(); |
62e76326 | 454 | |
14942edd | 455 | for (i = 0; i < urlcnt; ++i) { |
62e76326 | 456 | safe_free(urls[i].url); |
457 | safe_free(urls[i].host); | |
9ce5e3e6 | 458 | } |
62e76326 | 459 | |
9ce5e3e6 | 460 | safe_free(urls); |
96a5be3d | 461 | /* mb was absorbed in httpBodySet call, so we must not clean it */ |
06d2839d | 462 | storeUnregister(urnState->sc, urlres_e, urnState); |
62e76326 | 463 | |
bb9edbb2 | 464 | urnHandleReplyError(urnState, urlres_e); |
85491f8d | 465 | } |
466 | ||
9ce5e3e6 | 467 | static url_entry * |
60745f24 | 468 | urnParseReply(const char *inbuf, const HttpRequestMethod& m) |
85491f8d | 469 | { |
23d92c64 | 470 | char *buf = xstrdup(inbuf); |
471 | char *token; | |
9ce5e3e6 | 472 | char *url; |
473 | char *host; | |
9ce5e3e6 | 474 | url_entry *list; |
475 | url_entry *old; | |
476 | int n = 32; | |
477 | int i = 0; | |
bf8fe701 | 478 | debugs(52, 3, "urnParseReply"); |
e6ccf245 | 479 | list = (url_entry *)xcalloc(n + 1, sizeof(*list)); |
62e76326 | 480 | |
23d92c64 | 481 | for (token = strtok(buf, crlf); token; token = strtok(NULL, crlf)) { |
bf8fe701 | 482 | debugs(52, 3, "urnParseReply: got '" << token << "'"); |
62e76326 | 483 | |
484 | if (i == n) { | |
485 | old = list; | |
486 | n <<= 2; | |
487 | list = (url_entry *)xcalloc(n + 1, sizeof(*list)); | |
41d00cd3 | 488 | memcpy(list, old, i * sizeof(*list)); |
62e76326 | 489 | safe_free(old); |
490 | } | |
491 | ||
492 | url = xstrdup(token); | |
493 | host = urlHostname(url); | |
494 | ||
495 | if (NULL == host) | |
496 | continue; | |
497 | ||
9b5c4a9a AJ |
498 | #if USE_ICMP |
499 | list[i].rtt = netdbHostRtt(host); | |
62e76326 | 500 | |
9b5c4a9a | 501 | if (0 == list[i].rtt) { |
bf8fe701 | 502 | debugs(52, 3, "urnParseReply: Pinging " << host); |
62e76326 | 503 | netdbPingSite(host); |
504 | } | |
9b5c4a9a AJ |
505 | #else |
506 | list[i].rtt = 0; | |
507 | #endif | |
62e76326 | 508 | |
509 | list[i].url = url; | |
510 | list[i].host = xstrdup(host); | |
5bd484b5 AR |
511 | // TODO: Use storeHas() or lock/unlock entry to avoid creating unlocked |
512 | // ones. | |
62e76326 | 513 | list[i].flags.cached = storeGetPublic(url, m) ? 1 : 0; |
14942edd | 514 | ++i; |
85491f8d | 515 | } |
62e76326 | 516 | |
bf8fe701 | 517 | debugs(52, 3, "urnParseReply: Found " << i << " URLs"); |
9ce5e3e6 | 518 | return list; |
85491f8d | 519 | } |