Skip to content

Commit

Permalink
feat(coap-in): Add simple ./well-known/core handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jul 4, 2024
1 parent d42a434 commit 887f4e5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions coap/coap-in.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<label for="node-config-input-port"><span data-i18n="coapServer.configInputPort.label"></span></label>
<input type="text" id="node-config-input-port" placeholder="5683" />
</div>
<div class="form-row">
<input type="checkbox" id="node-config-input-wellknowncore" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-config-input-wellknowncore" style="width: auto" data-i18n="coapServer.configInputwellKnownCore.label"></label>
</div>
<div class="form-tips">
<span data-i18n="[html]coapServer.tip">
</div>
Expand All @@ -26,6 +30,7 @@
name: { value: "" },
port: { value: 5683, required: true },
ipv6: { value: false, required: true },
wellknowncore: { value: false, required: true },
},
inputs: 0,
outputs: 0,
Expand Down
19 changes: 18 additions & 1 deletion coap/coap-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ module.exports = function (RED) {

node._inputNodes = []; // collection of "coap in" nodes that represent coap resources

/**
* Resource list for /.well-known/core
*/
node._resourceList = [];

// Setup node-coap server and start
const serverSettings = {};
if (config.ipv6) {
serverSettings.type = "udp6";
} else {
serverSettings.type = "udp4";
}
node._supportWellKnownCore = config.wellknowncore ?? false;
node.server = new coap.createServer(serverSettings);
node.server.on("request", function (req, res) {
node.handleRequest(req, res);
Expand Down Expand Up @@ -62,6 +68,14 @@ module.exports = function (RED) {
}
};

CoapServerNode.prototype._handleWellKnownCore = function (res) {
// TODO: Expand capabilities of the handler for /.well-known/core
res.code = "2.05";
res.setOption("Content-Format", "application/link-format");
const payload = this._resourceList.map((resource) => `<${resource}>`).join(",")
return res.end(payload);
}

function _getPayload(inNode, rawBuffer, payload, contentFormat) {
if (rawBuffer) {
return payload;
Expand Down Expand Up @@ -96,7 +110,10 @@ module.exports = function (RED) {
}

CoapServerNode.prototype.handleRequest = function (req, res) {
//TODO: Check if there are any matching resource. If the resource is .well-known return the resource directory to the client
if (this._supportWellKnownCore && req.url == "/.well-known/core" && req.method == "GET") {
return this._handleWellKnownCore(res);
}

const node = this;
let matchResource = false;
let matchMethod = false;
Expand Down
3 changes: 3 additions & 0 deletions coap/locales/de/coap-in.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"configInputPort": {
"label": "Port"
},
"configInputwellKnownCore": {
"label": "/.well-known/core aktivieren"
},
"tip": "<em>CoAP</em> basiert auf <em>UDP</em>, weshalb es sein kann, dass Sie einen <em>UDP</em>-Port in Ihrer Firewall öffnen müssen."
}
}
3 changes: 3 additions & 0 deletions coap/locales/en-US/coap-in.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"configInputPort": {
"label": "Port"
},
"configInputwellKnownCore": {
"label": "Activate /.well-known/core"
},
"tip": "<em>CoAP</em> is based on <em>UDP</em> so you may need to open a <em>UDP</em> port in your firewall."
}
}

0 comments on commit 887f4e5

Please sign in to comment.