Skip to content

15 windows msvc portability - #19

Merged
EdmondDantes merged 3 commits into
mainfrom
15-windows-msvc-portability
May 12, 2026
Merged

EdmondDantes merged 3 commits into
mainfrom
15-windows-msvc-portability

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

No description provided.

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
@EdmondDantes EdmondDantes linked an issue May 12, 2026 that may be closed by this pull request
6 tasks
@EdmondDantes
EdmondDantes requested a review from Copilot May 12, 2026 09:47
@EdmondDantes EdmondDantes self-assigned this May 12, 2026
@EdmondDantes EdmondDantes added the bug Something isn't working label May 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 77.73% → 77.65% (-0.08 pp)

File Baseline Current Δ Touched
src/compression/http_compression_negotiate.c 94.38% 94.38% +0.00 pp ●
src/core/body_pool.c 94.74% 94.74% +0.00 pp ●
src/core/http_connection.c 76.65% 76.62% -0.03 pp ●
src/fs_util.c 63.16% 63.16% +0.00 pp ●
src/http2/http2_static_response.c 82.16% 82.16% +0.00 pp ●
src/http3/http3_callbacks.c 79.21% 78.61% -0.59 pp
src/http_date.c 87.88% 76.34% -11.53 pp ●
src/http_etag.c 89.58% 89.58% +0.00 pp ●
src/http_precompressed.c 96.77% 96.77% +0.00 pp ●
src/http_response.c 84.30% 84.30% +0.00 pp ●
src/http_server_class.c 75.81% 75.81% +0.00 pp ●
src/send_file.c 84.19% 84.24% +0.05 pp ●
src/static/http_static.c 61.86% 61.86% +0.00 pp ●
src/static/http_static_cache.c 71.43% 71.43% +0.00 pp ●
src/static/http_static_path.c 94.62% 94.62% +0.00 pp ●
src/static/http_static_safety.c 77.03% 77.03% +0.00 pp ●
src/uploaded_file.c 75.00% 75.00% +0.00 pp ●

❌ Regression in touched files (> 1.0 pp drop)

  • src/http_date.c dropped -11.53 pp

Add [coverage-drop-ok] to a commit message in this PR to override.

@EdmondDantes
EdmondDantes merged commit c932b50 into main May 12, 2026
8 checks passed
@EdmondDantes
EdmondDantes deleted the 15-windows-msvc-portability branch May 12, 2026 09:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.h and 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_fstat and 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 thread src/http_date.c
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 thread tests/CMakeLists.txt
Comment on lines +30 to +31
set(PHP_SRC_DIR "E:/php/php-src" CACHE PATH "PHP source root directory")

Comment thread CMakeLists.txt
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows MSVC portability

2 participants