# Request Decompress

`request-decompress` 插件解压使用 gzip 或 deflate 压缩的请求体，供同一路由上的其他插件使用。客户端可以发送压缩请求，验证、转换、安全和日志插件仍可处理解压后的内容。

该插件自 API7 Enterprise 3.10.9 和 APISIX 3.20.0 起可用。有关请求大小和转发选项，请参阅[配置](https://docs.apiseven.com/hub/request-decompress/configuration.md)。

## 请求处理[​](#request-handling "请求处理的直接链接")

该插件支持 `gzip`、`deflate` 和 `identity` 内容编码。请求体采用多种编码时，插件按相反顺序解压。对于拼接的 gzip 流，插件会处理其中的每个成员。不带 `Content-Encoding` 请求头的请求沿同一路由原样通过。

默认情况下，上游服务收到解压后的请求体，`Content-Length` 会更新，`Content-Encoding` 请求头会移除。如果上游需要接收编码后的数据，请启用压缩转发。其他插件仍可在请求体恢复或重新压缩前处理解压后的内容。

对于带有 `Content-Encoding` 请求头且无法处理的请求，网关会按下表返回错误。

| 条件                                                 | 状态码 | 响应                                    |
| ---------------------------------------------------- | ------ | --------------------------------------- |
| 内容编码不受支持                                     | `415`  | 包含 `Accept-Encoding: gzip, deflate`。 |
| 压缩请求体格式错误，或最后一个 gzip 成员之后仍有数据 | `400`  | 提示无法解压请求体。                    |
| 编码后的请求体或解压结果超过配置的限制               | `413`  | 提示请求体过大。                        |

请求体签名

默认情况下，`request-decompress` 先于 [`hmac-auth`](https://docs.apiseven.com/hub/hmac-auth.md) 和其他身份验证插件运行。如果 `hmac-auth` 验证请求体，它会对解压后的内容计算哈希。如果客户端对压缩后的字节签名，请将 `hmac-auth` 的优先级设为大于 `2850`。配置适当的请求体大小限制，以限制未认证请求的解压开销。

## 示例[​](#examples "示例的直接链接")

第一个示例配置一条路由，并验证上游收到未压缩的 JSON。第二个示例更新同一条路由，使上游收到压缩数据。

如果已启用 Admin API 身份验证，请将管理员角色的密钥导出为 `ADMIN_API_KEY`。

### 解压请求体[​](#decompress-a-request-body "解压请求体的直接链接")

创建一条路由，在将请求转发到 httpbin.org 前解压请求体。

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes/request-decompress-route" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "uri": "/anything",

    "plugins": {

      "request-decompress": {}

    },

    "upstream": {

      "type": "roundrobin",

      "nodes": {

        "httpbin.org:80": 1

      }

    }

  }'
```

adc.yaml

```
services:

  - name: request-decompress-service

    labels:

      docs-example: request-decompress

    routes:

      - name: request-decompress-route

        uris:

          - /anything

        plugins:

          request-decompress: {}

    upstream:

      type: roundrobin

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1
```

预览改动，并确认仅影响本示例使用的资源。

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-decompress
```

将配置同步到网关。

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-decompress
```

* Gateway API
* APISIX CRD

request-decompress-ic.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  type: ExternalName

  externalName: httpbin.org

---

apiVersion: apisix.apache.org/v1alpha1

kind: PluginConfig

metadata:

  namespace: aic

  name: request-decompress-plugin-config

spec:

  plugins:

    - name: request-decompress

      config: {}

---

apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: request-decompress-route

spec:

  parentRefs:

    - name: apisix

  rules:

    - matches:

        - path:

            type: Exact

            value: /anything

      filters:

        - type: ExtensionRef

          extensionRef:

            group: apisix.apache.org

            kind: PluginConfig

            name: request-decompress-plugin-config

      backendRefs:

        - name: httpbin-external-domain

          port: 80
```

request-decompress-ic.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixUpstream

metadata:

  namespace: aic

  name: httpbin-external-domain

spec:

  ingressClassName: apisix

  externalNodes:

    - type: Domain

      name: httpbin.org

---

apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: request-decompress-route

spec:

  ingressClassName: apisix

  http:

    - name: request-decompress-route

      match:

        paths:

          - /anything

      upstreams:

        - name: httpbin-external-domain

      plugins:

        - name: request-decompress

          enable: true

          config: {}
```

将配置应用到集群。

```
kubectl apply -f request-decompress-ic.yaml
```

向路由发送 gzip 压缩的 JSON 请求体，并提取用于确认上游收到哪些内容的响应字段。

```
echo -n '{"name":"world"}' | gzip | \

  curl "http://127.0.0.1:9080/anything" -X POST \

    -H "Content-Type: application/json" \

    -H "Content-Encoding: gzip" \

    --data-binary @- | \

  jq '{

    data,

    content_encoding: .headers["Content-Encoding"]

  }'
```

上游收到未压缩的 JSON 请求体，且没有内容编码。

```
{

  "data": "{\"name\":\"world\"}",

  "content_encoding": null

}
```

### 保持发往上游的请求体为压缩格式[​](#keep-the-upstream-request-compressed "保持发往上游的请求体为压缩格式的直接链�接")

更新上一个示例的插件配置，使同一路由上的其他插件继续读取解压后的内容，而上游收到压缩的请求体。

* Admin API
* ADC
* Ingress Controller

```
curl "http://127.0.0.1:9180/apisix/admin/routes/request-decompress-route" -X PATCH \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "plugins": {

      "request-decompress": {

        "forward_compressed": true

      }

    }

  }'
```

在 `adc.yaml` 中为现有路由启用压缩转发。

```
plugins:

  request-decompress:

    forward_compressed: true
```

预览并同步更新后的配置文件。

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-decompress



adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=request-decompress
```

* Gateway API
* APISIX CRD

更新现有 `PluginConfig` 中的插件配置。

```
spec:

  plugins:

    - name: request-decompress

      config:

        forward_compressed: true
```

更新现有 `ApisixRoute` 中的插件配置。

```
plugins:

  - name: request-decompress

    enable: true

    config:

      forward_compressed: true
```

应用更新后的配置。

```
kubectl apply -f request-decompress-ic.yaml
```

再发送一个 gzip 压缩的请求，并检查上游是否收到了内容编码请求头和二进制请求体。

```
echo -n '{"name":"world"}' | gzip | \

  curl "http://127.0.0.1:9080/anything" -X POST \

    -H "Content-Type: application/json" \

    -H "Content-Encoding: gzip" \

    --data-binary @- | \

  jq '{

    content_encoding: .headers["Content-Encoding"],

    body_is_binary: (.data | startswith("data:application/octet-stream;base64,"))

  }'
```

响应确认上游收到了 gzip 压缩的数据。

```
{

  "content_encoding": "gzip",

  "body_is_binary": true

}
```
