/* * Escape characters as %XX sequences. * Return value: New string. */ char *a_Misc_escape_chars(const char *str, const char *esc_set) { static const char *const hex = "0123456789ABCDEF"; char *p = NULL; Dstr *dstr; int i; dstr = dStr_sized_new(64); for (i = 0; str[i]; ++i) { if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) { dStr_append_c(dstr, '%'); dStr_append_c(dstr, hex[(str[i] >> 4) & 15]); dStr_append_c(dstr, hex[str[i] & 15]); } else {
/* * Escape URI characters in 'esc_set' as %XX sequences. * Return value: New escaped string. */ char *Escape_uri_str(const char *str, const char *p_esc_set) { static const char *esc_set, *hex = "0123456789ABCDEF"; char *p; Dstr *dstr; int i; esc_set = (p_esc_set) ? p_esc_set : "%#:' "; dstr = dStr_sized_new(64); for (i = 0; str[i]; ++i) { if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) { dStr_append_c(dstr, '%'); dStr_append_c(dstr, hex[(str[i] >> 4) & 15]); dStr_append_c(dstr, hex[str[i] & 15]); } else {
/* * Unquote the content of a (potentially) quoted string. * Return: newly allocated unquoted content. * * Arguments: * valuep: pointer to a pointer to the first char. * * Preconditions: * *valuep points to a correctly quoted and escaped string. * * Postconditions: * *valuep points to the first not processed char. * */ static Dstr *Auth_unquote_value(char **valuep) { char c, quoted; char *value = *valuep; Dstr *result; while (*value == ' ' || *value == '\t') value++; if ((quoted = *value == '"')) value++; result = dStr_new(NULL); while ((c = *value) && (( quoted && c != '"') || (!quoted && Auth_is_token_char(c)))) { dStr_append_c(result, (c == '\\' && value[1]) ? *++value : c); value++; } if (quoted && *value == '\"') value++; *valuep = value; return result; }
/* * Consume bytes until the whole header is got (up to a "\r\n\r\n" sequence) * (Also unfold multi-line fields and strip '\r' chars from header) */ static int Cache_get_header(CacheEntry_t *entry, const char *buf, size_t buf_size) { size_t N, i; Dstr *hdr = entry->Header; /* Header finishes when N = 2 */ N = (hdr->len && hdr->str[hdr->len - 1] == '\n'); for (i = 0; i < buf_size && N < 2; ++i) { if (buf[i] == '\r' || !buf[i]) continue; if (N == 1 && (buf[i] == ' ' || buf[i] == '\t')) { /* unfold multiple-line header */ _MSG("Multiple-line header!\n"); dStr_erase(hdr, hdr->len - 1, 1); } N = (buf[i] == '\n') ? N + 1 : 0; dStr_append_c(hdr, buf[i]); } if (N == 2) { /* Got whole header */ _MSG("Header [buf_size=%d]\n%s", i, hdr->str); entry->Flags |= CA_GotHeader; dStr_fit(hdr); /* Return number of header bytes in 'buf' [1 based] */ return i; } return 0; }
/* * Printf like function for building dpip commands. * It takes care of dpip escaping of its arguments. * NOTE : It ONLY accepts string parameters, and * only one %s per parameter. */ char *a_Dpip_build_cmd(const char *format, ...) { va_list argp; char *p, *q, *s; Dstr *cmd; /* Don't allow Quote characters in attribute names */ if (strchr(format, Quote)) return NULL; cmd = dStr_sized_new(64); dStr_append_c(cmd, '<'); va_start(argp, format); for (p = q = (char*)format; *q; ) { p = strstr(q, "%s"); if (!p) { dStr_append(cmd, q); break; } else { /* Copy format's part */ while (q != p) dStr_append_c(cmd, *q++); q += 2; dStr_append_c(cmd, Quote); /* Stuff-copy of argument */ s = va_arg (argp, char *); for ( ; *s; ++s) { dStr_append_c(cmd, *s); if (*s == Quote) dStr_append_c(cmd, *s); } dStr_append_c(cmd, Quote); } } va_end(argp); dStr_append_c(cmd, ' '); dStr_append_c(cmd, Quote); dStr_append_c(cmd, '>'); p = cmd->str; dStr_free(cmd, FALSE); return p; }
/* * Generate Content-Type header value for a POST query. */ static Dstr *Http_make_content_type(const DilloUrl *url) { Dstr *dstr; if (URL_FLAGS(url) & URL_MultipartEnc) { MSG("submitting multipart/form-data!\n"); dstr = dStr_new("multipart/form-data; boundary=\""); if (URL_DATA(url)->len > 2) { /* boundary lines have "--" prepended. Skip that. */ const char *start = URL_DATA(url)->str + 2; char *eol = strchr(start, '\r'); if (eol) dStr_append_l(dstr, start, eol - start); } else { /* Zero parts; arbitrary boundary */ dStr_append_c(dstr, '0'); } dStr_append_c(dstr,'"'); } else { dstr = dStr_new("application/x-www-form-urlencoded"); } return dstr; }