Repository navigation
15 windows msvc portability - #19
Merged
Merged
Conversation
Adds include/win32_compat.h — a lean shim covering only what php.h
does not already provide on Windows:
- O_CLOEXEC, O_NOFOLLOW → 0 (Linux/BSD extensions absent from ucrt)
- open() → php_win32_ioutil_open() (ucrt has no bare open)
- close/read/unlink → _close/_read/_unlink (MSVC deprecated POSIX names)
Everything else (ssize_t, S_IS* macros, MAXPATHLEN, lstat, realpath,
fstat, O_RDONLY/CREAT/TRUNC etc.) is already provided by php.h via
config.w32.h, zend_virtual_cwd.h, and win32/ioutil.h.
Platform-specific changes:
- fs_util.c, send_file.c: guard <unistd.h> with #ifndef PHP_WIN32
- http2/http2_session.h: guard <sys/types.h> with #ifndef PHP_WIN32
- http3/http3_listener.h: add win32_compat.h for ssize_t in prototype
- http_static_safety.c: guard <unistd.h>/<sys/param.h>; add O_BINARY
for open(); handle '\' path separator in verify_path_owner_chain
and http_static_resolved_under_root; skip st_uid and TOCTOU dev/ino
checks on Windows (always 0 in MSVC stat)
- http_static.c: use plain close(fd) via win32_compat.h macro
config.w32 synchronized with config.m4: adds ~20 missing source files
(http_mime, http_date, http_etag, http_conditional, http_range, fs_util,
http_rfc5987, http_precompressed, http_param_parse, send_file,
http_send_file*, body_pool, http1_sendfile, http3_stream_pool,
static handler sources, http2_static_response) and the include/static
path.
Closes #15
tests/unit/static/test_win32_compat.c covers:
- O_CLOEXEC and O_NOFOLLOW are defined (and 0 on Windows)
- http_static_resolved_under_root accepts '\' separator on Windows
- http_static_try_open_candidate opens/reads/closes a real temp file
via the open/read/close compat macros
- http_static_symlink_policy_admits: FOLLOW always admits on Windows
because S_ISLNK is always 0; REJECT also admits regular files
Tests compile and run on Linux/macOS (POSIX paths) and Windows (MSVC).
Windows-only assertions are guarded with #ifdef PHP_WIN32.
Fixes all portability issues blocking compilation of php_true_async_server.dll under MSVC (Visual Studio 2022) without using the PHP ext/ directory tree. Key changes: - CMakeLists.txt: auto-detect PHP Debug_TS/Release_TS lib, add missing sources (http_connection_tls.c, http_compression_negotiate.c), exclude win32/ dir from include path (it shadows <time.h> with PHP's win32/time.h), suppress expected warnings for llhttp generated code (/wd4244 /wd4152 per source file), add /wd4100 /wd4127 /wd4456 for PHP extension API patterns - body_pool.c: __thread → ZEND_TLS (which already includes static; drop the redundant keyword to avoid C2159 "more than one storage class") - http_date.c: replace strptime/timegm (POSIX-only) with sscanf-based parser; add #define http_date_timegm _mkgmtime for MSVC - http_response.c: guard <strings.h> (PHP's zend_config.w32.h already defines strncasecmp via #define) - http_precompressed.c: remove <sys/param.h> (MAXPATHLEN from php.h), use zend_stat_t + VCWD_STAT instead of struct stat + stat() - send_file.c, http_static_cache.[ch], http_static.c: struct stat → zend_stat_t throughout; use php_win32_ioutil_fstat on Windows for fd-based stat - http_static_path.c: provide minimal fnmatch() shim for Windows (FNM_PATHNAME semantics with case-insensitive matching) - http_server_class.c: fix C4244 (unsigned:1 bitfield → bool with != 0), C4267 (strlen → int cast), C4244 (zend_long → int with explicit cast) - uploaded_file.c: remove conflicting #define mode_t int — PHP's zend_virtual_cwd.h already typedefs mode_t as unsigned short; add (mode_t) cast for octal literal - http_compression_negotiate.c: guard http_compression_lookup calls with #ifdef HAVE_HTTP_COMPRESSION so the file compiles as a standalone unit
Closed
6 tasks
Contributor
CoverageTotal lines: 77.73% → 77.65% (-0.08 pp)
❌ Regression in touched files (> 1.0 pp drop)
Add |
There was a problem hiding this comment.
Pull request overview
This PR focuses on improving Windows/MSVC portability across the extension and its unit-test build, primarily by introducing a Win32 compatibility shim, switching stat usage to Zend’s portable types, and adding Windows-focused regression tests.
Changes:
- Added
win32_compat.hand integrated it into public headers to normalize Windows/POSIX filesystem/IO APIs (e.g.,open/close/read/unlink,O_CLOEXEC,O_NOFOLLOW,ssize_t). - Updated static-handler and sendfile-related code to use
zend_stat_t/VCWD_STAT/php_sys_fstatand to handle Windows path separators and CRT differences. - Updated CMake/config.w32 build plumbing for Windows and added a dedicated Windows/MSVC portability unit test.
Reviewed changes
Copilot reviewed 30 out of 30 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/static/test_win32_compat.c | New unit test covering Win32 compat macros and static safety path/IO behavior. |
| tests/unit/CMakeLists.txt | Adds Win32Compat test target and Windows PATH handling; guards StaticDecoders on source existence. |
| tests/common/php_sapi_test.c | Adjusts weak linker stubs for MSVC compatibility (currently disables stubs under MSVC). |
| tests/CMakeLists.txt | Raises required CMake version; adds Windows PHP/cmocka discovery logic and Windows compile defs. |
| src/uploaded_file.c | Removes Windows mode_t redefinition and adjusts mkdir_recursive call type. |
| src/static/http_static.c | Avoids POSIX-only headers on Windows; uses zend_stat_t and VCWD_STAT. |
| src/static/http_static_safety.c | Windows path-separator handling, zend_stat_t migration, O_BINARY, Windows-specific TOCTOU handling. |
| src/static/http_static_path.c | Adds Windows fallback fnmatch implementation (POSIX fnmatch.h not available). |
| src/static/http_static_cache.c | Migrates cached stat type to zend_stat_t; fixes zend_hash_init size type. |
| src/send_file.c | Windows-friendly stat/unlink handling (zend_stat_t, _unlink, Windows fstat helper). |
| src/http3/http3_listener.h | Includes Win32 compat header to ensure ssize_t availability on Windows. |
| src/http2/http2_static_response.c | Avoids <unistd.h> on Windows. |
| src/http_server_class.c | Fixes MSVC type/boolean strictness (send length cast, bool/long conversions). |
| src/http_response.c | Avoids <strings.h> on Windows; relies on PHP-provided strncasecmp. |
| src/http_precompressed.c | Switches to Zend stat/VCWD APIs; adds MAXPATHLEN/S_ISREG fallbacks for portability. |
| src/http_etag.c | Uses zend_stat_t in ETag formatting helpers. |
| src/http_date.c | Replaces strptime/timegm parsing with sscanf + _mkgmtime for MSVC compatibility. |
| src/fs_util.c | Uses _read on Windows (avoids POSIX <unistd.h>). |
| src/core/http_connection.c | Moves extern declaration to file scope; normalizes keep-alive booleans. |
| src/core/body_pool.c | Replaces __thread with Zend TLS macro for MSVC support. |
| src/compression/http_compression_negotiate.c | Guards codec selection behind HAVE_HTTP_COMPRESSION. |
| include/win32_compat.h | New Win32/MSVC portability shim header. |
| include/static/http_static_safety.h | Updates http_static_try_open_candidate signature to zend_stat_t. |
| include/static/http_static_cache.h | Migrates public cache view and insert API to zend_stat_t. |
| include/php_http_server.h | Pulls in win32 compatibility shims centrally. |
| include/http2/http2_session.h | Uses win32 compat header for ssize_t when building on Windows. |
| include/http_etag.h | Updates public ETag API to accept zend_stat_t. |
| include/fs_util.h | Includes win32 compat header for Windows type/function shims. |
| config.w32 | Expands Windows build source list (static handler, sendfile, HTTP utils, etc.). |
| CMakeLists.txt | Raises required CMake version; adds Windows PHP lib detection and modernizes target setup. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
25
to
+49
| @@ -57,6 +46,7 @@ __attribute__((weak)) bool http_server_should_drain_now(http_server_object *serv | |||
| (void)server; (void)conn; (void)now_ns; | |||
| return false; | |||
| } | |||
| #endif /* !_MSC_VER */ | |||
Comment on lines
+146
to
160
| /* IMF-fixdate: "Sun, 06 Nov 1994 08:49:37 GMT" — comma at position 3 */ | ||
| if (src_len >= 26 && buf[3] == ',') { | ||
| memset(&tm, 0, sizeof(tm)); | ||
| memset(mon, 0, sizeof(mon)); | ||
| if (sscanf(buf + 5, "%d %3s %d %d:%d:%d", | ||
| &mday, mon, &year, &hour, &min, &sec) == 6) { | ||
| int m = http_date_month_from_abbr(mon); | ||
| if (m >= 0) { | ||
| tm.tm_mday = mday; tm.tm_mon = m; tm.tm_year = year - 1900; | ||
| tm.tm_hour = hour; tm.tm_min = min; tm.tm_sec = sec; | ||
| if (tm_fields_in_range(&tm)) return http_date_timegm(&tm); | ||
| } | ||
| } | ||
| return (time_t)-1; | ||
| } |
Comment on lines
+30
to
+31
| set(PHP_SRC_DIR "E:/php/php-src" CACHE PATH "PHP source root directory") | ||
|
|
Comment on lines
+7
to
8
| set(PHP_INCLUDE_DIR "E:/php/php-src" CACHE PATH "PHP source directory") | ||
|
|
Comment on lines
+138
to
+140
| /* Append a forward-slash subpath — resolved_under_root must accept it. */ | ||
| char path[MAXPATHLEN]; | ||
| snprintf(path, sizeof(path), "%s\\existing_sub", cwd); |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.