Repository navigation
Expand file tree
/
Copy pathSimpleHttpServer.py
More file actions
420 lines (369 loc) · 17.2 KB
/
Copy pathSimpleHttpServer.py
File metadata and controls
420 lines (369 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import http.server
import configparser
import threading
from threading import Event
import logging
import os
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import sys
import secrets
import sqlite3
from http.cookies import CookieError, SimpleCookie
from datetime import datetime
from urllib.parse import parse_qs, urlsplit
import version
import json
from Cinema import Cinema
from ThreadingHTTPServerWithArgs import ThreadingHTTPServerWithArgs
default_values = {
"httpPort": 8000,
"httpAddress": "0.0.0.0",
"corsAllowedOrigins": "http://localhost:5173"
}
current_dir = os.path.dirname(sys.executable)
web_folder = current_dir + '\\public'
log_folder = current_dir + '\\log'
serviceLogger = logging.getLogger(__name__)
serviceLogger.setLevel(logging.DEBUG)
class SessionStore:
def __init__(self):
self._connection = sqlite3.connect(":memory:", check_same_thread=False)
self._lock = threading.Lock()
with self._lock:
self._connection.execute(
"CREATE TABLE sessions (session_id TEXT PRIMARY KEY, cinema_id TEXT NOT NULL)"
)
def is_valid(self, cinema, session_id):
with self._lock:
result = self._connection.execute(
"SELECT 1 FROM sessions WHERE cinema_id = ? AND session_id = ?",
(cinema, session_id)
).fetchone()
return result is not None
def create(self, cinema, session_id):
with self._lock:
self._connection.execute(
"INSERT INTO sessions (session_id, cinema_id) VALUES (?, ?)",
(session_id, cinema)
)
self._connection.commit()
def delete(self, cinema, session_id):
with self._lock:
self._connection.execute(
"DELETE FROM sessions WHERE cinema_id = ? AND session_id = ?",
(cinema, session_id)
)
self._connection.commit()
class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
# Override the __init__ method to specify the directory
def __init__(self, request, client_address, server, *args, **kwargs):
# Questi sono gli argomenti aggiuntivi che passerai al server, qui li estraiamo.
self.cinema_instances = kwargs.pop('cinema_instances', {})
self._pending_session_cookie = None
# Ora passiamo tutti gli argomenti al costruttore della classe base.
super().__init__(request, client_address, server, directory=web_folder, *args, **kwargs)
def log_message(self, format, *args):
serviceLogger.info(format % args)
def end_headers(self):
# Aggiunge l'header CORS a ogni risposta (GET, POST, ecc.)
origin = self.headers.get('Origin')
if origin in self.server.cors_allowed_origins:
self.send_header('Access-Control-Allow-Origin', origin)
self.send_header('Access-Control-Allow-Credentials', 'true')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, X-Requested-With')
self.send_header('Vary', 'Origin')
if self._pending_session_cookie is not None:
cookie_name, session_id = self._pending_session_cookie
if session_id is None:
self.send_header(
'Set-Cookie',
f'{cookie_name}=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax'
)
else:
self.send_header(
'Set-Cookie',
f'{cookie_name}={session_id}; Path=/; HttpOnly; SameSite=Lax'
)
self._pending_session_cookie = None
super().end_headers()
def do_OPTIONS(self):
# Risponde alla richiesta preflight del browser
self.send_response(200, "ok")
self.end_headers()
def checkAuth(self, cinema):
cookie_name = self._session_cookie_name(cinema)
session_id = self._get_session_id(cookie_name)
if session_id is not None and self.server.session_store.is_valid(cinema, session_id):
return True
auth_header = self.headers.get('Authorization')
if auth_header != self.cinema_instances[cinema]._password:
self.send_response(401) # 401 Unauthorized
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "error", "msg": "Accesso negato: password errata"}
self.wfile.write(json.dumps(response).encode('utf-8'))
return False
session_id = secrets.token_urlsafe(32)
self.server.session_store.create(cinema, session_id)
self._pending_session_cookie = (cookie_name, session_id)
return True
def _get_session_id(self, cookie_name):
cookies = SimpleCookie()
try:
cookies.load(self.headers.get('Cookie', ''))
except CookieError:
return None
session_cookie = cookies.get(cookie_name)
return session_cookie.value if session_cookie is not None else None
def _session_cookie_name(self, cinema):
return f'session_{cinema}'
def getCinema(self):
# Controlla se l'URL richiesto è quello della nostra pagina dinamica
cinema = None
service = None
try:
cinema, service = urlsplit(self.path).path[1:].split('/')[:2]
except Exception as e:
pass
return cinema, service
def do_POST(self):
cinema, service = self.getCinema()
if cinema not in self.cinema_instances:
self.send_error(404)
return
if service == 'logout':
cookie_name = self._session_cookie_name(cinema)
session_id = self._get_session_id(cookie_name)
if session_id is not None:
self.server.session_store.delete(cinema, session_id)
self._pending_session_cookie = (cookie_name, None)
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(b'{"status": "success"}')
return
if not self.checkAuth(cinema):
return
if cinema in self.cinema_instances:
if service == 'force_update':
self.cinema_instances[cinema].forceDownload()
self.send_response(201) # 201 Created è standard per le POST
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "success"}
self.wfile.write(json.dumps(response).encode('utf-8'))
elif service == 'set_param':
# 2. Leggi la lunghezza del corpo della richiesta
content_length = int(self.headers.get('Content-Length', 0))
# 3. Leggi i dati dal buffer
post_data = self.rfile.read(content_length)
try:
data = post_data.decode('utf-8')
# 4. Decodifica i byte in stringa e poi in JSON (dizionario Python)
self.cinema_instances[cinema].setParamsJson(data)
# 5. Risposta al client
self.send_response(201) # 201 Created è standard per le POST
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {"status": "success", "received": data}
self.wfile.write(json.dumps(response).encode('utf-8'))
except json.JSONDecodeError:
# Se il corpo non è un JSON valido
self.send_response(400) # Bad Request
self.end_headers()
self.wfile.write(b'{"status": "error", "message": "Invalid JSON"}')
else:
self.send_response(400) # Bad Request
self.end_headers()
self.wfile.write(b'{"status": "error", "message": "Invalid request"}')
def do_GET(self):
if self.path == '/version':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(f"SimpleHttpService: {version.SIMPLEHTTPSERVICE_VERSION}", "utf8"))
return
# Controlla se l'URL richiesto è quello della nostra pagina dinamica
cinema, service = self.getCinema()
query = parse_qs(urlsplit(self.path).query)
ignore_matinee_filter = query.get('ignoreMatineeFilter', ['false'])[0].lower() == 'true'
if cinema in self.cinema_instances:
# Prepara e invia la risposta per la pagina dinamica
if self.path.endswith('dashboard'):
self.send_response(302)
self.send_header('Location', self.path + '/')
self.end_headers()
elif service == "get_param":
if not self.checkAuth(cinema):
return
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(self.cinema_instances[cinema].getParamsJson(), "utf8"))
elif service == "dashboard":
# Path override
path = urlsplit(self.path).path
parts = [p for p in path.split('/') if p]
ends = '/' if path.endswith('/') else ''
self.path = '/' + '/'.join(parts[1:]) + ends
super().do_GET()
else:
self.send_response(200)
if service == "films_json":
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(self.cinema_instances[cinema].getFilmsJson(), "utf8"))
elif service == "db": # legacy... to mantain for backward compatibility
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(json.dumps(self.cinema_instances[cinema].getDbJson(ignore_matinee_filter)), "utf8"))
elif service == "db_config":
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(bytes(json.dumps(self.cinema_instances[cinema].getDbConfigJson(ignore_matinee_filter)), "utf8"))
else:
self.send_header('Content-type', 'text/xml')
self.end_headers()
if service == "films":
self.wfile.write(bytes(self.cinema_instances[cinema].getFilmsXml(), "utf8"))
elif service == "sale":
self.wfile.write(bytes(self.cinema_instances[cinema].getSaleXml(), "utf8"))
else:
super().do_GET()
else:
# Per tutte le altre richieste, usa il comportamento predefinito
super().do_GET()
class SimpleHttpService(win32serviceutil.ServiceFramework):
_svc_name_ = 'SimpleHttpService'
_svc_display_name_ = 'Simple Http Service'
_svc_description_ = 'It serves simple http requests'
def __init__(self, args):
self.log = serviceLogger
try:
args_list = list(args)
if not(len(args_list) > 1 and args_list[1].lower() == 'standalone'):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
self.cinema_data_managers = {}
self.stop_event = Event()
self.start_event = Event()
self.session_store = SessionStore()
except Exception as e:
self.log.error(f"Exception initializing SimpleHttpService: {e}")
def SvcStop(self):
self.log.info(f"Stopping service v{version.SIMPLEHTTPSERVICE_VERSION}")
self.stop_event.set()
try:
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.server.shutdown()
win32event.SetEvent(self.hWaitStop)
self.log.info(f'Server stopped')
except Exception as e:
self.log.exception(e)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
daily_log_handler_thread = threading.Thread(target=self.Schedule_daily_log_handler)
daily_log_handler_thread.start()
self.start_event.wait()
self.log.info(f"Starting service v{version.SIMPLEHTTPSERVICE_VERSION}")
self.Run()
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
def Create_daily_log_handler(self):
today = datetime.today()
if not os.path.exists(log_folder):
os.mkdir(log_folder)
log_filename = today.strftime(log_folder +'\\http_server_%Y-%m-%d.log')
fileHandler = logging.FileHandler(log_filename)
fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)-7.7s %(message)s'))
for handler in self.log.handlers[:]:
if isinstance(handler, logging.FileHandler):
self.log.removeHandler(handler)
handler.close()
self.log.addHandler(fileHandler)
def Schedule_daily_log_handler(self):
first_run=True
while not self.stop_event.is_set():
self.Create_daily_log_handler()
if first_run:
self.start_event.set()
first_run=False
current_time = datetime.now()
midnight = datetime(year=current_time.year, month=current_time.month, day=current_time.day, hour=23, minute=59, second=59)
time_to_midnight = midnight - current_time
seconds_until_midnight = time_to_midnight.total_seconds() + 2
self.stop_event.wait(timeout=seconds_until_midnight)
def Run(self):
config = configparser.ConfigParser(default_values)
config.read(current_dir + '\\config.ini')
httpPort = int(config['DEFAULT']['httpPort'])
httpAddress = config['DEFAULT']['httpAddress']
cors_allowed_origins = {
origin.strip()
for origin in config['DEFAULT']['corsAllowedOrigins'].split(',')
if origin.strip()
}
for section in config.sections():
if not section.startswith('DEFAULT'):
id = section
password = config[section]['password']
numero_sale = int(config[section]['numero_sale'])
films_url = config[section]['films_url']
sale_url = config[section]['sale_url']
name = config[section]['name']
films_interval = int(config[section]['films_interval'])
sale_interval = int(config[section]['sale_interval'])
self.cinema_data_managers[id] = Cinema(
id = id,
name = name,
films_url=films_url,
sale_url=sale_url,
logger=serviceLogger,
films_interval=films_interval,
sale_interval=sale_interval,
db_file=current_dir + '\\cinema_' + id,
password=password,
numero_sale=numero_sale)
# Avvia i thread di polling
for cinema_managed in self.cinema_data_managers.values():
cinema_managed.start()
while not self.stop_event.is_set():
try:
self.server = ThreadingHTTPServerWithArgs(
(httpAddress, httpPort),
MyRequestHandler,
self.cinema_data_managers,
self.session_store,
cors_allowed_origins
)
self.log.info(f'Serving on interface {httpAddress} port {httpPort} from "{web_folder}"')
self.server.serve_forever()
except Exception as e:
self.log.info(f"Error starting HTTP server: {e}")
self.stop_event.set()
for cinema_managed in self.cinema_data_managers.values():
cinema_managed.stop()
if __name__ == '__main__':
if len(sys.argv) == 1:
try:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(SimpleHttpService)
servicemanager.StartServiceCtrlDispatcher()
except Exception:
win32serviceutil.HandleCommandLine(SimpleHttpService)
elif sys.argv[1].lower() == 'standalone':
# Run the service in standalone mode
web_folder = 'public'
current_dir = '.'
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)-7.7s %(message)s')
serviceLogger.info("Running in standalone mode")
service = SimpleHttpService(sys.argv)
service.Run()
else:
win32serviceutil.HandleCommandLine(SimpleHttpService)