Skip to content

Commit

Permalink
implement intended url plus path composition for http command and agent
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs authored and MaartendeKruijf committed May 13, 2024
1 parent 8c578cf commit a25062f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
26 changes: 26 additions & 0 deletions test/unittest/utils/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,32 @@ func TestHttpPathParser(t *testing.T) {
assert.Equal(t, parsedUrl, "https://godcapability.tno.nl")
}

func TestHttpPathUrlComposition(t *testing.T) {
target := cacao.AgentTarget{
Address: map[cacao.NetAddressType][]string{
"url": {"https://godcapability.tno.nl/isp"},
},
}

command := cacao.Command{
Type: "http-api",
Command: "POST /isp/cst HTTP/1.1",
Headers: map[string][]string{"accept": {"application/json"}},
}
httpOptions := http.HttpOptions{
Target: &target,
Command: &command,
}

parsedUrl, err := httpOptions.ExtractUrl()
if err != nil {
t.Error("failed test because: ", err)
}
// Duplication of path values if present is INTENDED behaviour and
// a warning will be issued
assert.Equal(t, parsedUrl, "https://godcapability.tno.nl/isp/isp/cst")
}

func TestHttpPathBreakingParser(t *testing.T) {
target := cacao.AgentTarget{
Address: map[cacao.NetAddressType][]string{
Expand Down
29 changes: 25 additions & 4 deletions utils/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ func (httpOptions *HttpOptions) ExtractUrl() (string, error) {
}
}

// If for an http-api command the agent-target address is a URL, it must be handled differently than dname and ip addresses
// If for an http-api command the agent-target address is a URL,
// we should warn for misuse of the field when appendind command-specified path
if len(target.Address["url"]) > 0 {
if target.Address["url"][0] != "" {
urlObject, err := parsePathBasedUrl(target.Address["url"][0])
if err != nil {
return "", err
}
if (urlObject.Path != "" && urlObject.Path != "/") && urlObject.Path != path {
log.Warn("agent-target url has path that does not match http-api command path")
if (urlObject.Path != "" && urlObject.Path != "/") && urlObject.Path == path {
log.Warn("possible http api invocation path duplication: agent-target url has same path of http-api command path")
}
return urlObject.String(), nil
}
}
return buildSchemeAndHostname(path, target)
Expand All @@ -229,6 +229,20 @@ func buildSchemeAndHostname(path string, target *cacao.AgentTarget) (string, err
return "", err
}

// If only URL is used to compose the URL target, then
// scheme and port are not considered
if len(target.Address["url"]) > 0 &&
len(target.Address["dname"]) == 0 &&
len(target.Address["ipv4"]) == 0 &&
len(target.Address["ipv6"]) == 0 {

url := strings.TrimSuffix(hostname, "/")
if len(path) > 1 && !strings.HasPrefix(path, "/") {
path = "/" + path
}
return strings.TrimSuffix(url+path, "/"), nil
}

parsedUrl := &url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s:%s", hostname, target.Port),
Expand Down Expand Up @@ -268,6 +282,13 @@ func extractHostname(target *cacao.AgentTarget) (string, error) {
}
address = target.Address["ipv4"][0]

} else if len(target.Address["url"]) > 0 {
_, err := parsePathBasedUrl(target.Address["url"][0])
if err != nil {
return "", err
}
address = target.Address["url"][0]

} else {
return "", errors.New("unsupported target address type")
}
Expand Down

0 comments on commit a25062f

Please sign in to comment.