---
eip: 7618
label: ERC-7618
title: Content encoding in ERC-5219 mode Web3 URL
status: Draft
type: Standards Track
category: ERC
created: 2024-02-08
requires: 5219, 6944
official: https://eips.ethereum.org/EIPS/eip-7618
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7618.md
canonical: https://eips.huatuo.xyz/zh/eip-7618/
lang: zh
tldr: "对 ERC-5219 resolve 模式下 web3:// URL 的草案扩展：如果合约返回了客户端无法处理的 Content-Encoding 头（gzip 或 brotli），协议会在提供数据之前先将其解压，使网站可以把压缩后的资源存放在链上。"
tags: [web3-url, web3, storage, compression, http]
---

# ERC-7618: Content encoding in ERC-5219 mode Web3 URL

> In ERC-5219 resolve mode, add ability to decode a compressed resource before serving it to the client

**TL;DR:** 对 ERC-5219 resolve 模式下 web3:// URL 的草案扩展：如果合约返回了客户端无法处理的 Content-Encoding 头（gzip 或 brotli），协议会在提供数据之前先将其解压，使网站可以把压缩后的资源存放在链上。

## 它解决什么问题？

`web3://` URL（[ERC-6860](/eip-6860/)）让浏览器或网关可以直接从智能合约加载网站。在 [ERC-6944](/eip-6944/) 的"resolve 模式"中，实现了 [ERC-5219](/eip-5219/) `request()` 的合约会像网页服务器一样返回状态码、正文和 HTTP 风格的头。

链上存储很昂贵，所以把 HTML、JavaScript 和图片压缩后存储可以节省大量 gas。但是直接返回 `Content-Encoding: gzip` 并不安全：发起请求的客户端可能不支持该算法。完整的 HTTP 风格协商（客户端发送 `Accept-Encoding`，服务器选择）也不太合适，因为合约需要为每个文件存储多个版本。

ERC-7618 通过让 `web3://` 协议层在需要时负责解压来解决这个问题。

## 它如何运作

1. 合约把资源压缩后只存一份，其 `request()` 返回压缩后的字节以及一个 `Content-Encoding` 头（例如 `br`）。
2. `web3://` 客户端或网关将该编码与终端客户端在 `Accept-Encoding` 中声明的编码进行比较。
3. 如果终端客户端支持，字节可以保持压缩状态直接透传。
4. 如果不支持，或者没有给出 `Accept-Encoding`，协议 **必须自行解码** 内容，并在转发之前移除 `Content-Encoding` 头。
5. 协议实现必须支持 `gzip` 和 `br`（brotli）。任何其他需要解码的编码都会导致"unsupported content encoding"错误。

无需修改接口：它复用现有 ERC-5219 `request()` 的返回值和标准的 HTTP 头名称。

## 示例代码

一个在 resolve 模式下提供 brotli 压缩页面的合约（简化版）：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract CompressedSite {
    struct KeyValue { string key; string value; }

    bytes private indexBr; // brotli-compressed index.html, uploaded by the owner

    function resolveMode() external pure returns (bytes32) {
        return "5219";
    }

    function request(string[] memory resource, KeyValue[] memory /* params */)
        external view
        returns (uint statusCode, string memory body, KeyValue[] memory headers)
    {
        if (resource.length == 0) {
            headers = new KeyValue[](2);
            headers[0] = KeyValue("Content-Type", "text/html");
            headers[1] = KeyValue("Content-Encoding", "br");
            return (200, string(indexBr), headers);
        }
        return (404, "", new KeyValue[](0));
    }
}
```

如果客户端不接受 `br`，则请求 `web3://<contract-address>/` 的客户端会收到纯 HTML。

## 需要注意

- **状态：Draft**，相关的 [ERC-6944](/eip-6944/) 以及基于 [ERC-6860](/eip-6860/) 的扩展也是如此。
- 该提案改变的是 `web3://` 客户端和网关的行为，而不是合约的行为；合约只需要正确设置头。
- 对于大文件，它可以与 [ERC-7617](/eip-7617/)（分块响应）自然结合。
- 解压发生在客户端的链下，因此不增加 gas 成本；它只会减小链上存储的大小。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7618
