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 | |
51 | void url_initenc(url_ectx *ctx) |
52 | { |
53 | ctx->f = 0; |
54 | } |
55 | |
56 | /* --- @encode@ --- * |
57 | * |
58 | * Arguments: @dstr *d@ = pointer to output string |
59 | * @const char *p@ = pointer to thing to encode |
60 | * |
61 | * Returns: --- |
62 | * |
63 | * Use: Encodes the input string into the output string. |
64 | */ |
65 | |
66 | static void encode(dstr *d, const char *p) |
67 | { |
68 | while (*p) { |
69 | switch (*p) { |
70 | case ' ': |
71 | DPUTC(d, '+'); |
72 | break; |
73 | default: |
000b4322 |
74 | if (isalnum((unsigned char)*p)) |
0f2a8846 |
75 | DPUTC(d, *p); |
76 | else |
000b4322 |
77 | dstr_putf(d, "%%%02x", *p); |
0f2a8846 |
78 | break; |
79 | } |
80 | p++; |
81 | } |
82 | } |
83 | |
84 | /* --- @url_enc@ --- * |
85 | * |
86 | * Arguments: @url_ectx *ctx@ = pointer to encoding context |
87 | * @dstr *d@ = pointer to output string |
88 | * @const char *name@ = pointer to name |
89 | * @const char *value@ = pointer to value |
90 | * |
91 | * Returns: --- |
92 | * |
93 | * Use: Writes an assignment between @name@ and @value@ to the |
94 | * output string, encoding the values properly. |
95 | */ |
96 | |
97 | void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value) |
98 | { |
99 | if (ctx->f & URLF_SEP) |
100 | DPUTC(d, '&'); |
101 | encode(d, name); |
102 | DPUTC(d, '='); |
103 | encode(d, value); |
104 | DPUTZ(d); |
105 | ctx->f |= URLF_SEP; |
106 | } |
107 | |
108 | /* --- @url_initdec@ --- * |
109 | * |
110 | * Arguments: @url_dctx *ctx@ = pointer to context block |
111 | * @const char *p@ = string to read data from |
112 | * |
113 | * Returns: --- |
114 | * |
115 | * Use: Initializes a URL decoding context. |
116 | */ |
117 | |
118 | void url_initdec(url_dctx *ctx, const char *p) |
119 | { |
120 | ctx->p = p; |
121 | } |
122 | |
123 | /* --- @decode@ --- * |
124 | * |
125 | * Arguments: @dstr *d@ = pointer to output string |
126 | * @const char *p@ = pointer to input data |
127 | * @int eq@ = whether to stop at `=' characters |
128 | * |
129 | * Returns: Pointer to next available character. |
130 | * |
131 | * Use: Does a URL decode. |
132 | */ |
133 | |
134 | static const char *decode(dstr *d, const char *p, int eq) |
135 | { |
136 | if (!*p) |
137 | return (0); |
138 | for (;;) { |
139 | switch (*p) { |
140 | case '=': |
141 | if (eq) |
142 | return (p); |
143 | DPUTC(d, *p); |
144 | break; |
145 | case 0: |
146 | case '&': |
147 | return (p); |
148 | case '+': |
149 | DPUTC(d, ' '); |
150 | break; |
151 | case '%': { |
152 | unsigned int ch; |
153 | int n; |
154 | int x = sscanf(p + 1, "%2x%n", &ch, &n); |
155 | if (x == 1) { |
156 | DPUTC(d, ch); |
157 | p += n; |
158 | break; |
159 | } |
160 | } |
161 | default: |
162 | DPUTC(d, *p); |
163 | break; |
164 | } |
165 | p++; |
166 | } |
167 | } |
168 | |
169 | /* --- @url_dec@ --- * |
170 | * |
171 | * Arguments: @url_dctx *ctx@ = pointer to decode context |
172 | * @dstr *n@ = pointer to output string for name |
173 | * @dstr *v@ = pointer to output string for value |
174 | * |
175 | * Returns: Nonzero if it read something, zero if there's nothing left |
176 | * |
177 | * Use: Decodes the next name/value pair from a urlencoded string. |
178 | */ |
179 | |
180 | int url_dec(url_dctx *ctx, dstr *n, dstr *v) |
181 | { |
182 | const char *p = ctx->p; |
183 | size_t l = n->len; |
184 | |
185 | again: |
186 | if ((p = decode(n, p, 1)) == 0 || *p == 0) |
187 | return (0); |
188 | if (*p != '=') { |
189 | p++; |
190 | n->len = l; |
191 | goto again; |
192 | } |
193 | p++; |
194 | if ((p = decode(v, p, 0)) == 0) |
195 | return (0); |
196 | DPUTZ(n); |
197 | DPUTZ(v); |
198 | ctx->p = p; |
199 | return (1); |
200 | } |
201 | |
202 | /*----- That's all, folks -------------------------------------------------*/ |