Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/wavly/shawty
Browse files Browse the repository at this point in the history
  • Loading branch information
statulr committed Sep 12, 2024
2 parents 3597f37 + fbb6eb9 commit f64f477
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions handlers/shawty.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"html/template"
"log"
"net/http"
"net/url"
"strings"

"github.com/wavly/shawty/asserts"
Expand All @@ -24,11 +25,15 @@ func Shawty(w http.ResponseWriter, r *http.Request) {

longUrl := r.FormValue("url")

// Check if longUrl contains valid schema "://" and if not then added it manually
// Check if longUrl contains "://" and add "https://" if missing
if !strings.Contains(longUrl, "://") {
longUrl = "https://" + longUrl
} else if !strings.Contains(longUrl, "http") { // Check if longUrl schema is http(s)
w.Write([]byte("Only HTTP or HTTPS schema is allowed"))
}

// Parse the URL to validate it and check its scheme
parsedUrl, err := url.Parse(longUrl)
if err != nil || parsedUrl.Scheme != "https" {
w.Write([]byte("Invalid URL. Only HTTPS schema is allowed"))
return
}

Expand Down

0 comments on commit f64f477

Please sign in to comment.