]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/webfs/client.c
merge
[plan9front.git] / sys / src / cmd / webfs / client.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ip.h>
5 #include <plumb.h>
6 #include <thread.h>
7 #include <fcall.h>
8 #include <9p.h>
9 #include "dat.h"
10 #include "fns.h"
11
12 int nclient;
13 Client **client;
14
15 static void clientthread(void*);
16 int
17 newclient(int plumbed)
18 {
19         int i;
20         Client *c;
21
22         for(i=0; i<nclient; i++)
23                 if(client[i]->ref==0)
24                         return i;
25
26         c = emalloc(sizeof(Client));
27         c->plumbed = plumbed;
28         c->creq = chancreate(sizeof(Req*), 8);
29         threadcreate(clientthread, c, STACK);
30
31         c->io = ioproc();
32         c->num = nclient;
33         c->ctl = globalctl;
34         clonectl(&c->ctl);
35         if(nclient%16 == 0)
36                 client = erealloc(client, (nclient+16)*sizeof(client[0]));
37         client[nclient++] = c;
38         return nclient-1;
39 }
40
41 void
42 closeclient(Client *c)
43 {
44         if(--c->ref == 0){
45                 if(c->bodyopened){
46                         if(c->url && c->url->close)
47                                 (*c->url->close)(c);
48                         c->bodyopened = 0;
49                 }
50                 free(c->contenttype);
51                 c->contenttype = nil;
52                 free(c->postbody);
53                 c->postbody = nil;
54                 freeurl(c->url);
55                 c->url = nil;
56                 free(c->redirect);
57                 c->redirect = nil;
58                 free(c->authenticate);
59                 c->authenticate = nil;
60                 c->npostbody = 0;
61                 c->havepostbody = 0;
62                 c->bodyopened = 0;
63         }
64 }
65
66 void
67 clonectl(Ctl *c)
68 {
69         if(c->useragent)
70                 c->useragent = estrdup(c->useragent);
71 }
72
73 void
74 clientbodyopen(Client *c, Req *r)
75 {
76         char e[ERRMAX], *next;
77         int i, nauth;
78         Url *u;
79
80         nauth = 0;
81         next = nil;
82         for(i=0; i<=c->ctl.redirectlimit; i++){
83                 if(c->url == nil){
84                         werrstr("nil url");
85                         goto Error;
86                 }
87                 if(c->url->open == nil){
88                         werrstr("unsupported url type");
89                         goto Error;
90                 }
91                 if(fsdebug)
92                         fprint(2, "try %s\n", c->url->url);
93                 if(c->url->open(c, c->url) < 0){
94                 Error:
95                         if(next)
96                                 fprint(2, "next %s (but for error)\n", next);
97                         free(next);
98                         rerrstr(e, sizeof e);
99                         c->iobusy = 0;
100                         if(r != nil)
101                                 r->fid->omode = -1;
102                         closeclient(c); /* not opening */
103                         if(r != nil)
104                                 respond(r, e);
105                         return;
106                 }
107                 if (c->authenticate && nauth++ < 1)
108                                 continue;
109                 if(!c->redirect)
110                         break;
111                 next = c->redirect;
112                 c->redirect = nil;
113                 if(i==c->ctl.redirectlimit){
114                         werrstr("redirect limit reached");
115                         goto Error;
116                 }
117                 if((u = parseurl(next, c->url)) == nil)
118                         goto Error;
119                 if(urldebug)
120                         fprint(2, "parseurl %s got scheme %d\n", next, u->ischeme);
121                 if(u->ischeme == USunknown){
122                         werrstr("redirect with unknown URL scheme");
123                         goto Error;
124                 }
125                 if(u->ischeme == UScurrent){
126                         werrstr("redirect to URL relative to current document");
127                         goto Error;
128                 }
129                 freeurl(c->url);
130                 c->url = u;
131         }
132         free(next);
133         c->iobusy = 0;
134         if(r != nil)
135                 respond(r, nil);
136 }
137
138 void
139 plumburl(char *url, char *base)
140 {
141         int i;
142         Client *c;
143         Url *ubase, *uurl;
144
145         ubase = nil;
146         if(base){
147                 ubase = parseurl(base, nil);
148                 if(ubase == nil)
149                         return;
150         }
151         uurl = parseurl(url, ubase);
152         if(uurl == nil){
153                 freeurl(ubase);
154                 return;
155         }
156         i = newclient(1);
157         c = client[i];
158         c->ref++;
159         c->baseurl = ubase;
160         c->url = uurl;
161         sendp(c->creq, nil);
162 }
163
164 void
165 clientbodyread(Client *c, Req *r)
166 {
167         char e[ERRMAX];
168
169         if(c->url->read == nil){
170                 respond(r, "unsupported url type");
171                 return;
172         }
173         if(c->url->read(c, r) < 0){
174                 rerrstr(e, sizeof e);
175                 c->iobusy = 0;
176                 respond(r, e);
177                 return;
178         }
179         c->iobusy = 0;
180         respond(r, nil);
181 }
182
183 static void
184 clientthread(void *a)
185 {
186         Client *c;
187         Req *r;
188
189         c = a;
190         if(c->plumbed) {
191                 recvp(c->creq);
192                 if(c->url == nil){
193                         fprint(2, "bad url got plumbed\n");
194                         return;
195                 }
196                 clientbodyopen(c, nil);
197                 replumb(c);
198         }
199         while((r = recvp(c->creq)) != nil){
200                 if(fsdebug)
201                         fprint(2, "clientthread %F\n", &r->ifcall);
202                 switch(r->ifcall.type){
203                 case Topen:
204                         if(c->plumbed) {
205                                 c->plumbed = 0;
206                                 c->ref--;                       /* from plumburl() */
207                                 respond(r, nil);
208                         }
209                         else
210                                 clientbodyopen(c, r);
211                         break;
212                 case Tread:
213                         clientbodyread(c, r);
214                         break;
215                 case Tflush:
216                         respond(r, nil);
217                 }
218                 if(fsdebug)
219                         fprint(2, "clientthread finished req\n");
220         }
221 }
222         
223 enum
224 {
225         Bool,
226         Int,
227         String,
228         XRel,
229         XUrl,
230         Fn,
231 };
232
233 typedef struct Ctab Ctab;
234 struct Ctab {
235         char *name;
236         int type;
237         void *offset;
238 };
239
240 Ctab ctltab[] = {
241         "acceptcookies",        Bool,           (void*)offsetof(Ctl, acceptcookies),
242         "sendcookies",          Bool,           (void*)offsetof(Ctl, sendcookies),
243         "redirectlimit",                Int,            (void*)offsetof(Ctl, redirectlimit),
244         "useragent",            String, (void*)offsetof(Ctl, useragent),
245 };
246
247 Ctab globaltab[] = {
248         "chatty9p",             Int,            &chatty9p,
249         "fsdebug",              Int,            &fsdebug,
250         "cookiedebug",          Int,            &cookiedebug,
251         "urldebug",             Int,            &urldebug,
252         "httpdebug",            Int,            &httpdebug,
253 };
254
255 Ctab clienttab[] = {
256         "baseurl",                      XUrl,           (void*)offsetof(Client, baseurl),
257         "url",                          XRel,           (void*)offsetof(Client, url),
258 };
259
260 static Ctab*
261 findcmd(char *cmd, Ctab *tab, int ntab)
262 {
263         int i;
264
265         for(i=0; i<ntab; i++)
266                 if(strcmp(tab[i].name, cmd) == 0)
267                         return &tab[i];
268         return nil;
269 }
270
271 static void
272 parseas(Req *r, char *arg, int type, void *a)
273 {
274         Url *u, *base;
275         char e[ERRMAX];
276
277         base = nil;
278         switch(type){
279         case Bool:
280                 if(strcmp(arg, "on")==0 || strcmp(arg, "1")==0)
281                         *(int*)a = 1;
282                 else
283                         *(int*)a = 0;
284                 break;
285         case String:
286                 free(*(char**)a);
287                 *(char**)a = estrdup(arg);
288                 break;
289         case XRel:
290                 base = ((Client*)a)->baseurl;
291         case XUrl:
292                 u = parseurl(arg, base);
293                 if(u == nil){
294                         snprint(e, sizeof e, "parseurl: %r");
295                         respond(r, e);
296                         return;
297                 }
298                 freeurl(*(Url**)a);
299                 *(Url**)a = u;
300                 break;
301         case Int:
302                 if(strcmp(arg, "on")==0)
303                         *(int*)a = 1;
304                 else
305                         *(int*)a = atoi(arg);
306                 break;
307         }
308         respond(r, nil);
309 }
310
311 int
312 ctlwrite(Req *r, Ctl *ctl, char *cmd, char *arg)
313 {
314         void *a;
315         Ctab *t;
316
317         if((t = findcmd(cmd, ctltab, nelem(ctltab))) == nil)
318                 return 0;
319         a = (void*)((uintptr)ctl+(uintptr)t->offset);
320         parseas(r, arg, t->type, a);
321         return 1;
322 }
323
324 int
325 clientctlwrite(Req *r, Client *c, char *cmd, char *arg)
326 {
327         void *a;
328         Ctab *t;
329
330         if((t = findcmd(cmd, clienttab, nelem(clienttab))) == nil)
331                 return 0;
332         a = (void*)((uintptr)c+(uintptr)t->offset);
333         parseas(r, arg, t->type, a);
334         return 1;
335 }
336
337 int
338 globalctlwrite(Req *r, char *cmd, char *arg)
339 {
340         void *a;
341         Ctab *t;
342
343         if((t = findcmd(cmd, globaltab, nelem(globaltab))) == nil)
344                 return 0;
345         a = t->offset;
346         parseas(r, arg, t->type, a);
347         return 1;
348 }
349
350 static void
351 ctlfmt(Ctl *c, char *s)
352 {
353         int i;
354         void *a;
355         char *t;
356
357         for(i=0; i<nelem(ctltab); i++){
358                 a = (void*)((uintptr)c+(uintptr)ctltab[i].offset);
359                 switch(ctltab[i].type){
360                 case Bool:
361                         s += sprint(s, "%s %s\n", ctltab[i].name, *(int*)a ? "on" : "off");
362                         break;
363                 case Int:
364                         s += sprint(s, "%s %d\n", ctltab[i].name, *(int*)a);
365                         break;
366                 case String:
367                         t = *(char**)a;
368                         if(t != nil)
369                                 s += sprint(s, "%s %.*s%s\n", ctltab[i].name, utfnlen(t, 100), t, strlen(t)>100 ? "..." : "");
370                         break;
371                 }
372         }
373 }
374
375 void
376 ctlread(Req *r, Client *c)
377 {
378         char buf[1024];
379
380         sprint(buf, "%11d \n", c->num);
381         ctlfmt(&c->ctl, buf+strlen(buf));
382         readstr(r, buf);
383         respond(r, nil);
384 }
385
386 void
387 globalctlread(Req *r)
388 {
389         char buf[1024], *s;
390         int i;
391
392         s = buf;
393         for(i=0; i<nelem(globaltab); i++)
394                 s += sprint(s, "%s %d\n", globaltab[i].name, *(int*)globaltab[i].offset);
395         ctlfmt(&globalctl, s);
396         readstr(r, buf);
397         respond(r, nil);
398 }