Skip to content
This repository has been archived by the owner on Feb 14, 2021. It is now read-only.

Commit

Permalink
Release 1.3.0
Browse files Browse the repository at this point in the history
Added POST request handling
Added broadcastToSubscribers for POST payload
Misc cleanup
  • Loading branch information
FlyingDiver committed Dec 27, 2018
1 parent 9acf299 commit 1fcbafc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
4 changes: 2 additions & 2 deletions HTTPd.indigoPlugin/Contents/Info.plist
Expand Up @@ -3,13 +3,13 @@
<plist version="1.0">
<dict>
<key>PluginVersion</key>
<string>1.2.0</string>
<string>1.3.0</string>
<key>ServerApiVersion</key>
<string>2.0</string>
<key>IwsApiVersion</key>
<string>1.0.0</string>
<key>CFBundleName</key>
<string>SMTPd</string>
<string>HTTPd</string>
<key>CFBundleDisplayName</key>
<string>HTTPd</string>
<key>CFBundleIdentifier</key>
Expand Down
77 changes: 60 additions & 17 deletions HTTPd.indigoPlugin/Contents/Server Plugin/plugin.py
Expand Up @@ -32,16 +32,6 @@ def do_POST(self):
client_host, client_port = self.client_address
self.logger.debug("AuthHandler: POST from %s:%s to %s" % (str(client_host), str(client_port), self.path))

self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()


def do_GET(self):
self.logger = logging.getLogger("Plugin.AuthHandler")
client_host, client_port = self.client_address
self.logger.debug("AuthHandler: GET from %s:%s for %s" % (str(client_host), str(client_port), self.path))

auth_header = self.headers.getheader('Authorization')

if auth_header == None:
Expand All @@ -61,6 +51,7 @@ def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

self.wfile.write("<html>\n<head><title>Indigo HTTPd Plugin</title></head>\n<body>")
request = urlparse(self.path)

Expand All @@ -72,23 +63,75 @@ def do_GET(self):
updateVar("httpd_"+key, value, indigo.activePlugin.pluginPrefs["folderId"])
self.wfile.write("\n<p>Updated variable httpd_%s to '%s'</p>" % (key, value))

indigo.activePlugin.triggerCheck()
else:
self.logger.debug(u"AuthHandler: Unknown request: %s" % request.path)
self.wfile.write("\n<p>Unknown request: {}".format(request.path))

self.wfile.write("\n</body>\n</html>\n")

payload = self.rfile.read(int(self.headers['Content-Length']))
indigo.server.broadcastToSubscribers(u"httpd_post_payload", payload)
self.logger.debug("POST Payload = {}".format(payload))

indigo.activePlugin.triggerCheck()

else:
self.logger.debug(u"AuthHandler: Request with invalid Authorization header")

self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm="My Realm"')
self.send_header("Content-type", "text/html")
self.end_headers()
self.logger.debug(u"Theirs: '%s' -> '%s'" % (auth_header, base64.b64decode(auth_header[6:])))
self.logger.debug(u"Ours: '%s' -> '%s'" % ('Basic ' + self.server.authKey, base64.b64decode(self.server.authKey)))
self.wfile.write("<html>\n<head><title>Indigo HTTPd Plugin</title></head>\n<body>")
self.wfile.write("\n<p>Invalid Authentication</p>")
self.wfile.write("\n</body>\n</html>\n")


elif request.path == "/stripvar":
def do_GET(self):
self.logger = logging.getLogger("Plugin.AuthHandler")
client_host, client_port = self.client_address
self.logger.debug("AuthHandler: GET from %s:%s for %s" % (str(client_host), str(client_port), self.path))

auth_header = self.headers.getheader('Authorization')

if auth_header == None:
self.logger.debug("AuthHandler: Request has no Authorization header")

self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm="My Realm"')
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("<html>\n<head><title>Indigo HTTPd Plugin</title></head>\n<body>")
self.wfile.write("\n<p>Basic Authentication Required</p>")
self.wfile.write("\n</body>\n</html>\n")

elif auth_header == ('Basic ' + self.server.authKey):
self.logger.debug(u"AuthHandler: Request has correct Authorization header")

self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write("<html>\n<head><title>Indigo HTTPd Plugin</title></head>\n<body>")
request = urlparse(self.path)

if request.path == "/setvar":
query = parse_qs(request.query)
for key in query:
value = query[key][0].strip()
value = query[key][0]
self.logger.debug(u"AuthHandler: setting variable httpd_%s to '%s'" % (key, value))
updateVar("httpd_"+key, value, indigo.activePlugin.pluginPrefs["folderId"])
self.wfile.write("\n<p>Updated variable httpd_%s to '%s'</p>" % (key, value))

indigo.activePlugin.triggerCheck()

else:
self.logger.debug(u"AuthHandler: Unknown request: %s" % self.request)
self.logger.debug(u"AuthHandler: Unknown request: %s" % request.path)
self.wfile.write("\n<p>Unknown request: {}".format(request.path))

self.wfile.write("\n</body>\n</html>\n")

indigo.activePlugin.triggerCheck()

else:
self.logger.debug(u"AuthHandler: Request with invalid Authorization header")

Expand All @@ -99,7 +142,7 @@ def do_GET(self):
self.logger.debug(u"Theirs: '%s' -> '%s'" % (auth_header, base64.b64decode(auth_header[6:])))
self.logger.debug(u"Ours: '%s' -> '%s'" % ('Basic ' + self.server.authKey, base64.b64decode(self.server.authKey)))
self.wfile.write("<html>\n<head><title>Indigo HTTPd Plugin</title></head>\n<body>")
self.wfile.write("\n<p>Invalid Authentication</p>")
self.wfile.write("\n<p>Invalid Authentication Header</p>")
self.wfile.write("\n</body>\n</html>\n")


Expand Down

0 comments on commit 1fcbafc

Please sign in to comment.