14 .TH url 3mLib "20 June 1999" mLib
16 url \- manipulation of form-urlencoded strings
19 .B "#include <mLib/url.h>
21 .BI "void url_initenc(url_ectx *" ctx );
22 .BI "void url_enc(url_ectx *" ctx ", dstr *" d ,
23 .BI " const char *" name ", const char *" value );
25 .BI "void url_initdec(url_dctx *" ctx ", const char *" p );
26 .BI "int url_dec(url_dctx *" ctx ", dstr *" n ", dstr *" v );
31 read and write `form-urlencoded' data, as specified in RFC1866. The
32 encoding represents a sequence of name/value pairs where both the name
33 and value are arbitrary binary strings (although the format is optimized
34 for textual data). An encoded string contains no nonprintable
35 characters or whitespace. This interface is capable of decoding any
36 urlencoded string; however, it can currently only
38 names and values which do not contain null bytes, because the encoding
39 interface uses standard C strings.
41 Encoding a sequence of name/value pairs is achieved using the
43 function. It requires as input an
44 .IR "encoding context" ,
45 represented as an object of type
47 This must be initialized before use by passing it to the function
51 encodes one name/value pair, appending the encoded output to a dynamic
56 Decoding a sequence of name/value pairs is performed using the
58 function. It requires as input a
59 .IR "decoding context" ,
60 represented as an object of type
62 This must be initialized before use by passing it to the function
64 along with the address of the urlencoded string to decode. The string
65 is not modified during decoding. Each call to
67 extracts a name/value pair. The name and value are written to the
72 so you probably want to reset them before each call. If there are no
73 more name/value pairs to read,
75 returns zero; otherwise it returns a nonzero value.
77 The example code below demonstrates converting between a symbol table
78 and a urlencoded representation. The code is untested.
81 #include <mLib/alloc.h>
82 #include <mLib/dstr.h>
91 void decode(sym_table *t, const char *p)
94 dstr n = DSTR_INIT, v = DSTR_INIT;
96 for (url_initdec(&c, p); url_dec(&c, &n, &v); ) {
98 val *vv = sym_find(t, n.buf, -1, sizeof(*vv), &f);
101 vv->v = xstrdup(v.buf);
109 void encode(sym_table *t, dstr *d)
116 for (sym_mkiter(&i, t); (v = sym_next(&i)) != 0; )
117 url_enc(&c, d, SYM_NAME(v), v->v);
121 Mark Wooding, <mdw@nsict.org>.