prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Wildcard domains and TrustedhostMiddleware
  Support for wildcard domains
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `starlette/middleware/trustedhost.py`
  Content:
  ```
  1 from starlette.datastructures import Headers
  2 from starlette.responses import PlainTextResponse
  3 from starlette.types import ASGIApp, ASGIInstance, Scope
  4 import typing
  5 
  6 
  7 class TrustedHostMiddleware:
  8     def __init__(
  9         self, app: ASGIApp, allowed_hosts: typing.Sequence[str] = ["*"]
  10     ) -> None:
  11         self.app = app
  12         self.allowed_hosts = allowed_hosts
  13         self.allow_any = "*" in allowed_hosts
  14 
  15     def __call__(self, scope: Scope) -> ASGIInstance:
  16         if scope["type"] in ("http", "websocket") and not self.allow_any:
  17             headers = Headers(scope=scope)
  18             host = headers.get("host")
  19             if host not in self.allowed_hosts:
  20                 return PlainTextResponse("Invalid host header", status_code=400)
  21 
  22         return self.app(scope)
  23
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/starlette/middleware/trustedhost.py b/starlette/middleware/trustedhost.py
  --- a/starlette/middleware/trustedhost.py
  +++ b/starlette/middleware/trustedhost.py
  @@ -4,10 +4,17 @@
   import typing
   
   
  +ENFORCE_DOMAIN_WILDCARD = "Domain wildcard patterns must be like '*.example.com'."
  +
  +
   class TrustedHostMiddleware:
       def __init__(
           self, app: ASGIApp, allowed_hosts: typing.Sequence[str] = ["*"]
       ) -> None:
  +        for pattern in allowed_hosts:
  +            assert "*" not in pattern[1:], ENFORCE_DOMAIN_WILDCARD
  +            if pattern.startswith("*") and pattern != "*":
  +                assert pattern.startswith("*."), ENFORCE_DOMAIN_WILDCARD
           self.app = app
           self.allowed_hosts = allowed_hosts
           self.allow_any = "*" in allowed_hosts
  @@ -15,8 +22,15 @@
       def __call__(self, scope: Scope) -> ASGIInstance:
           if scope["type"] in ("http", "websocket") and not self.allow_any:
               headers = Headers(scope=scope)
  -            host = headers.get("host")
  -            if host not in self.allowed_hosts:
  +            host = headers.get("host", "").split(":")[0]
  +            for pattern in self.allowed_hosts:
  +                if (
  +                    host == pattern
  +                    or pattern.startswith("*")
  +                    and host.endswith(pattern[1:])
  +                ):
  +                    break
  +            else:
                   return PlainTextResponse("Invalid host header", status_code=400)
   
           return self.app(scope)
  
verification_info: {"golden_diff": "diff --git a/starlette/middleware/trustedhost.py b/starlette/middleware/trustedhost.py\n--- a/starlette/middleware/trustedhost.py\n+++ b/starlette/middleware/trustedhost.py\n@@ -4,10 +4,17 @@\n import typing\n \n \n+ENFORCE_DOMAIN_WILDCARD = \"Domain wildcard patterns must be like '*.example.com'.\"\n+\n+\n class TrustedHostMiddleware:\n     def __init__(\n         self, app: ASGIApp, allowed_hosts: typing.Sequence[str] = [\"*\"]\n     ) -> None:\n+        for pattern in allowed_hosts:\n+            assert \"*\" not in pattern[1:], ENFORCE_DOMAIN_WILDCARD\n+            if pattern.startswith(\"*\") and pattern != \"*\":\n+                assert pattern.startswith(\"*.\"), ENFORCE_DOMAIN_WILDCARD\n         self.app = app\n         self.allowed_hosts = allowed_hosts\n         self.allow_any = \"*\" in allowed_hosts\n@@ -15,8 +22,15 @@\n     def __call__(self, scope: Scope) -> ASGIInstance:\n         if scope[\"type\"] in (\"http\", \"websocket\") and not self.allow_any:\n             headers = Headers(scope=scope)\n-            host = headers.get(\"host\")\n-            if host not in self.allowed_hosts:\n+            host = headers.get(\"host\", \"\").split(\":\")[0]\n+            for pattern in self.allowed_hosts:\n+                if (\n+                    host == pattern\n+                    or pattern.startswith(\"*\")\n+                    and host.endswith(pattern[1:])\n+                ):\n+                    break\n+            else:\n                 return PlainTextResponse(\"Invalid host header\", status_code=400)\n \n         return self.app(scope)\n", "issue": "Wildcard domains and TrustedhostMiddleware\nSupport for wildcard domains\n", "before_files": [{"content": "from starlette.datastructures import Headers\nfrom starlette.responses import PlainTextResponse\nfrom starlette.types import ASGIApp, ASGIInstance, Scope\nimport typing\n\n\nclass TrustedHostMiddleware:\n    def __init__(\n        self, app: ASGIApp, allowed_hosts: typing.Sequence[str] = [\"*\"]\n    ) -> None:\n        self.app = app\n        self.allowed_hosts = allowed_hosts\n        self.allow_any = \"*\" in allowed_hosts\n\n    def __call__(self, scope: Scope) -> ASGIInstance:\n        if scope[\"type\"] in (\"http\", \"websocket\") and not self.allow_any:\n            headers = Headers(scope=scope)\n            host = headers.get(\"host\")\n            if host not in self.allowed_hosts:\n                return PlainTextResponse(\"Invalid host header\", status_code=400)\n\n        return self.app(scope)\n", "path": "starlette/middleware/trustedhost.py"}], "after_files": [{"content": "from starlette.datastructures import Headers\nfrom starlette.responses import PlainTextResponse\nfrom starlette.types import ASGIApp, ASGIInstance, Scope\nimport typing\n\n\nENFORCE_DOMAIN_WILDCARD = \"Domain wildcard patterns must be like '*.example.com'.\"\n\n\nclass TrustedHostMiddleware:\n    def __init__(\n        self, app: ASGIApp, allowed_hosts: typing.Sequence[str] = [\"*\"]\n    ) -> None:\n        for pattern in allowed_hosts:\n            assert \"*\" not in pattern[1:], ENFORCE_DOMAIN_WILDCARD\n            if pattern.startswith(\"*\") and pattern != \"*\":\n                assert pattern.startswith(\"*.\"), ENFORCE_DOMAIN_WILDCARD\n        self.app = app\n        self.allowed_hosts = allowed_hosts\n        self.allow_any = \"*\" in allowed_hosts\n\n    def __call__(self, scope: Scope) -> ASGIInstance:\n        if scope[\"type\"] in (\"http\", \"websocket\") and not self.allow_any:\n            headers = Headers(scope=scope)\n            host = headers.get(\"host\", \"\").split(\":\")[0]\n            for pattern in self.allowed_hosts:\n                if (\n                    host == pattern\n                    or pattern.startswith(\"*\")\n                    and host.endswith(pattern[1:])\n                ):\n                    break\n            else:\n                return PlainTextResponse(\"Invalid host header\", status_code=400)\n\n        return self.app(scope)\n", "path": "starlette/middleware/trustedhost.py"}]}