コード例 #1
0
ファイル: hints.c プロジェクト: aahud/harvey
void
hintprint(HConnect *hc, Hio *hout, char *uri, int thresh, int havej)
{
	int i, j, pr, prefix, fd, siz, havei, newhint = 0, n;
	char *query, *sf, etag[32], *wurl;
	Dir *dir;
	Hint *h, *haveh;

	query = hstrdup(hc, uri);
	urlcanon(query);
	j = urllookup(hashstr(query));
	if(j < 0)
		return;
	query = strrchr(uri,'/');
	if(!query)
		return;  /* can't happen */
	prefix = query-uri+1;  /* = strlen(dirname)+1 */
	h = hints[j];
	for(i=0; i<nhint[j]; i++){
		if(havej > 0 && havej < URLmax){ /* exclude hints client has */
			haveh = hints[havej];
			for(havei=0; havei<nhint[havej]; havei++)
				if( haveh[havei].url == h[i].url)
					goto continuei;
		}
		sf = urlname[h[i].url];
		pr = h[i].prob;
		if(pr<thresh)
			break;
		n = strlen(webroot) + strlen(sf) + 1;
		wurl = halloc(hc, n);
		strcpy(wurl, webroot);
		strcat(wurl, sf);
		fd = open(wurl, OREAD);
		if(fd<0)
			continue;
		dir = dirfstat(fd);
		if(dir == nil){
			close(fd);
			continue;
		}
		close(fd);
		snprint(etag, sizeof(etag), "\"%lluxv%lx\"", dir->qid.path, dir->qid.vers);
		siz = (int)( log((double)dir->length) * RECIPLOG2 + 0.9999);
		free(dir);
		if(strncmp(uri,sf,prefix)==0 && strchr(sf+prefix,'/')==0 && sf[prefix]!=0)
			sf = sf+prefix;
		hprint(hout, "Fresh: %d,%s,%d,%s\r\n", pr, etag, siz, sf);
		newhint++;
continuei: ;
	}
	if(newhint)
		hprint(hout, "Fresh: have/%d\r\n", j);
}
コード例 #2
0
/* this is a HACK */
Rune*
urlcombine(Rune *b, Rune *u)
{
	Rune *p, *q, *sep, *s;
	Rune endrune[] = { L'?', L'#' };
	int i, restore;

	if(u == nil)
		error("urlcombine: u == nil");

	if(validurl(u))
		return erunestrdup(u);

	if(b==nil || !validurl(b))
		error("urlcombine: b==nil || !validurl(b)");

	if(runestrncmp(u, L"//", 2) == 0){
		q =  runestrchr(b, L':');
		return runesmprint("%.*S:%S", (int)(q-b), b, u);
	}
	p = runestrstr(b, L"://");
	if(p != nil)
		p += 3;
	sep = L"";
	q = nil;
	if(*u ==L'/')
		q = runestrchr(p, L'/');
	else if(*u==L'#' || *u==L'?'){
		for(i=0; i<nelem(endrune); i++)
			if(q = runestrchr(p, endrune[i]))
				break;
	}else if(p != nil){
		sep = L"/";
		restore = 0;
		s = runestrchr(p, L'?');
		if(s != nil){
			*s = '\0';
			restore = 1;
		}
		q = runestrrchr(p, L'/');
		if(restore)
			*s = L'?';
	}else
		sep = L"/";
	if(q == nil)
		p = runesmprint("%S%S%S", b, sep, u);
	else
		p = runesmprint("%.*S%S%S", (int)(q-b), b, sep, u);
	urlcanon(p);
	return p;
}