From 9dc511e46d5acad665175981f824288062e27671 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Wed, 15 Mar 2006 01:31:26 +0000 Subject: [PATCH] url: Allow `;' to separate key/value pairs in URL-encoded strings. The RFC recommends this be allowed, because `&' is special in HTML. It's also used by various CGI scripts. --- url.c | 21 +++++++++++++-------- url.h | 9 ++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/url.c b/url.c index f7e11b3..2dc1c50 100644 --- a/url.c +++ b/url.c @@ -112,7 +112,7 @@ static void encode(url_ectx *ctx, dstr *d, const char *p) void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value) { if (ctx->f & URLF_SEP) - DPUTC(d, '&'); + DPUTC(d, (ctx->f & URLF_SEMI) ? ';' : '&'); encode(ctx, d, name); DPUTC(d, '='); encode(ctx, d, value); @@ -130,11 +130,12 @@ void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value) * Use: Initializes a URL decoding context. */ -void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; } +void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; ctx->f = 0; } /* --- @decode@ --- * * - * Arguments: @dstr *d@ = pointer to output string + * Arguments: @url_dctx *ctx@ = pointer to the context + * @dstr *d@ = pointer to output string * @const char *p@ = pointer to input data * @int eq@ = whether to stop at `=' characters * @@ -143,7 +144,7 @@ void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; } * Use: Does a URL decode. */ -static const char *decode(dstr *d, const char *p, int eq) +static const char *decode(url_dctx *ctx, dstr *d, const char *p, int eq) { if (!*p) return (0); @@ -152,8 +153,11 @@ static const char *decode(dstr *d, const char *p, int eq) case '=': if (eq) return (p); - DPUTC(d, *p); - break; + goto boring; + case ';': + if (ctx->f & URLF_SEMI) + return (p); + goto boring; case 0: case '&': return (p); @@ -171,6 +175,7 @@ static const char *decode(dstr *d, const char *p, int eq) } } default: + boring: DPUTC(d, *p); break; } @@ -195,7 +200,7 @@ int url_dec(url_dctx *ctx, dstr *n, dstr *v) size_t l = n->len; again: - if ((p = decode(n, p, 1)) == 0 || *p == 0) + if ((p = decode(ctx, n, p, 1)) == 0 || *p == 0) return (0); if (*p != '=') { p++; @@ -203,7 +208,7 @@ again: goto again; } p++; - if ((p = decode(v, p, 0)) == 0) + if ((p = decode(ctx, v, p, 0)) == 0) return (0); DPUTZ(n); DPUTZ(v); diff --git a/url.h b/url.h index be38bdc..6f82d12 100644 --- a/url.h +++ b/url.h @@ -46,14 +46,17 @@ typedef struct url_ectx { unsigned f; } url_ectx; -#define URLF_SEP 1u -#define URLF_STRICT 2u -#define URLF_LAX 4u typedef struct url_dctx { const char *p; + unsigned f; } url_dctx; +#define URLF_SEP 1u +#define URLF_STRICT 2u +#define URLF_LAX 4u +#define URLF_SEMI 8u + /*----- Functions provided ------------------------------------------------*/ /* --- @url_initenc@ --- * -- 2.11.0