Skip to content

Commit

Permalink
feat(request.url): append host to input
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Apr 4, 2021
1 parent 72fe0c5 commit ea8d41d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions request/url.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package request

import (
"net"
"net/url"
"regexp"
"strconv"
Expand All @@ -14,6 +15,7 @@ type URLInput struct {
URL string `json:"url" binding:"required"`
ExpiresOn string `json:"expires_on"`
Keywords []string `json:"keywords"`
Host string `json:"-"`
}

// URLFilter defines structure for short code list and search request
Expand Down Expand Up @@ -48,7 +50,7 @@ var (

// Validate validates the url input before saving to db
// It returns error if something is not valid.
func (input URLInput) Validate() error {
func (input *URLInput) Validate() error {
if l := len(input.URL); l < URLMinLength || l > URLMaxLength {
return common.ErrInvalidURLLen
}
Expand All @@ -57,10 +59,16 @@ func (input URLInput) Validate() error {
return common.ErrFilteredURL
}

if _, err := url.ParseRequestURI(input.URL); err != nil {
uri, err := url.ParseRequestURI(input.URL)
if err != nil {
return common.ErrInvalidURL
}

input.Host = uri.Host
if host, _, _ := net.SplitHostPort(uri.Host); host != "" {
input.Host = host
}

if !urlRe.MatchString(input.URL) {
return common.ErrInvalidURL
}
Expand Down

0 comments on commit ea8d41d

Please sign in to comment.