added URL validation to webpage part

pull/22/head
Adam Townsend 2023-10-12 23:42:52 -07:00
parent 72f3e03edd
commit 144731062b
1 changed files with 14 additions and 0 deletions

14
app.rb
View File

@ -34,6 +34,20 @@ class App < Roda
flash['message'] = "Please enter a valid URL"; flash['message'] = "Please enter a valid URL";
r.redirect '/' r.redirect '/'
end end
begin
OpenURI.open_uri(url)
rescue URI::BadURIError
flash['message'] = "Invalid URL"
r.redirect '/'
rescue OpenURI::HTTPError
flash['message'] = "URL not found"
r.redirect '/'
rescue SocketError => e
flash['message'] = "URL does not resolve"
r.redirect '/'
end
if links.filter(:url => url).first.nil? if links.filter(:url => url).first.nil?
code = SecureRandom.urlsafe_base64 4 code = SecureRandom.urlsafe_base64 4
links.insert(url: url, code: code) links.insert(url: url, code: code)