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

gh-100463: Add handling for extensionless URLs in http.server #100464

Closed
wants to merge 3 commits into from
Closed

gh-100463: Add handling for extensionless URLs in http.server #100464

wants to merge 3 commits into from

Conversation

jfrost-mo
Copy link

@jfrost-mo jfrost-mo commented Dec 23, 2022

Adds handling for extensionless URLs in http.server, where they will translated into the same filename with the .html extension. This PR adds a special case for URLs without any extension that will check common extensions before returning a 404 error.

Extensionless files are checked first for an exact match, then sequentially for the defined default_extensions (which unless overridden are .html and .htm), before returning a Not Found error if none exist.

The list of default extensions can be changed at runtime my passing a list of extensions to SimpleHTTPRequestHandler initiation.

Fixes #100463

A common pattern in URL design is to omit the file extension for pages, as this
both makes the URL look nicer and makes it technology agnostic (it doesn't
matter if the page is written in PHP or HTML). This commit adds a special case
for URLs without any extension that will check common extensions before
returning a 404 error.

The motivation for this change is the common use case of the python HTTP server
for static website development, especially amongst those learning HTML/CSS.
This change allows them to use the best practice of omitting extensions in
links and still having navigation between multiple pages working.

Extensionless files are checked first for an exact match, then sequentially for
the defined default_extensions (which unless overridden are .html and .htm),
before returning a Not Found error if none exist.

The list of default extensions can be changed at runtime my passing a list of
extensions to SimpleHTTPRequestHandler initiation.
Checks that it will read /spam.html for path /spam and checks that if /spam
does exist that it will be read with priority.
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented Dec 23, 2022

All commit authors signed the Contributor License Agreement.
CLA signed

@jfrost-mo jfrost-mo closed this Dec 23, 2022
@ethanfurman
Copy link
Member

@jfrost-mo Can you sign the CLA, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serve HTML from extensionless URLs in http.server
3 participants