82 lines
2.3 KiB
Lua
82 lines
2.3 KiB
Lua
local config = require("config")
|
|
|
|
local function paste_get(req)
|
|
--Get the paste page
|
|
local host = http_request_get_host(req)
|
|
local text
|
|
local author,_ = get_session(req)
|
|
if host == config.domain and author then
|
|
http_response_header(req,"Location",string.format("https://%s.%s/_paste",author,config.domain))
|
|
http_response(req,303,"")
|
|
return
|
|
elseif host == config.domain and author == nil then
|
|
text = cache.render(string.format("%s/_paste",host),function()
|
|
print("Cache missing, rendering post page")
|
|
return pages.paste{
|
|
domain = domain,
|
|
err = "",
|
|
}
|
|
end)
|
|
http_response(req,200,text)
|
|
elseif host ~= config.domain and author then
|
|
|
|
elseif host ~= config.domain and author == nil then
|
|
|
|
else
|
|
error(string.format(
|
|
"Unable to find a good case for paste:%s,%s,%s",
|
|
host,
|
|
config.domain,
|
|
author
|
|
))
|
|
end
|
|
if host == config.domain then
|
|
local author,_ = get_session(req)
|
|
if author then
|
|
http_response_header(req,"Location",string.format("https://%s.%s/_paste",author,domain))
|
|
http_response(req,303,"")
|
|
return
|
|
else
|
|
--For an anonymous user
|
|
ret = cache.render(string.format("%s/_paste",host),function()
|
|
print("Cache missing, rendering post page")
|
|
return pages.paste{
|
|
domain = domain,
|
|
err = "",
|
|
}
|
|
end)
|
|
end
|
|
|
|
else
|
|
--Or for someone that's logged in
|
|
print("Looks like a logged in user wants to paste!")
|
|
local subdomain = host:match("([^%.]+)")
|
|
local author,_ = get_session(req)
|
|
print("subdomain:",subdomain,"author:",author)
|
|
--If they try to paste as an author, but are on the
|
|
--wrong subdomain, or or not logged in, redirect them
|
|
--to the right place. Their own subdomain for authors
|
|
--or the anonymous paste page for not logged in users.
|
|
if author == nil then
|
|
http_response_header(req,"Location","https://"..domain.."/_paste")
|
|
http_response(req,303,"")
|
|
return
|
|
end
|
|
if author ~= subdomain then
|
|
http_response_header(req,"Location",string.format("https://%s.%s/_paste",author,domain))
|
|
http_response(req,303,"")
|
|
return
|
|
end
|
|
assert(author == subdomain,"someone wants to paste as someone else")
|
|
--We're where we want to be, serve up this users's
|
|
--paste page. No cache, because how often is a user
|
|
--going to paste?
|
|
ret = pages.author_paste{
|
|
domain = domain,
|
|
user = author,
|
|
text = "",
|
|
err = "",
|
|
}
|
|
end
|
|
end
|