mipha/gemini.moon

56 lines
1.2 KiB
Plaintext

cqueues = require "cqueues"
socket = require "cqueues.socket"
ln = require "ln"
url = require "net.url"
srv = socket.listen "127.0.0.1", "58816"
cq = cqueues.new!
class ProxyRequestRefusedHandler
run: (ctx, conn) =>
conn\write "53 Proxy request refused \r\n"
ln.log ctx, {status: 53}
class Server
new: (cq, hdlr) =>
@cq = cq
@hdlr = hdlr
listen: (srv) =>
@cq\wrap ->
for conn in srv\clients!
@cq\wrap ->
with conn
_, ip = \peername!
request = \read "*l"
u = url.parse request
ctx =
:ip
host: u.host
path: u.path
query: u.query
@hdlr\run ctx, conn
\close!
-- cq\wrap ->
-- for conn in srv\clients!
-- cq\wrap ->
-- with conn
-- _, ip = \peername!
-- request = \read "*l"
-- u = url.parse request
-- ctx =
-- :ip
-- host: u.host
-- path: u.path
-- query: u.query
-- ln.log ctx
-- \write "53 Proxy request refused\r\n"
-- \close!
with Server cq, ProxyRequestRefusedHandler!
\listen srv
assert cq\loop!