]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/mothra/url.c
exec(2): fix prototypes
[plan9front.git] / sys / src / cmd / mothra / url.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5 #include <panel.h>
6 #include "mothra.h"
7
8 static int
9 fileget(Url *url)
10 {
11         char *rel, *base, *x;
12
13         rel = base = nil;
14         if(cistrncmp(url->basename, "file:", 5) == 0)
15                 base = url->basename+5;
16         if(cistrncmp(url->reltext, "file:", 5) == 0)
17                 rel = url->reltext+5;
18         if(rel == nil && base == nil)
19                 return -1;
20         if(rel == nil)
21                 rel = url->reltext;
22         if(base && base[0] == '/' && rel[0] != '/'){
23                 if(x = strrchr(base, '/'))
24                         *x = 0;
25                 snprint(url->fullname, sizeof(url->fullname), "%s/%s", base, rel);
26                 if(x)   *x = '/';
27         }else
28                 snprint(url->fullname, sizeof(url->fullname), "%s", rel);
29         url->tag[0] = 0;
30         if(x = strrchr(url->fullname, '#')){
31                 *x++ = 0;
32                 nstrcpy(url->tag, x, sizeof(url->tag));
33         }
34         base = cleanname(url->fullname);
35         x = base + strlen(base)+1;
36         if((x - base) > sizeof(url->fullname)-5)
37                 return -1;
38         memmove(url->fullname+5, base, x - base);
39         memmove(url->fullname, "file:", 5);
40         return open(url->fullname+5, OREAD);
41 }
42
43 char *mtpt="/mnt/web";
44
45 static int
46 webclone(Url *url, char *buf, int nbuf)
47 {
48         int n, conn, fd;
49
50         snprint(buf, nbuf, "%s/clone", mtpt);
51         if((fd = open(buf, ORDWR)) < 0)
52                 return -1;
53         if((n = read(fd, buf, nbuf-1)) <= 0){
54                 close(fd);
55                 return -1;
56         }
57         buf[n] = 0;
58         conn = atoi(buf);
59         if(url && url->reltext[0]){
60                 if(url->basename[0]){
61                         n = snprint(buf, nbuf, "baseurl %s", url->basename);
62                         write(fd, buf, n);
63                 }
64                 n = snprint(buf, nbuf, "url %s", url->reltext);
65                 if(write(fd, buf, n) < 0){
66                         close(fd);
67                         return -1;
68                 }
69         }
70         snprint(buf, nbuf, "%s/%d", mtpt, conn);
71         return fd;
72 }
73
74 static int
75 readstr(char *path, char *buf, int nbuf){
76         int n, fd;
77
78         n = 0;
79         if((fd = open(path, OREAD)) >= 0){
80                 if((n = read(fd, buf, nbuf-1)) < 0)
81                         n = 0;
82                 close(fd);
83         }
84         buf[n] = 0;
85         return n;
86 }
87
88 int
89 urlpost(Url *url, char *ctype)
90 {
91         char buf[1024];
92         int n, fd;
93
94         if((fd = webclone(url, buf, sizeof(buf))) < 0)
95                 return -1;
96         if(ctype && *ctype)
97                 fprint(fd, "contenttype %s", ctype);
98         n = strlen(buf);
99         snprint(buf+n, sizeof(buf)-n, "/postbody");
100         n = open(buf, OWRITE);
101         close(fd);
102         return n;
103 }
104
105 int
106 urlget(Url *url, int body)
107 {
108         char buf[1024];
109         int n, fd;
110
111         if(body < 0){
112                 if((fd = fileget(url)) >= 0)
113                         return fd;
114                 if((fd = webclone(url, buf, sizeof(buf))) < 0)
115                         return -1;
116         }else{
117                 char *x;
118
119                 if(fd2path(body, buf, sizeof(buf))){
120                         close(body);
121                         return -1;
122                 }
123                 if(x = strrchr(buf, '/'))
124                         *x = 0;
125                 fd = open(buf, OREAD);
126                 close(body);
127         }
128         n = strlen(buf);
129         snprint(buf+n, sizeof(buf)-n, "/body");
130         body = open(buf, OREAD);
131         close(fd);
132         fd = body;
133         if(fd < 0)
134                 return -1;
135
136         snprint(buf+n, sizeof(buf)-n, "/parsed/url");
137         readstr(buf, url->fullname, sizeof(url->fullname));
138
139         snprint(buf+n, sizeof(buf)-n, "/parsed/fragment");
140         readstr(buf, url->tag, sizeof(url->tag));
141
142         snprint(buf+n, sizeof(buf)-n, "/contentencoding");
143         readstr(buf, buf, sizeof(buf));
144
145         if(!cistrcmp(buf, "compress"))
146                 fd = pipeline(fd, "exec uncompress");
147         else if(!cistrcmp(buf, "gzip"))
148                 fd = pipeline(fd, "exec gunzip");
149         else if(!cistrcmp(buf, "bzip2"))
150                 fd = pipeline(fd, "exec bunzip2");
151
152         return fd;
153 }
154
155 int
156 urlresolve(Url *url)
157 {
158         char buf[1024];
159         int n, fd;
160
161         if((fd = webclone(url, buf, sizeof(buf))) < 0)
162                 return -1;
163         n = strlen(buf);
164         snprint(buf+n, sizeof(buf)-n, "/parsed/url");
165         readstr(buf, url->fullname, sizeof(url->fullname));
166         snprint(buf+n, sizeof(buf)-n, "/parsed/fragment");
167         readstr(buf, url->tag, sizeof(url->tag));
168         close(fd);
169         return 0;
170 }