|
1 |
| -# Python WebUI v2.5.5 |
| 1 | +# Python WebUI v2.5.6 |
2 | 2 | #
|
3 | 3 | # http://webui.me
|
4 | 4 | # https://github.com/webui-dev/python-webui
|
|
20 | 20 | from . import webui_bindings as _raw
|
21 | 21 |
|
22 | 22 |
|
23 |
| -# C function type for the file handler window |
24 |
| -filehandler_window_callback = CFUNCTYPE(c_void_p, c_size_t, c_char_p, POINTER(c_int)) |
25 |
| - |
26 |
| - |
27 | 23 | # == Enums ====================================================================
|
28 | 24 |
|
29 | 25 |
|
@@ -635,8 +631,8 @@ def __init__(self, window_id: Optional[int] = None):
|
635 | 631 | self._cb_func_list: dict = {}
|
636 | 632 |
|
637 | 633 | # gets used for both filehandler and filehandler_window, should wipe out the other just how it does in C
|
638 |
| - self._file_handler_cb: Any = None |
639 |
| - self._buffers: list = [] |
| 634 | + self._file_handler_cb = None |
| 635 | + self._buffers = [] |
640 | 636 |
|
641 | 637 | # -- dispatcher for function bindings -----------
|
642 | 638 | def _make_dispatcher(self):
|
@@ -895,58 +891,64 @@ def set_root_folder(self, path: str) -> bool:
|
895 | 891 | """
|
896 | 892 | return bool(_raw.webui_set_root_folder(c_size_t(self._window), path.encode("utf-8")))
|
897 | 893 |
|
898 |
| - # -- set_file_handler --------------------------- # TODO: still errors on call to c bind |
899 |
| - # def set_file_handler(self, handler: Callable[[str], Optional[str]]) -> None: |
900 |
| - # """Set a custom file handler for serving files. |
901 |
| - # |
902 |
| - # This function registers a custom file handler that processes file requests |
903 |
| - # and serves HTTP responses. The handler must return a full HTTP response |
904 |
| - # (headers and body) as a UTF-8 encoded string. Setting a new handler overrides |
905 |
| - # any previously registered file handler. |
906 |
| - # |
907 |
| - # Args: |
908 |
| - # handler (Callable[[str], str]): A function that takes a filename as input |
909 |
| - # and returns a complete HTTP response as a string. |
910 |
| - # |
911 |
| - # Returns: |
912 |
| - # None |
913 |
| - # |
914 |
| - # Example: |
915 |
| - # def my_handler(filename: str) -> str: |
916 |
| - # response_body = "Hello, World!" |
917 |
| - # response_headers = ( |
918 |
| - # "HTTP/1.1 200 OK\r\n" |
919 |
| - # "Content-Type: text/plain\r\n" |
920 |
| - # f"Content-Length: {len(response_body)}\r\n" |
921 |
| - # "\r\n" |
922 |
| - # ) |
923 |
| - # return response_headers + response_body |
924 |
| - # |
925 |
| - # my_window.set_file_handler(my_handler) |
926 |
| - # """ |
927 |
| - # def _internal_file_handler(filename_ptr: c_char_p, length_ptr: POINTER(c_int)) -> c_void_p: |
928 |
| - # """ |
929 |
| - # Internal C callback that matches the signature required by webui_set_file_handler_window. |
930 |
| - # """ |
931 |
| - # # Decode the incoming filename from C |
932 |
| - # filename = filename_ptr.decode('utf-8') if filename_ptr else "" |
933 |
| - # |
934 |
| - # # Call the Python-level handler to get the HTTP response |
935 |
| - # response_bytes = handler(filename).encode("utf-8") |
936 |
| - # |
937 |
| - # # Create a ctypes buffer from the Python bytes; this buffer must remain alive |
938 |
| - # # at least until WebUI is done with it. |
939 |
| - # buf = create_string_buffer(response_bytes) |
940 |
| - # |
941 |
| - # # Set the length (the int* that C expects) |
942 |
| - # length_ptr[0] = len(response_bytes) |
943 |
| - # |
944 |
| - # # Return a pointer (void*) to the buffer |
945 |
| - # return cast(buf, c_void_p) |
946 |
| - # |
947 |
| - # # Keep a reference so it doesn't get garbage collected |
948 |
| - # self._file_handler_cb = filehandler_window_callback(_internal_file_handler) |
949 |
| - # _raw.webui_set_file_handler_window(c_size_t(self._window), _raw.FILE_HANDLER_CB(self._file_handler_cb)) |
| 894 | + # -- set_file_handler --------------------------- |
| 895 | + def set_file_handler(self, handler: Callable[[str], Optional[str]]) -> None: |
| 896 | + """Set a custom file handler for serving files. |
| 897 | + |
| 898 | + This function registers a custom file handler that processes file requests |
| 899 | + and serves HTTP responses. The handler must return a full HTTP response |
| 900 | + (headers and body) as a UTF-8 encoded string. Setting a new handler overrides |
| 901 | + any previously registered file handler. |
| 902 | + |
| 903 | + Args: |
| 904 | + handler (Callable[[str], str]): A function that takes a filename as input |
| 905 | + and returns a complete HTTP response as a string. |
| 906 | + |
| 907 | + Returns: |
| 908 | + None |
| 909 | + |
| 910 | + Example: |
| 911 | + def my_handler(filename: str) -> Optional[str]: |
| 912 | + response_body = "Hello, World!" |
| 913 | + response_headers = ( |
| 914 | + "HTTP/1.1 200 OK\r\n" |
| 915 | + "Content-Type: text/plain\r\n" |
| 916 | + f"Content-Length: {len(response_body)}\r\n" |
| 917 | + "\r\n" |
| 918 | + ) |
| 919 | + return response_headers + response_body |
| 920 | + |
| 921 | + my_window.set_file_handler(my_handler) |
| 922 | + """ |
| 923 | + # _raw bindings moved here due to CFUNCTYPE factory conflicts |
| 924 | + callback_handler_type = CFUNCTYPE(c_void_p, c_char_p, POINTER(c_int)) |
| 925 | + _raw.webui_set_file_handler.argtypes = [c_size_t, callback_handler_type] |
| 926 | + _raw.webui_set_file_handler.restype = None |
| 927 | + |
| 928 | + self._buffers.clear() |
| 929 | + |
| 930 | + def _c_handler(filename_ptr, length_ptr): |
| 931 | + path = filename_ptr.decode("utf-8") |
| 932 | + response_str = handler(path) |
| 933 | + |
| 934 | + # None, tells WebUI to look for files elsewhere |
| 935 | + if response_str is None: |
| 936 | + length_ptr[0] = 0 |
| 937 | + return 0 |
| 938 | + |
| 939 | + data = response_str.encode("latin-1") |
| 940 | + length_ptr[0] = len(data) |
| 941 | + |
| 942 | + # allocate and pin |
| 943 | + buf = create_string_buffer(data) |
| 944 | + self._buffers.append(buf) |
| 945 | + |
| 946 | + # return the raw address as an integer |
| 947 | + return addressof(buf) |
| 948 | + |
| 949 | + # Keep a reference so it doesn't get garbage collected |
| 950 | + self._file_handler_cb = callback_handler_type(_c_handler) |
| 951 | + _raw.webui_set_file_handler(self._window, self._file_handler_cb) |
950 | 952 |
|
951 | 953 |
|
952 | 954 | # -- set_file_handler_window -------------------- # TODO: still errors on call to c bind
|
|
0 commit comments