Skip to content

Commit a01e20d

Browse files
committed
implement intended url plus path composition for http command and agent
1 parent 49b881d commit a01e20d

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

test/unittest/utils/http/http_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,32 @@ func TestHttpPathParser(t *testing.T) {
417417
assert.Equal(t, parsedUrl, "https://godcapability.tno.nl")
418418
}
419419

420+
func TestHttpPathUrlComposition(t *testing.T) {
421+
target := cacao.AgentTarget{
422+
Address: map[cacao.NetAddressType][]string{
423+
"url": {"https://godcapability.tno.nl/isp"},
424+
},
425+
}
426+
427+
command := cacao.Command{
428+
Type: "http-api",
429+
Command: "POST /isp/cst HTTP/1.1",
430+
Headers: map[string][]string{"accept": {"application/json"}},
431+
}
432+
httpOptions := http.HttpOptions{
433+
Target: &target,
434+
Command: &command,
435+
}
436+
437+
parsedUrl, err := httpOptions.ExtractUrl()
438+
if err != nil {
439+
t.Error("failed test because: ", err)
440+
}
441+
// Duplication of path values if present is INTENDED behaviour and
442+
// a warning will be issued
443+
assert.Equal(t, parsedUrl, "https://godcapability.tno.nl/isp/isp/cst")
444+
}
445+
420446
func TestHttpPathBreakingParser(t *testing.T) {
421447
target := cacao.AgentTarget{
422448
Address: map[cacao.NetAddressType][]string{

utils/http/http.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,17 @@ func (httpOptions *HttpOptions) ExtractUrl() (string, error) {
174174
}
175175
}
176176

177-
// If for an http-api command the agent-target address is a URL, it must be handled differently than dname and ip addresses
177+
// If for an http-api command the agent-target address is a URL,
178+
// we should warn for misuse of the field when appendind command-specified path
178179
if len(target.Address["url"]) > 0 {
179180
if target.Address["url"][0] != "" {
180181
urlObject, err := parsePathBasedUrl(target.Address["url"][0])
181182
if err != nil {
182183
return "", err
183184
}
184-
if (urlObject.Path != "" && urlObject.Path != "/") && urlObject.Path != path {
185-
log.Warn("agent-target url has path that does not match http-api command path")
185+
if (urlObject.Path != "" && urlObject.Path != "/") && urlObject.Path == path {
186+
log.Warn("possible http api invocation path duplication: agent-target url has same path of http-api command path")
186187
}
187-
return urlObject.String(), nil
188188
}
189189
}
190190
return buildSchemeAndHostname(path, target)
@@ -199,6 +199,20 @@ func buildSchemeAndHostname(path string, target *cacao.AgentTarget) (string, err
199199
return "", err
200200
}
201201

202+
// If only URL is used to compose the URL target, then
203+
// scheme and port are not considered
204+
if len(target.Address["url"]) > 0 &&
205+
len(target.Address["dname"]) == 0 &&
206+
len(target.Address["ipv4"]) == 0 &&
207+
len(target.Address["ipv6"]) == 0 {
208+
209+
url := strings.TrimSuffix(hostname, "/")
210+
if len(path) > 1 && !strings.HasPrefix(path, "/") {
211+
path = "/" + path
212+
}
213+
return strings.TrimSuffix(url+path, "/"), nil
214+
}
215+
202216
parsedUrl := &url.URL{
203217
Scheme: scheme,
204218
Host: fmt.Sprintf("%s:%s", hostname, target.Port),
@@ -238,6 +252,13 @@ func extractHostname(target *cacao.AgentTarget) (string, error) {
238252
}
239253
address = target.Address["ipv4"][0]
240254

255+
} else if len(target.Address["url"]) > 0 {
256+
_, err := parsePathBasedUrl(target.Address["url"][0])
257+
if err != nil {
258+
return "", err
259+
}
260+
address = target.Address["url"][0]
261+
241262
} else {
242263
return "", errors.New("unsupported target address type")
243264
}

0 commit comments

Comments
 (0)