Skip to content

Instantly share code, notes, and snippets.

@jul
Last active October 25, 2022 20:47
Show Gist options
  • Save jul/7f5e01fb484352c5bd3ef52a6e612a34 to your computer and use it in GitHub Desktop.
Save jul/7f5e01fb484352c5bd3ef52a6e612a34 to your computer and use it in GitHub Desktop.
the kind of simple wsgi with a pretty dynamic router (please NEVER do this at home)
#!/usr/bin/env python
# requires pip install multipart
import multipart
from wsgiref.simple_server import make_server
from json import dumps
from urllib.parse import parse_qsl
# require pip install confined my OWN FORTH BASED templating SYNTAX
from confined import templatize
class e: a=0
router=dict(
tmpl=lambda o:o.get("tmpl","{_path}").format(**o)+"\n",
conf=lambda o:templatize(o,o.get("tmpl", "<: $tmpl :>"))+"\n",
templating=lambda bo:eval("".join(map(str,("f\"\"\"{",
eval(
bo.get(
"template",
"setattr(e,'a',e.a.__add__(1)) or e.a"
)),
"}\ntemplate was '{bo.get(\"template\")}'\n \"\"\"","\n")))),
hello=lambda bo:"hello world\n",
)
def simple_app(environ, start_response):
fo,fi=multipart.parse_form_data(environ)
fo.update(**{ k: dict(
name=fi.filename,
content=fi.file.read().decode('utf-8', 'backslashreplace'),
content_type =fi.content_type,
) for k,v in fi.items()})
fo = dict(**fo.items())
fo["_path"]=environ["PATH_INFO"]
fo.update(**dict(parse_qsl(environ["QUERY_STRING"])))
start_response('200 OK', [('Content-type', 'text/plain; charset=utf-8')])
return [ router.get(fo['_path'][1:],lambda fo:dumps(fo, indent=4))(fo).encode()]
print("Serving on port 5000...")
make_server('', 5000, simple_app).serve_forever()
@jul
Copy link
Author

jul commented Oct 25, 2022

Je suis hyper fier, mes messages d'erreurs sont certes pas compréhensible (dans confined) mais ils remontent hyper bien dans la stack et sont déterministes. Je sais exactement où est l'erreur

curl -s -X POST 'http://localhost:5000/conf?desc=whatever' -F "now=$(date)" -F "qty=2" -F "prc=1.6" -F 'tmpl="le template «<: $tmpl :> <: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> -<: 2 2: ADD :>- <: $now \" \": CAT DUP CAT :>» donne"'
le template «le template «<: $tmpl :> <: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> -<: 2 2: ADD :>- <: $now " ": CAT DUP CAT :>» donne 2 * 1.6 = 3.2 -
Type: >TypeCheck< 
====================
("
Type: >UnrecognizedToken< 
====================
*2* in 
 2 2: ADD 
====================
":ERROR) (new pos 2) str is not of type num
====================
- mar. 25 oct. 2022 22:30:20 CEST mar. 25 oct. 2022 22:30:20 CEST » donne
(p3) jul@plumeau:~/src$ curl -s -X POST 'http://localhost:5000/conf?desc=whatever' -F "now=$(date)" -F "qty=2" -F "prc=1.6" -F 'tmpl="le template «<: $tmpl :> <: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> -<: 2: 2: ADD :>- <: $now \" \": CAT DUP CAT :>» donne"'
le template «le template «<: $tmpl :> <: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> -<: 2: 2: ADD :>- <: $now " ": CAT DUP CAT :>» donne 2 * 1.6 = 3.2 -4.0- mar. 25 oct. 2022 22:30:28 CEST mar. 25 oct. 2022 22:30:28 CEST » donne

J'ai fait "2" + 2 dans un fragment (ç'aurait du être 2:empty_tag_comme_en_RPN_HP_48 2: ADD ) et le message est clair : tu additionnes pas des str et des num. En tant que concepteur de confined, j'admets qu'il pourrait il y avoir l'information que c'est ADD qui est pas content que son contrat d'interface ne soit pas respecté (généré par https://github.com/jul/confined/blob/master/confined/__init__.py#L249, je crois, j'espère)

curl -s -X POST 'http://localhost:5000/conf?desc=whatever' -F "now=$(date)" -F "qty=2" -F "prc=1.6" -F 'tmpl="le template «<: $tmpl :> <: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> -<: \"2\": 2: ADD :>- <: $now \" \": CAT DUP CAT :>» donne"'
le template «le template «<: $tmpl :> <: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> -<: "2": 2: ADD :>- <: $now " ": CAT DUP CAT :>» donne 2 * 1.6 = 3.2 -
Type: >TypeCheck< 
====================
("2":) (new pos 2) str is not of type num
====================
- mar. 25 oct. 2022 22:34:20 CEST mar. 25 oct. 2022 22:34:20 CEST » donne

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment