We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
CORSMiddleware is sending an extra 'http.response.body'
It seems that even with all tests passing and cors being successfully applied, CORSMiddleware still raises a runtime error.

Code being tested:
```python
app = Starlette()

app.add_middleware(CORSMiddleware, allow_origins=["*"])

@app.route("/")
async def homepage(request):
    return PlainTextResponse('Hello', status_code=200)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
```

And the error being produced:
```
ERROR: Exception in ASGI application
Traceback (most recent call last):
  File "/home/alexbotello/.local/share/virtualenvs/starlette-dshJy1CJ/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 384, in run_asgi
    result = await asgi(self.receive, self.send)
  File "/home/alexbotello/Code/starlette/starlette/exceptions.py", line 60, in app
    raise exc from None
  File "/home/alexbotello/Code/starlette/starlette/exceptions.py", line 52, in app
    await instance(receive, sender)
  File "/home/alexbotello/Code/starlette/starlette/middleware/cors.py", line 116, in simple_response
    await inner(receive, send)
  File "/home/alexbotello/Code/starlette/starlette/applications.py", line 26, in awaitable
    await response(receive, send)
  File "/home/alexbotello/Code/starlette/starlette/responses.py", line 100, in __call__
    await send({"type": "http.response.body", "body": self.body})
  File "/home/alexbotello/Code/starlette/starlette/middleware/cors.py", line 130, in send
    await send(message)
  File "/home/alexbotello/Code/starlette/starlette/exceptions.py", line 47, in sender
    await send(message)
  File "/home/alexbotello/.local/share/virtualenvs/starlette-dshJy1CJ/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 518, in send
    raise RuntimeError(msg % message_type)
RuntimeError: Unexpected ASGI message 'http.response.body' sent, after response already completed.
```
It seems the issue is originating from `send`. Specifically:
```python
if message["type"] != "http.response.start":
    await send(message)
```
Removing this fixes the issue and does not break any tests.

