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