From 145983bc20c08aa46e362a7d4578bf059fc7e50b Mon Sep 17 00:00:00 2001 From: Ben Sykes Date: Mon, 18 Apr 2022 05:25:21 -0700 Subject: [PATCH] Include `Content-Length` header when POSTing to sockets (#900) Newer sockets close the connection when starting to write data without this header. --- hardware/wemo/lib/wemo.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/hardware/wemo/lib/wemo.js b/hardware/wemo/lib/wemo.js index 05d9ba68..cd097ddf 100644 --- a/hardware/wemo/lib/wemo.js +++ b/hardware/wemo/lib/wemo.js @@ -338,6 +338,17 @@ WeMoNG.prototype.get = function get(deviceID) { } WeMoNG.prototype.toggleSocket = function toggleSocket(socket, on) { + + var body = [ + postbodyheader, + '', + '%s', + '', + postbodyfooter + ].join('\n'); + + var data = util.format(body, on); + var postoptions = { host: socket.ip, port: socket.port, @@ -346,7 +357,8 @@ WeMoNG.prototype.toggleSocket = function toggleSocket(socket, on) { headers: { 'SOAPACTION': '"urn:Belkin:service:basicevent:1#SetBinaryState"', 'Content-Type': 'text/xml; charset="utf-8"', - 'Accept': '' + 'Accept': '', + 'Content-Length': data.length } }; @@ -372,15 +384,7 @@ WeMoNG.prototype.toggleSocket = function toggleSocket(socket, on) { post_request.abort(); }); - var body = [ - postbodyheader, - '', - '%s', - '', - postbodyfooter - ].join('\n'); - - post_request.write(util.format(body, on)); + post_request.write(data); post_request.end(); } @@ -394,6 +398,7 @@ WeMoNG.prototype.getSocketStatus = function getSocketStatus(socket) { 'SOAPACTION': getSocketState.action, 'Content-Type': 'text/xml; charset="utf-8"', 'Accept': '' + 'Content-Length': getSocketState.body.length } }