mirror of
https://github.com/4rkal/shortr.git
synced 2025-01-28 18:46:28 +02:00
fixed url validation
This commit is contained in:
parent
d1f48ae00e
commit
64dcf4614e
28
cmd/main.go
28
cmd/main.go
@ -5,6 +5,8 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/4rkal/shortr/models"
|
||||
@ -50,6 +52,10 @@ func RedirectHandler(c echo.Context) error {
|
||||
return c.String(http.StatusNotFound, "Link not found")
|
||||
}
|
||||
|
||||
if !strings.Contains(link.Url, "://") {
|
||||
link.Url = "http://" + link.Url
|
||||
}
|
||||
|
||||
link.Clicks = link.Clicks + 1
|
||||
|
||||
return c.Redirect(http.StatusMovedPermanently, link.Url)
|
||||
@ -102,8 +108,26 @@ func StatsSubmissionHandler(c echo.Context) error {
|
||||
}
|
||||
|
||||
func isURL(s string) bool {
|
||||
_, err := url.ParseRequestURI(s)
|
||||
return err == nil
|
||||
if !strings.Contains(s, "://") {
|
||||
s = "http://" + s
|
||||
}
|
||||
|
||||
parsedUrl, err := url.ParseRequestURI(s)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if parsedUrl.Host == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
domainRegex := `^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$`
|
||||
matched, err := regexp.MatchString(domainRegex, parsedUrl.Host)
|
||||
if err != nil || !matched {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func generateRandomString(length int) string {
|
||||
|
@ -7,7 +7,7 @@ templ Index(){
|
||||
<h1>Blazingly fast URL shortener</h1>
|
||||
<p>Shorten URLs and track clicks</p>
|
||||
<form action="/submit" method="post">
|
||||
<input type="url" id="url" name="url" placeholder="Enter URL here...">
|
||||
<input type="text" id="url" name="url" placeholder="Enter URL here...">
|
||||
<button type="submit">Shorten URL</button>
|
||||
</form>
|
||||
</div>
|
||||
|
@ -41,7 +41,7 @@ func Index() templ.Component {
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"card\"><h1>Blazingly fast URL shortener</h1><p>Shorten URLs and track clicks</p><form action=\"/submit\" method=\"post\"><input type=\"url\" id=\"url\" name=\"url\" placeholder=\"Enter URL here...\"> <button type=\"submit\">Shorten URL</button></form></div>")
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"card\"><h1>Blazingly fast URL shortener</h1><p>Shorten URLs and track clicks</p><form action=\"/submit\" method=\"post\"><input type=\"text\" id=\"url\" name=\"url\" placeholder=\"Enter URL here...\"> <button type=\"submit\">Shorten URL</button></form></div>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user