+static void trim(struct charbuf *buf)
+{
+ char *p;
+
+ if(buf->d > 0) {
+ 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 be taken from a subpart of the old one. */
+ tmp = head->rest;
+ head->rest = sstrdup(rest);
+ free(tmp);
+}
+