shithub: riscv

Download patch

ref: feca85431791736244f7e2c1505ad9976314d9b4
parent: 3b2b18328b6d6e9fb8005c28d8abce59c4bc44e0
author: cinap_lenrek <[email protected]>
date: Fri May 18 14:19:33 EDT 2012

abaco: url handling

--- a/sys/src/cmd/abaco/urls.c
+++ b/sys/src/cmd/abaco/urls.c
@@ -29,13 +29,14 @@
 void
 urlfree(Url *u)
 {
-	if(u && decref(u)==0){
-		closerunestr(&u->src);
-		closerunestr(&u->act);
-		closerunestr(&u->post);
-		closerunestr(&u->ctype);
-		free(u);
-	}
+	if(u==nil || decref(u) > 0)
+		return;
+
+	closerunestr(&u->src);
+	closerunestr(&u->act);
+	closerunestr(&u->post);
+	closerunestr(&u->ctype);
+	free(u);
 }
 
 Url *
@@ -52,8 +53,7 @@
 	return b;
 }
 
-static
-Runestr
+static Runestr
 getattr(int conn, char *s)
 {
 	char buf[BUFSIZE];
@@ -60,11 +60,9 @@
 	int fd, n;
 
 	n = 0;
-	snprint(buf, sizeof(buf), "%s/%d/%s", webmountpt, conn, s);
-	fd = open(buf, OREAD);
-	if(fd >= 0){
-		n = read(fd, buf, sizeof(buf)-1);
-		if(n < 0)
+	snprint(buf, sizeof buf, "%s/%d/%s", webmountpt, conn, s);
+	if((fd = open(buf, OREAD)) >= 0){
+		if((n = read(fd, buf, sizeof(buf)-1)) < 0)
 			n = 0;
 		close(fd);
 	}
@@ -72,6 +70,50 @@
 	return (Runestr){runesmprint("%s", buf), n};
 }
 
+// tired of typing http://, tired of going to google first.
+void
+justgoogleit(Url *u)
+{
+	Rune *s;
+	
+	s = ucvt(u->src.r+2);
+	free(u->src.r);
+	u->src.r = runesmprint("http://www.google.com/search?hl=en&ie=UTF-8&q=%S", s);
+	free(s);
+	u->src.nr = runestrlen(u->src.r);
+}
+
+void
+addhttp(Url *u)
+{
+	Rune *s;
+	if(validurl(u->src.r))
+		return;
+	s = u->src.r;
+	u->src.r = runesmprint("http://%S", u->src.r);
+	free(s);
+	u->src.nr = runestrlen(u->src.r);
+}
+
+struct{
+	void	(*f)(Url*);
+	Rune	*lead;
+	int	len;
+} ctab[] = {
+	justgoogleit,	L"g ",		2,
+	addhttp,		L"",		0,
+};
+
+void
+urlconvience(Url *u)
+{
+	int i;
+
+	for(i = 0; u->src.nr >= ctab[i].len && runestrncmp(u->src.r, ctab[i].lead, ctab[i].len) != 0; i++)
+		;
+	ctab[i].f(u);
+}
+
 int
 urlopen(Url *u)
 {
@@ -78,6 +120,7 @@
 	char buf[BUFSIZE];
 	int cfd, fd, conn, n;
 
+	urlconvience(u);
 	snprint(buf, sizeof(buf), "%s/clone", webmountpt);
 	cfd = open(buf, ORDWR);
 	if(cfd < 0)
@@ -125,61 +168,81 @@
 }
 
 void
-urlcanon(Rune *name){
-	Rune *s, *t;
+urlcanon(Rune *name)
+{
+	Rune *s, *e, *tail, tailr;
 	Rune **comp, **p, **q;
-	int rooted;
+	int n;
 
-	name = runestrchr(name, L'/')+2;
-	rooted=name[0]==L'/';
+	name = runestrstr(name, L"://");
+	if(name == nil)
+		return;
+	name = runestrchr(name+3, '/');
+	if(name == nil)
+		return;
+	if(*name == L'/')
+		name++;
+
+	n = 0;
+	for(e = name; *e != 0; e++)
+		if(*e == L'/')
+			n++;
+	comp = emalloc((n+2)*sizeof *comp);
+
 	/*
 	 * Break the name into a list of components
 	 */
-	comp=emalloc(runestrlen(name)*sizeof(char *));
-	p=comp;
-	*p++=name;
-	for(s=name;;s++){
-		if(*s==L'/'){
-			*p++=s+1;
-			*s='\0';
-		}
-		else if(*s=='\0')
+	p = comp;
+	*p++ = name;
+	tail = nil;
+	tailr = L'☺';	/* silence compiler */
+	for(s = name; *s != 0; s++){
+		if(*s == '?' || *s == '#'){
+			tail = s+1;
+			tailr = *s;
+			*s = 0;
 			break;
+		}
+		else if(*s == L'/'){
+			*p++ = s+1;
+			*s = 0;
+		}
 	}
-	*p=0;
+
 	/*
 	 * go through the component list, deleting components that are empty (except
-	 * the last component) or ., and any .. and its non-.. predecessor.
+	 * the last component) or ., and any .. and its predecessor.
 	 */
-	p=q=comp;
-	while(*p){
-		if(runestrcmp(*p, L"")==0 && p[1]!=0
-		|| runestrcmp(*p, L".")==0)
-			p++;
-		else if(runestrcmp(*p, L"..")==0 && q!=comp && runestrcmp(q[-1], L"..")!=0){
-			--q;
-			p++;
-		}
+	for(p = q = comp; *p != nil; p++){
+		if(runestrcmp(*p, L"") == 0 && p[1] != nil
+		|| runestrcmp(*p, L".") == 0)
+			continue;
+		else if(q>comp && runestrcmp(*p, L"..") == 0 && runestrcmp(q[-1], L"..") != 0)
+			q--;
 		else
-			*q++=*p++;
+			*q++ = *p;
 	}
-	*q=0;
+	*q = nil;
+
 	/*
 	 * rebuild the path name
 	 */
-	s=name;
-	if(rooted) *s++='/';
-	for(p=comp;*p;p++){
-		t=*p;
-		while(*t) *s++=*t++;
-		if(p[1]!=0) *s++='/';
+	s = name;
+	for(p = comp; p<q; p++){
+		n = runestrlen(*p);
+		memmove(s, *p, sizeof(Rune)*n);
+		s += n;
+		if(p[1] != nil)
+			*s++ = '/';
 	}
-	*s='\0';
+	*s = 0;
+	if(tail)
+		runeseprint(s, e+1, "%C%S", tailr, tail);
 	free(comp);
 }
 
 /* this is a HACK */
-Rune *
+Rune*
 urlcombine(Rune *b, Rune *u)
 {
 	Rune *p, *q, *sep, *s;
@@ -199,7 +262,9 @@
 		q =  runestrchr(b, L':');
 		return runesmprint("%.*S:%S", (int)(q-b), b, u);
 	}
-	p = runestrstr(b, L"://")+3;
+	p = runestrstr(b, L"://");
+	if(p != nil)
+		p += 3;
 	sep = L"";
 	q = nil;
 	if(*u ==L'/')
@@ -208,7 +273,7 @@
 		for(i=0; i<nelem(endrune); i++)
 			if(q = runestrchr(p, endrune[i]))
 				break;
-	}else{
+	}else if(p != nil){
 		sep = L"/";
 		restore = 0;
 		s = runestrchr(p, L'?');
@@ -219,7 +284,8 @@
 		q = runestrrchr(p, L'/');
 		if(restore)
 			*s = L'?';
-	}
+	}else
+		sep = L"/";
 	if(q == nil)
 		p = runesmprint("%S%S%S", b, sep, u);
 	else