Build a 204 response that satisfies a CORS preflight locally. Apps Script's UrlFetchApp does not support OPTIONS, so preflights must be answered here rather than forwarded to the relay.
(origin: str, acr_method: str, acr_headers: str)
| 262 | # ── CORS helpers ────────────────────────────────────────────────────────────── |
| 263 | |
| 264 | def cors_preflight_response(origin: str, acr_method: str, acr_headers: str) -> bytes: |
| 265 | """Build a 204 response that satisfies a CORS preflight locally. |
| 266 | |
| 267 | Apps Script's UrlFetchApp does not support OPTIONS, so preflights must |
| 268 | be answered here rather than forwarded to the relay. |
| 269 | """ |
| 270 | allow_origin = origin or "*" |
| 271 | allow_methods = ( |
| 272 | f"{acr_method}, GET, POST, PUT, DELETE, PATCH, OPTIONS" |
| 273 | if acr_method else |
| 274 | "GET, POST, PUT, DELETE, PATCH, OPTIONS" |
| 275 | ) |
| 276 | allow_headers = acr_headers or "*" |
| 277 | return ( |
| 278 | "HTTP/1.1 204 No Content\r\n" |
| 279 | f"Access-Control-Allow-Origin: {allow_origin}\r\n" |
| 280 | f"Access-Control-Allow-Methods: {allow_methods}\r\n" |
| 281 | f"Access-Control-Allow-Headers: {allow_headers}\r\n" |
| 282 | "Access-Control-Allow-Credentials: true\r\n" |
| 283 | "Access-Control-Max-Age: 86400\r\n" |
| 284 | "Vary: Origin\r\n" |
| 285 | "Content-Length: 0\r\n" |
| 286 | "\r\n" |
| 287 | ).encode() |
| 288 | |
| 289 | |
| 290 | def inject_cors_headers(response: bytes, origin: str) -> bytes: |
no outgoing calls
no test coverage detected