Skip to content

Commit 9f369a2

Browse files
committed
Complete URL parsing
1 parent 869719d commit 9f369a2

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

cmd/post.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020

2121
var (
2222
template, clusterUUID, caseID string
23+
isURL bool
24+
HTMLBody []byte
2325
Message servicelog.Message
2426
GoodReply servicelog.GoodReply
2527
BadReply servicelog.BadReply
@@ -98,10 +100,13 @@ func accessTemplate(template string) (err error) {
98100
if err := utils.IsOnline(*urlPage); err != nil {
99101
log.Errorf("host %q is not accessible", template)
100102
} else {
103+
HTMLBody, err = utils.CurlThis(urlPage.String())
104+
if err == nil {
105+
isURL = true
106+
}
101107
return err
102108
}
103109
}
104-
105110
return fmt.Errorf("cannot read the template %q", template)
106111

107112
}
@@ -120,10 +125,17 @@ func parseBadReply(jsonFile []byte) error {
120125
}
121126

122127
func readTemplate() {
123-
if err := accessTemplate(template); err == nil {
124-
file, err := ioutil.ReadFile(template)
125-
if err != nil {
126-
log.Fatalf("Cannot not read the file.\nError: %q\n", err)
128+
if err := accessTemplate(template); err == nil { // check if this URL or file and if we can access it
129+
var file []byte
130+
if isURL {
131+
// template is URL on the web
132+
file = HTMLBody
133+
} else {
134+
// template is file on the disk
135+
file, err = ioutil.ReadFile(template) // this works only for files
136+
if err != nil {
137+
log.Fatalf("Cannot not read the file.\nError: %q\n", err)
138+
}
127139
}
128140

129141
if err = parseTemplate(file); err != nil {

internal/utils/network.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"fmt"
5+
"io/ioutil"
56
"net/http"
67
"net/url"
78
"time"
@@ -42,3 +43,18 @@ func IsValidUrl(toTest string) bool {
4243

4344
return true
4445
}
46+
47+
func CurlThis(webpage string) (body []byte, err error) {
48+
resp, err := http.Get(webpage)
49+
defer func() {
50+
err = resp.Body.Close()
51+
}()
52+
if resp.StatusCode == http.StatusOK {
53+
bodyBytes, err := ioutil.ReadAll(resp.Body)
54+
if err != nil {
55+
return body, err
56+
}
57+
body = bodyBytes
58+
}
59+
return body, err
60+
}

0 commit comments

Comments
 (0)