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>"
26 .B "\h'4n'unsigned f;"
31 .B "\h'4n'unsigned f;"
35 .B "#define URLF_STRICT ..."
36 .B "#define URLF_LAX ..."
37 .B "#define URLF_SEMI ..."
39 .BI "void url_initenc(url_ectx *" ctx );
40 .ds mT \fBvoid url_enc(
41 .BI "\*(mTurl_ectx *" ctx ", dstr *" d ,
42 .BI "\h'\w'\*(mT'u'const char *" name ", const char *" value );
44 .BI "void url_initdec(url_dctx *" ctx ", const char *" p );
45 .BI "int url_dec(url_dctx *" ctx ", dstr *" n ", dstr *" v );
50 read and write `form-urlencoded' data, as specified in RFC1866. The
51 encoding represents a sequence of name/value pairs where both the name
52 and value are arbitrary binary strings (although the format is optimized
53 for textual data). An encoded string contains no nonprintable
54 characters or whitespace. This interface is capable of decoding any
55 urlencoded string; however, it can currently only
57 names and values which do not contain null bytes, because the encoding
58 interface uses standard C strings.
60 Encoding a sequence of name/value pairs is achieved using the
62 function. It requires as input an
63 .IR "encoding context" ,
64 represented as an object of type
66 This must be initialized before use by passing it to the function
70 encodes one name/value pair, appending the encoded output to a dynamic
75 You can set flags in the encoding context's
80 Be strict about escaping non-alphanumeric characters. Without this,
81 potentially unsafe characters such as
85 will be left unescaped, which makes encoded filenames (for example) more
89 Be very lax about non-alphanumeric characters. Everything except
90 obviously-unsafe characters like
99 to separate name/value pairs, rather than the ampersand
102 Decoding a sequence of name/value pairs is performed using the
104 function. It requires as input a
105 .IR "decoding context" ,
106 represented as an object of type
108 This must be initialized before use by passing it to the function
110 along with the address of the urlencoded string to decode. The string
111 is not modified during decoding. Each call to
113 extracts a name/value pair. The name and value are written to the
118 so you probably want to reset them before each call. If there are no
119 more name/value pairs to read,
121 returns zero; otherwise it returns a nonzero value.
123 You can set flags in the encoding context's
130 to separate name/value pairs,
134 Without this flag, the semicolon is considered an `ordinary' character
135 which can appear unescaped as part of names and values. (Note the
136 difference from the same flag's meaning when encoding. When encoding,
139 the use of the semicolon, and when decoding, it
143 The example code below demonstrates converting between a symbol table
144 and a urlencoded representation. The code is untested.
147 #include <mLib/alloc.h>
148 #include <mLib/dstr.h>
149 #include <mLib/sym.h>
150 #include <mLib/url.h>
157 void decode(sym_table *t, const char *p)
160 dstr n = DSTR_INIT, v = DSTR_INIT;
162 for (url_initdec(&c, p); url_dec(&c, &n, &v); ) {
164 val *vv = sym_find(t, n.buf, -1, sizeof(*vv), &f);
167 vv->v = xstrdup(v.buf);
175 void encode(sym_table *t, dstr *d)
182 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
183 url_enc(&c, d, SYM_NAME(v), v->v);
189 Mark Wooding, <mdw@distorted.org.uk>.