client.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package client
  17. import (
  18. "archive/tar"
  19. "bytes"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "io/ioutil"
  25. "mime"
  26. "mime/multipart"
  27. "net/http"
  28. "net/textproto"
  29. "os"
  30. "path/filepath"
  31. "regexp"
  32. "strconv"
  33. "strings"
  34. "github.com/ethereum/go-ethereum/swarm/api"
  35. "github.com/ethereum/go-ethereum/swarm/storage/mru"
  36. )
  37. var (
  38. DefaultGateway = "http://localhost:8500"
  39. DefaultClient = NewClient(DefaultGateway)
  40. )
  41. var (
  42. ErrUnauthorized = errors.New("unauthorized")
  43. )
  44. func NewClient(gateway string) *Client {
  45. return &Client{
  46. Gateway: gateway,
  47. }
  48. }
  49. // Client wraps interaction with a swarm HTTP gateway.
  50. type Client struct {
  51. Gateway string
  52. }
  53. // UploadRaw uploads raw data to swarm and returns the resulting hash. If toEncrypt is true it
  54. // uploads encrypted data
  55. func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool) (string, error) {
  56. if size <= 0 {
  57. return "", errors.New("data size must be greater than zero")
  58. }
  59. addr := ""
  60. if toEncrypt {
  61. addr = "encrypt"
  62. }
  63. req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/"+addr, r)
  64. if err != nil {
  65. return "", err
  66. }
  67. req.ContentLength = size
  68. res, err := http.DefaultClient.Do(req)
  69. if err != nil {
  70. return "", err
  71. }
  72. defer res.Body.Close()
  73. if res.StatusCode != http.StatusOK {
  74. return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
  75. }
  76. data, err := ioutil.ReadAll(res.Body)
  77. if err != nil {
  78. return "", err
  79. }
  80. return string(data), nil
  81. }
  82. // DownloadRaw downloads raw data from swarm and it returns a ReadCloser and a bool whether the
  83. // content was encrypted
  84. func (c *Client) DownloadRaw(hash string) (io.ReadCloser, bool, error) {
  85. uri := c.Gateway + "/bzz-raw:/" + hash
  86. res, err := http.DefaultClient.Get(uri)
  87. if err != nil {
  88. return nil, false, err
  89. }
  90. if res.StatusCode != http.StatusOK {
  91. res.Body.Close()
  92. return nil, false, fmt.Errorf("unexpected HTTP status: %s", res.Status)
  93. }
  94. isEncrypted := (res.Header.Get("X-Decrypted") == "true")
  95. return res.Body, isEncrypted, nil
  96. }
  97. // File represents a file in a swarm manifest and is used for uploading and
  98. // downloading content to and from swarm
  99. type File struct {
  100. io.ReadCloser
  101. api.ManifestEntry
  102. }
  103. // Open opens a local file which can then be passed to client.Upload to upload
  104. // it to swarm
  105. func Open(path string) (*File, error) {
  106. f, err := os.Open(path)
  107. if err != nil {
  108. return nil, err
  109. }
  110. stat, err := f.Stat()
  111. if err != nil {
  112. f.Close()
  113. return nil, err
  114. }
  115. return &File{
  116. ReadCloser: f,
  117. ManifestEntry: api.ManifestEntry{
  118. ContentType: mime.TypeByExtension(filepath.Ext(path)),
  119. Mode: int64(stat.Mode()),
  120. Size: stat.Size(),
  121. ModTime: stat.ModTime(),
  122. },
  123. }, nil
  124. }
  125. // Upload uploads a file to swarm and either adds it to an existing manifest
  126. // (if the manifest argument is non-empty) or creates a new manifest containing
  127. // the file, returning the resulting manifest hash (the file will then be
  128. // available at bzz:/<hash>/<path>)
  129. func (c *Client) Upload(file *File, manifest string, toEncrypt bool) (string, error) {
  130. if file.Size <= 0 {
  131. return "", errors.New("file size must be greater than zero")
  132. }
  133. return c.TarUpload(manifest, &FileUploader{file}, "", toEncrypt)
  134. }
  135. // Download downloads a file with the given path from the swarm manifest with
  136. // the given hash (i.e. it gets bzz:/<hash>/<path>)
  137. func (c *Client) Download(hash, path string) (*File, error) {
  138. uri := c.Gateway + "/bzz:/" + hash + "/" + path
  139. res, err := http.DefaultClient.Get(uri)
  140. if err != nil {
  141. return nil, err
  142. }
  143. if res.StatusCode != http.StatusOK {
  144. res.Body.Close()
  145. return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
  146. }
  147. return &File{
  148. ReadCloser: res.Body,
  149. ManifestEntry: api.ManifestEntry{
  150. ContentType: res.Header.Get("Content-Type"),
  151. Size: res.ContentLength,
  152. },
  153. }, nil
  154. }
  155. // UploadDirectory uploads a directory tree to swarm and either adds the files
  156. // to an existing manifest (if the manifest argument is non-empty) or creates a
  157. // new manifest, returning the resulting manifest hash (files from the
  158. // directory will then be available at bzz:/<hash>/path/to/file), with
  159. // the file specified in defaultPath being uploaded to the root of the manifest
  160. // (i.e. bzz:/<hash>/)
  161. func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bool) (string, error) {
  162. stat, err := os.Stat(dir)
  163. if err != nil {
  164. return "", err
  165. } else if !stat.IsDir() {
  166. return "", fmt.Errorf("not a directory: %s", dir)
  167. }
  168. if defaultPath != "" {
  169. if _, err := os.Stat(filepath.Join(dir, defaultPath)); err != nil {
  170. if os.IsNotExist(err) {
  171. return "", fmt.Errorf("the default path %q was not found in the upload directory %q", defaultPath, dir)
  172. }
  173. return "", fmt.Errorf("default path: %v", err)
  174. }
  175. }
  176. return c.TarUpload(manifest, &DirectoryUploader{dir}, defaultPath, toEncrypt)
  177. }
  178. // DownloadDirectory downloads the files contained in a swarm manifest under
  179. // the given path into a local directory (existing files will be overwritten)
  180. func (c *Client) DownloadDirectory(hash, path, destDir, credentials string) error {
  181. stat, err := os.Stat(destDir)
  182. if err != nil {
  183. return err
  184. } else if !stat.IsDir() {
  185. return fmt.Errorf("not a directory: %s", destDir)
  186. }
  187. uri := c.Gateway + "/bzz:/" + hash + "/" + path
  188. req, err := http.NewRequest("GET", uri, nil)
  189. if err != nil {
  190. return err
  191. }
  192. if credentials != "" {
  193. req.SetBasicAuth("", credentials)
  194. }
  195. req.Header.Set("Accept", "application/x-tar")
  196. res, err := http.DefaultClient.Do(req)
  197. if err != nil {
  198. return err
  199. }
  200. defer res.Body.Close()
  201. switch res.StatusCode {
  202. case http.StatusOK:
  203. case http.StatusUnauthorized:
  204. return ErrUnauthorized
  205. default:
  206. return fmt.Errorf("unexpected HTTP status: %s", res.Status)
  207. }
  208. tr := tar.NewReader(res.Body)
  209. for {
  210. hdr, err := tr.Next()
  211. if err == io.EOF {
  212. return nil
  213. } else if err != nil {
  214. return err
  215. }
  216. // ignore the default path file
  217. if hdr.Name == "" {
  218. continue
  219. }
  220. dstPath := filepath.Join(destDir, filepath.Clean(strings.TrimPrefix(hdr.Name, path)))
  221. if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
  222. return err
  223. }
  224. var mode os.FileMode = 0644
  225. if hdr.Mode > 0 {
  226. mode = os.FileMode(hdr.Mode)
  227. }
  228. dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
  229. if err != nil {
  230. return err
  231. }
  232. n, err := io.Copy(dst, tr)
  233. dst.Close()
  234. if err != nil {
  235. return err
  236. } else if n != hdr.Size {
  237. return fmt.Errorf("expected %s to be %d bytes but got %d", hdr.Name, hdr.Size, n)
  238. }
  239. }
  240. }
  241. // DownloadFile downloads a single file into the destination directory
  242. // if the manifest entry does not specify a file name - it will fallback
  243. // to the hash of the file as a filename
  244. func (c *Client) DownloadFile(hash, path, dest, credentials string) error {
  245. hasDestinationFilename := false
  246. if stat, err := os.Stat(dest); err == nil {
  247. hasDestinationFilename = !stat.IsDir()
  248. } else {
  249. if os.IsNotExist(err) {
  250. // does not exist - should be created
  251. hasDestinationFilename = true
  252. } else {
  253. return fmt.Errorf("could not stat path: %v", err)
  254. }
  255. }
  256. manifestList, err := c.List(hash, path, credentials)
  257. if err != nil {
  258. return err
  259. }
  260. switch len(manifestList.Entries) {
  261. case 0:
  262. return fmt.Errorf("could not find path requested at manifest address. make sure the path you've specified is correct")
  263. case 1:
  264. //continue
  265. default:
  266. return fmt.Errorf("got too many matches for this path")
  267. }
  268. uri := c.Gateway + "/bzz:/" + hash + "/" + path
  269. req, err := http.NewRequest("GET", uri, nil)
  270. if err != nil {
  271. return err
  272. }
  273. if credentials != "" {
  274. req.SetBasicAuth("", credentials)
  275. }
  276. res, err := http.DefaultClient.Do(req)
  277. if err != nil {
  278. return err
  279. }
  280. defer res.Body.Close()
  281. switch res.StatusCode {
  282. case http.StatusOK:
  283. case http.StatusUnauthorized:
  284. return ErrUnauthorized
  285. default:
  286. return fmt.Errorf("unexpected HTTP status: expected 200 OK, got %d", res.StatusCode)
  287. }
  288. filename := ""
  289. if hasDestinationFilename {
  290. filename = dest
  291. } else {
  292. // try to assert
  293. re := regexp.MustCompile("[^/]+$") //everything after last slash
  294. if results := re.FindAllString(path, -1); len(results) > 0 {
  295. filename = results[len(results)-1]
  296. } else {
  297. if entry := manifestList.Entries[0]; entry.Path != "" && entry.Path != "/" {
  298. filename = entry.Path
  299. } else {
  300. // assume hash as name if there's nothing from the command line
  301. filename = hash
  302. }
  303. }
  304. filename = filepath.Join(dest, filename)
  305. }
  306. filePath, err := filepath.Abs(filename)
  307. if err != nil {
  308. return err
  309. }
  310. if err := os.MkdirAll(filepath.Dir(filePath), 0777); err != nil {
  311. return err
  312. }
  313. dst, err := os.Create(filename)
  314. if err != nil {
  315. return err
  316. }
  317. defer dst.Close()
  318. _, err = io.Copy(dst, res.Body)
  319. return err
  320. }
  321. // UploadManifest uploads the given manifest to swarm
  322. func (c *Client) UploadManifest(m *api.Manifest, toEncrypt bool) (string, error) {
  323. data, err := json.Marshal(m)
  324. if err != nil {
  325. return "", err
  326. }
  327. return c.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
  328. }
  329. // DownloadManifest downloads a swarm manifest
  330. func (c *Client) DownloadManifest(hash string) (*api.Manifest, bool, error) {
  331. res, isEncrypted, err := c.DownloadRaw(hash)
  332. if err != nil {
  333. return nil, isEncrypted, err
  334. }
  335. defer res.Close()
  336. var manifest api.Manifest
  337. if err := json.NewDecoder(res).Decode(&manifest); err != nil {
  338. return nil, isEncrypted, err
  339. }
  340. return &manifest, isEncrypted, nil
  341. }
  342. // List list files in a swarm manifest which have the given prefix, grouping
  343. // common prefixes using "/" as a delimiter.
  344. //
  345. // For example, if the manifest represents the following directory structure:
  346. //
  347. // file1.txt
  348. // file2.txt
  349. // dir1/file3.txt
  350. // dir1/dir2/file4.txt
  351. //
  352. // Then:
  353. //
  354. // - a prefix of "" would return [dir1/, file1.txt, file2.txt]
  355. // - a prefix of "file" would return [file1.txt, file2.txt]
  356. // - a prefix of "dir1/" would return [dir1/dir2/, dir1/file3.txt]
  357. //
  358. // where entries ending with "/" are common prefixes.
  359. func (c *Client) List(hash, prefix, credentials string) (*api.ManifestList, error) {
  360. req, err := http.NewRequest(http.MethodGet, c.Gateway+"/bzz-list:/"+hash+"/"+prefix, nil)
  361. if err != nil {
  362. return nil, err
  363. }
  364. if credentials != "" {
  365. req.SetBasicAuth("", credentials)
  366. }
  367. res, err := http.DefaultClient.Do(req)
  368. if err != nil {
  369. return nil, err
  370. }
  371. defer res.Body.Close()
  372. switch res.StatusCode {
  373. case http.StatusOK:
  374. case http.StatusUnauthorized:
  375. return nil, ErrUnauthorized
  376. default:
  377. return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
  378. }
  379. var list api.ManifestList
  380. if err := json.NewDecoder(res.Body).Decode(&list); err != nil {
  381. return nil, err
  382. }
  383. return &list, nil
  384. }
  385. // Uploader uploads files to swarm using a provided UploadFn
  386. type Uploader interface {
  387. Upload(UploadFn) error
  388. }
  389. type UploaderFunc func(UploadFn) error
  390. func (u UploaderFunc) Upload(upload UploadFn) error {
  391. return u(upload)
  392. }
  393. // DirectoryUploader uploads all files in a directory, optionally uploading
  394. // a file to the default path
  395. type DirectoryUploader struct {
  396. Dir string
  397. }
  398. // Upload performs the upload of the directory and default path
  399. func (d *DirectoryUploader) Upload(upload UploadFn) error {
  400. return filepath.Walk(d.Dir, func(path string, f os.FileInfo, err error) error {
  401. if err != nil {
  402. return err
  403. }
  404. if f.IsDir() {
  405. return nil
  406. }
  407. file, err := Open(path)
  408. if err != nil {
  409. return err
  410. }
  411. relPath, err := filepath.Rel(d.Dir, path)
  412. if err != nil {
  413. return err
  414. }
  415. file.Path = filepath.ToSlash(relPath)
  416. return upload(file)
  417. })
  418. }
  419. // FileUploader uploads a single file
  420. type FileUploader struct {
  421. File *File
  422. }
  423. // Upload performs the upload of the file
  424. func (f *FileUploader) Upload(upload UploadFn) error {
  425. return upload(f.File)
  426. }
  427. // UploadFn is the type of function passed to an Uploader to perform the upload
  428. // of a single file (for example, a directory uploader would call a provided
  429. // UploadFn for each file in the directory tree)
  430. type UploadFn func(file *File) error
  431. // TarUpload uses the given Uploader to upload files to swarm as a tar stream,
  432. // returning the resulting manifest hash
  433. func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, toEncrypt bool) (string, error) {
  434. reqR, reqW := io.Pipe()
  435. defer reqR.Close()
  436. addr := hash
  437. // If there is a hash already (a manifest), then that manifest will determine if the upload has
  438. // to be encrypted or not. If there is no manifest then the toEncrypt parameter decides if
  439. // there is encryption or not.
  440. if hash == "" && toEncrypt {
  441. // This is the built-in address for the encrypted upload endpoint
  442. addr = "encrypt"
  443. }
  444. req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+addr, reqR)
  445. if err != nil {
  446. return "", err
  447. }
  448. req.Header.Set("Content-Type", "application/x-tar")
  449. if defaultPath != "" {
  450. q := req.URL.Query()
  451. q.Set("defaultpath", defaultPath)
  452. req.URL.RawQuery = q.Encode()
  453. }
  454. // use 'Expect: 100-continue' so we don't send the request body if
  455. // the server refuses the request
  456. req.Header.Set("Expect", "100-continue")
  457. tw := tar.NewWriter(reqW)
  458. // define an UploadFn which adds files to the tar stream
  459. uploadFn := func(file *File) error {
  460. hdr := &tar.Header{
  461. Name: file.Path,
  462. Mode: file.Mode,
  463. Size: file.Size,
  464. ModTime: file.ModTime,
  465. Xattrs: map[string]string{
  466. "user.swarm.content-type": file.ContentType,
  467. },
  468. }
  469. if err := tw.WriteHeader(hdr); err != nil {
  470. return err
  471. }
  472. _, err = io.Copy(tw, file)
  473. return err
  474. }
  475. // run the upload in a goroutine so we can send the request headers and
  476. // wait for a '100 Continue' response before sending the tar stream
  477. go func() {
  478. err := uploader.Upload(uploadFn)
  479. if err == nil {
  480. err = tw.Close()
  481. }
  482. reqW.CloseWithError(err)
  483. }()
  484. res, err := http.DefaultClient.Do(req)
  485. if err != nil {
  486. return "", err
  487. }
  488. defer res.Body.Close()
  489. if res.StatusCode != http.StatusOK {
  490. return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
  491. }
  492. data, err := ioutil.ReadAll(res.Body)
  493. if err != nil {
  494. return "", err
  495. }
  496. return string(data), nil
  497. }
  498. // MultipartUpload uses the given Uploader to upload files to swarm as a
  499. // multipart form, returning the resulting manifest hash
  500. func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error) {
  501. reqR, reqW := io.Pipe()
  502. defer reqR.Close()
  503. req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+hash, reqR)
  504. if err != nil {
  505. return "", err
  506. }
  507. // use 'Expect: 100-continue' so we don't send the request body if
  508. // the server refuses the request
  509. req.Header.Set("Expect", "100-continue")
  510. mw := multipart.NewWriter(reqW)
  511. req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", mw.Boundary()))
  512. // define an UploadFn which adds files to the multipart form
  513. uploadFn := func(file *File) error {
  514. hdr := make(textproto.MIMEHeader)
  515. hdr.Set("Content-Disposition", fmt.Sprintf("form-data; name=%q", file.Path))
  516. hdr.Set("Content-Type", file.ContentType)
  517. hdr.Set("Content-Length", strconv.FormatInt(file.Size, 10))
  518. w, err := mw.CreatePart(hdr)
  519. if err != nil {
  520. return err
  521. }
  522. _, err = io.Copy(w, file)
  523. return err
  524. }
  525. // run the upload in a goroutine so we can send the request headers and
  526. // wait for a '100 Continue' response before sending the multipart form
  527. go func() {
  528. err := uploader.Upload(uploadFn)
  529. if err == nil {
  530. err = mw.Close()
  531. }
  532. reqW.CloseWithError(err)
  533. }()
  534. res, err := http.DefaultClient.Do(req)
  535. if err != nil {
  536. return "", err
  537. }
  538. defer res.Body.Close()
  539. if res.StatusCode != http.StatusOK {
  540. return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
  541. }
  542. data, err := ioutil.ReadAll(res.Body)
  543. if err != nil {
  544. return "", err
  545. }
  546. return string(data), nil
  547. }
  548. // CreateResource creates a Mutable Resource with the given name and frequency, initializing it with the provided
  549. // data. Data is interpreted as multihash or not depending on the multihash parameter.
  550. // startTime=0 means "now"
  551. // Returns the resulting Mutable Resource manifest address that you can use to include in an ENS Resolver (setContent)
  552. // or reference future updates (Client.UpdateResource)
  553. func (c *Client) CreateResource(request *mru.Request) (string, error) {
  554. responseStream, err := c.updateResource(request)
  555. if err != nil {
  556. return "", err
  557. }
  558. defer responseStream.Close()
  559. body, err := ioutil.ReadAll(responseStream)
  560. if err != nil {
  561. return "", err
  562. }
  563. var manifestAddress string
  564. if err = json.Unmarshal(body, &manifestAddress); err != nil {
  565. return "", err
  566. }
  567. return manifestAddress, nil
  568. }
  569. // UpdateResource allows you to set a new version of your content
  570. func (c *Client) UpdateResource(request *mru.Request) error {
  571. _, err := c.updateResource(request)
  572. return err
  573. }
  574. func (c *Client) updateResource(request *mru.Request) (io.ReadCloser, error) {
  575. body, err := request.MarshalJSON()
  576. if err != nil {
  577. return nil, err
  578. }
  579. req, err := http.NewRequest("POST", c.Gateway+"/bzz-resource:/", bytes.NewBuffer(body))
  580. if err != nil {
  581. return nil, err
  582. }
  583. res, err := http.DefaultClient.Do(req)
  584. if err != nil {
  585. return nil, err
  586. }
  587. return res.Body, nil
  588. }
  589. // GetResource returns a byte stream with the raw content of the resource
  590. // manifestAddressOrDomain is the address you obtained in CreateResource or an ENS domain whose Resolver
  591. // points to that address
  592. func (c *Client) GetResource(manifestAddressOrDomain string) (io.ReadCloser, error) {
  593. res, err := http.Get(c.Gateway + "/bzz-resource:/" + manifestAddressOrDomain)
  594. if err != nil {
  595. return nil, err
  596. }
  597. return res.Body, nil
  598. }
  599. // GetResourceMetadata returns a structure that describes the Mutable Resource
  600. // manifestAddressOrDomain is the address you obtained in CreateResource or an ENS domain whose Resolver
  601. // points to that address
  602. func (c *Client) GetResourceMetadata(manifestAddressOrDomain string) (*mru.Request, error) {
  603. responseStream, err := c.GetResource(manifestAddressOrDomain + "/meta")
  604. if err != nil {
  605. return nil, err
  606. }
  607. defer responseStream.Close()
  608. body, err := ioutil.ReadAll(responseStream)
  609. if err != nil {
  610. return nil, err
  611. }
  612. var metadata mru.Request
  613. if err := metadata.UnmarshalJSON(body); err != nil {
  614. return nil, err
  615. }
  616. return &metadata, nil
  617. }