chiark / gitweb /
url: Allow `;' to separate key/value pairs in URL-encoded strings.
[mLib] / url.c
CommitLineData
0f2a8846 1/* -*-c-*-
2 *
8656dc50 3 * $Id: url.c,v 1.5 2004/04/08 01:36:13 mdw Exp $
0f2a8846 4 *
5 * Parsing and construction of url-encoded name/value pairs
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
0f2a8846 30/*----- Header files ------------------------------------------------------*/
31
78b1464e 32#include <ctype.h>
0f2a8846 33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include "dstr.h"
38#include "url.h"
39
40/*----- Main code ---------------------------------------------------------*/
41
42/* --- @url_initenc@ --- *
43 *
44 * Arguments: @url_ectx *ctx@ = pointer to context block
45 *
46 * Returns: ---
47 *
48 * Use: Initializes a URL encoding context.
49 */
50
0ddebb8f 51void url_initenc(url_ectx *ctx) { ctx->f = 0; }
0f2a8846 52
53/* --- @encode@ --- *
54 *
7e4708e4
MW
55 * Arguments: @url_ectx *ctx@ = encoding context
56 * @dstr *d@ = pointer to output string
0f2a8846 57 * @const char *p@ = pointer to thing to encode
58 *
59 * Returns: ---
60 *
61 * Use: Encodes the input string into the output string.
62 */
63
7e4708e4 64static void encode(url_ectx *ctx, dstr *d, const char *p)
0f2a8846 65{
66 while (*p) {
67 switch (*p) {
68 case ' ':
69 DPUTC(d, '+');
70 break;
71 default:
7e4708e4
MW
72 if ((ctx->f & URLF_LAX) || isalnum((unsigned char)*p))
73 goto safe;
74 else
75 goto unsafe;
76 case '/':
77 case '~':
78 if (ctx->f & URLF_STRICT)
79 goto unsafe;
80 case '-':
81 case '.':
82 case '_':
83 safe:
84 DPUTC(d, *p);
85 break;
86 unsafe:
87 case '+':
88 case '%':
89 case '=':
90 case '&':
91 case ';':
92 dstr_putf(d, "%%%02x", *p);
93 break;
0f2a8846 94 }
95 p++;
96 }
97}
98
99/* --- @url_enc@ --- *
100 *
101 * Arguments: @url_ectx *ctx@ = pointer to encoding context
102 * @dstr *d@ = pointer to output string
103 * @const char *name@ = pointer to name
104 * @const char *value@ = pointer to value
105 *
106 * Returns: ---
107 *
108 * Use: Writes an assignment between @name@ and @value@ to the
109 * output string, encoding the values properly.
110 */
111
112void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
113{
114 if (ctx->f & URLF_SEP)
9dc511e4 115 DPUTC(d, (ctx->f & URLF_SEMI) ? ';' : '&');
7e4708e4 116 encode(ctx, d, name);
0f2a8846 117 DPUTC(d, '=');
7e4708e4 118 encode(ctx, d, value);
0f2a8846 119 DPUTZ(d);
120 ctx->f |= URLF_SEP;
121}
122
123/* --- @url_initdec@ --- *
124 *
125 * Arguments: @url_dctx *ctx@ = pointer to context block
126 * @const char *p@ = string to read data from
127 *
128 * Returns: ---
129 *
130 * Use: Initializes a URL decoding context.
131 */
132
9dc511e4 133void url_initdec(url_dctx *ctx, const char *p) { ctx->p = p; ctx->f = 0; }
0f2a8846 134
135/* --- @decode@ --- *
136 *
9dc511e4
MW
137 * Arguments: @url_dctx *ctx@ = pointer to the context
138 * @dstr *d@ = pointer to output string
0f2a8846 139 * @const char *p@ = pointer to input data
140 * @int eq@ = whether to stop at `=' characters
141 *
142 * Returns: Pointer to next available character.
143 *
144 * Use: Does a URL decode.
145 */
146
9dc511e4 147static const char *decode(url_dctx *ctx, dstr *d, const char *p, int eq)
0f2a8846 148{
149 if (!*p)
150 return (0);
151 for (;;) {
152 switch (*p) {
153 case '=':
154 if (eq)
155 return (p);
9dc511e4
MW
156 goto boring;
157 case ';':
158 if (ctx->f & URLF_SEMI)
159 return (p);
160 goto boring;
0f2a8846 161 case 0:
162 case '&':
163 return (p);
164 case '+':
165 DPUTC(d, ' ');
166 break;
167 case '%': {
168 unsigned int ch;
169 int n;
170 int x = sscanf(p + 1, "%2x%n", &ch, &n);
171 if (x == 1) {
172 DPUTC(d, ch);
173 p += n;
174 break;
175 }
176 }
177 default:
9dc511e4 178 boring:
0f2a8846 179 DPUTC(d, *p);
180 break;
181 }
182 p++;
183 }
184}
185
186/* --- @url_dec@ --- *
187 *
188 * Arguments: @url_dctx *ctx@ = pointer to decode context
189 * @dstr *n@ = pointer to output string for name
190 * @dstr *v@ = pointer to output string for value
191 *
192 * Returns: Nonzero if it read something, zero if there's nothing left
193 *
194 * Use: Decodes the next name/value pair from a urlencoded string.
195 */
196
197int url_dec(url_dctx *ctx, dstr *n, dstr *v)
198{
199 const char *p = ctx->p;
200 size_t l = n->len;
201
202again:
9dc511e4 203 if ((p = decode(ctx, n, p, 1)) == 0 || *p == 0)
0f2a8846 204 return (0);
205 if (*p != '=') {
206 p++;
207 n->len = l;
208 goto again;
209 }
210 p++;
9dc511e4 211 if ((p = decode(ctx, v, p, 0)) == 0)
0f2a8846 212 return (0);
213 DPUTZ(n);
214 DPUTZ(v);
215 ctx->p = p;
216 return (1);
217}
218
219/*----- That's all, folks -------------------------------------------------*/