From 18385995445fa15ddca75af42b859da958402af8 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Wed, 22 Feb 2023 20:43:42 +0900 Subject: Add more user error handling --- hybring.cr | 13 ++++++++++++- style.css | 2 +- xhtml.cr | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/hybring.cr b/hybring.cr index 1c5015e..4b37469 100644 --- a/hybring.cr +++ b/hybring.cr @@ -60,6 +60,7 @@ end File.write "index.xhtml", page server = HTTP::Server.new do |context| + # Manually crafted request next http_error context, 405 if context.request.method != "POST" content_length = context.request.content_length next http_error context, 411 unless content_length @@ -73,6 +74,9 @@ server = HTTP::Server.new do |context| params[key] = value case key when "nick" + if /^[0-9a-z]+$/ !~ value + next errors["nick"] = "Must be ASCII lowercase alphanumeric" + end when "opennic" uri = URI.parse value next errors["opennic"] = "Must be absolute URL" unless uri.absolute? @@ -95,9 +99,16 @@ server = HTTP::Server.new do |context| break invalid_param = true end end + + # Manually crafted request next http_error context, 400, "Invalid Parameter" if invalid_param + next http_error context, 400, "Missing Parameter" unless params.size == 3 - context.response.status_code = 400 unless errors.empty? + if errors.empty? + # TODO: write feed + else + context.response.status_code = 400 unless errors.empty? + end context.response.content_type = "application/xhtml+xml" context.response.print page errors, params # TODO: schedule dynamic check diff --git a/style.css b/style.css index 395c35d..938af97 100644 --- a/style.css +++ b/style.css @@ -2,7 +2,7 @@ html { margin: auto; max-width: 72ch; } -h1, h2, h3, h4, h5, h6 { margin-bottom: 1ex } +h1, h2, h3, h4, h5, h6 { margin: 1ex 0 } form { display: grid; grid-template-columns: max-content 1fr; diff --git a/xhtml.cr b/xhtml.cr index 2dc8aea..4fd58eb 100644 --- a/xhtml.cr +++ b/xhtml.cr @@ -58,7 +58,7 @@ def form(xml, errors = {} of String => String, params = {} of String => String) # FIXME: get URL from configuration xml.element "form", action: "http://127.0.0.1:8080", method: "POST" do - input xml, "nick", "Nickname", "digits and ASCII lowercase letters", + input xml, "nick", "Nickname", "digits and lowercase letters", errors.fetch("nick", nil), params.fetch("nick", nil) input xml, "opennic", "OpenNIC URL", "e.g. http://example.null", errors.fetch("opennic", nil), params.fetch("opennic", nil) -- cgit 1.4.1