anki-connect/plugin/web.py

187 lines
4.9 KiB
Python
Raw Normal View History

2021-01-18 06:13:27 +00:00
# Copyright 2016-2021 Alex Yatskov
2020-01-05 23:42:08 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import select
import socket
2021-07-18 17:34:13 +00:00
2020-01-05 23:42:08 +00:00
#
# WebRequest
#
class WebRequest:
def __init__(self, headers, body):
self.headers = headers
self.body = body
#
# WebClient
#
class WebClient:
def __init__(self, sock, handler):
2021-07-18 20:55:41 +00:00
self.socket = sock
2020-01-05 23:42:08 +00:00
self.handler = handler
self.readBuff = bytes()
self.writeBuff = bytes()
2021-07-18 20:55:41 +00:00
def advance(self):
if not self.socket:
2020-01-05 23:42:08 +00:00
return False
2021-07-18 20:55:41 +00:00
rlist, wlist = select.select([self.socket], [self.socket], [], 0)[:2]
self.socket.settimeout(5.0)
2020-01-05 23:42:08 +00:00
if rlist:
while True:
try:
2021-07-18 20:55:41 +00:00
data = self.socket.recv(1024)
if not data:
raise Exception('failed to get data from socket')
except:
self.close()
return False
2021-07-18 20:55:41 +00:00
self.readBuff += data
request, length = self.parseRequest(self.readBuff.decode('utf-8'))
if request:
self.readBuff = self.readBuff[length:]
2021-07-18 20:55:41 +00:00
self.writeBuff += self.handler(request).encode('utf-8')
break
2020-01-05 23:42:08 +00:00
if wlist and self.writeBuff:
try:
2021-07-18 20:55:41 +00:00
length = self.socket.send(self.writeBuff)
self.writeBuff = self.writeBuff[length:]
if not self.writeBuff:
self.close()
return False
except:
2020-01-05 23:42:08 +00:00
self.close()
return False
2021-07-18 20:55:41 +00:00
2020-01-05 23:42:08 +00:00
return True
def close(self):
2021-07-18 20:55:41 +00:00
if self.socket:
self.socket.close()
self.socket = None
2020-01-05 23:42:08 +00:00
self.readBuff = bytes()
self.writeBuff = bytes()
def parseRequest(self, data):
2021-07-18 20:55:41 +00:00
parts = data.split('\r\n\r\n', 1)
2020-01-05 23:42:08 +00:00
if len(parts) == 1:
return None, 0
headers = {}
2021-07-18 20:55:41 +00:00
for line in parts[0].split('\r\n'):
pair = line.split(': ', 2)
if len(pair) == 2:
headers[pair[0].lower()] = pair[1]
else:
headers[pair[0]] = None
2020-01-05 23:42:08 +00:00
headerLength = len(parts[0]) + 4
2021-07-18 20:55:41 +00:00
bodyLength = int(headers.get('content-length', '0'))
2020-01-05 23:42:08 +00:00
totalLength = headerLength + bodyLength
if totalLength > len(data):
return None, 0
2021-07-18 20:55:41 +00:00
body = data[headerLength:totalLength]
2020-01-05 23:42:08 +00:00
return WebRequest(headers, body), totalLength
#
# WebServer
#
class WebServer:
2021-07-19 03:19:37 +00:00
def __init__(self, handler):
2020-01-05 23:42:08 +00:00
self.handler = handler
self.clients = []
2021-07-18 20:55:41 +00:00
self.socket = None
2020-01-05 23:42:08 +00:00
def advance(self):
2021-07-18 20:55:41 +00:00
if self.socket:
2020-01-05 23:42:08 +00:00
self.acceptClients()
self.advanceClients()
def acceptClients(self):
2021-07-18 20:55:41 +00:00
rlist = select.select([self.socket], [], [], 0)[0]
2020-01-05 23:42:08 +00:00
if not rlist:
return
2021-07-18 20:55:41 +00:00
socket = self.socket.accept()[0]
if socket:
socket.setblocking(False)
self.clients.append(WebClient(socket, self.handlerWrapper))
2020-01-05 23:42:08 +00:00
def advanceClients(self):
self.clients = list(filter(lambda c: c.advance(), self.clients))
2021-07-18 20:55:41 +00:00
def bindAndListen(self, address, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setblocking(False)
self.socket.bind((address, port))
self.socket.listen(5)
2020-01-05 23:42:08 +00:00
2021-07-18 20:55:41 +00:00
def handlerWrapper(self, request):
try:
2021-07-18 23:54:45 +00:00
if request.body:
2021-07-19 03:19:37 +00:00
body = json.dumps(self.handler(json.loads(request.body)))
else:
2021-07-18 20:55:41 +00:00
body = 'AnkiConnect'
except Exception as e:
body = str(e)
headers = [
['HTTP/1.1 200 OK', None],
['Content-Type', 'text/json'],
2021-07-19 03:19:37 +00:00
# ['Access-Control-Allow-Origin', origin],
# ['Access-Control-Allow-Headers', '*'],
2021-07-18 20:55:41 +00:00
['Content-Length', len(body.encode('utf-8'))]
]
2021-07-18 23:54:45 +00:00
header = ''
2020-01-05 23:42:08 +00:00
for key, value in headers:
2021-07-18 20:55:41 +00:00
header += f'{key}: {value}\r\n'
2020-01-05 23:42:08 +00:00
2021-07-18 20:55:41 +00:00
return header + '\r\n' + body
2020-01-05 23:42:08 +00:00
def close(self):
2021-07-18 20:55:41 +00:00
if self.socket:
self.socket.close()
self.socket = None
2020-01-05 23:42:08 +00:00
for client in self.clients:
client.close()
self.clients = []