14 .TH url 3 "20 June 1999" "Straylight/Edgeware" "mLib utilities library"
16 url \- manipulation of form-urlencoded strings
23 .B "#include <mLib/url.h>"
36 .B "#define URLF_STRICT ..."
37 .B "#define URLF_LAX ..."
38 .B "#define URLF_SEMI ..."
40 .BI "void url_initenc(url_ectx *" ctx );
41 .ta \w'\fBvoid url_enc('u
42 .BI "void url_enc(url_ectx *" ctx ", dstr *" d ,
43 .BI " const char *" name ", const char *" value );
45 .BI "void url_initdec(url_dctx *" ctx ", const char *" p );
46 .BI "int url_dec(url_dctx *" ctx ", dstr *" n ", dstr *" v );
51 read and write `form-urlencoded' data, as specified in RFC1866. The
52 encoding represents a sequence of name/value pairs where both the name
53 and value are arbitrary binary strings (although the format is optimized
54 for textual data). An encoded string contains no nonprintable
55 characters or whitespace. This interface is capable of decoding any
56 urlencoded string; however, it can currently only
58 names and values which do not contain null bytes, because the encoding
59 interface uses standard C strings.
61 Encoding a sequence of name/value pairs is achieved using the
63 function. It requires as input an
64 .IR "encoding context" ,
65 represented as an object of type
67 This must be initialized before use by passing it to the function
71 encodes one name/value pair, appending the encoded output to a dynamic
76 You can set flags in the encoding context's
81 Be strict about escaping non-alphanumeric characters. Without this,
82 potentially unsafe characters such as
86 will be left unescaped, which makes encoded filenames (for example) more
90 Be very lax about non-alphanumeric characters. Everything except
91 obviously-unsafe characters like
100 to separate name/value pairs, rather than the ampersand
103 Decoding a sequence of name/value pairs is performed using the
105 function. It requires as input a
106 .IR "decoding context" ,
107 represented as an object of type
109 This must be initialized before use by passing it to the function
111 along with the address of the urlencoded string to decode. The string
112 is not modified during decoding. Each call to
114 extracts a name/value pair. The name and value are written to the
119 so you probably want to reset them before each call. If there are no
120 more name/value pairs to read,
122 returns zero; otherwise it returns a nonzero value.
124 You can set flags in the encoding context's
131 to separate name/value pairs,
135 Without this flag, the semicolon is considered an `ordinary' character
136 which can appear unescaped as part of names and values. (Note the
137 difference from the same flag's meaning when encoding. When encoding,
140 the use of the semicolon, and when decoding, it
144 The example code below demonstrates converting between a symbol table
145 and a urlencoded representation. The code is untested.
149 #include <mLib/alloc.h>
150 #include <mLib/dstr.h>
151 #include <mLib/sym.h>
152 #include <mLib/url.h>
159 void decode(sym_table *t, const char *p)
162 dstr n = DSTR_INIT, v = DSTR_INIT;
166 for (url_initdec(&c, p); url_dec(&c, &n, &v); ) {
167 vv = sym_find(t, n.buf, -1, sizeof(*vv), &f);
169 vv->v = xstrdup(v.buf);
173 dstr_destroy(&n); dstr_destroy(&v);
176 void encode(sym_table *t, dstr *d)
183 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
184 url_enc(&c, d, SYM_NAME(v), v->v);
190 Mark Wooding, <mdw@distorted.org.uk>.