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)
58 free(c->authenticate);
59 c->authenticate = nil;
70 c->useragent = estrdup(c->useragent);
74 clientbodyopen(Client *c, Req *r)
76 char e[ERRMAX], *next;
82 for(i=0; i<=c->ctl.redirectlimit; i++){
87 if(c->url->open == nil){
88 werrstr("unsupported url type");
92 fprint(2, "try %s\n", c->url->url);
93 if(c->url->open(c, c->url) < 0){
96 fprint(2, "next %s (but for error)\n", next);
102 closeclient(c); /* not opening */
107 if (c->authenticate && nauth++ < 1)
113 if(i==c->ctl.redirectlimit){
114 werrstr("redirect limit reached");
117 if((u = parseurl(next, c->url)) == nil)
120 fprint(2, "parseurl %s got scheme %d\n", next, u->ischeme);
121 if(u->ischeme == USunknown){
122 werrstr("redirect with unknown URL scheme");
125 if(u->ischeme == UScurrent){
126 werrstr("redirect to URL relative to current document");
139 plumburl(char *url, char *base)
147 ubase = parseurl(base, nil);
151 uurl = parseurl(url, ubase);
165 clientbodyread(Client *c, Req *r)
169 if(c->url->read == nil){
170 respond(r, "unsupported url type");
173 if(c->url->read(c, r) < 0){
174 rerrstr(e, sizeof e);
184 clientthread(void *a)
193 fprint(2, "bad url got plumbed\n");
196 clientbodyopen(c, nil);
199 while((r = recvp(c->creq)) != nil){
201 fprint(2, "clientthread %F\n", &r->ifcall);
202 switch(r->ifcall.type){
206 c->ref--; /* from plumburl() */
210 clientbodyopen(c, r);
213 clientbodyread(c, r);
219 fprint(2, "clientthread finished req\n");
233 typedef struct Ctab Ctab;
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),
248 "chatty9p", Int, &chatty9p,
249 "fsdebug", Int, &fsdebug,
250 "cookiedebug", Int, &cookiedebug,
251 "urldebug", Int, &urldebug,
252 "httpdebug", Int, &httpdebug,
256 "baseurl", XUrl, (void*)offsetof(Client, baseurl),
257 "url", XRel, (void*)offsetof(Client, url),
261 findcmd(char *cmd, Ctab *tab, int ntab)
265 for(i=0; i<ntab; i++)
266 if(strcmp(tab[i].name, cmd) == 0)
272 parseas(Req *r, char *arg, int type, void *a)
280 if(strcmp(arg, "on")==0 || strcmp(arg, "1")==0)
287 *(char**)a = estrdup(arg);
290 base = ((Client*)a)->baseurl;
292 u = parseurl(arg, base);
294 snprint(e, sizeof e, "parseurl: %r");
302 if(strcmp(arg, "on")==0)
305 *(int*)a = atoi(arg);
312 ctlwrite(Req *r, Ctl *ctl, char *cmd, char *arg)
317 if((t = findcmd(cmd, ctltab, nelem(ctltab))) == nil)
319 a = (void*)((uintptr)ctl+(uintptr)t->offset);
320 parseas(r, arg, t->type, a);
325 clientctlwrite(Req *r, Client *c, char *cmd, char *arg)
330 if((t = findcmd(cmd, clienttab, nelem(clienttab))) == nil)
332 a = (void*)((uintptr)c+(uintptr)t->offset);
333 parseas(r, arg, t->type, a);
338 globalctlwrite(Req *r, char *cmd, char *arg)
343 if((t = findcmd(cmd, globaltab, nelem(globaltab))) == nil)
346 parseas(r, arg, t->type, a);
351 ctlfmt(Ctl *c, char *s)
357 for(i=0; i<nelem(ctltab); i++){
358 a = (void*)((uintptr)c+(uintptr)ctltab[i].offset);
359 switch(ctltab[i].type){
361 s += sprint(s, "%s %s\n", ctltab[i].name, *(int*)a ? "on" : "off");
364 s += sprint(s, "%s %d\n", ctltab[i].name, *(int*)a);
369 s += sprint(s, "%s %.*s%s\n", ctltab[i].name, utfnlen(t, 100), t, strlen(t)>100 ? "..." : "");
376 ctlread(Req *r, Client *c)
380 sprint(buf, "%11d \n", c->num);
381 ctlfmt(&c->ctl, buf+strlen(buf));
387 globalctlread(Req *r)
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);