Return the equivalent of the HTTP request's SCRIPT_NAME environment variable. If Apache mod_rewrite is used, return what would have been the script name prior to any rewriting (so it's the script name as seen from the client's perspective), unless the FORCE_SCRIPT_NAME setting is
(environ)
| 152 | |
| 153 | |
| 154 | def get_script_name(environ): |
| 155 | """ |
| 156 | Return the equivalent of the HTTP request's SCRIPT_NAME environment |
| 157 | variable. If Apache mod_rewrite is used, return what would have been |
| 158 | the script name prior to any rewriting (so it's the script name as seen |
| 159 | from the client's perspective), unless the FORCE_SCRIPT_NAME setting is |
| 160 | set (to anything). |
| 161 | """ |
| 162 | if settings.FORCE_SCRIPT_NAME is not None: |
| 163 | return settings.FORCE_SCRIPT_NAME |
| 164 | |
| 165 | # If Apache's mod_rewrite had a whack at the URL, Apache set either |
| 166 | # SCRIPT_URL or REDIRECT_URL to the full resource URL before applying any |
| 167 | # rewrites. Unfortunately not every web server (lighttpd!) passes this |
| 168 | # information through all the time, so FORCE_SCRIPT_NAME, above, is still |
| 169 | # needed. |
| 170 | script_url = get_bytes_from_wsgi(environ, "SCRIPT_URL", "") or get_bytes_from_wsgi( |
| 171 | environ, "REDIRECT_URL", "" |
| 172 | ) |
| 173 | |
| 174 | if script_url: |
| 175 | if b"//" in script_url: |
| 176 | # mod_wsgi squashes multiple successive slashes in PATH_INFO, |
| 177 | # do the same with script_url before manipulating paths (#17133). |
| 178 | script_url = _slashes_re.sub(b"/", script_url) |
| 179 | path_info = get_bytes_from_wsgi(environ, "PATH_INFO", "") |
| 180 | script_name = script_url.removesuffix(path_info) |
| 181 | else: |
| 182 | script_name = get_bytes_from_wsgi(environ, "SCRIPT_NAME", "") |
| 183 | |
| 184 | return script_name.decode() |
| 185 | |
| 186 | |
| 187 | def get_bytes_from_wsgi(environ, key, default): |