--- 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/cors.py`
Content:
```
1 from starlette.datastructures import Headers, MutableHeaders, URL
2 from starlette.responses import PlainTextResponse
3 from starlette.types import ASGIApp, ASGIInstance, Scope
4 import functools
5 import typing
6 
7 
8 ALL_METHODS = ("DELETE", "GET", "OPTIONS", "PATCH", "POST", "PUT")
9 
10 
11 class CORSMiddleware:
12     def __init__(
13         self,
14         app: ASGIApp,
15         allow_origins: typing.Sequence[str] = (),
16         allow_methods: typing.Sequence[str] = ("GET",),
17         allow_headers: typing.Sequence[str] = (),
18         allow_credentials: bool = False,
19         expose_headers: typing.Sequence[str] = (),
20         max_age: int = 600,
21     ):
22 
23         if "*" in allow_methods:
24             allow_methods = ALL_METHODS
25 
26         simple_headers = {}
27         if "*" in allow_origins:
28             simple_headers["Access-Control-Allow-Origin"] = "*"
29         if allow_credentials:
30             simple_headers["Access-Control-Allow-Credentials"] = "true"
31         if expose_headers:
32             simple_headers["Access-Control-Expose-Headers"] = ", ".join(expose_headers)
33 
34         preflight_headers = {}
35         if "*" in allow_origins:
36             preflight_headers["Access-Control-Allow-Origin"] = "*"
37         else:
38             preflight_headers["Vary"] = "Origin"
39         preflight_headers.update(
40             {
41                 "Access-Control-Allow-Methods": ", ".join(allow_methods),
42                 "Access-Control-Max-Age": str(max_age),
43             }
44         )
45         if allow_headers and "*" not in allow_headers:
46             preflight_headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
47         if allow_credentials:
48             preflight_headers["Access-Control-Allow-Credentials"] = "true"
49 
50         self.app = app
51         self.allow_origins = allow_origins
52         self.allow_methods = allow_methods
53         self.allow_headers = allow_headers
54         self.allow_all_origins = "*" in allow_origins
55         self.allow_all_headers = "*" in allow_headers
56         self.simple_headers = simple_headers
57         self.preflight_headers = preflight_headers
58 
59     def __call__(self, scope: Scope):
60         if scope["type"] == "http":
61             method = scope["method"]
62             headers = Headers(scope["headers"])
63             origin = headers.get("origin")
64 
65             if origin is not None:
66                 if method == "OPTIONS" and "access-control-request-method" in headers:
67                     return self.preflight_response(request_headers=headers)
68                 else:
69                     return functools.partial(
70                         self.simple_response, scope=scope, origin=origin
71                     )
72 
73         return self.app(scope)
74 
75     def preflight_response(self, request_headers):
76         requested_origin = request_headers["origin"]
77         requested_method = request_headers["access-control-request-method"]
78         requested_headers = request_headers.get("access-control-request-headers")
79         requested_cookie = "cookie" in request_headers
80 
81         headers = dict(self.preflight_headers)
82         failures = []
83 
84         # If we only allow specific origins, then we have to mirror back
85         # the Origin header in the response.
86         if not self.allow_all_origins:
87             if requested_origin in self.allow_origins:
88                 headers["Access-Control-Allow-Origin"] = requested_origin
89             else:
90                 failures.append("origin")
91 
92         if requested_method not in self.allow_methods:
93             failures.append("method")
94 
95         # If we allow all headers, then we have to mirror back any requested
96         # headers in the response.
97         if self.allow_all_headers and requested_headers is not None:
98             headers["Access-Control-Allow-Headers"] = requested_headers
99         elif requested_headers is not None:
100             for header in requested_headers.split(","):
101                 if header.strip() not in self.allow_headers:
102                     failures.append("headers")
103 
104         # We don't strictly need to use 400 responses here, since its up to
105         # the browser to enforce the CORS policy, but its more informative
106         # if we do.
107         if failures:
108             failure_text = "Disallowed CORS " + ", ".join(failures)
109             return PlainTextResponse(failure_text, status_code=400, headers=headers)
110 
111         return PlainTextResponse("OK", status_code=200, headers=headers)
112 
113     async def simple_response(self, receive, send, scope=None, origin=None):
114         inner = self.app(scope)
115         send = functools.partial(self.send, send=send, origin=origin)
116         await inner(receive, send)
117 
118     async def send(self, message, send=None, origin=None):
119         if message["type"] != "http.response.start":
120             await send(message)
121 
122         message.setdefault("headers", [])
123         headers = MutableHeaders(message["headers"])
124 
125         # If we only allow specific origins, then we have to mirror back
126         # the Origin header in the response.
127         if not self.allow_all_origins and origin in self.allow_origins:
128             headers["Access-Control-Allow-Origin"] = origin
129         headers.update(self.simple_headers)
130         await send(message)
131
```
--- 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()

```

diff --git a/starlette/middleware/cors.py b/starlette/middleware/cors.py
--- a/starlette/middleware/cors.py
+++ b/starlette/middleware/cors.py
@@ -118,6 +118,7 @@
     async def send(self, message, send=None, origin=None):
         if message["type"] != "http.response.start":
             await send(message)
+            return
 
         message.setdefault("headers", [])
         headers = MutableHeaders(message["headers"])

