X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Freq.c;h=8ea2fbc0241e2552d9cd7b51a45a9b215533dbca;hb=c3b910928f34306f34ee6a9c3c13debbf8ff67f4;hp=0ede22906869c55d308fd9f3e5e4d9632ce35fe4;hpb=9e9eca797684e57318dde54f5a89a3029181590e;p=ashd.git diff --git a/lib/req.c b/lib/req.c index 0ede229..8ea2fbc 100644 --- a/lib/req.c +++ b/lib/req.c @@ -21,6 +21,8 @@ #include #include #include +#include +#include #ifdef HAVE_CONFIG_H #include @@ -89,12 +91,79 @@ char *getheader(struct hthead *head, char *name) return(NULL); } +static void trim(struct charbuf *buf) +{ + char *p; + + for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++); + memmove(buf->b, p, buf->d -= (p - buf->b)); + for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--); +} + +int parseheaders(struct hthead *head, FILE *in) +{ + int c, state; + struct charbuf name, val; + + bufinit(name); + bufinit(val); + state = 0; + while(1) { + c = fgetc(in); + again: + if(state == 0) { + if(c == '\r') { + } else if(c == '\n') { + break; + } else if(c == EOF) { + goto fail; + } else { + state = 1; + goto again; + } + } else if(state == 1) { + if(c == ':') { + trim(&name); + bufadd(name, 0); + state = 2; + } else if(c == '\r') { + } else if(c == '\n') { + goto fail; + } else if(c == EOF) { + goto fail; + } else { + bufadd(name, c); + } + } else if(state == 2) { + if(c == '\r') { + } else if(c == '\n') { + trim(&val); + bufadd(val, 0); + headappheader(head, name.b, val.b); + buffree(name); + buffree(val); + state = 0; + } else if(c == EOF) { + goto fail; + } else { + bufadd(val, c); + } + } + } + return(0); + +fail: + buffree(name); + buffree(val); + return(-1); +} + void replrest(struct hthead *head, char *rest) { char *tmp; /* Do not free the current rest string yet, so that the new one - * can a subpart of the old one. */ + * can be taken from a subpart of the old one. */ tmp = head->rest; head->rest = sstrdup(rest); free(tmp); @@ -121,14 +190,24 @@ void headappheader(struct hthead *head, const char *name, const char *val) head->headers[i][1] = sstrdup(val); } -int sendreq(int sock, struct hthead *req) +int writeresp(FILE *out, struct hthead *resp) +{ + int i; + + if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0) + return(-1); + for(i = 0; i < resp->noheaders; i++) { + if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0) + return(-1); + } + return(0); +} + +int sendreq(int sock, struct hthead *req, int fd) { int ret, i; - int pfds[2]; struct charbuf buf; - if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds)) - return(-1); bufinit(buf); bufcatstr2(buf, req->method); bufcatstr2(buf, req->url); @@ -139,15 +218,12 @@ int sendreq(int sock, struct hthead *req) bufcatstr2(buf, req->headers[i][1]); } bufcatstr2(buf, ""); - ret = sendfd(sock, pfds[0], buf.b, buf.d); + ret = sendfd(sock, fd, buf.b, buf.d); buffree(buf); - close(pfds[0]); - if(ret < 0) { - close(pfds[1]); + if(ret < 0) return(-1); - } else { - return(pfds[1]); - } + else + return(0); } int recvreq(int sock, struct hthead **reqp)