client.c 7.1 KB

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