/* godlighty -- highly-customizable HTTP, HTTP/2, HTTPS server Copyright (C) 2021-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package meta4 import ( "encoding/hex" "encoding/xml" ) var KnownHashes = map[string]string{ "blake3-256": "BLAKE3-256", "sha-256": "SHA-256", "sha-512": "SHA-512", "shake128": "SHAKE128", "shake256": "SHAKE256", "skein-256": "Skein-256", "skein-512": "Skein-512", "streebog-256": "Streebog-256", "streebog-512": "Streebog-512", } type ForHTTP struct { Hashes map[string][]byte URLs []string Torrents []string } func Parse(fn string, data []byte) (*ForHTTP, error) { var meta Metalink err := xml.Unmarshal(data, &meta) if err != nil { return nil, err } for _, f := range meta.Files { if f.Name != fn { continue } forHTTP := ForHTTP{Hashes: make(map[string][]byte)} for _, h := range f.Hashes { name := KnownHashes[h.Type] if name == "" { continue } digest, err := hex.DecodeString(h.Hash) if err != nil { return nil, err } forHTTP.Hashes[name] = digest } for _, u := range f.URLs { forHTTP.URLs = append(forHTTP.URLs, u.URL) } for _, m := range f.MetaURLs { if m.MediaType == "torrent" { forHTTP.Torrents = append(forHTTP.Torrents, m.URL) } } return &forHTTP, nil } return nil, nil }