Commits

Miki Tebeka committed 0bec5ba

Add scheme if needed and check for valid url

  • Participants
  • Parent commits f9917ea

Comments (0)

Files changed (1)

shortie/shortie.go

 	"http"
 	"log"
 	"os"
+	"regexp"
 	"strings"
 	"template"
 	"time"
 	return fmt.Sprintf("http://%s/%s", r.Host, id)
 }
 
+func hasSchema(url string) bool {
+	match, _ := regexp.MatchString("^[a-zA-Z]+://", url)
+	return match
+}
+
 func rootHandler(w http.ResponseWriter, r *http.Request) {
 	if r.URL.Path != "/" {
 		redirectHandler(w, r)
 			return
 		}
 
+		if !hasSchema(longURL) {
+			longURL = fmt.Sprintf("http://%s", longURL)
+		}
+
+		if !strings.Contains(longURL, ".") {
+			err = os.NewError(fmt.Sprintf("Bad URL - %s", longURL))
+			return
+		}
+
 		var id string
 		id, err = nextId(ctx)
 		if err != nil {