Skip to content

Commit

Permalink
fixup! refactor(coap-server): refactor request handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jul 24, 2023
1 parent 42cdad6 commit 1b1766b
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export default class CoapServer implements ProtocolServer {

if (isUnsupportedAcceptValue) {
debug(`Request contained an accept option with value ${accept} which is not supported.`);
this.sendErrorResponse(res, "4.06", `Content-Format ${accept} is not supported by this resource.`);
this.sendResponse(res, "4.06", `Content-Format ${accept} is not supported by this resource.`);
return;
}

Expand Down Expand Up @@ -375,7 +375,7 @@ export default class CoapServer implements ProtocolServer {
const method = req.method;

if (!this.checkContentTypeSupportForInput(method, contentType)) {
this.sendErrorResponse(res, "4.15", "Unsupported Media Type");
this.sendResponse(res, "4.15", "Unsupported Media Type");
return;
}

Expand Down Expand Up @@ -464,7 +464,7 @@ export default class CoapServer implements ProtocolServer {
break;
case "PUT":
if (property.readOnly === true) {
this.sendErrorResponse(res, "4.00", "Property readOnly");
this.sendResponse(res, "4.00", "Property readOnly");
return;
}

Expand Down Expand Up @@ -495,7 +495,7 @@ export default class CoapServer implements ProtocolServer {
this.streamContentResponse(res, content);
} catch (err) {
error(`CoapServer on port ${this.getPort()} got internal error on read '${req.url}': ${err.message}`);
this.sendErrorResponse(res, "5.00", err.message);
this.sendResponse(res, "5.00", err.message);
}
}

Expand All @@ -510,7 +510,7 @@ export default class CoapServer implements ProtocolServer {
this.streamContentResponse(res, content, { end: true });
} catch (err) {
error(`CoapServer on port ${this.getPort()} got internal error on read '${req.url}': ${err.message}`);
this.sendErrorResponse(res, "5.00", err.message);
this.sendResponse(res, "5.00", err.message);
}
};

Expand Down Expand Up @@ -556,7 +556,7 @@ export default class CoapServer implements ProtocolServer {
this.sendChangedResponse(res);
} catch (err) {
error(`CoapServer on port ${this.getPort()} got internal error on write '${req.url}': ${err.message}`);
this.sendErrorResponse(res, "5.00", err.message);
this.sendResponse(res, "5.00", err.message);
}
}

Expand Down Expand Up @@ -599,7 +599,7 @@ export default class CoapServer implements ProtocolServer {
}
} catch (err) {
error(`CoapServer on port ${this.getPort()} got internal error on invoke '${req.url}': ${err.message}`);
this.sendErrorResponse(res, "5.00", err.message);
this.sendResponse(res, "5.00", err.message);
}
}

Expand Down Expand Up @@ -648,7 +648,7 @@ export default class CoapServer implements ProtocolServer {
req.rsinfo.address
)}:${req.rsinfo.port}`
);
this.sendErrorResponse(res, "4.00", "No Observe Option");
this.sendResponse(res, "4.00", "No Observe Option");
return;
}

Expand All @@ -673,7 +673,7 @@ export default class CoapServer implements ProtocolServer {
this.streamContentResponse(res, value);
} catch (err) {
debug(`CoapServer on port ${this.getPort()} failed '${affordanceKey}' subscription`);
this.sendErrorResponse(res, "5.00", `Subscription to event failed`);
this.sendResponse(res, "5.00", `Subscription to event failed`);
}
};

Expand Down Expand Up @@ -701,7 +701,7 @@ export default class CoapServer implements ProtocolServer {
);
// TODO: Check if this has been fixed in the meantime
// node-coap does not support GET cancellation
this.sendErrorResponse(res, "5.01", "node-coap issue: no GET cancellation, send RST");
this.sendResponse(res, "5.01", "node-coap issue: no GET cancellation, send RST");
}
}

Expand Down Expand Up @@ -771,13 +771,11 @@ export default class CoapServer implements ProtocolServer {

private sendContentResponse(res: OutgoingMessage, payload: Buffer | string, contentType: string) {
res.setOption("Content-Format", contentType);
res.code = "2.05";
res.end(payload);
this.sendResponse(res, "2.05", payload);
}

private sendChangedResponse(res: OutgoingMessage, payload?: Buffer | string) {
res.code = "2.04";
res.end(payload);
this.sendResponse(res, "2.04", payload);
}

// TODO: The name of this method might not be ideal yet.
Expand All @@ -788,16 +786,16 @@ export default class CoapServer implements ProtocolServer {
}

private sendNotFoundResponse(res: OutgoingMessage) {
this.sendErrorResponse(res, "4.04", "Not Found");
this.sendResponse(res, "4.04", "Not Found");
}

private sendMethodNotAllowedResponse(res: OutgoingMessage) {
this.sendErrorResponse(res, "4.05", "Method Not Allowed");
this.sendResponse(res, "4.05", "Method Not Allowed");
}

private sendErrorResponse(res: OutgoingMessage, errorCode: string, errorMessage: string) {
res.code = errorCode;
res.end(errorMessage);
private sendResponse(res: OutgoingMessage, responseCode: string, payload?: string | Buffer) {
res.code = responseCode;
res.end(payload);
}

private formatRequestOrigin(req: IncomingMessage) {
Expand Down

0 comments on commit 1b1766b

Please sign in to comment.