11 if(c >= '0' && c <= '9')
13 if(c >= 'a' && c <= 'f')
15 if(c >= 'A' && c <= 'F')
21 dechex(uchar *out, int lim, char *in, int n)
28 while(n-- > 0 && out < end){
36 if(n < 0 || (c = hexdigit(*in++)) == -1)
38 if((c = (c << 4) | hexdigit(*in++)) == -1)
49 int (*decfun)(uchar *, int, char *, int) = dechex;
54 if(cistrncmp(s, "data:", 5) != 0)
57 if((p = strchr(s, ',')) != nil){
59 if(strstr(s, "base64") != nil)
68 strcpy(p, "/tmp/duXXXXXXXXXXX");
69 if((fd = create(mktemp(p), ORDWR|ORCLOSE, 0600)) < 0){
73 if((m = (*decfun)((uchar*)p, m, s, n)) < 0 || write(fd, p, m) != m){
89 if(cistrncmp(url->basename, "file:", 5) == 0)
90 base = url->basename+5;
91 if(cistrncmp(url->reltext, "file:", 5) == 0)
93 if(rel == nil && base == nil)
97 if(base && base[0] == '/' && rel[0] != '/'){
98 if(x = strrchr(base, '/'))
100 snprint(url->fullname, sizeof(url->fullname), "%s/%s", base, rel);
103 snprint(url->fullname, sizeof(url->fullname), "%s", rel);
105 if(x = strrchr(url->fullname, '#')){
107 nstrcpy(url->tag, x, sizeof(url->tag));
109 base = cleanname(url->fullname);
110 x = base + strlen(base)+1;
111 if((x - base) > sizeof(url->fullname)-5)
113 memmove(url->fullname+5, base, x - base);
114 memmove(url->fullname, "file:", 5);
115 return open(url->fullname+5, OREAD);
118 char *mtpt="/mnt/web";
121 webclone(Url *url, char *buf, int nbuf)
125 snprint(buf, nbuf, "%s/clone", mtpt);
126 if((fd = open(buf, ORDWR)) < 0)
128 if((n = read(fd, buf, nbuf-1)) <= 0){
134 if(url && url->reltext[0]){
135 if(url->basename[0]){
136 n = snprint(buf, nbuf, "baseurl %s", url->basename);
139 n = snprint(buf, nbuf, "url %s", url->reltext);
140 if(write(fd, buf, n) < 0){
145 snprint(buf, nbuf, "%s/%d", mtpt, conn);
150 readstr(char *path, char *buf, int nbuf){
154 if((fd = open(path, OREAD)) >= 0){
155 if((n = read(fd, buf, nbuf-1)) < 0)
164 urlpost(Url *url, char *ctype)
169 if((fd = webclone(url, buf, sizeof(buf))) < 0)
172 fprint(fd, "contenttype %s", ctype);
174 snprint(buf+n, sizeof(buf)-n, "/postbody");
175 n = open(buf, OWRITE);
181 urlget(Url *url, int body)
187 if((fd = dataget(url)) >= 0)
189 if((fd = fileget(url)) >= 0)
191 if((fd = webclone(url, buf, sizeof(buf))) < 0)
196 if(fd2path(body, buf, sizeof(buf))){
200 if(x = strrchr(buf, '/'))
202 fd = open(buf, OREAD);
206 snprint(buf+n, sizeof(buf)-n, "/body");
207 body = open(buf, OREAD);
213 snprint(buf+n, sizeof(buf)-n, "/parsed/url");
214 readstr(buf, url->fullname, sizeof(url->fullname));
216 snprint(buf+n, sizeof(buf)-n, "/parsed/fragment");
217 readstr(buf, url->tag, sizeof(url->tag));
219 snprint(buf+n, sizeof(buf)-n, "/contentencoding");
220 readstr(buf, buf, sizeof(buf));
222 if(!cistrcmp(buf, "compress"))
223 fd = pipeline(fd, "exec uncompress");
224 else if(!cistrcmp(buf, "gzip"))
225 fd = pipeline(fd, "exec gunzip");
226 else if(!cistrcmp(buf, "bzip2"))
227 fd = pipeline(fd, "exec bunzip2");
238 if((fd = webclone(url, buf, sizeof(buf))) < 0)
241 snprint(buf+n, sizeof(buf)-n, "/parsed/url");
242 readstr(buf, url->fullname, sizeof(url->fullname));
243 snprint(buf+n, sizeof(buf)-n, "/parsed/fragment");
244 readstr(buf, url->tag, sizeof(url->tag));