### -*-pyrex-*- ### ### Form-urlencoding functions ### ### (c) 2006 Straylight/Edgeware ### ###----- Licensing notice --------------------------------------------------- ### ### This file is part of the Python interface to mLib. ### ### mLib/Python is free software; you can redistribute it and/or modify ### it under the terms of the GNU General Public License as published by ### the Free Software Foundation; either version 2 of the License, or ### (at your option) any later version. ### ### mLib/Python is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ### GNU General Public License for more details. ### ### You should have received a copy of the GNU General Public License ### along with mLib/Python; if not, write to the Free Software Foundation, ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cdef class URLEncode: """URLEncode([strictp = False], [laxp = False], [semip = False])""" cdef url_ectx ctx cdef dstr d def __cinit__(me, *hunoz, **hukairz): url_initenc(&me.ctx) DCREATE(&me.d) def __dealloc__(me): dstr_destroy(&me.d) def __init__(me, strictp = False, laxp = False, semip = False): cdef unsigned f f = 0 if strictp: f |= URLF_STRICT if laxp: f |= URLF_LAX if semip: f |= URLF_SEMI me.ctx.f = f def encode(me, object name, object value): """UE.encode(NAME, VALUE): encode a key/value pair""" url_enc(&me.ctx, &me.d, TEXT_PTR(name), TEXT_PTR(value)) return me @property def result(me): """UE.result -> STR: the encoded string""" return TEXT_FROMSTRLEN(me.d.buf, me.d.len) @property def strictp(me): """UE.strictp -> BOOL: strictly escape non-alphanumerics?""" return (me.ctx.f&URLF_STRICT) @strictp.setter def strictp(me, val): if val: me.ctx.f |= URLF_STRICT else: me.ctx.f &= ~URLF_STRICT @property def laxp(me): """UE.laxp -> BOOL: only escape obviously unsafe characters?""" return (me.ctx.f&URLF_LAX) @laxp.setter def laxp(me, val): if val: me.ctx.f |= URLF_LAX else: me.ctx.f &= ~URLF_LAX @property def semip(me): """UE.semip -> BOOL: separate key/value pairs with semicolons?""" return (me.ctx.f&URLF_SEMI) @semip.setter def semip(me, val): if val: me.ctx.f |= URLF_SEMI else: me.ctx.f &= ~URLF_SEMI cdef class URLDecode: """URLDecode(STR, [semip = False]): iterator over (KEY, VALUE) pairs""" cdef url_dctx ctx cdef char *p def __cinit__(me, *hunoz, **hukairz): me.p = NULL url_initdec(&me.ctx, me.p) def __dealloc__(me): xfree(me.p) def __init__(me, object string, semip = False): cdef unsigned f f = 0 if semip: f |= URLF_SEMI xfree(me.p) me.p = xstrdup(TEXT_PTR(string)) me.ctx.p = me.p me.ctx.f = f def __iter__(me): return me def __next__(me): cdef dstr n cdef dstr v cdef object nn cdef object vv cdef int anyp DCREATE(&n) DCREATE(&v) anyp = url_dec(&me.ctx, &n, &v) if anyp: nn = TEXT_FROMSTRLEN(n.buf, n.len) vv = TEXT_FROMSTRLEN(v.buf, v.len) dstr_destroy(&n) dstr_destroy(&v) if not anyp: raise StopIteration return nn, vv @property def semip(me): """UE.semip -> BOOL: separate key/value pairs with semicolons?""" return (me.ctx.f&URLF_SEMI) @semip.setter def semip(me, val): if val: me.ctx.f |= URLF_SEMI else: me.ctx.f &= ~URLF_SEMI ###----- That's all, folks --------------------------------------------------