Skip to content

Commit

Permalink
fix: bug when http or https in included in the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamza12700 committed Sep 12, 2024
1 parent 4b5aa69 commit d966d49
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 d966d49

Please sign in to comment.