From: Mark Wooding Date: Wed, 15 Mar 2006 01:35:36 +0000 (+0000) Subject: url: Support form-urlencoding functions. X-Git-Tag: 1.0.1~16 X-Git-Url: https://git.distorted.org.uk/~mdw/mLib-python/commitdiff_plain/5ce5170ce3969dca8366a9d051e4f887ee162df8 url: Support form-urlencoding functions. --- diff --git a/defs.pxi b/defs.pxi index 69f4adb..2de0ab9 100644 --- a/defs.pxi +++ b/defs.pxi @@ -162,6 +162,23 @@ cdef extern from 'mLib/str.h': int str_match(char *p, char *s) void str_sanitize(char *d, char *p, size_t sz) +#----- Form-urlencoding functions ------------------------------------------- + +cdef extern from 'mLib/url.h': + struct url_ectx: + unsigned f + struct url_dctx: + char *p + unsigned f + enum: + URLF_STRICT + URLF_LAX + URLF_SEMI + void url_initenc(url_ectx *ctx) + void url_enc(url_ectx *ctx, dstr *d, char *name, char *value) + void url_initdec(url_dctx *ctx, char *p) + int url_dec(url_dctx *ctx, dstr *n, dstr *v) + #----- Atom stuff ----------------------------------------------------------- # --- Atoms --- diff --git a/mLib.pyx b/mLib.pyx index 577a699..61ae890 100644 --- a/mLib.pyx +++ b/mLib.pyx @@ -56,6 +56,7 @@ include 'str.pyx' include 'base64.pyx' include 'base32.pyx' include 'hex.pyx' +include 'url.pyx' # --- Error reporting --- diff --git a/url.pyx b/url.pyx new file mode 100644 index 0000000..20c51ce --- /dev/null +++ b/url.pyx @@ -0,0 +1,124 @@ +# -*-pyrex-*- +# +# $Id$ +# +# 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: + cdef url_ectx ctx + cdef dstr d + + def __new__(me, *hunoz, **hukairz): + url_initenc(&me.ctx) + DCREATE(&me.d) + def __init__(me, strictp = False, laxp = False, semip = False): + cdef unsigned f + f = 0 + if strictp: + f = f | URLF_STRICT + if laxp: + f = f | URLF_LAX + if semip: + f = f | URLF_SEMI + me.ctx.f = f + def encode(me, char *name, char *value): + url_enc(&me.ctx, &me.d, name, value) + return me + property result: + def __get__(me): + return PyString_FromStringAndSize(me.d.buf, me.d.len) + property strictp: + def __get__(me): + return _tobool(me.ctx.f & URLF_STRICT) + def __set__(me, val): + if val: + me.ctx.f = me.ctx.f | URLF_STRICT + else: + me.ctx.f = me.ctx.f & ~URLF_STRICT + property laxp: + def __get__(me): + return _tobool(me.ctx.f & URLF_LAX) + def __set__(me, val): + if val: + me.ctx.f = me.ctx.f | URLF_LAX + else: + me.ctx.f = me.ctx.f & ~URLF_LAX + property semip: + def __get__(me): + return _tobool(me.ctx.f & URLF_SEMI) + def __set__(me, val): + if val: + me.ctx.f = me.ctx.f | URLF_SEMI + else: + me.ctx.f = me.ctx.f & ~URLF_SEMI + def __del__(me): + dstr_destroy(&me.d) + +cdef class URLDecode: + cdef url_dctx ctx + cdef char *p + + def __new__(me, *hunoz, **hukairz): + me.p = xstrdup('') + url_initdec(&me.ctx, me.p) + def __init__(me, char *string, semip = False): + cdef unsigned f + f = 0 + if semip: + f = f | URLF_SEMI + xfree(me.p) + me.p = xstrdup(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 = PyString_FromStringAndSize(n.buf, n.len) + vv = PyString_FromStringAndSize(v.buf, v.len) + dstr_destroy(&n) + dstr_destroy(&v) + if not anyp: + raise StopIteration + return nn, vv + property semip: + def __get__(me): + return _tobool(me.ctx.f & URLF_SEMI) + def __set__(me, val): + if val: + me.ctx.f = me.ctx.f | URLF_SEMI + else: + me.ctx.f = me.ctx.f & ~URLF_SEMI + def __del__(me): + xfree(me.p) + +#----- That's all, folks ----------------------------------------------------