gemini/src/gemini/response.cr
2022-01-05 17:02:05 -03:00

47 lines
923 B
Crystal

# Gemini response
class Gemini::Response
getter server : Server
getter connection : IO
getter request : Request
property status : Int32 = 20
property meta : String = "text/gemini"
def initialize(@server, @connection, @request)
end
# Send the file
def send : Nil
unless exists?
@status = 51
@meta = "Not found"
end
if removed?
@status = 52
@meta = "Gone"
end
@server.puts @connection, "#{@status} #{@meta}"
Log.info { "#{@request.host}: #{@status} #{@meta} #{@request.path}" }
send_file if @status == 20
end
def exists? : Bool
File.exists? @request.path
end
# Files can be specifically removed by appending .remove at the end.
def removed? : Bool
File.exists? "#{@request.path}.remove"
end
# Sends the file by copying its contents
private def send_file : Nil
IO.copy File.open(@request.path), @connection
end
end