(
zelf: PyRef<Self>,
args: WrapSocketArgs,
vm: &VirtualMachine,
)
| 1830 | |
| 1831 | #[pymethod] |
| 1832 | fn _wrap_socket( |
| 1833 | zelf: PyRef<Self>, |
| 1834 | args: WrapSocketArgs, |
| 1835 | vm: &VirtualMachine, |
| 1836 | ) -> PyResult<PyRef<PySSLSocket>> { |
| 1837 | // Convert server_hostname to Option<String> |
| 1838 | // Handle both missing argument and None value |
| 1839 | let hostname = match args.server_hostname.into_option().flatten() { |
| 1840 | Some(hostname_str) => { |
| 1841 | let hostname = hostname_str.as_str(); |
| 1842 | |
| 1843 | // Validate hostname |
| 1844 | if hostname.is_empty() { |
| 1845 | return Err(vm.new_value_error("server_hostname cannot be an empty string")); |
| 1846 | } |
| 1847 | |
| 1848 | // Check if it starts with a dot |
| 1849 | if hostname.starts_with('.') { |
| 1850 | return Err(vm.new_value_error("server_hostname cannot start with a dot")); |
| 1851 | } |
| 1852 | |
| 1853 | // IP addresses are allowed |
| 1854 | // SNI will not be sent for IP addresses |
| 1855 | |
| 1856 | // Check for NULL bytes |
| 1857 | if hostname.contains('\0') { |
| 1858 | return Err(vm.new_type_error("embedded null character")); |
| 1859 | } |
| 1860 | |
| 1861 | Some(hostname.to_string()) |
| 1862 | } |
| 1863 | None => None, |
| 1864 | }; |
| 1865 | |
| 1866 | // Validate socket type and context protocol |
| 1867 | if args.server_side && zelf.protocol == PROTOCOL_TLS_CLIENT { |
| 1868 | return Err(vm |
| 1869 | .new_os_subtype_error( |
| 1870 | PySSLError::class(&vm.ctx).to_owned(), |
| 1871 | None, |
| 1872 | "Cannot create a server socket with a PROTOCOL_TLS_CLIENT context", |
| 1873 | ) |
| 1874 | .upcast()); |
| 1875 | } |
| 1876 | if !args.server_side && zelf.protocol == PROTOCOL_TLS_SERVER { |
| 1877 | return Err(vm |
| 1878 | .new_os_subtype_error( |
| 1879 | PySSLError::class(&vm.ctx).to_owned(), |
| 1880 | None, |
| 1881 | "Cannot create a client socket with a PROTOCOL_TLS_SERVER context", |
| 1882 | ) |
| 1883 | .upcast()); |
| 1884 | } |
| 1885 | |
| 1886 | // Create _SSLSocket instance |
| 1887 | let ssl_socket = PySSLSocket { |
| 1888 | sock: args.sock.clone(), |
| 1889 | context: PyRwLock::new(zelf), |
no test coverage detected