From df9ecdd7aa972e7be7a9b9386b7270f8177b7327 Mon Sep 17 00:00:00 2001 From: danielpeintner Date: Wed, 30 Jan 2019 09:37:42 +0100 Subject: [PATCH] fix: add pattern to href in case of uriVariables e.g., decrement{?step} --- packages/binding-http/src/http-server.ts | 28 +++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/binding-http/src/http-server.ts b/packages/binding-http/src/http-server.ts index c4964d56b..d223d6113 100644 --- a/packages/binding-http/src/http-server.ts +++ b/packages/binding-http/src/http-server.ts @@ -147,6 +147,25 @@ export default class HttpServer implements ProtocolServer { return -1; } } + + + private updateInteractionNameWithUriVariablePattern(interactionName: string, uriVariables: {[key: string]: WoT.DataSchema;}) : string { + if(uriVariables && Object.keys(uriVariables).length > 0) { + let pattern = "{?" + let index = 0; + for(let key in uriVariables) { + if(index != 0) { + pattern += ","; + } + pattern += encodeURIComponent(key); + index++; + } + pattern += "}"; + return encodeURIComponent(interactionName) + pattern; + } else { + return encodeURIComponent(interactionName); + } + } public expose(thing: ExposedThing): Promise { @@ -176,7 +195,8 @@ export default class HttpServer implements ProtocolServer { } for (let propertyName in thing.properties) { - let href = base + "/" + this.PROPERTY_DIR + "/" + encodeURIComponent(propertyName); + let propertyNamePattern = this.updateInteractionNameWithUriVariablePattern(propertyName, thing.properties[propertyName].uriVariables); + let href = base + "/" + this.PROPERTY_DIR + "/" + propertyNamePattern; let form = new TD.Form(href, type); if (thing.properties[propertyName].readOnly) { form.op = ["readproperty"]; @@ -201,7 +221,8 @@ export default class HttpServer implements ProtocolServer { } for (let actionName in thing.actions) { - let href = base + "/" + this.ACTION_DIR + "/" + encodeURIComponent(actionName); + let actionNamePattern = this.updateInteractionNameWithUriVariablePattern(actionName, thing.actions[actionName].uriVariables); + let href = base + "/" + this.ACTION_DIR + "/" + actionNamePattern; let form = new TD.Form(href, type); form.op = ["invokeaction"]; thing.actions[actionName].forms.push(form); @@ -209,7 +230,8 @@ export default class HttpServer implements ProtocolServer { } for (let eventName in thing.events) { - let href = base + "/" + this.EVENT_DIR + "/" + encodeURIComponent(eventName); + let eventNamePattern = this.updateInteractionNameWithUriVariablePattern(eventName, thing.events[eventName].uriVariables); + let href = base + "/" + this.EVENT_DIR + "/" + eventNamePattern; let form = new TD.Form(href, type); form.subprotocol = "longpoll"; form.op = ["subscribeevent"];