Skip to content

Instantly share code, notes, and snippets.

@jul
Last active October 25, 2022 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 24, 2022

I guess it is NOT SAFE. But, at least it's funny because you can dynamically change the routes of your router in 40 lines of code

date>clock.txt && curl -s -X POST 'http://localhost:5000/a?a=a' -F "file=@clock.txt;type=application/pdf;base64"

{
    "file": {
        "name": "clock.txt",
        "content": "lun. 24 oct. 2022 23:48:05 CEST\n",
        "content_type": "application/pdf"
    },
    "_path": "/a",
    "a": "a"
}

curl -s -X POST 'http://localhost:5000/hello?a=2' -F 'a=1'

hello world

curl -s -X POST 'http://localhost:5000/templating' -F 'notemplate=""'

1
template was 'None'

curl -s -X POST 'http://localhost:5000/templating' -F 'notemplate=""'

2
template was 'None'

curl -s -X POST 'http://localhost:5000/templating' -F 'template="2+2"'

4
template was '2+2'

curl -s -X POST 'http://localhost:5000/templating' -F 'template="router.__setitem__(\"a\",lambda b:\"powned\n\")"'

None
template was 'router.__setitem__("a",lambda b:"powned\n")'

curl -s -X POST 'http://localhost:5000/a' -F 'a=1'

powned

@jul
Copy link
Author

jul commented Oct 24, 2022

(p3) jul@plumeau:~/src$ curl -s -X POST 'http://localhost:5000/templating' -F 'template="router.__setitem__(\"a\",lambda b:f\"powned \" + ( setattr(e, \"a\", e.a+1) or \"\" )+ f\"{e.a} times\n\")"'
None
template was 'router.__setitem__("a",lambda b:f"powned " + ( setattr(e, "a", e.a+1) or "" )+ f"{e.a} times\n")'

(p3) jul@plumeau:~/src$ curl -s -X POST 'http://localhost:5000/a' -F 'a=1'
powned 1 times

(p3) jul@plumeau:~/src$ curl -s -X POST 'http://localhost:5000/a' -F 'a=1'
powned 2 times

(p3) jul@plumeau:~/src$ curl -s -X POST 'http://localhost:5000/a' -F 'a=1'
powned 3 times

@stephane-bard
Copy link

c'est beau mais j'ai plein de questions sur le code, je ne sais par où commencer.
des questions de syntaxe

  • pourquoi déclarer global router et global e, ils sont déjà globaux n'est-ce pas ?
  • pourquoi utiliser dict alors qu'une déclaration avec {} aurait pu être plus facile à comprendre
  • ton implem repose sur make_server, c'est super cheat

@jul
Copy link
Author

jul commented Oct 25, 2022

  • pour que ce qui est implicite soit explicite. Ce code est du troll. Moult dev ne voient pas le caractère global de variables qu'ils manipulent. Et environ est la pire (mets un point d'arrêt l27 et dump environ. (La vérité c'est j'avais oublié)
  • parce que { "a" : 1, "a" : 2} est ok en python mais que dict(a=1, a=2) lève l'exception que j'espère. Donc dans la vie courante ... par fainéantise je privilégie dict pour avoir moins de tests à écrire.
  • c'est super cheat, MAIS, car c'est l'implémentation de référence, c'est aussi ... super testé avec le minimum de gras. c'est donc super fainéant (et une pratique courante). Fait un grep sur asgiref dans fastapi (par exemple) et tu le verras partout.

PS : en virant les global je passe sous la barre des 42 et le comportement reste le même ... Bien ...
PPS : toujours aucune idée de pourquoi la ligne 32 https://gist.github.com/jul/7f5e01fb484352c5bd3ef52a6e612a34#file-lol-py-L32. (fo = dict(**fo.items()))

@jul
Copy link
Author

jul commented Oct 25, 2022

Évidemment @stephane-bard le langage de templating en forth est le meilleur :

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 :> <: $now \" \": CAT DUP CAT :>» donne"'

le template «le template «<: $tmpl :>
<: $qty :> * <:$prc:> = <: $qty >NUM $prc >NUM MUL :> <: $now "    ": CAT  DUP CAT  :>» donne
2 * 1.6 = 3.2 mar. 25 oct. 2022 22:21:29 CEST    mar. 25 oct. 2022 22:21:29 CEST    » donne

@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