From 6e02263f8dc7b5a53bdbadd4134b7ab215717a90 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Mon, 24 Jul 2023 17:26:42 +0200 Subject: [PATCH] fixup! refactor(coap-server): refactor request handling --- packages/binding-coap/src/coap-server.ts | 44 ++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/binding-coap/src/coap-server.ts b/packages/binding-coap/src/coap-server.ts index 181fa6070..c6980c33f 100644 --- a/packages/binding-coap/src/coap-server.ts +++ b/packages/binding-coap/src/coap-server.ts @@ -28,7 +28,7 @@ import Servient, { createLoggers, } from "@node-wot/core"; import { Socket } from "dgram"; -import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage } from "coap"; +import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage, OptionValue } from "coap"; import slugify from "slugify"; import { Readable } from "stream"; import { MdnsIntroducer } from "./mdns-introducer"; @@ -339,26 +339,42 @@ export default class CoapServer implements ProtocolServer { return; } - const accept = req.headers.Accept; + const { contentType, isSupported } = this.processAcceptValue(req); + + if (!isSupported) { + this.sendResponse(res, "4.06", `Content-Format ${contentType} is not supported by this resource.`); + return; + } - const contentSerdes = ContentSerdes.get(); + const content = ContentSerdes.get().valueToContent(thing.getThingDescription(), undefined, contentType); + const payload = await ProtocolHelpers.readStreamFully(content.body); - const isUnsupportedAcceptValue = typeof accept === "string" && !contentSerdes.isSupported(accept); + debug(`Sending CoAP response for TD with Content-Format ${contentType}.`); + this.sendContentResponse(res, payload, contentType); + } - if (isUnsupportedAcceptValue) { - debug(`Request contained an accept option with value ${accept} which is not supported.`); - this.sendResponse(res, "4.06", `Content-Format ${accept} is not supported by this resource.`); - return; + private processAcceptValue(req: IncomingMessage) { + const accept = req.headers.Accept; + + if (typeof accept !== "string") { + debug(`Request contained no Accept option.`); + return { + contentType: ContentSerdes.TD, + isSupported: true, + }; } - debug(`Received an available or no Content-Format (${accept}) in Accept option.`); - const contentFormat = (accept as string) ?? ContentSerdes.TD; + const isSupported = ContentSerdes.get().isSupported(accept); - const content = contentSerdes.valueToContent(thing.getThingDescription(), undefined, contentFormat); - const payload = await ProtocolHelpers.readStreamFully(content.body); + if (!isSupported) { + debug(`Request contained an accept option with value ${accept} which is not supported.`); + } - debug(`Sending CoAP response for TD with Content-Format ${contentFormat}.`); - this.sendContentResponse(res, payload, contentFormat); + debug(`Received an available Content-Format ${accept} in Accept option.`); + return { + contentType: accept, + isSupported, + }; } private async handleRequest(req: IncomingMessage, res: OutgoingMessage) {