Skip to content

Commit

Permalink
Merge pull request #4379 from Discookie/update-httpserver
Browse files Browse the repository at this point in the history
Bring code borrowed from `http.server` in sync with upstream
  • Loading branch information
bruntib authored Nov 21, 2024
2 parents 3229b9b + 208d43b commit 1b7e722
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions web/server/codechecker_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
from functools import partial
from http.server import HTTPServer, SimpleHTTPRequestHandler
import os
import posixpath
import shutil
import signal
import socket
import ssl
import sys
from typing import List, Optional, Tuple
import urllib

import multiprocess
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -79,6 +77,8 @@ class RequestHandler(SimpleHTTPRequestHandler):
def __init__(self, request, client_address, server):
self.path = None
super().__init__(request, client_address, server)
# GET requests are served from www_root.
self.directory = server.www_root

def log_message(self, *args):
""" Silencing http server. """
Expand Down Expand Up @@ -237,11 +237,14 @@ def do_GET(self):
# Check that path contains a product endpoint.
if product_endpoint is not None and product_endpoint != '':
self.path = self.path.replace(f"{product_endpoint}/", "", 1)
# Remove extra leading slashes, see cpython#93789.
self.path = '/' + self.path.lstrip('/')

if self.path == '/':
self.path = "index.html"

# Check that the given path is a file.
# The base directory is set to www_root.
if not os.path.exists(self.translate_path(self.path)):
self.path = 'index.html'

Expand Down Expand Up @@ -464,26 +467,6 @@ def list_directory(self, path):
""" Disable directory listing. """
self.send_error(405, "No permission to list directory")

def translate_path(self, path):
"""
Modified version from SimpleHTTPRequestHandler.
Path is set to www_root.
"""
# Abandon query parameters.
path = path.split('?', 1)[0]
path = path.split('#', 1)[0]
path = posixpath.normpath(urllib.parse.unquote(path))
words = path.split('/')
words = [_f for _f in words if _f]
path = self.server.www_root
for word in words:
_, word = os.path.splitdrive(word)
_, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path


class Product:
"""
Expand Down

0 comments on commit 1b7e722

Please sign in to comment.