44 lines
2.3 KiB
Markdown
44 lines
2.3 KiB
Markdown
# PES
|
|
|
|
This is a quick reference document on how to implement PES.
|
|
|
|
## Example script:
|
|
|
|
The default script is the following:
|
|
|
|
```python
|
|
import sys
|
|
import os
|
|
|
|
if not os.getcwd() in sys.path:
|
|
sys.path.append(os.getcwd())
|
|
import amethyst
|
|
|
|
|
|
class PES:
|
|
def __init__(self):
|
|
# DO NOT USE THIS FUNCTION FOR PROGRAM, ONLY ON_REQUEST PLEASE!!
|
|
# WARNING: ONLY CHANGE THE THREAD_SAFETY VARIABLE! DO NOT ADD OR REMOVE ANYTHING!
|
|
# THEY WILL COMPROMISE ANY THREAD-SAFETY SECURITY MECHANISMS AMETHYST HAS IN PLACE!
|
|
# YOU HAVE BEEN WARNED!
|
|
self.build_response = amethyst.WebServer.build_binary_response
|
|
self.fh = amethyst.FileHandler("..")
|
|
self.rq = amethyst.RequestParser()
|
|
self.THREAD_SAFETY: bool = True
|
|
|
|
def on_request(self, req):
|
|
return self.build_response(200, b"Heyhey! This is the default PES script!", "text/html")
|
|
```
|
|
|
|
## Threading and Thread-safety.
|
|
|
|
When you execute PES scripts, they have a high chance of not being thread-safe because they edit the page you visit. It means you cannot guarantee the data you want is actually the data getting sent over the wire. This issue can be fixed in multiple ways:
|
|
|
|
1. Disabling threading, then only one script can run at once, but this costs a lot of performance
|
|
|
|
2. Enforcing thread-safety on all scripts, this will mean every script can be trusted, but makes development difficult (especially for people with no coding experience)
|
|
|
|
3. Implementing mechanisms that will try to patch up holes if threading is enabled and a thread-unsafe script is loaded.
|
|
|
|
Amethyst uses fix 3. It only allows one script to run at a time (if both threading is enabled and a thread-unsafe script is loaded), but if you specify only one host to use PES, all other hosts still enjoy the benefits of a multithreaded server! This makes sure that if user 1 and user 2 both request something and PES is involved, user 1 receives part of the data they want and part of the data user 2 wants and vice versa. This security mechanism only works if you respect them. Amethyst will throw a warning if you have a situation you have an unsafe script and run threaded, this warning isn't critical, as the script will still execute with security mechanisms, but the warning in the script is clear. Amethyst can't help if you make it explicitly unsafe yourself.
|