@@ -64,6 +64,7 @@ export default class HttpServer implements ProtocolServer {
6464 private readonly address ?: string ;
6565 private readonly baseUri ?: string ;
6666 private readonly urlRewrite ?: Record < string , string > ;
67+ private readonly devFriendlyUri : boolean ;
6768 private readonly supportedSecuritySchemes : string [ ] = [ "nosec" ] ;
6869 private readonly validOAuthClients : RegExp = / .* / g;
6970 private readonly server : http . Server | https . Server ;
@@ -83,6 +84,7 @@ export default class HttpServer implements ProtocolServer {
8384 this . baseUri = config . baseUri ;
8485 this . urlRewrite = config . urlRewrite ;
8586 this . middleware = config . middleware ;
87+ this . devFriendlyUri = config . devFriendlyUri ?? true ;
8688
8789 const router = Router ( {
8890 ignoreTrailingSlash : true ,
@@ -267,21 +269,32 @@ export default class HttpServer implements ProtocolServer {
267269 }
268270
269271 public async expose ( thing : ExposedThing , tdTemplate : WoT . ExposedThingInit = { } ) : Promise < void > {
270- let urlPath = slugify ( thing . title , { lower : true } ) ;
271-
272- // avoid URL clashes
273- if ( this . things . has ( urlPath ) ) {
274- let uniqueUrlPath ;
275- let nameClashCnt = 2 ;
276- do {
277- uniqueUrlPath = urlPath + "_" + nameClashCnt ++ ;
278- } while ( this . things . has ( uniqueUrlPath ) ) ;
279- urlPath = uniqueUrlPath ;
280- }
281-
282272 if ( this . getPort ( ) !== - 1 ) {
283- debug ( `HttpServer on port ${ this . getPort ( ) } exposes '${ thing . title } ' as unique '/${ urlPath } '` ) ;
284- this . things . set ( urlPath , thing ) ;
273+ const paths : string [ ] = [ ] ;
274+ // If not id is given we create the path using the title even if devFriendlyUri is false.
275+ // in Thing Description 1.1 id is optional
276+ if ( this . devFriendlyUri || thing . id == null ) {
277+ let urlPath = slugify ( thing . title , { lower : true } ) ;
278+
279+ // avoid URL clashes
280+ if ( this . things . has ( urlPath ) ) {
281+ let uniqueUrlPath ;
282+ let nameClashCnt = 2 ;
283+ do {
284+ uniqueUrlPath = urlPath + "_" + nameClashCnt ++ ;
285+ } while ( this . things . has ( uniqueUrlPath ) ) ;
286+ urlPath = uniqueUrlPath ;
287+ }
288+ this . things . set ( urlPath , thing ) ;
289+ paths . push ( urlPath ) ;
290+ debug ( "HttpServer on port %d exposes %s as unique '/%s'" , this . getPort ( ) , thing . name , urlPath ) ;
291+ }
292+
293+ if ( thing . id != null ) {
294+ this . things . set ( thing . id , thing ) ;
295+ paths . push ( thing . id ) ;
296+ debug ( "HttpServer on port %d exposes %s as unique '/%s'" , this . getPort ( ) , thing . name , thing . id ) ;
297+ }
285298
286299 if ( this . scheme === "http" && Object . keys ( thing . securityDefinitions ) . length !== 0 ) {
287300 warn ( `HTTP Server will attempt to use your security schemes even if you are not using HTTPS.` ) ;
@@ -290,16 +303,20 @@ export default class HttpServer implements ProtocolServer {
290303 this . fillSecurityScheme ( thing ) ;
291304
292305 if ( this . baseUri !== undefined ) {
293- const base : string = this . baseUri . concat ( "/" , encodeURIComponent ( urlPath ) ) ;
294- info ( "HttpServer TD hrefs using baseUri " + this . baseUri ) ;
295- this . addEndpoint ( thing , tdTemplate , base ) ;
306+ for ( const path of paths ) {
307+ info ( "HttpServer TD hrefs using baseUri %s and path %s" , this . baseUri , path ) ;
308+ const base : string = this . baseUri . concat ( "/" , encodeURIComponent ( path ) ) ;
309+ this . addEndpoint ( thing , tdTemplate , base ) ;
310+ }
296311 } else {
297312 // fill in binding data
298313 for ( const address of Helpers . getAddresses ( ) ) {
299- const base : string =
300- this . scheme + "://" + address + ":" + this . getPort ( ) + "/" + encodeURIComponent ( urlPath ) ;
301-
302- this . addEndpoint ( thing , tdTemplate , base ) ;
314+ for ( const path of paths ) {
315+ const base : string =
316+ this . scheme + "://" + address + ":" + this . getPort ( ) + "/" + encodeURIComponent ( path ) ;
317+ info ( "HttpServer TD hrefs using address %s and path %s" , address , path ) ;
318+ this . addEndpoint ( thing , tdTemplate , base ) ;
319+ }
303320 }
304321 }
305322 }
@@ -311,6 +328,7 @@ export default class HttpServer implements ProtocolServer {
311328 for ( const [ name , thing ] of this . things . entries ( ) ) {
312329 if ( thing . id === thingId ) {
313330 this . things . delete ( name ) ;
331+ this . things . delete ( thingId ) ;
314332 info ( `HttpServer successfully destroyed '${ thing . title } '` ) ;
315333
316334 return true ;
0 commit comments