chiark / gitweb /
532a052c37f947ef19ef7e7d89f700b9e1cf3219
[mLib] / url.c
1 /* -*-c-*-
2  *
3  * $Id: url.c,v 1.4 2001/06/22 19:36:18 mdw Exp $
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
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: url.c,v $
33  * Revision 1.4  2001/06/22 19:36:18  mdw
34  * Include @<ctype.h>@.
35  *
36  * Revision 1.3  2001/01/20 12:06:21  mdw
37  * Be more conservative in base-64 encoding.
38  *
39  * Revision 1.2  1999/09/03 08:02:05  mdw
40  * Make `#' a special character which needs escaping.
41  *
42  * Revision 1.1  1999/06/01 09:49:48  mdw
43  * New files for url-encoding and decoding.
44  *
45  */
46
47 /*----- Header files ------------------------------------------------------*/
48
49 #include <ctype.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "dstr.h"
55 #include "url.h"
56
57 /*----- Main code ---------------------------------------------------------*/
58
59 /* --- @url_initenc@ --- *
60  *
61  * Arguments:   @url_ectx *ctx@ = pointer to context block
62  *
63  * Returns:     ---
64  *
65  * Use:         Initializes a URL encoding context.
66  */
67
68 void url_initenc(url_ectx *ctx)
69 {
70   ctx->f = 0;
71 }
72
73 /* --- @encode@ --- *
74  *
75  * Arguments:   @dstr *d@ = pointer to output string
76  *              @const char *p@ = pointer to thing to encode
77  *
78  * Returns:     ---
79  *
80  * Use:         Encodes the input string into the output string.
81  */
82
83 static void encode(dstr *d, const char *p)
84 {
85   while (*p) {
86     switch (*p) {
87       case ' ':
88         DPUTC(d, '+');
89         break;
90       default:
91         if (isalnum((unsigned char)*p))
92           DPUTC(d, *p);
93         else
94           dstr_putf(d, "%%%02x", *p);
95         break;
96     }
97     p++;
98   }
99 }
100
101 /* --- @url_enc@ --- *
102  *
103  * Arguments:   @url_ectx *ctx@ = pointer to encoding context
104  *              @dstr *d@ = pointer to output string
105  *              @const char *name@ = pointer to name
106  *              @const char *value@ = pointer to value
107  *
108  * Returns:     ---
109  *
110  * Use:         Writes an assignment between @name@ and @value@ to the
111  *              output string, encoding the values properly.
112  */
113
114 void url_enc(url_ectx *ctx, dstr *d, const char *name, const char *value)
115 {
116   if (ctx->f & URLF_SEP)
117     DPUTC(d, '&');
118   encode(d, name);
119   DPUTC(d, '=');
120   encode(d, value);
121   DPUTZ(d);
122   ctx->f |= URLF_SEP;
123 }
124
125 /* --- @url_initdec@ --- *
126  *
127  * Arguments:   @url_dctx *ctx@ = pointer to context block
128  *              @const char *p@ = string to read data from
129  *
130  * Returns:     ---
131  *
132  * Use:         Initializes a URL decoding context.
133  */
134
135 void url_initdec(url_dctx *ctx, const char *p)
136 {
137   ctx->p = p;
138 }
139
140 /* --- @decode@ --- *
141  *
142  * Arguments:   @dstr *d@ = pointer to output string
143  *              @const char *p@ = pointer to input data
144  *              @int eq@ = whether to stop at `=' characters
145  *
146  * Returns:     Pointer to next available character.
147  *
148  * Use:         Does a URL decode.
149  */
150
151 static const char *decode(dstr *d, const char *p, int eq)
152 {
153   if (!*p)
154     return (0);
155   for (;;) {
156     switch (*p) {
157       case '=':
158         if (eq)
159           return (p);
160         DPUTC(d, *p);
161         break;
162       case 0:
163       case '&':
164         return (p);
165       case '+':
166         DPUTC(d, ' ');
167         break;
168       case '%': {
169         unsigned int ch;
170         int n;
171         int x = sscanf(p + 1, "%2x%n", &ch, &n);
172         if (x == 1) {
173           DPUTC(d, ch);
174           p += n;
175           break;
176         }
177       }
178       default:
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
197 int url_dec(url_dctx *ctx, dstr *n, dstr *v)
198 {
199   const char *p = ctx->p;
200   size_t l = n->len;
201
202 again:
203   if ((p = decode(n, p, 1)) == 0 || *p == 0)
204     return (0);
205   if (*p != '=') {
206     p++;
207     n->len = l;
208     goto again;
209   }
210   p++;
211   if ((p = decode(v, p, 0)) == 0)
212     return (0);
213   DPUTZ(n);
214   DPUTZ(v);
215   ctx->p = p;
216   return (1);
217 }
218
219 /*----- That's all, folks -------------------------------------------------*/