Skip to content

Commit

Permalink
Refactor the webserver into r2ai.web and more options
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed May 9, 2024
1 parent fa1f3b9 commit 89140cb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
3 changes: 3 additions & 0 deletions r2ai/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,9 @@ def __init__(self):
self.env["data.hist"] = "false"
self.env["key.mastodon"] = ""
self.env["key.openai"] = ""
self.env["http.port"] = "8080"
self.env["http.path"] = ""
self.env["http.chatctx"] = "false"
if have_rlang:
self.env["chat.live"] = "false"
else:
Expand Down
38 changes: 3 additions & 35 deletions r2ai/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import traceback
have_readline = False
from .const import R2AI_HISTFILE, R2AI_HOMEDIR, R2AI_RCFILE, R2AI_USERDIR
from .web import start_http_server
import r2ai
import sys
import os
Expand Down Expand Up @@ -34,40 +35,6 @@ def r2_cmd(x):
r2.cmd('e scr.color=' + oc)
return res

# TODO : move into r2ai/http.py
def start_http_server(ai):
import http.server
import socketserver

PORT = 8000
BASEPATH = ""

Handler = http.server.SimpleHTTPRequestHandler

class SimpleHTTPRequestHandler(Handler):
def do_GET(self):
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(f'Invalid request. Use POST and /{BASEPATH}', 'utf-8'))
def do_POST(self):
if self.path.startswith(BASEPATH):
content_length = int(self.headers['Content-Length'])
msg = self.rfile.read(content_length).decode('utf-8')
self.send_response(200)
self.end_headers()
res = runline2(ai, msg)
self.wfile.write(bytes(f'{res}','utf-8'))
else:
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(f'Invalid request. Use {BASEPATH}'))

Handler.protocol_version = "HTTP/1.0"
server = socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler)
server.allow_reuse_address = True
server.allow_reuse_port = True
print("Serving at port", PORT)
server.serve_forever()

help_message = """Usage: r2ai [-option] ([query] | [script.py])
r2ai . [file] interpret r2ai script with access to globals
Expand Down Expand Up @@ -266,8 +233,9 @@ def runline(ai, usertext):
print("Invalid config key", file=sys.stderr)
pass
elif usertext.startswith("-w"):
start_http_server(ai)
start_http_server(ai, runline2)
elif usertext.startswith("-s"):
ai.runline2 = runline2
r2ai_repl(ai)
elif usertext.startswith("-rf"):
if len(usertext) > 2:
Expand Down
37 changes: 37 additions & 0 deletions r2ai/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def start_http_server(ai, runline2):
import http.server
import socketserver

WANTCTX = ai.env["http.chatctx"] == "true"
PORT = int(ai.env["http.port"])
BASEPATH = ai.env["http.path"]

Handler = http.server.SimpleHTTPRequestHandler

class SimpleHTTPRequestHandler(Handler):
def do_GET(self):
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(f'Invalid request. Use POST and /{BASEPATH}', 'utf-8'))
def do_POST(self):
if self.path.startswith(BASEPATH):
content_length = int(self.headers['Content-Length'])
msg = self.rfile.read(content_length).decode('utf-8')
self.send_response(200)
self.end_headers()
if WANTCTX:
runline2(ai, "-R")
res = runline2(ai, msg)
self.wfile.write(bytes(f'{res}','utf-8'))
else:
self.send_response(404)
self.end_headers()
self.wfile.write(bytes(f'Invalid request. Use {BASEPATH}'))

print("[r2ai] Serving at port", PORT)
Handler.protocol_version = "HTTP/1.0"
server = socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler)
server.allow_reuse_address = True
server.allow_reuse_port = True
server.serve_forever()

0 comments on commit 89140cb

Please sign in to comment.