# Performance and response settings

> Per-connection speed limits, CORS configuration, download file names, and the CDN's default compression and error pages.

This page covers the settings that affect download speed and response content. For cache durations, see [Cache, purge, and prefetch](https://docs.bitiful.com/en/cdn/handbook/cache.md).

Entry point: **CDN › Static acceleration management (静态加速管理) ›** the domain's **Manage (管理) › Access control (访问控制)**. After you save, the status becomes **Deploying (部署中)**, which usually takes 30 seconds, and the change takes effect once the status returns to **Running (运行中)**. You can't make changes while the domain is **Stopped (已停止)**.

## Per-connection speed limit

This limits the maximum download speed of each request. Use it to control peak bandwidth, or to keep a few users from using up all the traffic.

1. On the **Per-connection speed limit (单线程限速)** card, click **Edit (编辑)** and select **On (开启)**.
2. Enter the **Speed limit (限速值)**: an integer from 1 to 32, in MiB/s.
3. Click **Save (保存)**.

Notes:

- The limit applies to a single request. When a multi-threaded download tool opens several requests at once, the total speed is the sum of the requests.
- The first 7 MiB of each request isn't limited, so small files such as images and scripts aren't affected.
- It applies to the whole CDN domain. You can't set it by file type or time of day. For different speeds, use different CDN domains.
- Going out of range shows "单线程限制值必须在\[1-32]之间" (the per-connection limit must be between 1 and 32).

By default there's no limit. This limit applies to the speed from the CDN to visitors, and is unrelated to the default speed limit on direct S4 downloads.

## CORS configuration

When a web page reads resources with JavaScript (`fetch`, `XMLHttpRequest`), or loads resources or fonts from another domain with `crossorigin`, the browser requires the response to carry CORS headers.

1. On the **CORS configuration (跨域访问配置)** card, click **Edit** and select **On** for **CORS rules (CORS 规则)**.
2. In **Origins**, enter the allowed origins, one per line.
3. Click **Save**.

| Format                    | Meaning                                                                        |
| ------------------------- | ------------------------------------------------------------------------------ |
| `https://www.example.com` | Allows this origin                                                             |
| `https://*.example.com`   | Allows subdomains. `*` matches any characters                                  |
| `*`                       | Allows all origins. As soon as one line is `*`, the other lines have no effect |

Rules and limits:

- An origin includes the protocol and no path. If the port isn't the default, include it, for example `http://localhost:5173`
- The interface says up to 100 entries, and each up to 50 characters
- No blank lines, and no quotes, semicolons, `$`, `#`, curly braces, or similar characters

When this is on, responses for matching origins carry:

| Response header                | Value                                                         |
| ------------------------------ | ------------------------------------------------------------- |
| `Access-Control-Allow-Origin`  | The request's origin. When the origin is set to `*`, it's `*` |
| `Access-Control-Allow-Methods` | `HEAD,GET,POST,OPTIONS,PUT,DELETE`                            |
| `Access-Control-Allow-Headers` | `*`                                                           |
| `Access-Control-Max-Age`       | `1728000` (20 days)                                           |

For a matching `OPTIONS` preflight request, the edge returns `204` directly. These response headers override response headers of the same name returned by your origin. `Access-Control-Allow-Credentials` and `Access-Control-Expose-Headers` aren't returned.

Verify:

```bash
curl -I -H "Origin: https://www.example.com" https://cdn.example.com/font.woff2
```

> **Note**
>
> A bucket's [CORS settings](https://docs.bitiful.com/en/bitiful-s4/handbook/cors.md) apply to direct access to S4 (for example, direct browser uploads). For access through a CDN domain, the settings here take precedence. Browsers cache preflight results, so test in an incognito window after changes.

## Download file name

Supported only for S4 bucket origin. Add the `response-content-disposition` parameter to the link to make the browser download the file instead of opening it, and to set the file name. The parameter value must be URL-encoded.

```text
https://cdn.example.com/files/a1b2.pdf?response-content-disposition=attachment%3B%20filename%3D%22report.pdf%22
```

| Parameter value (decoded)           | Response `Content-Disposition` |
| ----------------------------------- | ------------------------------ |
| `attachment; filename="report.pdf"` | Returned as is                 |
| `attachment`                        | `attachment`                   |
| `inline`                            | `inline`                       |

If [advanced authentication](https://docs.bitiful.com/en/best-practice/token-link.md) is on, this parameter must also be part of the signature.

## Default behavior

These behaviors are on by default and have no settings:

| Behavior                 | Description                                                                                                                  |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------- |
| Compression              | When the visitor supports gzip, text, JavaScript, JSON, XML, and CSS are compressed with gzip.                               |
| `server` response header | Always `Bitiful`.                                                                                                            |
| Error pages              | 400, 401, 402, 403, and 405 show the 403 page. 404 shows the 404 page. 429 shows the 429 page. 500 to 506 show the 502 page. |
| Origin errors            | When the origin connection fails, times out, or returns 500, the edge returns its stale cached copy first if it has one.     |

The console currently has no settings for custom response headers, M3U8 rewriting, or custom error pages.

## FAQ

<details>
<summary>I set a speed limit, but small files are still fast</summary>

The first 7 MiB of each request isn't limited. This is expected.

</details>

<details>
<summary>After enabling CORS, the browser still reports a CORS error</summary>

Check that the origin's protocol and port match the page; that the status has returned to **Running**; whether the request returns an error such as 403 or 404; and whether the request uses `credentials: "include"` (cross-origin requests that carry credentials aren't supported).

</details>
