Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Accept Url escape in API path #1502

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func SendEvent(event *dtos.Event, correlationID string, dic *di.Container) {
ctx = context.WithValue(ctx, common.ContentType, encoding) // nolint: staticcheck
envelope := types.NewMessageEnvelope(bytes, ctx)
serviceName := container.DeviceServiceFrom(dic.Get).Name
publishTopic := common.BuildTopic(configuration.MessageBus.GetBaseTopicPrefix(), common.EventsPublishTopic, DeviceServiceEventPrefix, serviceName, event.ProfileName, event.DeviceName, common.URLEncode(event.SourceName))
publishTopic := common.BuildTopic(configuration.MessageBus.GetBaseTopicPrefix(), common.EventsPublishTopic, DeviceServiceEventPrefix, common.URLEncode(serviceName), common.URLEncode(event.ProfileName), common.URLEncode(event.DeviceName), common.URLEncode(event.SourceName))
err = mc.Publish(envelope, publishTopic)
if err != nil {
lc.Errorf("Failed to publish event to MessageBus: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/messaging/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func MetadataSystemEventsCallback(ctx context.Context, serviceBaseName string, d
messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus
deviceService := container.DeviceServiceFrom(dic.Get)
metadataSystemEventTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(),
common.MetadataSystemEventSubscribeTopic, deviceService.Name, "#")
common.MetadataSystemEventSubscribeTopic, common.URLEncode(deviceService.Name), "#")

lc.Infof("Subscribing to System Events on topic: %s", metadataSystemEventTopic)

Expand All @@ -49,7 +49,7 @@ func MetadataSystemEventsCallback(ctx context.Context, serviceBaseName string, d
// Must replace the first wildcard with the type for Provision Watchers
baseSubscribeTopic := strings.Replace(common.MetadataSystemEventSubscribeTopic, "+", common.ProvisionWatcherSystemEventType, 1)
provisionWatcherSystemEventSubscribeTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(),
baseSubscribeTopic, serviceBaseName, "#")
baseSubscribeTopic, common.URLEncode(serviceBaseName), "#")

topics = append(topics, types.TopicChannel{
Topic: provisionWatcherSystemEventSubscribeTopic,
Expand Down
13 changes: 9 additions & 4 deletions internal/controller/messaging/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ func SubscribeCommands(ctx context.Context, dic *di.Container) errors.EdgeX {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus
deviceService := container.DeviceServiceFrom(dic.Get)
escapedDeviceServiceName := common.URLEncode(deviceService.Name)

requestSubscribeTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.CommandRequestSubscribeTopic, deviceService.Name, "#")
requestSubscribeTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.CommandRequestSubscribeTopic, escapedDeviceServiceName, "#")
lc.Infof("Subscribing to command requests on topic: %s", requestSubscribeTopic)

responsePublishTopicPrefix := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.ResponseTopic, deviceService.Name)
responsePublishTopicPrefix := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), common.ResponseTopic, escapedDeviceServiceName)
lc.Infof("Responses to command requests will be published on topic: %s/<requestId>", responsePublishTopicPrefix)

messages := make(chan types.MessageEnvelope, 1)
Expand Down Expand Up @@ -71,10 +72,14 @@ func SubscribeCommands(ctx context.Context, dic *di.Container) errors.EdgeX {
}

// expected command response topic scheme: #/<service-name>/<device-name>/<command-name>/<method>
deviceName := topicLevels[length-3]
deviceName, err := url.PathUnescape(topicLevels[length-3])
if err != nil {
lc.Errorf("Failed to unescape device name: %s", err.Error())
continue
}
commandName, err := url.PathUnescape(topicLevels[length-2])
if err != nil {
lc.Errorf("Failed to unescape command name '%s'", commandName)
lc.Errorf("Failed to unescape command name: %s", err.Error())
continue
}
method := topicLevels[length-1]
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/messaging/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func SubscribeDeviceValidation(ctx context.Context, dic *di.Container) errors.EdgeX {
lc := bootstrapContainer.LoggingClientFrom(dic.Get)
messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus
serviceName := container.DeviceServiceFrom(dic.Get).Name
serviceName := common.URLEncode(container.DeviceServiceFrom(dic.Get).Name)

requestTopic := common.BuildTopic(messageBusInfo.GetBaseTopicPrefix(), serviceName, common.ValidateDeviceSubscribeTopic)
lc.Infof("Subscribing to device validation requests on topic: %s", requestTopic)
Expand Down