This document specifies how the gateway operates. For illustration, the base-domain of our gateway is assumed to be https://gateway.tld located at IP 1.2.3.4. CID refers to Content ID, OriginID refers to an Origin's key, but also can include DNSLinks.
In the Bash implementation, this is handled by the handler function.
GET or HEAD requests for resources local to the gateway server. For these, the gateway behaves as a simple file server. All other methods are forbidden with 405 Method Not Allowed.
These requests are served as a subdomain-IPFS-Gateway, i.e. a 303 See Other redirect is issued to https://CID.ipfs.gateway.tld/*. If a remote subdomain-IPFS-gateway is used for offloading, then that is where the redirect is targeted.
The OriginID is first transformed into base36 libp2p-key. If OriginID is a DNSLink that points to an IPFS path, then gateway behaves as subdomain-IPFS-gateway as described above. Otherwise, a 307 Temporary Redirect is issued to https://OriginID.ipns.gateway.tld/* where OriginID is in base36.
These requests are served as a subdomain-IPFS-Gateway. If a remote subdomain-IPFS-gateway, e.g. cf-ipfs.com, is used for offloading, then a 303 redirect is issued to https://CID.ipfs.cf-ipfs.com/*.
OriginID in this context must be a base36 libp2p-key or a DNSLink. This is the main type of URL that is directly handled by the IPNS-Link gateway. Discussed in detail below.
This type of requests may be formed when an IPNS OriginID is DNSLink'd to say dnslink.tldand the DNSLink is CNAME'd to gateway.tld. In this case, gateway first obtains the OriginID by resolving the DNSLink. Then, the behavior is same as that of https://OriginID.ipns.gateway.tld/*, described below.
307 redirect to https://gateway.tld/*
Ordered according to precedence. In the Bash implementation, this is handled by the forward function.
path=/ipfs/CID/*.AND.Method==GET: Same behavior as https://gateway.tld/ipfs/CID/* or https://www.gateway.tld/ipfs/CID/*, described above. Exit.path=/ipfs/OriginID/*: 307 redirect to https://gateway.tld/path. Exit.- Transform
OriginIDto base36 libp2p key. This is not possible if OriginID is a DNSLink that points to an IPFS path. In that case, same behavior as https://CID.ipfs.gateway.tld/* and Exit. Otherwise, proceed with the base36OriginID. - Check if
OriginIDis blocked. In case it is issue451 Unavailable For Legal Reasonsand Exit. - Access the JSON for
OriginIDfrom cache. If there is none in the cache or the cache is older than 24 hrs then:- Retrieve IPNS records for
OriginIDand read the Manifest therefrom. If timed out waiting for IPNS record, try to access it from a well-connected public gateway such asipfs.iothat has IPNS-pubsub enabled. If IPNS record is found but no Manifest, behave as a subdomain-IPFS-gateway and Exit. Otherwise: - Process Manifest as follows:
- If there is no IPNS-Link comment block in the Manifest, behave as a subdomain-IPFS-gateway and Exit. Otherwise:
- Read the
secretcorresponding toOriginIDas provided locally by the gateway maintainer. - Try to decipher the first line (ciphertext) of the comment block with a) the secret and b) the private-key. If successful, then cache the deciphered JSON against
OriginID. Otherwise, pick a trusted-gateway randomly from the Manifest and 307 redirect to https://trusted-gateway.tld/ipns/`OriginID`/path and Exit. If no such gateway is provided in the Manifest, then401 Unauthorized.
- Retrieve IPNS records for
Method==GET.AND.path=cache_path/*: Serve the content of/ipfs/cache_cid/*and Exit.- Read the Listener's multiaddress from the cached JSON and try to connect to it.
- On successful connection, put it in the peering subsystem so that the connection is maintained indefinitely and retried automatically on disconnect.
- On failure, assume cache is stale and try retrieve the Manifest. If connection still fails after that then 307 redirect to the
on_failURL and Exit. In caseon_failis empty, issue a504 Gateway Timeoutand Exit.
Method==GET.AND.path=stream_path/*: Serve the content of/ipns/stream_OriginID/*and Exit.- Setup a p2p-forward to the Listener-node for
OriginIDwith protocol/x/ipns-link/OriginID, if not setup already. Forward the web-request to Origin through that.
Web-request >> Redirect or Manifest retrieval and serve from IPFS if possible >> p2p-forward (proxy)