int _twflush(Fd *f) { int m; assert(f->mode == OWRITE); /* * this loop shouldn't be necessary - threadwrite * already tries to write the whole thing, but maybe * we'll get a better error this way. */ //fprint(2, "%s: twflush %p\n", argv0, f); while(f->p > f->buf){ //fprint(2, "%s: twflush: %p %d\n", argv0, f, f->p-f->buf); //fprint(2, "%s: twflush %d %d %.*H\n", argv0, f->fd, f->p-f->buf, f->p-f->buf, f->buf); m = iowrite(f->io, f->fd, f->buf, f->p-f->buf); //Xfprint(2, "%s: twflush: %p %d got %d - %d writes\n", argv0, f, f->p-f->buf, m, f->nw); dbg(DbgFdbuf, "_twflush fd=%d n=%d wrote=%d\n", f->fd, f->p-f->buf, m); if(m <= 0){ fprint(2, "%s: twflush: %r\n", argv0); return -1; } f->nw = 0; assert(f->p-f->buf >= m); f->p -= m; memmove(f->buf, f->buf+m, (f->p-f->buf)); } return 0; }
int xiowrite(int fd, void *v, int n) { int m; Ioproc *io; if((io = xioproc()) == nil) return -1; m = iowrite(io, fd, v, n); closexioproc(io); if(m != n) return -1; return n; }
static void writethread(void *a) { char e[ERRMAX]; uchar *buf; int n; Ioproc *io; Req *r; Client *c; char tmp[32]; c = a; snprint(tmp, sizeof tmp, "write%d", c->num); threadsetname(tmp); buf = emalloc(8192); io = c->writerproc; for(;;){ while(c->wq == nil){ if(c->moribund) goto Out; recvp(c->writerkick); if(c->moribund) goto Out; } r = c->wq; c->wq = r->aux; c->curw = r; n = iowrite(io, c->fd[1], r->ifcall.data, r->ifcall.count); c->curw = nil; if(chatty9p) fprint(2, "io->write returns %d\n", n); if(n >= 0){ r->ofcall.count = n; respond(r, nil); }else{ rerrstr(e, sizeof e); respond(r, e); } } Out: free(buf); die(c); }
int mwrite9p(Ioproc *io, int fd, uchar *pkt) { int n, nfd; n = GBIT32(pkt); if(verbose > 2) fprint(2, "%T write %d %d %.*H\n", fd, n, n, pkt); if(verbose > 1) fprint(2, "%T before iowrite\n"); if(iowrite(io, fd, pkt, n) != n){ fprint(2, "%T write error: %r\n"); return -1; } if(verbose > 1) fprint(2, "%T after iowrite\n"); if(pkt[4] == Ropenfd){ nfd = GBIT32(pkt+n-4); if(iosendfd(io, fd, nfd) < 0){ fprint(2, "%T send fd error: %r\n"); return -1; } } return 0; }
int httpopen(Client *c, Url *url) { int fd, code, redirect, authenticate; char *cookies; Ioproc *io; HttpState *hs; char *service; if(httpdebug) fprint(2, "httpopen\n"); io = c->io; hs = emalloc(sizeof(*hs)); hs->c = c; if(url->port) service = url->port; else service = url->scheme; hs->netaddr = estrdup(netmkaddr(url->host, 0, service)); c->aux = hs; if(httpdebug){ fprint(2, "dial %s\n", hs->netaddr); fprint(2, "dial port: %s\n", url->port); } fd = iotlsdial(io, hs->netaddr, 0, 0, 0, url->ischeme==UShttps); if(fd < 0){ Error: if(httpdebug) fprint(2, "iodial: %r\n"); free(hs->location); free(hs->setcookie); free(hs->netaddr); free(hs->credentials); if(fd >= 0) ioclose(io, hs->fd); hs->fd = -1; free(hs); c->aux = nil; return -1; } hs->fd = fd; if(httpdebug) fprint(2, "<- %s %s HTTP/1.0\n<- Host: %s\n", c->havepostbody? "POST": "GET", url->http.page_spec, url->host); ioprint(io, fd, "%s %s HTTP/1.0\r\nHost: %s\r\n", c->havepostbody? "POST" : "GET", url->http.page_spec, url->host); if(httpdebug) fprint(2, "<- User-Agent: %s\n", c->ctl.useragent); if(c->ctl.useragent) ioprint(io, fd, "User-Agent: %s\r\n", c->ctl.useragent); if(c->ctl.sendcookies){ /* should we use url->page here? sometimes it is nil. */ cookies = httpcookies(url->host, url->http.page_spec, url->ischeme == UShttps); if(cookies && cookies[0]) ioprint(io, fd, "%s", cookies); if(httpdebug) fprint(2, "<- %s", cookies); free(cookies); } if(c->havepostbody){ ioprint(io, fd, "Content-type: %s\r\n", PostContentType); ioprint(io, fd, "Content-length: %ud\r\n", c->npostbody); if(httpdebug){ fprint(2, "<- Content-type: %s\n", PostContentType); fprint(2, "<- Content-length: %ud\n", c->npostbody); } } if(c->authenticate){ ioprint(io, fd, "Authorization: %s\r\n", c->authenticate); if(httpdebug) fprint(2, "<- Authorization: %s\n", c->authenticate); } ioprint(io, fd, "\r\n"); if(c->havepostbody) if(iowrite(io, fd, c->postbody, c->npostbody) != c->npostbody) goto Error; redirect = 0; authenticate = 0; initibuf(&hs->b, io, fd); code = httprcode(hs); switch(code){ case -1: /* connection timed out */ goto Error; /* case Eof: werrstr("EOF from HTTP server"); goto Error; */ case 200: /* OK */ case 201: /* Created */ case 202: /* Accepted */ case 204: /* No Content */ case 205: /* Reset Content */ #ifdef NOT_DEFINED if(ofile == nil && r->start != 0) sysfatal("page changed underfoot"); #endif break; case 206: /* Partial Content */ werrstr("Partial Content (206)"); goto Error; case 301: /* Moved Permanently */ case 302: /* Moved Temporarily */ case 303: /* See Other */ case 307: /* Temporary Redirect */ redirect = 1; break; case 304: /* Not Modified */ break; case 400: /* Bad Request */ werrstr("Bad Request (400)"); goto Error; case 401: /* Unauthorized */ if(c->authenticate){ werrstr("Authentication failed (401)"); goto Error; } authenticate = 1; break; case 402: /* Payment Required */ werrstr("Payment Required (402)"); goto Error; case 403: /* Forbidden */ werrstr("Forbidden by server (403)"); goto Error; case 404: /* Not Found */ werrstr("Not found on server (404)"); goto Error; case 405: /* Method Not Allowed */ werrstr("Method not allowed (405)"); goto Error; case 406: /* Not Acceptable */ werrstr("Not Acceptable (406)"); goto Error; case 407: /* Proxy auth */ werrstr("Proxy authentication required (407)"); goto Error; case 408: /* Request Timeout */ werrstr("Request Timeout (408)"); goto Error; case 409: /* Conflict */ werrstr("Conflict (409)"); goto Error; case 410: /* Gone */ werrstr("Gone (410)"); goto Error; case 411: /* Length Required */ werrstr("Length Required (411)"); goto Error; case 412: /* Precondition Failed */ werrstr("Precondition Failed (412)"); goto Error; case 413: /* Request Entity Too Large */ werrstr("Request Entity Too Large (413)"); goto Error; case 414: /* Request-URI Too Long */ werrstr("Request-URI Too Long (414)"); goto Error; case 415: /* Unsupported Media Type */ werrstr("Unsupported Media Type (415)"); goto Error; case 416: /* Requested Range Not Satisfiable */ werrstr("Requested Range Not Satisfiable (416)"); goto Error; case 417: /* Expectation Failed */ werrstr("Expectation Failed (417)"); goto Error; case 500: /* Internal server error */ werrstr("Server choked (500)"); goto Error; case 501: /* Not implemented */ werrstr("Server can't do it (501)"); goto Error; case 502: /* Bad gateway */ werrstr("Bad gateway (502)"); goto Error; case 503: /* Service unavailable */ werrstr("Service unavailable (503)"); goto Error; default: /* Bogus: we should treat unknown code XYZ as code X00 */ werrstr("Unknown response code %d", code); goto Error; } if(httpheaders(hs) < 0) goto Error; if(c->ctl.acceptcookies && hs->setcookie) httpsetcookie(hs->setcookie, url->host, url->path); if(authenticate){ if(!hs->credentials){ if(hs->autherror[0]) werrstr("%s", hs->autherror); else werrstr("unauthorized; no www-authenticate: header"); goto Error; } c->authenticate = hs->credentials; hs->credentials = nil; }else if(c->authenticate) c->authenticate = 0; if(redirect){ if(!hs->location){ werrstr("redirection without Location: header"); goto Error; } c->redirect = hs->location; hs->location = nil; } return 0; }
static void openfdthread(void *v) { Conn *c; Fid *fid; Msg *m; int n; vlong tot; Ioproc *io; char buf[1024]; c = v; fid = c->fdfid; io = ioproc(); threadsetname("openfd %s", c->fdfid); tot = 0; m = nil; if(c->fdmode == OREAD){ for(;;){ if(verbose) fprint(2, "%T tread..."); m = msgnew(0); m->internal = 1; m->c = c; m->tx.type = Tread; m->tx.count = msize - IOHDRSZ; m->tx.fid = fid->fid; m->tx.tag = m->tag; m->tx.offset = tot; m->fid = fid; fid->ref++; msgincref(m); sendomsg(m); recvp(c->internal); if(m->rx.type == Rerror){ /* fprint(2, "%T read error: %s\n", m->rx.ename); */ break; } if(m->rx.count == 0) break; tot += m->rx.count; if(iowrite(io, c->fd, m->rx.data, m->rx.count) != m->rx.count){ /* fprint(2, "%T pipe write error: %r\n"); */ break; } msgput(m); msgput(m); m = nil; } }else{ for(;;){ if(verbose) fprint(2, "%T twrite..."); n = sizeof buf; if(n > msize) n = msize; if((n=ioread(io, c->fd, buf, n)) <= 0){ if(n < 0) fprint(2, "%T pipe read error: %r\n"); break; } m = msgnew(0); m->internal = 1; m->c = c; m->tx.type = Twrite; m->tx.fid = fid->fid; m->tx.data = buf; m->tx.count = n; m->tx.tag = m->tag; m->tx.offset = tot; m->fid = fid; fid->ref++; msgincref(m); sendomsg(m); recvp(c->internal); if(m->rx.type == Rerror){ /* fprint(2, "%T write error: %s\n", m->rx.ename); */ } tot += n; msgput(m); msgput(m); m = nil; } } if(verbose) fprint(2, "%T eof on %d fid %d\n", c->fd, fid->fid); close(c->fd); closeioproc(io); if(m){ msgput(m); msgput(m); } if(verbose) fprint(2, "%T eof on %d fid %d ref %d\n", c->fd, fid->fid, fid->ref); if(--fid->openfd == 0){ m = msgnew(0); m->internal = 1; m->c = c; m->tx.type = Tclunk; m->tx.tag = m->tag; m->tx.fid = fid->fid; m->fid = fid; fid->ref++; msgincref(m); sendomsg(m); recvp(c->internal); msgput(m); msgput(m); } fidput(fid); c->fdfid = nil; chanfree(c->internal); c->internal = 0; free(c); }