| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  | __filename__ = "daemon_get_css.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							| 
									
										
										
										
											2024-12-22 23:37:30 +00:00
										 |  |  | __version__ = "1.6.0" | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@libreserver.org" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2024-12-25 14:31:14 +00:00
										 |  |  | __module_group__ = "Daemon GET" | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | from httpcodes import http_304 | 
					
						
							|  |  |  | from httpcodes import http_404 | 
					
						
							|  |  |  | from httpcodes import write2 | 
					
						
							|  |  |  | from httpheaders import set_headers_etag | 
					
						
							|  |  |  | from httpheaders import set_headers | 
					
						
							| 
									
										
										
										
											2024-04-10 10:23:04 +00:00
										 |  |  | from utils import string_ends_with | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  | from utils import get_css | 
					
						
							|  |  |  | from fitnessFunctions import fitness_performance | 
					
						
							|  |  |  | from daemon_utils import etag_exists | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_style_sheet(self, base_dir: str, calling_domain: str, path: str, | 
					
						
							| 
									
										
										
										
											2024-04-08 14:08:19 +00:00
										 |  |  |                     getreq_start_time, debug: bool, | 
					
						
							|  |  |  |                     css_cache: {}, | 
					
						
							|  |  |  |                     fitness: {}) -> bool: | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |     """Returns the content of a css file
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     # get the last part of the path | 
					
						
							|  |  |  |     # eg. /my/path/file.css becomes file.css | 
					
						
							|  |  |  |     if '/' in path: | 
					
						
							|  |  |  |         path = path.split('/')[-1] | 
					
						
							|  |  |  |     path = base_dir + '/' + path | 
					
						
							|  |  |  |     css = None | 
					
						
							| 
									
										
										
										
											2024-04-08 14:08:19 +00:00
										 |  |  |     if css_cache.get(path): | 
					
						
							|  |  |  |         css = css_cache[path] | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |     elif os.path.isfile(path): | 
					
						
							|  |  |  |         tries = 0 | 
					
						
							|  |  |  |         while tries < 5: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2024-04-08 09:09:10 +00:00
										 |  |  |                 css = get_css(base_dir, path) | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |                 if css: | 
					
						
							| 
									
										
										
										
											2024-04-08 14:08:19 +00:00
										 |  |  |                     css_cache[path] = css | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |                     break | 
					
						
							|  |  |  |             except BaseException as ex: | 
					
						
							|  |  |  |                 print('EX: _get_style_sheet ' + path + ' ' + | 
					
						
							|  |  |  |                       str(tries) + ' ' + str(ex)) | 
					
						
							|  |  |  |                 time.sleep(1) | 
					
						
							|  |  |  |                 tries += 1 | 
					
						
							|  |  |  |     if css: | 
					
						
							|  |  |  |         msg = css.encode('utf-8') | 
					
						
							|  |  |  |         msglen = len(msg) | 
					
						
							|  |  |  |         set_headers(self, 'text/css', msglen, | 
					
						
							|  |  |  |                     None, calling_domain, False) | 
					
						
							|  |  |  |         write2(self, msg) | 
					
						
							|  |  |  |         fitness_performance(getreq_start_time, | 
					
						
							| 
									
										
										
										
											2024-04-08 14:08:19 +00:00
										 |  |  |                             fitness, | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |                             '_GET', '_get_style_sheet', | 
					
						
							| 
									
										
										
										
											2024-04-08 14:08:19 +00:00
										 |  |  |                             debug) | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |         return True | 
					
						
							|  |  |  |     http_404(self, 92) | 
					
						
							|  |  |  |     return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_fonts(self, calling_domain: str, path: str, | 
					
						
							|  |  |  |               base_dir: str, debug: bool, | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |               getreq_start_time, fitness: {}, | 
					
						
							|  |  |  |               fonts_cache: {}, | 
					
						
							|  |  |  |               domain_full: str) -> None: | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |     """Returns a font
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     font_str = path.split('/fonts/')[1] | 
					
						
							| 
									
										
										
										
											2024-04-10 10:23:04 +00:00
										 |  |  |     possible_extensions = ('.otf', '.ttf', '.woff', '.woff2') | 
					
						
							|  |  |  |     if string_ends_with(font_str, possible_extensions): | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |         if font_str.endswith('.otf'): | 
					
						
							|  |  |  |             font_type = 'font/otf' | 
					
						
							|  |  |  |         elif font_str.endswith('.ttf'): | 
					
						
							|  |  |  |             font_type = 'font/ttf' | 
					
						
							|  |  |  |         elif font_str.endswith('.woff'): | 
					
						
							|  |  |  |             font_type = 'font/woff' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             font_type = 'font/woff2' | 
					
						
							|  |  |  |         font_filename = \ | 
					
						
							|  |  |  |             base_dir + '/fonts/' + font_str | 
					
						
							|  |  |  |         if etag_exists(self, font_filename): | 
					
						
							|  |  |  |             # The file has not changed | 
					
						
							|  |  |  |             http_304(self) | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |         if fonts_cache.get(font_str): | 
					
						
							|  |  |  |             font_binary = fonts_cache[font_str] | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |             set_headers_etag(self, font_filename, | 
					
						
							|  |  |  |                              font_type, | 
					
						
							|  |  |  |                              font_binary, None, | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |                              domain_full, False, None) | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |             write2(self, font_binary) | 
					
						
							|  |  |  |             if debug: | 
					
						
							|  |  |  |                 print('font sent from cache: ' + | 
					
						
							|  |  |  |                       path + ' ' + calling_domain) | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |             fitness_performance(getreq_start_time, fitness, | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |                                 '_GET', '_get_fonts cache', | 
					
						
							|  |  |  |                                 debug) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         if os.path.isfile(font_filename): | 
					
						
							|  |  |  |             font_binary = None | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2024-07-16 11:21:28 +00:00
										 |  |  |                 with open(font_filename, 'rb') as fp_font: | 
					
						
							|  |  |  |                     font_binary = fp_font.read() | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |             except OSError: | 
					
						
							|  |  |  |                 print('EX: unable to load font ' + font_filename) | 
					
						
							|  |  |  |             if font_binary: | 
					
						
							|  |  |  |                 set_headers_etag(self, font_filename, | 
					
						
							|  |  |  |                                  font_type, | 
					
						
							|  |  |  |                                  font_binary, None, | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |                                  domain_full, | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |                                  False, None) | 
					
						
							|  |  |  |                 write2(self, font_binary) | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |                 fonts_cache[font_str] = font_binary | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |             if debug: | 
					
						
							|  |  |  |                 print('font sent from file: ' + | 
					
						
							|  |  |  |                       path + ' ' + calling_domain) | 
					
						
							| 
									
										
										
										
											2024-04-08 14:12:35 +00:00
										 |  |  |             fitness_performance(getreq_start_time, fitness, | 
					
						
							| 
									
										
										
										
											2024-03-02 14:54:38 +00:00
										 |  |  |                                 '_GET', '_get_fonts', debug) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |     if debug: | 
					
						
							|  |  |  |         print('font not found: ' + path + ' ' + calling_domain) | 
					
						
							|  |  |  |     http_404(self, 21) |