-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
from socketserver import ThreadingMixIn | ||
from urllib import request | ||
|
||
|
||
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): | ||
"""Handle requests in a separate thread.""" | ||
daemon_threads = True # Optional: Set True to make the threads exit when the main thread does. | ||
|
||
|
||
class CORSProxyHTTPRequestHandler(BaseHTTPRequestHandler): | ||
def do_REQUEST(self, method): | ||
target_url = self.path[1:] # Remove the leading '/' | ||
|
||
if not is_url_allowed(target_url): | ||
self.send_response(403) | ||
self.end_headers() | ||
self.wfile.write(b"403 Forbidden") | ||
return | ||
|
||
content_length = int(self.headers.get('Content-Length', 0)) | ||
body = self.rfile.read(content_length) if content_length else None | ||
headers = {key: val for (key, val) in self.headers.items() if key.lower() not in ('host', 'connection', 'content-length', 'content-type')} | ||
|
||
try: | ||
req = request.Request(target_url, data=body, headers=headers, method=method) | ||
with request.urlopen(req) as response: | ||
self.send_response(response.status) | ||
|
||
# Set up response headers, excluding the Access-Control-Allow-Origin header | ||
for header, value in response.headers.items(): | ||
if header.lower() != 'access-control-allow-origin': | ||
self.send_header(header, value) | ||
|
||
# Replace the Access-Control-Allow-Origin header with our own | ||
self.send_header('Access-Control-Allow-Origin', '*') | ||
|
||
self.end_headers() | ||
self.wfile.write(response.read()) | ||
except Exception as e: | ||
self.send_response(500) | ||
self.end_headers() | ||
self.wfile.write(str(e).encode()) | ||
|
||
def do_HEAD(self): | ||
self.do_REQUEST('HEAD') | ||
|
||
def do_GET(self): | ||
self.do_REQUEST('GET') | ||
|
||
def do_POST(self): | ||
self.do_REQUEST('POST') | ||
|
||
def do_PUT(self): | ||
self.do_REQUEST('PUT') | ||
|
||
def do_DELETE(self): | ||
self.do_REQUEST('DELETE') | ||
|
||
def do_OPTIONS(self): | ||
self.do_REQUEST('OPTIONS') | ||
|
||
|
||
def is_url_allowed(url): | ||
return True | ||
|
||
|
||
def run(server_class=ThreadedHTTPServer, handler_class=CORSProxyHTTPRequestHandler, port=8080): | ||
server_address = ('', port) | ||
httpd = server_class(server_address, handler_class) | ||
print(f"CORS proxy server listening on port {port}") | ||
httpd.serve_forever() | ||
|
||
|
||
if __name__ == '__main__': | ||
run(port=3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,41 @@ | ||
import init, { run } from "./dist/phatjs.js"; | ||
import init, { run, setHook } from "./dist/phatjs.js"; | ||
|
||
// Provide custom fetch implementation for phatjs | ||
window.phatjsFetch = (resource, options) => { | ||
console.log("Fetch: ", resource, options); | ||
return fetch(resource, options); | ||
function setRunable(enabled, runner) { | ||
document.getElementById("btn-run").disabled = !enabled; | ||
if (runner) { | ||
document.getElementById("btn-run").onclick = runner; | ||
} | ||
} | ||
|
||
function setOutput(text) { | ||
document.getElementById("output").value = text; | ||
} | ||
|
||
async function runScript() { | ||
const script = document.getElementById("input-code").value; | ||
document.getElementById("btn-run").disabled = true; | ||
const args = ["42"]; | ||
try { | ||
setRunable(false); | ||
setOutput("Running..."); | ||
const output = await run(["phatjs", "-c", script, "--", ...args]); | ||
document.getElementById("output").innerText = output; | ||
} finally { | ||
document.getElementById("btn-run").disabled = false; | ||
setOutput(output); | ||
} catch (error) { | ||
setOutput(error); | ||
} | ||
finally { | ||
setRunable(true); | ||
} | ||
} | ||
|
||
async function main() { | ||
await init(); | ||
document.getElementById("btn-run").onclick = runScript; | ||
document.getElementById("btn-run").disabled = false; | ||
|
||
// Provide custom fetch implementation for phatjs | ||
setHook("fetch", (req) => { | ||
// req = new Request("http://localhost:3000/" + req.url, req); | ||
return fetch(req); | ||
}); | ||
setRunable(true, runScript); | ||
} | ||
|
||
main().catch(console.error) |