{"golden_diff": "diff --git a/starlette/middleware/cors.py b/starlette/middleware/cors.py\n--- a/starlette/middleware/cors.py\n+++ b/starlette/middleware/cors.py\n@@ -118,6 +118,7 @@\n     async def send(self, message, send=None, origin=None):\n         if message[\"type\"] != \"http.response.start\":\n             await send(message)\n+            return\n \n         message.setdefault(\"headers\", [])\n         headers = MutableHeaders(message[\"headers\"])\n", "issue": "CORSMiddleware is sending an extra 'http.response.body'\nIt seems that even with all tests passing and cors being successfully applied, CORSMiddleware still raises a runtime error.\r\n\r\nCode being tested:\r\n```python\r\napp = Starlette()\r\n\r\napp.add_middleware(CORSMiddleware, allow_origins=[\"*\"])\r\n\r\n@app.route(\"/\")\r\nasync def homepage(request):\r\n    return PlainTextResponse('Hello', status_code=200)\r\n\r\nif __name__ == \"__main__\":\r\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)\r\n```\r\n\r\nAnd the error being produced:\r\n```\r\nERROR: Exception in ASGI application\r\nTraceback (most recent call last):\r\n  File \"/home/alexbotello/.local/share/virtualenvs/starlette-dshJy1CJ/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py\", line 384, in run_asgi\r\n    result = await asgi(self.receive, self.send)\r\n  File \"/home/alexbotello/Code/starlette/starlette/exceptions.py\", line 60, in app\r\n    raise exc from None\r\n  File \"/home/alexbotello/Code/starlette/starlette/exceptions.py\", line 52, in app\r\n    await instance(receive, sender)\r\n  File \"/home/alexbotello/Code/starlette/starlette/middleware/cors.py\", line 116, in simple_response\r\n    await inner(receive, send)\r\n  File \"/home/alexbotello/Code/starlette/starlette/applications.py\", line 26, in awaitable\r\n    await response(receive, send)\r\n  File \"/home/alexbotello/Code/starlette/starlette/responses.py\", line 100, in __call__\r\n    await send({\"type\": \"http.response.body\", \"body\": self.body})\r\n  File \"/home/alexbotello/Code/starlette/starlette/middleware/cors.py\", line 130, in send\r\n    await send(message)\r\n  File \"/home/alexbotello/Code/starlette/starlette/exceptions.py\", line 47, in sender\r\n    await send(message)\r\n  File \"/home/alexbotello/.local/share/virtualenvs/starlette-dshJy1CJ/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py\", line 518, in send\r\n    raise RuntimeError(msg % message_type)\r\nRuntimeError: Unexpected ASGI message 'http.response.body' sent, after response already completed.\r\n```\r\nIt seems the issue is originating from `send`. Specifically:\r\n```python\r\nif message[\"type\"] != \"http.response.start\":\r\n    await send(message)\r\n```\r\nRemoving this fixes the issue and does not break any tests.\n", "before_files": [{"content": "from starlette.datastructures import Headers, MutableHeaders, URL\nfrom starlette.responses import PlainTextResponse\nfrom starlette.types import ASGIApp, ASGIInstance, Scope\nimport functools\nimport typing\n\n\nALL_METHODS = (\"DELETE\", \"GET\", \"OPTIONS\", \"PATCH\", \"POST\", \"PUT\")\n\n\nclass CORSMiddleware:\n    def __init__(\n        self,\n        app: ASGIApp,\n        allow_origins: typing.Sequence[str] = (),\n        allow_methods: typing.Sequence[str] = (\"GET\",),\n        allow_headers: typing.Sequence[str] = (),\n        allow_credentials: bool = False,\n        expose_headers: typing.Sequence[str] = (),\n        max_age: int = 600,\n    ):\n\n        if \"*\" in allow_methods:\n            allow_methods = ALL_METHODS\n\n        simple_headers = {}\n        if \"*\" in allow_origins:\n            simple_headers[\"Access-Control-Allow-Origin\"] = \"*\"\n        if allow_credentials:\n            simple_headers[\"Access-Control-Allow-Credentials\"] = \"true\"\n        if expose_headers:\n            simple_headers[\"Access-Control-Expose-Headers\"] = \", \".join(expose_headers)\n\n        preflight_headers = {}\n        if \"*\" in allow_origins:\n            preflight_headers[\"Access-Control-Allow-Origin\"] = \"*\"\n        else:\n            preflight_headers[\"Vary\"] = \"Origin\"\n        preflight_headers.update(\n            {\n                \"Access-Control-Allow-Methods\": \", \".join(allow_methods),\n                \"Access-Control-Max-Age\": str(max_age),\n            }\n        )\n        if allow_headers and \"*\" not in allow_headers:\n            preflight_headers[\"Access-Control-Allow-Headers\"] = \", \".join(allow_headers)\n        if allow_credentials:\n            preflight_headers[\"Access-Control-Allow-Credentials\"] = \"true\"\n\n        self.app = app\n        self.allow_origins = allow_origins\n        self.allow_methods = allow_methods\n        self.allow_headers = allow_headers\n        self.allow_all_origins = \"*\" in allow_origins\n        self.allow_all_headers = \"*\" in allow_headers\n        self.simple_headers = simple_headers\n        self.preflight_headers = preflight_headers\n\n    def __call__(self, scope: Scope):\n        if scope[\"type\"] == \"http\":\n            method = scope[\"method\"]\n            headers = Headers(scope[\"headers\"])\n            origin = headers.get(\"origin\")\n\n            if origin is not None:\n                if method == \"OPTIONS\" and \"access-control-request-method\" in headers:\n                    return self.preflight_response(request_headers=headers)\n                else:\n                    return functools.partial(\n                        self.simple_response, scope=scope, origin=origin\n                    )\n\n        return self.app(scope)\n\n    def preflight_response(self, request_headers):\n        requested_origin = request_headers[\"origin\"]\n        requested_method = request_headers[\"access-control-request-method\"]\n        requested_headers = request_headers.get(\"access-control-request-headers\")\n        requested_cookie = \"cookie\" in request_headers\n\n        headers = dict(self.preflight_headers)\n        failures = []\n\n        # If we only allow specific origins, then we have to mirror back\n        # the Origin header in the response.\n        if not self.allow_all_origins:\n            if requested_origin in self.allow_origins:\n                headers[\"Access-Control-Allow-Origin\"] = requested_origin\n            else:\n                failures.append(\"origin\")\n\n        if requested_method not in self.allow_methods:\n            failures.append(\"method\")\n\n        # If we allow all headers, then we have to mirror back any requested\n        # headers in the response.\n        if self.allow_all_headers and requested_headers is not None:\n            headers[\"Access-Control-Allow-Headers\"] = requested_headers\n        elif requested_headers is not None:\n            for header in requested_headers.split(\",\"):\n                if header.strip() not in self.allow_headers:\n                    failures.append(\"headers\")\n\n        # We don't strictly need to use 400 responses here, since its up to\n        # the browser to enforce the CORS policy, but its more informative\n        # if we do.\n        if failures:\n            failure_text = \"Disallowed CORS \" + \", \".join(failures)\n            return PlainTextResponse(failure_text, status_code=400, headers=headers)\n\n        return PlainTextResponse(\"OK\", status_code=200, headers=headers)\n\n    async def simple_response(self, receive, send, scope=None, origin=None):\n        inner = self.app(scope)\n        send = functools.partial(self.send, send=send, origin=origin)\n        await inner(receive, send)\n\n    async def send(self, message, send=None, origin=None):\n        if message[\"type\"] != \"http.response.start\":\n            await send(message)\n\n        message.setdefault(\"headers\", [])\n        headers = MutableHeaders(message[\"headers\"])\n\n        # If we only allow specific origins, then we have to mirror back\n        # the Origin header in the response.\n        if not self.allow_all_origins and origin in self.allow_origins:\n            headers[\"Access-Control-Allow-Origin\"] = origin\n        headers.update(self.simple_headers)\n        await send(message)\n", "path": "starlette/middleware/cors.py"}], "after_files": [{"content": "from starlette.datastructures import Headers, MutableHeaders, URL\nfrom starlette.responses import PlainTextResponse\nfrom starlette.types import ASGIApp, ASGIInstance, Scope\nimport functools\nimport typing\n\n\nALL_METHODS = (\"DELETE\", \"GET\", \"OPTIONS\", \"PATCH\", \"POST\", \"PUT\")\n\n\nclass CORSMiddleware:\n    def __init__(\n        self,\n        app: ASGIApp,\n        allow_origins: typing.Sequence[str] = (),\n        allow_methods: typing.Sequence[str] = (\"GET\",),\n        allow_headers: typing.Sequence[str] = (),\n        allow_credentials: bool = False,\n        expose_headers: typing.Sequence[str] = (),\n        max_age: int = 600,\n    ):\n\n        if \"*\" in allow_methods:\n            allow_methods = ALL_METHODS\n\n        simple_headers = {}\n        if \"*\" in allow_origins:\n            simple_headers[\"Access-Control-Allow-Origin\"] = \"*\"\n        if allow_credentials:\n            simple_headers[\"Access-Control-Allow-Credentials\"] = \"true\"\n        if expose_headers:\n            simple_headers[\"Access-Control-Expose-Headers\"] = \", \".join(expose_headers)\n\n        preflight_headers = {}\n        if \"*\" in allow_origins:\n            preflight_headers[\"Access-Control-Allow-Origin\"] = \"*\"\n        else:\n            preflight_headers[\"Vary\"] = \"Origin\"\n        preflight_headers.update(\n            {\n                \"Access-Control-Allow-Methods\": \", \".join(allow_methods),\n                \"Access-Control-Max-Age\": str(max_age),\n            }\n        )\n        if allow_headers and \"*\" not in allow_headers:\n            preflight_headers[\"Access-Control-Allow-Headers\"] = \", \".join(allow_headers)\n        if allow_credentials:\n            preflight_headers[\"Access-Control-Allow-Credentials\"] = \"true\"\n\n        self.app = app\n        self.allow_origins = allow_origins\n        self.allow_methods = allow_methods\n        self.allow_headers = allow_headers\n        self.allow_all_origins = \"*\" in allow_origins\n        self.allow_all_headers = \"*\" in allow_headers\n        self.simple_headers = simple_headers\n        self.preflight_headers = preflight_headers\n\n    def __call__(self, scope: Scope):\n        if scope[\"type\"] == \"http\":\n            method = scope[\"method\"]\n            headers = Headers(scope[\"headers\"])\n            origin = headers.get(\"origin\")\n\n            if origin is not None:\n                if method == \"OPTIONS\" and \"access-control-request-method\" in headers:\n                    return self.preflight_response(request_headers=headers)\n                else:\n                    return functools.partial(\n                        self.simple_response, scope=scope, origin=origin\n                    )\n\n        return self.app(scope)\n\n    def preflight_response(self, request_headers):\n        requested_origin = request_headers[\"origin\"]\n        requested_method = request_headers[\"access-control-request-method\"]\n        requested_headers = request_headers.get(\"access-control-request-headers\")\n        requested_cookie = \"cookie\" in request_headers\n\n        headers = dict(self.preflight_headers)\n        failures = []\n\n        # If we only allow specific origins, then we have to mirror back\n        # the Origin header in the response.\n        if not self.allow_all_origins:\n            if requested_origin in self.allow_origins:\n                headers[\"Access-Control-Allow-Origin\"] = requested_origin\n            else:\n                failures.append(\"origin\")\n\n        if requested_method not in self.allow_methods:\n            failures.append(\"method\")\n\n        # If we allow all headers, then we have to mirror back any requested\n        # headers in the response.\n        if self.allow_all_headers and requested_headers is not None:\n            headers[\"Access-Control-Allow-Headers\"] = requested_headers\n        elif requested_headers is not None:\n            for header in requested_headers.split(\",\"):\n                if header.strip() not in self.allow_headers:\n                    failures.append(\"headers\")\n\n        # We don't strictly need to use 400 responses here, since its up to\n        # the browser to enforce the CORS policy, but its more informative\n        # if we do.\n        if failures:\n            failure_text = \"Disallowed CORS \" + \", \".join(failures)\n            return PlainTextResponse(failure_text, status_code=400, headers=headers)\n\n        return PlainTextResponse(\"OK\", status_code=200, headers=headers)\n\n    async def simple_response(self, receive, send, scope=None, origin=None):\n        inner = self.app(scope)\n        send = functools.partial(self.send, send=send, origin=origin)\n        await inner(receive, send)\n\n    async def send(self, message, send=None, origin=None):\n        if message[\"type\"] != \"http.response.start\":\n            await send(message)\n            return\n\n        message.setdefault(\"headers\", [])\n        headers = MutableHeaders(message[\"headers\"])\n\n        # If we only allow specific origins, then we have to mirror back\n        # the Origin header in the response.\n        if not self.allow_all_origins and origin in self.allow_origins:\n            headers[\"Access-Control-Allow-Origin\"] = origin\n        headers.update(self.simple_headers)\n        await send(message)\n", "path": "starlette/middleware/cors.py"}]}