Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default url rewrites to /index.php #1072

Merged
merged 4 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions packages/php-wasm/node/src/test/php-request-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe.each(SupportedPHPVersions)(
*/
php.writeFile(
'/index.php',
`<?php
`<?php
echo json_encode($_POST);`
);
const response = await handler.request({
Expand All @@ -163,7 +163,7 @@ describe.each(SupportedPHPVersions)(
*/
php.writeFile(
'/index.php',
`<?php
`<?php
move_uploaded_file($_FILES["myFile"]["tmp_name"], '/tmp/moved.txt');
echo json_encode(file_exists('/tmp/moved.txt'));`
);
Expand All @@ -180,7 +180,7 @@ describe.each(SupportedPHPVersions)(
it('Should allow mixing data and files when `body` is a JavaScript object', async () => {
php.writeFile(
'/index.php',
`<?php
`<?php
move_uploaded_file($_FILES["myFile"]["tmp_name"], '/tmp/moved.txt');
echo json_encode(array_merge(
$_POST,
Expand All @@ -203,7 +203,7 @@ describe.each(SupportedPHPVersions)(
it('Should handle an empty file object and post data', async () => {
await php.writeFile(
'/index.php',
`<?php
`<?php
echo json_encode($_POST);`
);
const response = await handler.request({
Expand Down Expand Up @@ -235,6 +235,34 @@ describe.each(SupportedPHPVersions)(
expect((await response1).httpStatusCode).toEqual(200);
expect((await response2).httpStatusCode).toEqual(502);
});

it('should return 200 and pass query strings when a valid request is made to a PHP file', async () => {
php.writeFile('/test.php', `<?php echo $_GET['key'];`);
const response = await handler.request({
url: '/test.php?key=value',
});
expect(response.httpStatusCode).toEqual(200);
expect(response.text).toEqual('value');
});

it('should return 200 status and pass query strings when a valid request is made to a WordPress permalink', async () => {
php.writeFile('/index.php', `<?php echo $_GET['key'];`);
const response = await handler.request({
url: '/category/uncategorized/?key=value',
});
expect(response.httpStatusCode).toEqual(200);
expect(response.text).toEqual('value');
});

it('should return 200 and pass query strings when a valid request is made to a folder', async () => {
php.mkdirTree('/folder');
php.writeFile('/folder/index.php', `<?php echo $_GET['key'];`);
const response = await handler.request({
url: '/folder/?key=value',
});
expect(response.httpStatusCode).toEqual(200);
expect(response.text).toEqual('value');
});
}
);

Expand Down
15 changes: 8 additions & 7 deletions packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,18 @@ export class PHPRequestHandler implements RequestHandler {
#resolvePHPFilePath(requestedPath: string): string {
let filePath = removePathPrefix(requestedPath, this.#PATHNAME);

// If the path mentions a .php extension, that's our file's path.
if (filePath.includes('.php')) {
// If the path mentions a .php extension, that's our file's path.
filePath = filePath.split('.php')[0] + '.php';
} else {
// Otherwise, let's assume the file is $request_path/index.php
} else if (this.php.isDir(`${this.#DOCROOT}${filePath}`)) {
if (!filePath.endsWith('/')) {
filePath += '/';
}
if (!filePath.endsWith('index.php')) {
filePath += 'index.php';
filePath = `${filePath}/`;
}
// If the path is a directory, let's assume the file is index.php
filePath = `${filePath}index.php`;
} else {
// Otherwise, let's assume the file is /index.php
filePath = '/index.php';
}

const resolvedFsPath = `${this.#DOCROOT}${filePath}`;
Expand Down
Loading