15 static void clientthread(void*);
17 newclient(int plumbed)
22 for(i=0; i<nclient; i++)
26 c = emalloc(sizeof(Client));
28 c->creq = chancreate(sizeof(Req*), 8);
29 threadcreate(clientthread, c, STACK);
36 client = erealloc(client, (nclient+16)*sizeof(client[0]));
37 client[nclient++] = c;
42 closeclient(Client *c)
46 if(c->url && c->url->close)
60 free(c->authenticate);
61 c->authenticate = nil;
72 c->useragent = estrdup(c->useragent);
76 clientbodyopen(Client *c, Req *r)
78 char e[ERRMAX], *next, *frag;
84 for(i=0; i<=c->ctl.redirectlimit; i++){
89 if(c->url->open == nil){
90 werrstr("unsupported url type");
94 fprint(2, "try %s\n", c->url->url);
95 if(c->url->open(c, c->url) < 0){
99 fprint(2, "next %s (but for error)\n", next);
104 closeclient(c); /* not opening */
112 if(c->authenticate && nauth++ < 1){
119 if(i==c->ctl.redirectlimit){
120 werrstr("redirect limit reached");
123 if((u = parseurl(next, c->url)) == nil)
125 /* if there was a redirect, carry over the fragment */
126 if((frag = c->url->fragment) && u->fragment == nil){
127 u->fragment = estrdup(frag);
131 fprint(2, "parseurl %s got scheme %d\n", next, u->ischeme);
132 if(u->ischeme == USunknown){
133 werrstr("redirect with unknown URL scheme");
136 if(u->ischeme == UScurrent){
137 werrstr("redirect to URL relative to current document");
152 plumburl(char *url, char *base)
160 ubase = parseurl(base, nil);
164 uurl = parseurl(url, ubase);
178 clientbodyread(Client *c, Req *r)
182 if(c->url->read == nil){
183 respond(r, "unsupported url type");
186 if(c->url->read(c, r) < 0){
187 rerrstr(e, sizeof e);
197 clientthread(void *a)
206 fprint(2, "bad url got plumbed\n");
209 clientbodyopen(c, nil);
212 while((r = recvp(c->creq)) != nil){
214 fprint(2, "clientthread %F\n", &r->ifcall);
215 switch(r->ifcall.type){
219 c->ref--; /* from plumburl() */
223 clientbodyopen(c, r);
226 clientbodyread(c, r);
232 fprint(2, "clientthread finished req\n");
246 typedef struct Ctab Ctab;
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),
261 "chatty9p", Int, &chatty9p,
262 "fsdebug", Int, &fsdebug,
263 "cookiedebug", Int, &cookiedebug,
264 "urldebug", Int, &urldebug,
265 "httpdebug", Int, &httpdebug,
269 "baseurl", XUrl, (void*)offsetof(Client, baseurl),
270 "url", XRel, (void*)offsetof(Client, url),
274 findcmd(char *cmd, Ctab *tab, int ntab)
278 for(i=0; i<ntab; i++)
279 if(strcmp(tab[i].name, cmd) == 0)
285 parseas(Req *r, char *arg, int type, void *a)
293 if(strcmp(arg, "on")==0 || strcmp(arg, "1")==0)
300 *(char**)a = estrdup(arg);
303 base = ((Client*)a)->baseurl;
305 u = parseurl(arg, base);
307 snprint(e, sizeof e, "parseurl: %r");
315 if(strcmp(arg, "on")==0)
318 *(int*)a = atoi(arg);
325 ctlwrite(Req *r, Ctl *ctl, char *cmd, char *arg)
330 if((t = findcmd(cmd, ctltab, nelem(ctltab))) == nil)
332 a = (void*)((uintptr)ctl+(uintptr)t->offset);
333 parseas(r, arg, t->type, a);
338 clientctlwrite(Req *r, Client *c, char *cmd, char *arg)
343 if((t = findcmd(cmd, clienttab, nelem(clienttab))) == nil)
345 a = (void*)((uintptr)c+(uintptr)t->offset);
346 parseas(r, arg, t->type, a);
351 globalctlwrite(Req *r, char *cmd, char *arg)
356 if((t = findcmd(cmd, globaltab, nelem(globaltab))) == nil)
359 parseas(r, arg, t->type, a);
364 ctlfmt(Ctl *c, char *s)
370 for(i=0; i<nelem(ctltab); i++){
371 a = (void*)((uintptr)c+(uintptr)ctltab[i].offset);
372 switch(ctltab[i].type){
374 s += sprint(s, "%s %s\n", ctltab[i].name, *(int*)a ? "on" : "off");
377 s += sprint(s, "%s %d\n", ctltab[i].name, *(int*)a);
382 s += sprint(s, "%s %.*s%s\n", ctltab[i].name, utfnlen(t, 100), t, strlen(t)>100 ? "..." : "");
389 ctlread(Req *r, Client *c)
393 sprint(buf, "%11d \n", c->num);
394 ctlfmt(&c->ctl, buf+strlen(buf));
400 globalctlread(Req *r)
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);