.BR dstr (3)
for details).
.PP
+You can set flags in the encoding context's
+.B f
+member:
+.TP
+.B URLF_STRICT
+Be strict about escaping non-alphanumeric characters. Without this,
+potentially unsafe characters such as
+.RB ` / '
+and
+.RB ` ~ '
+will be left unescaped, which makes encoded filenames (for example) more
+readable.
+.TP
+.B URLF_LAX
+Be very lax about non-alphanumeric characters. Everything except
+obviously-unsafe characters like
+.RB ` & '
+and
+.RB ` = '
+are left unescaped.
+.PP
Decoding a sequence of name/value pairs is performed using the
.B url_dec
function. It requires as input a
/* --- @encode@ --- *
*
- * Arguments: @dstr *d@ = pointer to output string
+ * Arguments: @url_ectx *ctx@ = encoding context
+ * @dstr *d@ = pointer to output string
* @const char *p@ = pointer to thing to encode
*
* Returns: ---
* Use: Encodes the input string into the output string.
*/
-static void encode(dstr *d, const char *p)
+static void encode(url_ectx *ctx, dstr *d, const char *p)
{
while (*p) {
switch (*p) {
DPUTC(d, '+');
break;
default:
- if (isalnum((unsigned char)*p))
- DPUTC(d, *p);
- else
- dstr_putf(d, "%%%02x", *p);
- break;
+ if ((ctx->f & URLF_LAX) || isalnum((unsigned char)*p))
+ goto safe;
+ else
+ goto unsafe;
+ case '/':
+ case '~':
+ if (ctx->f & URLF_STRICT)
+ goto unsafe;
+ case '-':
+ case '.':
+ case '_':
+ safe:
+ DPUTC(d, *p);
+ break;
+ unsafe:
+ case '+':
+ case '%':
+ case '=':
+ case '&':
+ case ';':
+ dstr_putf(d, "%%%02x", *p);
+ break;
}
p++;
}
{
if (ctx->f & URLF_SEP)
DPUTC(d, '&');
- encode(d, name);
+ encode(ctx, d, name);
DPUTC(d, '=');
- encode(d, value);
+ encode(ctx, d, value);
DPUTZ(d);
ctx->f |= URLF_SEP;
}