6 static char *hdrnext(char *s
, char *e
)
9 while (s
+ 1 < e
&& s
[0] != '\n' && (s
[0] != '\r' || s
[1] != '\n')) {
10 if (!(r
= memchr(s
, '\n', e
- s
- 1)))
12 if (isalpha((unsigned char) r
[1]))
19 /* return 1 if r starts with s */
20 int startswith(char *r
, char *s
)
23 if (tolower((unsigned char) *s
++) != tolower((unsigned char) *r
++))
28 char *msg_get(char *msg
, long msz
, char *hdr
)
33 if (startswith(s
, hdr
))
40 int msg_set(char *msg
, long msz
, char **mod
, long *modlen
, char *hdr
, char *val
)
42 char *h
= msg_get(msg
, msz
, hdr
);
44 int hlen
= h
? hdrlen(h
, msg
+ msz
- h
) : 0;
45 int vlen
= strlen(val
);
48 while (hdrnext(h
, msg
+ msz
))
49 h
= hdrnext(h
, msg
+ msz
);
50 h
= h
+ hdrlen(h
, msg
+ msz
- h
);
53 *modlen
= msz
- hlen
+ vlen
;
54 *mod
= malloc(*modlen
+ 1);
57 memcpy(*mod
, msg
, off
);
58 memcpy(*mod
+ off
, val
, vlen
);
59 memcpy(*mod
+ off
+ vlen
, msg
+ off
+ hlen
, msz
- off
- hlen
+ 1);
60 (*mod
)[*modlen
] = '\0';
64 int hdrlen(char *hdr
, long len
)
68 if (!(r
= strchr(s
, '\n')))
69 return strchr(s
, '\0') - hdr
;
71 } while (s
[0] && (s
[0] == ' ' || s
[0] == '\t'));