Skip to content

Conversation

adamziel
Copy link
Collaborator

Adjusts the CORS Proxy to relay chunked responses using chunks.

Implementation

Some APIs use fixed content-length and respond with a predictable response body. Other APIs respond with chunks.

Before this PR, whenever the CORS proxy received a chunked response, it would only relay the Transfer-Encoding: Chunked header without actually transmitting the response body as chunks.

Chunks are an essential HTTP 1.1 feature that cannot be disabled, We also can't just filter out that header in the proxy response as the client expects either a Content-Length or body chunks, and we may not know the Content-length upfront.

With this PR, the CORS proxy outputs its response using the chunked encoding whenever the remote endpoint streams its response using the chunked encoding.

Testing

Adjusts the CORS Proxy to relay chunked responses using chunks. ## Implementation Some APIs use fixed content-length and respond with a predictable response body. Other APIs respond with chunks. Before this PR, whenever the CORS proxy received a chunked response, it would only relay the `Transfer-Encoding: Chunked` header without actually transmitting the response body as chunks. Chunks are an essential HTTP 1.1 feature that cannot be disabled, We also can't just filter out that header in the proxy response as the client expects either a Content-Length or body chunks, and we may not know the Content-length upfront. With this PR, the CORS proxy outputs its response using the chunked encoding whenever the remote endpoint streams its response using the chunked encoding. ## Testing * Run `nx start playground-php-cors-proxy` * Fixed-length response: Go to http://127.0.0.1:5263/cors-proxy.php?https://raw.githubusercontent.com/WordPress/wordpress-playground/5e5ba3e0f5b984ceadd5cbe6e661828c14621d25/README.md * Confirm it loaded the file * Chunked encoding: Go to http://127.0.0.1:5263/cors-proxy.php?https://api.wordpress.org/plugins/update-check/1.1/ * Confirm the response says the word `error` * Error handling: Go to http://127.0.0.1:5263/cors-proxy.php?https://nosuchsite.playground.wordpress.net * Confirm the response says Bad Request Hostname could not be resolved
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Accept, Authorization, Content-Type, git-protocol');
header('Access-Control-Allow-Headers: Accept, Authorization, Content-Type, git-protocol, wp_blog, wp_install');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Casual addition – these two headers are used by WordPress core when requesting api.w.org

@adamziel adamziel merged commit 85c5d30 into trunk Dec 13, 2024
10 checks passed
@adamziel adamziel deleted the proxy-support-chunked-encoding branch December 13, 2024 08:44
edisplay pushed a commit to edisplay/wordpress-playground that referenced this pull request Dec 17, 2024
…he latest release (WordPress#2056) Fixes a regression created in WordPress#2077. The minified nightly build accidentally became a minified build of the latest WordPress version. In the multitude of resolveWordPressRelease() calls used in building the minified WordPress releases, the last call in the chain started transforming the nightly URL into a 6.7.1 release URL. This PR removes that one final call, and also simplifies the Playground CLI build pipeline. ## Testing instructions Apply this PR, switch WP version to nightly, confirm in wp-=admin it's indeed the nightly release
adamziel added a commit that referenced this pull request Jan 2, 2025
Adds support for `transfer-encoding: chunked` on Apache, Nginx, and other web servers. #2077 added support for `transfer-encoding: chunked` in a way that works on a local PHP dev server. The proxy manually chunks the output bytes and outputs each chunk's header, separator, body, and trailer. However, it doesn't work on playground.wordpress.net because the web server there handles the chunked encoding on its own. The bytes echoed by cors-proxy.php are treated as body bytes, which messes up the response body. This PR restricts the manual chunking to a local CLI dev server. ## Testing instructions Run cors-proxy.php in Apache or so and confirm that requesting resources served with chunked encoding works with this patch but not without it. One such URL is https://adamadam.blog/feed/.
adamziel added a commit that referenced this pull request Jan 9, 2025
#2114) Adds support for `transfer-encoding: chunked` on Apache, Nginx, and other web servers. #2077 added support for `transfer-encoding: chunked` in a way that works on a local PHP dev server. The proxy manually chunks the output bytes and outputs each chunk's header, separator, body, and trailer. However, it doesn't work on playground.wordpress.net because the web server there handles the chunked encoding on its own. The bytes echoed by cors-proxy.php are treated as body bytes, which messes up the response body. This PR restricts the manual chunking to a local CLI dev server. ## Testing instructions Run cors-proxy.php in Apache or so and confirm that requesting resources served with chunked encoding works with this patch but not without it. One such URL is https://adamadam.blog/feed/.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant