mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
amp package.
This package contains a CacheURL function that modifies a URL to be accessed through an AMP cache, and the "AMP armor" data encoding scheme for encoding data into the AMP subset of HTML.
This commit is contained in:
parent
0f34a7778f
commit
c9e0dd287f
8 changed files with 1223 additions and 0 deletions
44
common/amp/path.go
Normal file
44
common/amp/path.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package amp
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EncodePath encodes data in a way that is suitable for the suffix of an AMP
|
||||
// cache URL.
|
||||
func EncodePath(data []byte) string {
|
||||
var cacheBreaker [9]byte
|
||||
_, err := rand.Read(cacheBreaker[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
b64 := base64.RawURLEncoding.EncodeToString
|
||||
return "0" + b64(cacheBreaker[:]) + "/" + b64(data)
|
||||
}
|
||||
|
||||
// DecodePath decodes data from a path suffix as encoded by EncodePath. The path
|
||||
// must have already been trimmed of any directory prefix (as might be present
|
||||
// in, e.g., an HTTP request). That is, the first character of path should be
|
||||
// the "0" message format indicator.
|
||||
func DecodePath(path string) ([]byte, error) {
|
||||
if len(path) < 1 {
|
||||
return nil, fmt.Errorf("missing format indicator")
|
||||
}
|
||||
version := path[0]
|
||||
rest := path[1:]
|
||||
switch version {
|
||||
case '0':
|
||||
// Ignore everything else up to and including the final slash
|
||||
// (there must be at least one slash).
|
||||
i := strings.LastIndexByte(rest, '/')
|
||||
if i == -1 {
|
||||
return nil, fmt.Errorf("missing data")
|
||||
}
|
||||
return base64.RawURLEncoding.DecodeString(rest[i+1:])
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown format indicator %q", version)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue