Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
validating URLs before sending requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbmor committed Feb 9, 2020
1 parent 6ea227c commit 4ef9c82
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
6 changes: 3 additions & 3 deletions methods/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

//Deletebasic sends a basic DELETE request
func Deletebasic(c *cli.Context) error {
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
return nil
}
var jsonStr = []byte(c.String("body"))
Expand Down
6 changes: 3 additions & 3 deletions methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

//Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth
func Getbasic(c *cli.Context) {
var url = c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
var url, err = checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(0)
}
req, err := http.NewRequest("GET", url, nil)
Expand Down
6 changes: 3 additions & 3 deletions methods/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

//Patchbasic sends a basic PATCH request
func Patchbasic(c *cli.Context) {
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(0)
}
var jsonStr = []byte(c.String("body"))
Expand Down
6 changes: 3 additions & 3 deletions methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

//Postbasic sends a basic POST request
func Postbasic(c *cli.Context) {
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(0)
}
var jsonStr = []byte(c.String("body"))
Expand Down
6 changes: 3 additions & 3 deletions methods/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

//Putbasic sends a basic PUT request
func Putbasic(c *cli.Context) error {
url := c.Args().Get(0)
if url == "" {
fmt.Print("URL is needed")
url, err := checkURL(c.Args().Get(0))
if err != nil {
fmt.Printf("%s\n", err.Error())
return nil
}
var jsonStr = []byte(c.String("body"))
Expand Down
24 changes: 24 additions & 0 deletions methods/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package methods

import (
"fmt"
"net/url"
"strings"
)

func checkURL(urlStr string) (string, error) {
if urlStr == "" {
return "", fmt.Errorf("URL is needed")
}

prefixCheck := strings.HasPrefix(urlStr, "http://") || strings.HasPrefix(urlStr, "https://")
if !prefixCheck {
return "", fmt.Errorf("URL missing protocol or contains invalid protocol")
}

if _, err := url.Parse(urlStr); err != nil {
return "", fmt.Errorf("URL is invalid")
}

return urlStr, nil
}

0 comments on commit 4ef9c82

Please sign in to comment.