3 .\" Manual for form-urlencoding
5 .\" (c) 1999, 2001, 2005--2007, 2009, 2023, 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH url 3mLib "20 June 1999" "Straylight/Edgeware" "mLib utilities library"
37 .\"--------------------------------------------------------------------------
39 url \- manipulation of form-urlencoded strings
41 .\"--------------------------------------------------------------------------
45 .B "#include <mLib/url.h>"
58 .B "#define URLF_STRICT ..."
59 .B "#define URLF_LAX ..."
60 .B "#define URLF_SEMI ..."
62 .BI "void url_initenc(url_ectx *" ctx );
63 .ta \w'\fBvoid url_enc('u
64 .BI "void url_enc(url_ectx *" ctx ", dstr *" d ,
65 .BI " const char *" name ", const char *" value );
67 .BI "void url_initdec(url_dctx *" ctx ", const char *" p );
68 .BI "int url_dec(url_dctx *" ctx ", dstr *" n ", dstr *" v );
71 .\"--------------------------------------------------------------------------
76 read and write `form-urlencoded' data, as specified in RFC1866. The
77 encoding represents a sequence of name/value pairs where both the name
78 and value are arbitrary binary strings (although the format is optimized
79 for textual data). An encoded string contains no nonprintable
80 characters or whitespace. This interface is capable of decoding any
81 urlencoded string; however, it can currently only
83 names and values which do not contain null bytes, because the encoding
84 interface uses standard C strings.
86 Encoding a sequence of name/value pairs is achieved using the
88 function. It requires as input an
89 .IR "encoding context" ,
90 represented as an object of type
92 This must be initialized before use by passing it to the function
96 encodes one name/value pair, appending the encoded output to a dynamic
101 You can set flags in the encoding context's
106 Be strict about escaping non-alphanumeric characters. Without this,
107 potentially unsafe characters such as
111 will be left unescaped, which makes encoded filenames (for example) more
115 Be very lax about non-alphanumeric characters. Everything except
116 obviously-unsafe characters like
125 to separate name/value pairs, rather than the ampersand
128 Decoding a sequence of name/value pairs is performed using the
130 function. It requires as input a
131 .IR "decoding context" ,
132 represented as an object of type
134 This must be initialized before use by passing it to the function
136 along with the address of the urlencoded string to decode. The string
137 is not modified during decoding. Each call to
139 extracts a name/value pair. The name and value are written to the
144 so you probably want to reset them before each call. If there are no
145 more name/value pairs to read,
147 returns zero; otherwise it returns a nonzero value.
149 You can set flags in the encoding context's
156 to separate name/value pairs,
160 Without this flag, the semicolon is considered an `ordinary' character
161 which can appear unescaped as part of names and values. (Note the
162 difference from the same flag's meaning when encoding. When encoding,
165 the use of the semicolon, and when decoding, it
169 .\"--------------------------------------------------------------------------
172 The example code below demonstrates converting between a symbol table
173 and a urlencoded representation. The code is untested.
177 #include <mLib/alloc.h>
178 #include <mLib/dstr.h>
179 #include <mLib/sym.h>
180 #include <mLib/url.h>
187 void decode(sym_table *t, const char *p)
190 dstr n = DSTR_INIT, v = DSTR_INIT;
194 for (url_initdec(&c, p); url_dec(&c, &n, &v); ) {
195 vv = sym_find(t, n.buf, -1, sizeof(*vv), &f);
197 vv->v = xstrdup(v.buf);
201 dstr_destroy(&n); dstr_destroy(&v);
204 void encode(sym_table *t, dstr *d)
211 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
212 url_enc(&c, d, SYM_NAME(v), v->v);
216 .\"--------------------------------------------------------------------------
221 .\"--------------------------------------------------------------------------
224 Mark Wooding, <mdw@distorted.org.uk>.
226 .\"----- That's all, folks --------------------------------------------------