client.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. "context"
  21. "encoding/json"
  22. "errors"
  23. "fmt"
  24. "io"
  25. "io/ioutil"
  26. "mime/multipart"
  27. "net/http"
  28. "net/http/httptrace"
  29. "net/textproto"
  30. "net/url"
  31. "os"
  32. "path/filepath"
  33. "regexp"
  34. "strconv"
  35. "strings"
  36. "time"
  37. "github.com/ethereum/go-ethereum/log"
  38. "github.com/ethereum/go-ethereum/metrics"
  39. "github.com/ethereum/go-ethereum/swarm/api"
  40. "github.com/ethereum/go-ethereum/swarm/spancontext"
  41. "github.com/ethereum/go-ethereum/swarm/storage/feed"
  42. "github.com/pborman/uuid"
  43. )
  44. var (
  45. DefaultGateway = "http://localhost:8500"
  46. DefaultClient = NewClient(DefaultGateway)
  47. )
  48. var (
  49. ErrUnauthorized = errors.New("unauthorized")
  50. )
  51. func NewClient(gateway string) *Client {
  52. return &Client{
  53. Gateway: gateway,
  54. }
  55. }
  56. // Client wraps interaction with a swarm HTTP gateway.
  57. type Client struct {
  58. Gateway string
  59. }
  60. // UploadRaw uploads raw data to swarm and returns the resulting hash. If toEncrypt is true it
  61. // uploads encrypted data
  62. func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool) (string, error) {
  63. if size <= 0 {
  64. return "", errors.New("data size must be greater than zero")
  65. }
  66. addr := ""
  67. if toEncrypt {
  68. addr = "encrypt"
  69. }
  70. req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/"+addr, r)
  71. if err != nil {
  72. return "", err
  73. }
  74. req.ContentLength = size
  75. res, err := http.DefaultClient.Do(req)
  76. if err != nil {
  77. return "", err
  78. }
  79. defer res.Body.Close()
  80. if res.StatusCode != http.StatusOK {
  81. return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
  82. }
  83. data, err := ioutil.ReadAll(res.Body)
  84. if err != nil {
  85. return "", err
  86. }
  87. return string(data), nil
  88. }
  89. // DownloadRaw downloads raw data from swarm and it returns a ReadCloser and a bool whether the
  90. // content was encrypted
  91. func (c *Client) DownloadRaw(hash string) (io.ReadCloser, bool, error) {
  92. uri := c.Gateway + "/bzz-raw:/" + hash
  93. res, err := http.DefaultClient.Get(uri)
  94. if err != nil {
  95. return nil, false, err
  96. }
  97. if res.StatusCode != http.StatusOK {
  98. res.Body.Close()
  99. return nil, false, fmt.Errorf("unexpected HTTP status: %s", res.Status)
  100. }
  101. isEncrypted := (res.Header.Get("X-Decrypted") == "true")
  102. return res.Body, isEncrypted, nil
  103. }
  104. // File represents a file in a swarm manifest and is used for uploading and
  105. // downloading content to and from swarm
  106. type File struct {
  107. io.ReadCloser
  108. api.ManifestEntry
  109. }
  110. // Open opens a local file which can then be passed to client.Upload to upload
  111. // it to swarm
  112. func Open(path string) (*File, error) {
  113. f, err := os.Open(path)
  114. if err != nil {
  115. return nil, err
  116. }
  117. stat, err := f.Stat()
  118. if err != nil {
  119. f.Close()
  120. return nil, err
  121. }
  122. contentType, err := api.DetectContentType(f.Name(), f)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return &File{
  127. ReadCloser: f,
  128. ManifestEntry: api.ManifestEntry{
  129. ContentType: contentType,
  130. Mode: int64(stat.Mode()),
  131. Size: stat.Size(),
  132. ModTime: stat.ModTime(),
  133. },
  134. }, nil
  135. }
  136. // Upload uploads a file to swarm and either adds it to an existing manifest
  137. // (if the manifest argument is non-empty) or creates a new manifest containing
  138. // the file, returning the resulting manifest hash (the file will then be
  139. // available at bzz:/<hash>/<path>)
  140. func (c *Client) Upload(file *File, manifest string, toEncrypt bool) (string, error) {
  141. if file.Size <= 0 {
  142. return "", errors.New("file size must be greater than zero")
  143. }
  144. return c.TarUpload(manifest, &FileUploader{file}, "", toEncrypt)
  145. }
  146. // Download downloads a file with the given path from the swarm manifest with
  147. // the given hash (i.e. it gets bzz:/<hash>/<path>)
  148. func (c *Client) Download(hash, path string) (*File, error) {
  149. uri := c.Gateway + "/bzz:/" + hash + "/" + path
  150. res, err := http.DefaultClient.Get(uri)
  151. if err != nil {
  152. return nil, err
  153. }
  154. if res.StatusCode != http.StatusOK {
  155. res.Body.Close()
  156. return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
  157. }
  158. return &File{
  159. ReadCloser: res.Body,
  160. ManifestEntry: api.ManifestEntry{
  161. ContentType: res.Header.Get("Content-Type"),
  162. Size: res.ContentLength,
  163. },
  164. }, nil
  165. }
  166. // UploadDirectory uploads a directory tree to swarm and either adds the files
  167. // to an existing manifest (if the manifest argument is non-empty) or creates a
  168. // new manifest, returning the resulting manifest hash (files from the
  169. // directory will then be available at bzz:/<hash>/path/to/file), with
  170. // the file specified in defaultPath being uploaded to the root of the manifest
  171. // (i.e. bzz:/<hash>/)
  172. func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bool) (string, error) {
  173. stat, err := os.Stat(dir)
  174. if err != nil {
  175. return "", err
  176. } else if !stat.IsDir() {
  177. return "", fmt.Errorf("not a directory: %s", dir)
  178. }
  179. if defaultPath != "" {
  180. if _, err := os.Stat(filepath.Join(dir, defaultPath)); err != nil {
  181. if os.IsNotExist(err) {
  182. return "", fmt.Errorf("the default path %q was not found in the upload directory %q", defaultPath, dir)
  183. }
  184. return "", fmt.Errorf("default path: %v", err)
  185. }
  186. }
  187. return c.TarUpload(manifest, &DirectoryUploader{dir}, defaultPath, toEncrypt)
  188. }
  189. // DownloadDirectory downloads the files contained in a swarm manifest under
  190. // the given path into a local directory (existing files will be overwritten)
  191. func (c *Client) DownloadDirectory(hash, path, destDir, credentials string) error {
  192. stat, err := os.Stat(destDir)
  193. if err != nil {
  194. return err
  195. } else if !stat.IsDir() {
  196. return fmt.Errorf("not a directory: %s", destDir)
  197. }
  198. uri := c.Gateway + "/bzz:/" + hash + "/" + path
  199. req, err := http.NewRequest("GET", uri, nil)
  200. if err != nil {
  201. return err
  202. }
  203. if credentials != "" {
  204. req.SetBasicAuth("", credentials)
  205. }
  206. req.Header.Set("Accept", "application/x-tar")
  207. res, err := http.DefaultClient.Do(req)
  208. if err != nil {
  209. return err
  210. }
  211. defer res.Body.Close()
  212. switch res.StatusCode {
  213. case http.StatusOK:
  214. case http.StatusUnauthorized:
  215. return ErrUnauthorized
  216. default:
  217. return fmt.Errorf("unexpected HTTP status: %s", res.Status)
  218. }
  219. tr := tar.NewReader(res.Body)
  220. for {
  221. hdr, err := tr.Next()
  222. if err == io.EOF {
  223. return nil
  224. } else if err != nil {
  225. return err
  226. }
  227. // ignore the default path file
  228. if hdr.Name == "" {
  229. continue
  230. }
  231. dstPath := filepath.Join(destDir, filepath.Clean(strings.TrimPrefix(hdr.Name, path)))
  232. if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
  233. return err
  234. }
  235. var mode os.FileMode = 0644
  236. if hdr.Mode > 0 {
  237. mode = os.FileMode(hdr.Mode)
  238. }
  239. dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
  240. if err != nil {
  241. return err
  242. }
  243. n, err := io.Copy(dst, tr)
  244. dst.Close()
  245. if err != nil {
  246. return err
  247. } else if n != hdr.Size {
  248. return fmt.Errorf("expected %s to be %d bytes but got %d", hdr.Name, hdr.Size, n)
  249. }
  250. }
  251. }
  252. // DownloadFile downloads a single file into the destination directory
  253. // if the manifest entry does not specify a file name - it will fallback
  254. // to the hash of the file as a filename
  255. func (c *Client) DownloadFile(hash, path, dest, credentials string) error {
  256. hasDestinationFilename := false
  257. if stat, err := os.Stat(dest); err == nil {
  258. hasDestinationFilename = !stat.IsDir()
  259. } else {
  260. if os.IsNotExist(err) {
  261. // does not exist - should be created
  262. hasDestinationFilename = true
  263. } else {
  264. return fmt.Errorf("could not stat path: %v", err)
  265. }
  266. }
  267. manifestList, err := c.List(hash, path, credentials)
  268. if err != nil {
  269. return err
  270. }
  271. switch len(manifestList.Entries) {
  272. case 0:
  273. return fmt.Errorf("could not find path requested at manifest address. make sure the path you've specified is correct")
  274. case 1:
  275. //continue
  276. default:
  277. return fmt.Errorf("got too many matches for this path")
  278. }
  279. uri := c.Gateway + "/bzz:/" + hash + "/" + path
  280. req, err := http.NewRequest("GET", uri, nil)
  281. if err != nil {
  282. return err
  283. }
  284. if credentials != "" {
  285. req.SetBasicAuth("", credentials)
  286. }
  287. res, err := http.DefaultClient.Do(req)
  288. if err != nil {
  289. return err
  290. }
  291. defer res.Body.Close()
  292. switch res.StatusCode {
  293. case http.StatusOK:
  294. case http.StatusUnauthorized:
  295. return ErrUnauthorized
  296. default:
  297. return fmt.Errorf("unexpected HTTP status: expected 200 OK, got %d", res.StatusCode)
  298. }
  299. filename := ""
  300. if hasDestinationFilename {
  301. filename = dest
  302. } else {
  303. // try to assert
  304. re := regexp.MustCompile("[^/]+$") //everything after last slash
  305. if results := re.FindAllString(path, -1); len(results) > 0 {
  306. filename = results[len(results)-1]
  307. } else {
  308. if entry := manifestList.Entries[0]; entry.Path != "" && entry.Path != "/" {
  309. filename = entry.Path
  310. } else {
  311. // assume hash as name if there's nothing from the command line
  312. filename = hash
  313. }
  314. }
  315. filename = filepath.Join(dest, filename)
  316. }
  317. filePath, err := filepath.Abs(filename)
  318. if err != nil {
  319. return err
  320. }
  321. if err := os.MkdirAll(filepath.Dir(filePath), 0777); err != nil {
  322. return err
  323. }
  324. dst, err := os.Create(filename)
  325. if err != nil {
  326. return err
  327. }
  328. defer dst.Close()
  329. _, err = io.Copy(dst, res.Body)
  330. return err
  331. }
  332. // UploadManifest uploads the given manifest to swarm
  333. func (c *Client) UploadManifest(m *api.Manifest, toEncrypt bool) (string, error) {
  334. data, err := json.Marshal(m)
  335. if err != nil {
  336. return "", err
  337. }
  338. return c.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
  339. }
  340. // DownloadManifest downloads a swarm manifest
  341. func (c *Client) DownloadManifest(hash string) (*api.Manifest, bool, error) {
  342. res, isEncrypted, err := c.DownloadRaw(hash)
  343. if err != nil {
  344. return nil, isEncrypted, err
  345. }
  346. defer res.Close()
  347. var manifest api.Manifest
  348. if err := json.NewDecoder(res).Decode(&manifest); err != nil {
  349. return nil, isEncrypted, err
  350. }
  351. return &manifest, isEncrypted, nil
  352. }
  353. // List list files in a swarm manifest which have the given prefix, grouping
  354. // common prefixes using "/" as a delimiter.
  355. //
  356. // For example, if the manifest represents the following directory structure:
  357. //
  358. // file1.txt
  359. // file2.txt
  360. // dir1/file3.txt
  361. // dir1/dir2/file4.txt
  362. //
  363. // Then:
  364. //
  365. // - a prefix of "" would return [dir1/, file1.txt, file2.txt]
  366. // - a prefix of "file" would return [file1.txt, file2.txt]
  367. // - a prefix of "dir1/" would return [dir1/dir2/, dir1/file3.txt]
  368. //
  369. // where entries ending with "/" are common prefixes.
  370. func (c *Client) List(hash, prefix, credentials string) (*api.ManifestList, error) {
  371. req, err := http.NewRequest(http.MethodGet, c.Gateway+"/bzz-list:/"+hash+"/"+prefix, nil)
  372. if err != nil {
  373. return nil, err
  374. }
  375. if credentials != "" {
  376. req.SetBasicAuth("", credentials)
  377. }
  378. res, err := http.DefaultClient.Do(req)
  379. if err != nil {
  380. return nil, err
  381. }
  382. defer res.Body.Close()
  383. switch res.StatusCode {
  384. case http.StatusOK:
  385. case http.StatusUnauthorized:
  386. return nil, ErrUnauthorized
  387. default:
  388. return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
  389. }
  390. var list api.ManifestList
  391. if err := json.NewDecoder(res.Body).Decode(&list); err != nil {
  392. return nil, err
  393. }
  394. return &list, nil
  395. }
  396. // Uploader uploads files to swarm using a provided UploadFn
  397. type Uploader interface {
  398. Upload(UploadFn) error
  399. }
  400. type UploaderFunc func(UploadFn) error
  401. func (u UploaderFunc) Upload(upload UploadFn) error {
  402. return u(upload)
  403. }
  404. // DirectoryUploader uploads all files in a directory, optionally uploading
  405. // a file to the default path
  406. type DirectoryUploader struct {
  407. Dir string
  408. }
  409. // Upload performs the upload of the directory and default path
  410. func (d *DirectoryUploader) Upload(upload UploadFn) error {
  411. return filepath.Walk(d.Dir, func(path string, f os.FileInfo, err error) error {
  412. if err != nil {
  413. return err
  414. }
  415. if f.IsDir() {
  416. return nil
  417. }
  418. file, err := Open(path)
  419. if err != nil {
  420. return err
  421. }
  422. relPath, err := filepath.Rel(d.Dir, path)
  423. if err != nil {
  424. return err
  425. }
  426. file.Path = filepath.ToSlash(relPath)
  427. return upload(file)
  428. })
  429. }
  430. // FileUploader uploads a single file
  431. type FileUploader struct {
  432. File *File
  433. }
  434. // Upload performs the upload of the file
  435. func (f *FileUploader) Upload(upload UploadFn) error {
  436. return upload(f.File)
  437. }
  438. // UploadFn is the type of function passed to an Uploader to perform the upload
  439. // of a single file (for example, a directory uploader would call a provided
  440. // UploadFn for each file in the directory tree)
  441. type UploadFn func(file *File) error
  442. // TarUpload uses the given Uploader to upload files to swarm as a tar stream,
  443. // returning the resulting manifest hash
  444. func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, toEncrypt bool) (string, error) {
  445. ctx, sp := spancontext.StartSpan(context.Background(), "api.client.tarupload")
  446. defer sp.Finish()
  447. var tn time.Time
  448. reqR, reqW := io.Pipe()
  449. defer reqR.Close()
  450. addr := hash
  451. // If there is a hash already (a manifest), then that manifest will determine if the upload has
  452. // to be encrypted or not. If there is no manifest then the toEncrypt parameter decides if
  453. // there is encryption or not.
  454. if hash == "" && toEncrypt {
  455. // This is the built-in address for the encrypted upload endpoint
  456. addr = "encrypt"
  457. }
  458. req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+addr, reqR)
  459. if err != nil {
  460. return "", err
  461. }
  462. trace := GetClientTrace("swarm api client - upload tar", "api.client.uploadtar", uuid.New()[:8], &tn)
  463. req = req.WithContext(httptrace.WithClientTrace(ctx, trace))
  464. transport := http.DefaultTransport
  465. req.Header.Set("Content-Type", "application/x-tar")
  466. if defaultPath != "" {
  467. q := req.URL.Query()
  468. q.Set("defaultpath", defaultPath)
  469. req.URL.RawQuery = q.Encode()
  470. }
  471. // use 'Expect: 100-continue' so we don't send the request body if
  472. // the server refuses the request
  473. req.Header.Set("Expect", "100-continue")
  474. tw := tar.NewWriter(reqW)
  475. // define an UploadFn which adds files to the tar stream
  476. uploadFn := func(file *File) error {
  477. hdr := &tar.Header{
  478. Name: file.Path,
  479. Mode: file.Mode,
  480. Size: file.Size,
  481. ModTime: file.ModTime,
  482. Xattrs: map[string]string{
  483. "user.swarm.content-type": file.ContentType,
  484. },
  485. }
  486. if err := tw.WriteHeader(hdr); err != nil {
  487. return err
  488. }
  489. _, err = io.Copy(tw, file)
  490. return err
  491. }
  492. // run the upload in a goroutine so we can send the request headers and
  493. // wait for a '100 Continue' response before sending the tar stream
  494. go func() {
  495. err := uploader.Upload(uploadFn)
  496. if err == nil {
  497. err = tw.Close()
  498. }
  499. reqW.CloseWithError(err)
  500. }()
  501. tn = time.Now()
  502. res, err := transport.RoundTrip(req)
  503. if err != nil {
  504. return "", err
  505. }
  506. defer res.Body.Close()
  507. if res.StatusCode != http.StatusOK {
  508. return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
  509. }
  510. data, err := ioutil.ReadAll(res.Body)
  511. if err != nil {
  512. return "", err
  513. }
  514. return string(data), nil
  515. }
  516. // MultipartUpload uses the given Uploader to upload files to swarm as a
  517. // multipart form, returning the resulting manifest hash
  518. func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error) {
  519. reqR, reqW := io.Pipe()
  520. defer reqR.Close()
  521. req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+hash, reqR)
  522. if err != nil {
  523. return "", err
  524. }
  525. // use 'Expect: 100-continue' so we don't send the request body if
  526. // the server refuses the request
  527. req.Header.Set("Expect", "100-continue")
  528. mw := multipart.NewWriter(reqW)
  529. req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", mw.Boundary()))
  530. // define an UploadFn which adds files to the multipart form
  531. uploadFn := func(file *File) error {
  532. hdr := make(textproto.MIMEHeader)
  533. hdr.Set("Content-Disposition", fmt.Sprintf("form-data; name=%q", file.Path))
  534. hdr.Set("Content-Type", file.ContentType)
  535. hdr.Set("Content-Length", strconv.FormatInt(file.Size, 10))
  536. w, err := mw.CreatePart(hdr)
  537. if err != nil {
  538. return err
  539. }
  540. _, err = io.Copy(w, file)
  541. return err
  542. }
  543. // run the upload in a goroutine so we can send the request headers and
  544. // wait for a '100 Continue' response before sending the multipart form
  545. go func() {
  546. err := uploader.Upload(uploadFn)
  547. if err == nil {
  548. err = mw.Close()
  549. }
  550. reqW.CloseWithError(err)
  551. }()
  552. res, err := http.DefaultClient.Do(req)
  553. if err != nil {
  554. return "", err
  555. }
  556. defer res.Body.Close()
  557. if res.StatusCode != http.StatusOK {
  558. return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
  559. }
  560. data, err := ioutil.ReadAll(res.Body)
  561. if err != nil {
  562. return "", err
  563. }
  564. return string(data), nil
  565. }
  566. // ErrNoFeedUpdatesFound is returned when Swarm cannot find updates of the given feed
  567. var ErrNoFeedUpdatesFound = errors.New("No updates found for this feed")
  568. // CreateFeedWithManifest creates a feed manifest, initializing it with the provided
  569. // data
  570. // Returns the resulting feed manifest address that you can use to include in an ENS Resolver (setContent)
  571. // or reference future updates (Client.UpdateFeed)
  572. func (c *Client) CreateFeedWithManifest(request *feed.Request) (string, error) {
  573. responseStream, err := c.updateFeed(request, true)
  574. if err != nil {
  575. return "", err
  576. }
  577. defer responseStream.Close()
  578. body, err := ioutil.ReadAll(responseStream)
  579. if err != nil {
  580. return "", err
  581. }
  582. var manifestAddress string
  583. if err = json.Unmarshal(body, &manifestAddress); err != nil {
  584. return "", err
  585. }
  586. return manifestAddress, nil
  587. }
  588. // UpdateFeed allows you to set a new version of your content
  589. func (c *Client) UpdateFeed(request *feed.Request) error {
  590. _, err := c.updateFeed(request, false)
  591. return err
  592. }
  593. func (c *Client) updateFeed(request *feed.Request, createManifest bool) (io.ReadCloser, error) {
  594. URL, err := url.Parse(c.Gateway)
  595. if err != nil {
  596. return nil, err
  597. }
  598. URL.Path = "/bzz-feed:/"
  599. values := URL.Query()
  600. body := request.AppendValues(values)
  601. if createManifest {
  602. values.Set("manifest", "1")
  603. }
  604. URL.RawQuery = values.Encode()
  605. req, err := http.NewRequest("POST", URL.String(), bytes.NewBuffer(body))
  606. if err != nil {
  607. return nil, err
  608. }
  609. res, err := http.DefaultClient.Do(req)
  610. if err != nil {
  611. return nil, err
  612. }
  613. return res.Body, nil
  614. }
  615. // QueryFeed returns a byte stream with the raw content of the feed update
  616. // manifestAddressOrDomain is the address you obtained in CreateFeedWithManifest or an ENS domain whose Resolver
  617. // points to that address
  618. func (c *Client) QueryFeed(query *feed.Query, manifestAddressOrDomain string) (io.ReadCloser, error) {
  619. return c.queryFeed(query, manifestAddressOrDomain, false)
  620. }
  621. // queryFeed returns a byte stream with the raw content of the feed update
  622. // manifestAddressOrDomain is the address you obtained in CreateFeedWithManifest or an ENS domain whose Resolver
  623. // points to that address
  624. // meta set to true will instruct the node return feed metainformation instead
  625. func (c *Client) queryFeed(query *feed.Query, manifestAddressOrDomain string, meta bool) (io.ReadCloser, error) {
  626. URL, err := url.Parse(c.Gateway)
  627. if err != nil {
  628. return nil, err
  629. }
  630. URL.Path = "/bzz-feed:/" + manifestAddressOrDomain
  631. values := URL.Query()
  632. if query != nil {
  633. query.AppendValues(values) //adds query parameters
  634. }
  635. if meta {
  636. values.Set("meta", "1")
  637. }
  638. URL.RawQuery = values.Encode()
  639. res, err := http.Get(URL.String())
  640. if err != nil {
  641. return nil, err
  642. }
  643. if res.StatusCode != http.StatusOK {
  644. if res.StatusCode == http.StatusNotFound {
  645. return nil, ErrNoFeedUpdatesFound
  646. }
  647. errorMessageBytes, err := ioutil.ReadAll(res.Body)
  648. var errorMessage string
  649. if err != nil {
  650. errorMessage = "cannot retrieve error message: " + err.Error()
  651. } else {
  652. errorMessage = string(errorMessageBytes)
  653. }
  654. return nil, fmt.Errorf("Error retrieving feed updates: %s", errorMessage)
  655. }
  656. return res.Body, nil
  657. }
  658. // GetFeedRequest returns a structure that describes the referenced feed status
  659. // manifestAddressOrDomain is the address you obtained in CreateFeedWithManifest or an ENS domain whose Resolver
  660. // points to that address
  661. func (c *Client) GetFeedRequest(query *feed.Query, manifestAddressOrDomain string) (*feed.Request, error) {
  662. responseStream, err := c.queryFeed(query, manifestAddressOrDomain, true)
  663. if err != nil {
  664. return nil, err
  665. }
  666. defer responseStream.Close()
  667. body, err := ioutil.ReadAll(responseStream)
  668. if err != nil {
  669. return nil, err
  670. }
  671. var metadata feed.Request
  672. if err := metadata.UnmarshalJSON(body); err != nil {
  673. return nil, err
  674. }
  675. return &metadata, nil
  676. }
  677. func GetClientTrace(traceMsg, metricPrefix, ruid string, tn *time.Time) *httptrace.ClientTrace {
  678. trace := &httptrace.ClientTrace{
  679. GetConn: func(_ string) {
  680. log.Trace(traceMsg+" - http get", "event", "GetConn", "ruid", ruid)
  681. metrics.GetOrRegisterResettingTimer(metricPrefix+".getconn", nil).Update(time.Since(*tn))
  682. },
  683. GotConn: func(_ httptrace.GotConnInfo) {
  684. log.Trace(traceMsg+" - http get", "event", "GotConn", "ruid", ruid)
  685. metrics.GetOrRegisterResettingTimer(metricPrefix+".gotconn", nil).Update(time.Since(*tn))
  686. },
  687. PutIdleConn: func(err error) {
  688. log.Trace(traceMsg+" - http get", "event", "PutIdleConn", "ruid", ruid, "err", err)
  689. metrics.GetOrRegisterResettingTimer(metricPrefix+".putidle", nil).Update(time.Since(*tn))
  690. },
  691. GotFirstResponseByte: func() {
  692. log.Trace(traceMsg+" - http get", "event", "GotFirstResponseByte", "ruid", ruid)
  693. metrics.GetOrRegisterResettingTimer(metricPrefix+".firstbyte", nil).Update(time.Since(*tn))
  694. },
  695. Got100Continue: func() {
  696. log.Trace(traceMsg, "event", "Got100Continue", "ruid", ruid)
  697. metrics.GetOrRegisterResettingTimer(metricPrefix+".got100continue", nil).Update(time.Since(*tn))
  698. },
  699. DNSStart: func(_ httptrace.DNSStartInfo) {
  700. log.Trace(traceMsg, "event", "DNSStart", "ruid", ruid)
  701. metrics.GetOrRegisterResettingTimer(metricPrefix+".dnsstart", nil).Update(time.Since(*tn))
  702. },
  703. DNSDone: func(_ httptrace.DNSDoneInfo) {
  704. log.Trace(traceMsg, "event", "DNSDone", "ruid", ruid)
  705. metrics.GetOrRegisterResettingTimer(metricPrefix+".dnsdone", nil).Update(time.Since(*tn))
  706. },
  707. ConnectStart: func(network, addr string) {
  708. log.Trace(traceMsg, "event", "ConnectStart", "ruid", ruid, "network", network, "addr", addr)
  709. metrics.GetOrRegisterResettingTimer(metricPrefix+".connectstart", nil).Update(time.Since(*tn))
  710. },
  711. ConnectDone: func(network, addr string, err error) {
  712. log.Trace(traceMsg, "event", "ConnectDone", "ruid", ruid, "network", network, "addr", addr, "err", err)
  713. metrics.GetOrRegisterResettingTimer(metricPrefix+".connectdone", nil).Update(time.Since(*tn))
  714. },
  715. WroteHeaders: func() {
  716. log.Trace(traceMsg, "event", "WroteHeaders(request)", "ruid", ruid)
  717. metrics.GetOrRegisterResettingTimer(metricPrefix+".wroteheaders", nil).Update(time.Since(*tn))
  718. },
  719. Wait100Continue: func() {
  720. log.Trace(traceMsg, "event", "Wait100Continue", "ruid", ruid)
  721. metrics.GetOrRegisterResettingTimer(metricPrefix+".wait100continue", nil).Update(time.Since(*tn))
  722. },
  723. WroteRequest: func(_ httptrace.WroteRequestInfo) {
  724. log.Trace(traceMsg, "event", "WroteRequest", "ruid", ruid)
  725. metrics.GetOrRegisterResettingTimer(metricPrefix+".wroterequest", nil).Update(time.Since(*tn))
  726. },
  727. }
  728. return trace
  729. }