-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ph4r5h4d/http-support
Add Http support
- Loading branch information
Showing
7 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package httpChecker | ||
|
||
import "net/url" | ||
|
||
func (h *HttpCheck) validateUrl() bool { | ||
_, err := url.ParseRequestURI(h.Url) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
u, err := url.Parse(h.Url) | ||
if err != nil || u.Scheme == "" || u.Host == "" { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (h *HttpCheck) validateStatusCode() bool { | ||
// check against common status code | ||
if h.Status < 100 || h.Status > 599 { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package httpChecker | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"wait4it/model" | ||
) | ||
|
||
func (h *HttpCheck) BuildContext(cx model.CheckContext) { | ||
h.Url = cx.Host | ||
h.Status = cx.HttpConf.StatusCode | ||
if len(cx.HttpConf.Text) > 0 { | ||
h.Text = cx.HttpConf.Text | ||
} | ||
} | ||
|
||
func (h *HttpCheck) Validate() (bool, error) { | ||
if !h.validateUrl() { | ||
return false, errors.New("invalid URL provided") | ||
} | ||
|
||
if !h.validateStatusCode() { | ||
return false, errors.New("invalid status code provided") | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (h *HttpCheck) Check() (bool, bool, error) { | ||
resp, err := http.Get(h.Url) | ||
|
||
if err != nil { | ||
return false, true, err | ||
} | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return false, true, err | ||
} | ||
|
||
if resp.StatusCode != h.Status { | ||
return false, false, errors.New("invalid status code") | ||
} | ||
|
||
if len(h.Text) > 0 { | ||
if !strings.Contains(string(body), h.Text) { | ||
return false, false, errors.New("can't find substring in response") | ||
} | ||
} | ||
|
||
return true, false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package httpChecker | ||
|
||
type HttpCheck struct { | ||
Url string | ||
Status int | ||
Text string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters