client.go 23 KB
Newer Older
1 2
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
3
//
4 5
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
6 7 8
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10 11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU Lesser General Public License for more details.
13
//
14 15
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19

package client

import (
20
	"archive/tar"
21
	"bytes"
22
	"context"
23
	"encoding/json"
24
	"errors"
25 26 27
	"fmt"
	"io"
	"io/ioutil"
28
	"mime/multipart"
29
	"net/http"
30
	"net/http/httptrace"
31
	"net/textproto"
32
	"net/url"
33 34
	"os"
	"path/filepath"
35
	"regexp"
36
	"strconv"
37
	"strings"
38
	"time"
39

40 41
	"github.com/ethereum/go-ethereum/log"
	"github.com/ethereum/go-ethereum/metrics"
42
	"github.com/ethereum/go-ethereum/swarm/api"
43
	"github.com/ethereum/go-ethereum/swarm/spancontext"
44
	"github.com/ethereum/go-ethereum/swarm/storage/feed"
45
	"github.com/pborman/uuid"
46 47
)

48 49 50 51
var (
	ErrUnauthorized = errors.New("unauthorized")
)

52 53 54 55 56 57 58 59 60 61 62
func NewClient(gateway string) *Client {
	return &Client{
		Gateway: gateway,
	}
}

// Client wraps interaction with a swarm HTTP gateway.
type Client struct {
	Gateway string
}

63 64 65
// UploadRaw uploads raw data to swarm and returns the resulting hash. If toEncrypt is true it
// uploads encrypted data
func (c *Client) UploadRaw(r io.Reader, size int64, toEncrypt bool) (string, error) {
66 67
	if size <= 0 {
		return "", errors.New("data size must be greater than zero")
68
	}
69 70 71 72 73
	addr := ""
	if toEncrypt {
		addr = "encrypt"
	}
	req, err := http.NewRequest("POST", c.Gateway+"/bzz-raw:/"+addr, r)
74 75
	if err != nil {
		return "", err
76
	}
77 78 79 80
	req.ContentLength = size
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", err
81
	}
82 83 84
	defer res.Body.Close()
	if res.StatusCode != http.StatusOK {
		return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
85
	}
86
	data, err := ioutil.ReadAll(res.Body)
87 88 89
	if err != nil {
		return "", err
	}
90
	return string(data), nil
91 92
}

93 94 95
// DownloadRaw downloads raw data from swarm and it returns a ReadCloser and a bool whether the
// content was encrypted
func (c *Client) DownloadRaw(hash string) (io.ReadCloser, bool, error) {
96
	uri := c.Gateway + "/bzz-raw:/" + hash
97
	res, err := http.DefaultClient.Get(uri)
98
	if err != nil {
99
		return nil, false, err
100 101 102
	}
	if res.StatusCode != http.StatusOK {
		res.Body.Close()
103
		return nil, false, fmt.Errorf("unexpected HTTP status: %s", res.Status)
104
	}
105 106
	isEncrypted := (res.Header.Get("X-Decrypted") == "true")
	return res.Body, isEncrypted, nil
107 108
}

109 110 111 112 113 114 115 116 117 118 119
// File represents a file in a swarm manifest and is used for uploading and
// downloading content to and from swarm
type File struct {
	io.ReadCloser
	api.ManifestEntry
}

// Open opens a local file which can then be passed to client.Upload to upload
// it to swarm
func Open(path string) (*File, error) {
	f, err := os.Open(path)
120
	if err != nil {
121
		return nil, err
122
	}
123
	stat, err := f.Stat()
124
	if err != nil {
125 126
		f.Close()
		return nil, err
127
	}
128 129 130 131 132 133

	contentType, err := api.DetectContentType(f.Name(), f)
	if err != nil {
		return nil, err
	}

134 135 136
	return &File{
		ReadCloser: f,
		ManifestEntry: api.ManifestEntry{
137
			ContentType: contentType,
138 139 140 141 142 143 144 145 146 147 148
			Mode:        int64(stat.Mode()),
			Size:        stat.Size(),
			ModTime:     stat.ModTime(),
		},
	}, nil
}

// Upload uploads a file to swarm and either adds it to an existing manifest
// (if the manifest argument is non-empty) or creates a new manifest containing
// the file, returning the resulting manifest hash (the file will then be
// available at bzz:/<hash>/<path>)
149
func (c *Client) Upload(file *File, manifest string, toEncrypt bool) (string, error) {
150 151
	if file.Size <= 0 {
		return "", errors.New("file size must be greater than zero")
152
	}
153
	return c.TarUpload(manifest, &FileUploader{file}, "", toEncrypt)
154 155
}

156 157 158 159 160
// Download downloads a file with the given path from the swarm manifest with
// the given hash (i.e. it gets bzz:/<hash>/<path>)
func (c *Client) Download(hash, path string) (*File, error) {
	uri := c.Gateway + "/bzz:/" + hash + "/" + path
	res, err := http.DefaultClient.Get(uri)
161
	if err != nil {
162
		return nil, err
163
	}
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	if res.StatusCode != http.StatusOK {
		res.Body.Close()
		return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
	}
	return &File{
		ReadCloser: res.Body,
		ManifestEntry: api.ManifestEntry{
			ContentType: res.Header.Get("Content-Type"),
			Size:        res.ContentLength,
		},
	}, nil
}

// UploadDirectory uploads a directory tree to swarm and either adds the files
// to an existing manifest (if the manifest argument is non-empty) or creates a
// new manifest, returning the resulting manifest hash (files from the
// directory will then be available at bzz:/<hash>/path/to/file), with
// the file specified in defaultPath being uploaded to the root of the manifest
// (i.e. bzz:/<hash>/)
183
func (c *Client) UploadDirectory(dir, defaultPath, manifest string, toEncrypt bool) (string, error) {
184
	stat, err := os.Stat(dir)
185 186
	if err != nil {
		return "", err
187 188
	} else if !stat.IsDir() {
		return "", fmt.Errorf("not a directory: %s", dir)
189
	}
190 191 192 193 194 195 196 197 198
	if defaultPath != "" {
		if _, err := os.Stat(filepath.Join(dir, defaultPath)); err != nil {
			if os.IsNotExist(err) {
				return "", fmt.Errorf("the default path %q was not found in the upload directory %q", defaultPath, dir)
			}
			return "", fmt.Errorf("default path: %v", err)
		}
	}
	return c.TarUpload(manifest, &DirectoryUploader{dir}, defaultPath, toEncrypt)
199 200
}

201 202
// DownloadDirectory downloads the files contained in a swarm manifest under
// the given path into a local directory (existing files will be overwritten)
203
func (c *Client) DownloadDirectory(hash, path, destDir, credentials string) error {
204 205 206 207 208 209
	stat, err := os.Stat(destDir)
	if err != nil {
		return err
	} else if !stat.IsDir() {
		return fmt.Errorf("not a directory: %s", destDir)
	}
210

211 212
	uri := c.Gateway + "/bzz:/" + hash + "/" + path
	req, err := http.NewRequest("GET", uri, nil)
213
	if err != nil {
214
		return err
215
	}
216 217 218
	if credentials != "" {
		req.SetBasicAuth("", credentials)
	}
219 220
	req.Header.Set("Accept", "application/x-tar")
	res, err := http.DefaultClient.Do(req)
221
	if err != nil {
222
		return err
223
	}
224
	defer res.Body.Close()
225 226 227 228 229
	switch res.StatusCode {
	case http.StatusOK:
	case http.StatusUnauthorized:
		return ErrUnauthorized
	default:
230 231 232 233 234 235 236 237 238 239 240 241 242 243
		return fmt.Errorf("unexpected HTTP status: %s", res.Status)
	}
	tr := tar.NewReader(res.Body)
	for {
		hdr, err := tr.Next()
		if err == io.EOF {
			return nil
		} else if err != nil {
			return err
		}
		// ignore the default path file
		if hdr.Name == "" {
			continue
		}
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		dstPath := filepath.Join(destDir, filepath.Clean(strings.TrimPrefix(hdr.Name, path)))
		if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
			return err
		}
		var mode os.FileMode = 0644
		if hdr.Mode > 0 {
			mode = os.FileMode(hdr.Mode)
		}
		dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
		if err != nil {
			return err
		}
		n, err := io.Copy(dst, tr)
		dst.Close()
		if err != nil {
			return err
		} else if n != hdr.Size {
			return fmt.Errorf("expected %s to be %d bytes but got %d", hdr.Name, hdr.Size, n)
		}
	}
}
266

267 268 269
// DownloadFile downloads a single file into the destination directory
// if the manifest entry does not specify a file name - it will fallback
// to the hash of the file as a filename
270
func (c *Client) DownloadFile(hash, path, dest, credentials string) error {
271 272 273 274 275 276 277 278 279 280 281 282
	hasDestinationFilename := false
	if stat, err := os.Stat(dest); err == nil {
		hasDestinationFilename = !stat.IsDir()
	} else {
		if os.IsNotExist(err) {
			// does not exist - should be created
			hasDestinationFilename = true
		} else {
			return fmt.Errorf("could not stat path: %v", err)
		}
	}

283
	manifestList, err := c.List(hash, path, credentials)
284
	if err != nil {
285
		return err
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	}

	switch len(manifestList.Entries) {
	case 0:
		return fmt.Errorf("could not find path requested at manifest address. make sure the path you've specified is correct")
	case 1:
		//continue
	default:
		return fmt.Errorf("got too many matches for this path")
	}

	uri := c.Gateway + "/bzz:/" + hash + "/" + path
	req, err := http.NewRequest("GET", uri, nil)
	if err != nil {
		return err
	}
302 303 304
	if credentials != "" {
		req.SetBasicAuth("", credentials)
	}
305 306 307 308 309
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
310 311 312 313 314
	switch res.StatusCode {
	case http.StatusOK:
	case http.StatusUnauthorized:
		return ErrUnauthorized
	default:
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		return fmt.Errorf("unexpected HTTP status: expected 200 OK, got %d", res.StatusCode)
	}
	filename := ""
	if hasDestinationFilename {
		filename = dest
	} else {
		// try to assert
		re := regexp.MustCompile("[^/]+$") //everything after last slash

		if results := re.FindAllString(path, -1); len(results) > 0 {
			filename = results[len(results)-1]
		} else {
			if entry := manifestList.Entries[0]; entry.Path != "" && entry.Path != "/" {
				filename = entry.Path
			} else {
				// assume hash as name if there's nothing from the command line
				filename = hash
			}
		}
		filename = filepath.Join(dest, filename)
	}
	filePath, err := filepath.Abs(filename)
	if err != nil {
		return err
	}

	if err := os.MkdirAll(filepath.Dir(filePath), 0777); err != nil {
		return err
	}

	dst, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer dst.Close()

	_, err = io.Copy(dst, res.Body)
	return err
}

355
// UploadManifest uploads the given manifest to swarm
356
func (c *Client) UploadManifest(m *api.Manifest, toEncrypt bool) (string, error) {
357 358 359
	data, err := json.Marshal(m)
	if err != nil {
		return "", err
360
	}
361
	return c.UploadRaw(bytes.NewReader(data), int64(len(data)), toEncrypt)
362
}
363

364
// DownloadManifest downloads a swarm manifest
365 366
func (c *Client) DownloadManifest(hash string) (*api.Manifest, bool, error) {
	res, isEncrypted, err := c.DownloadRaw(hash)
367
	if err != nil {
368
		return nil, isEncrypted, err
369 370 371 372
	}
	defer res.Close()
	var manifest api.Manifest
	if err := json.NewDecoder(res).Decode(&manifest); err != nil {
373
		return nil, isEncrypted, err
374
	}
375
	return &manifest, isEncrypted, nil
376 377
}

378 379
// List list files in a swarm manifest which have the given prefix, grouping
// common prefixes using "/" as a delimiter.
380 381 382 383 384 385 386 387 388 389 390 391 392
//
// For example, if the manifest represents the following directory structure:
//
// file1.txt
// file2.txt
// dir1/file3.txt
// dir1/dir2/file4.txt
//
// Then:
//
// - a prefix of ""      would return [dir1/, file1.txt, file2.txt]
// - a prefix of "file"  would return [file1.txt, file2.txt]
// - a prefix of "dir1/" would return [dir1/dir2/, dir1/file3.txt]
393 394
//
// where entries ending with "/" are common prefixes.
395 396 397 398 399 400 401 402 403
func (c *Client) List(hash, prefix, credentials string) (*api.ManifestList, error) {
	req, err := http.NewRequest(http.MethodGet, c.Gateway+"/bzz-list:/"+hash+"/"+prefix, nil)
	if err != nil {
		return nil, err
	}
	if credentials != "" {
		req.SetBasicAuth("", credentials)
	}
	res, err := http.DefaultClient.Do(req)
404 405 406
	if err != nil {
		return nil, err
	}
407
	defer res.Body.Close()
408 409 410 411 412
	switch res.StatusCode {
	case http.StatusOK:
	case http.StatusUnauthorized:
		return nil, ErrUnauthorized
	default:
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
		return nil, fmt.Errorf("unexpected HTTP status: %s", res.Status)
	}
	var list api.ManifestList
	if err := json.NewDecoder(res.Body).Decode(&list); err != nil {
		return nil, err
	}
	return &list, nil
}

// Uploader uploads files to swarm using a provided UploadFn
type Uploader interface {
	Upload(UploadFn) error
}

type UploaderFunc func(UploadFn) error

func (u UploaderFunc) Upload(upload UploadFn) error {
	return u(upload)
}

// DirectoryUploader uploads all files in a directory, optionally uploading
// a file to the default path
type DirectoryUploader struct {
436
	Dir string
437
}
438

439 440 441 442 443 444 445
// Upload performs the upload of the directory and default path
func (d *DirectoryUploader) Upload(upload UploadFn) error {
	return filepath.Walk(d.Dir, func(path string, f os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if f.IsDir() {
446 447
			return nil
		}
448 449 450 451 452 453 454 455 456 457 458 459
		file, err := Open(path)
		if err != nil {
			return err
		}
		relPath, err := filepath.Rel(d.Dir, path)
		if err != nil {
			return err
		}
		file.Path = filepath.ToSlash(relPath)
		return upload(file)
	})
}
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
// FileUploader uploads a single file
type FileUploader struct {
	File *File
}

// Upload performs the upload of the file
func (f *FileUploader) Upload(upload UploadFn) error {
	return upload(f.File)
}

// UploadFn is the type of function passed to an Uploader to perform the upload
// of a single file (for example, a directory uploader would call a provided
// UploadFn for each file in the directory tree)
type UploadFn func(file *File) error

// TarUpload uses the given Uploader to upload files to swarm as a tar stream,
// returning the resulting manifest hash
478
func (c *Client) TarUpload(hash string, uploader Uploader, defaultPath string, toEncrypt bool) (string, error) {
479 480 481 482 483
	ctx, sp := spancontext.StartSpan(context.Background(), "api.client.tarupload")
	defer sp.Finish()

	var tn time.Time

484 485
	reqR, reqW := io.Pipe()
	defer reqR.Close()
486 487 488 489 490 491 492 493 494 495
	addr := hash

	// If there is a hash already (a manifest), then that manifest will determine if the upload has
	// to be encrypted or not. If there is no manifest then the toEncrypt parameter decides if
	// there is encryption or not.
	if hash == "" && toEncrypt {
		// This is the built-in address for the encrypted upload endpoint
		addr = "encrypt"
	}
	req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+addr, reqR)
496 497 498
	if err != nil {
		return "", err
	}
499 500 501 502 503 504

	trace := GetClientTrace("swarm api client - upload tar", "api.client.uploadtar", uuid.New()[:8], &tn)

	req = req.WithContext(httptrace.WithClientTrace(ctx, trace))
	transport := http.DefaultTransport

505
	req.Header.Set("Content-Type", "application/x-tar")
506 507 508 509 510
	if defaultPath != "" {
		q := req.URL.Query()
		q.Set("defaultpath", defaultPath)
		req.URL.RawQuery = q.Encode()
	}
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

	// use 'Expect: 100-continue' so we don't send the request body if
	// the server refuses the request
	req.Header.Set("Expect", "100-continue")

	tw := tar.NewWriter(reqW)

	// define an UploadFn which adds files to the tar stream
	uploadFn := func(file *File) error {
		hdr := &tar.Header{
			Name:    file.Path,
			Mode:    file.Mode,
			Size:    file.Size,
			ModTime: file.ModTime,
			Xattrs: map[string]string{
				"user.swarm.content-type": file.ContentType,
			},
		}
		if err := tw.WriteHeader(hdr); err != nil {
			return err
531
		}
532 533
		_, err = io.Copy(tw, file)
		return err
534 535
	}

536 537 538 539 540 541
	// run the upload in a goroutine so we can send the request headers and
	// wait for a '100 Continue' response before sending the tar stream
	go func() {
		err := uploader.Upload(uploadFn)
		if err == nil {
			err = tw.Close()
542
		}
543 544
		reqW.CloseWithError(err)
	}()
545 546
	tn = time.Now()
	res, err := transport.RoundTrip(req)
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	if err != nil {
		return "", err
	}
	defer res.Body.Close()
	if res.StatusCode != http.StatusOK {
		return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
	}
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	return string(data), nil
}

// MultipartUpload uses the given Uploader to upload files to swarm as a
// multipart form, returning the resulting manifest hash
func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error) {
	reqR, reqW := io.Pipe()
	defer reqR.Close()
	req, err := http.NewRequest("POST", c.Gateway+"/bzz:/"+hash, reqR)
	if err != nil {
		return "", err
569 570
	}

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	// use 'Expect: 100-continue' so we don't send the request body if
	// the server refuses the request
	req.Header.Set("Expect", "100-continue")

	mw := multipart.NewWriter(reqW)
	req.Header.Set("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%q", mw.Boundary()))

	// define an UploadFn which adds files to the multipart form
	uploadFn := func(file *File) error {
		hdr := make(textproto.MIMEHeader)
		hdr.Set("Content-Disposition", fmt.Sprintf("form-data; name=%q", file.Path))
		hdr.Set("Content-Type", file.ContentType)
		hdr.Set("Content-Length", strconv.FormatInt(file.Size, 10))
		w, err := mw.CreatePart(hdr)
		if err != nil {
			return err
		}
		_, err = io.Copy(w, file)
		return err
	}

	// run the upload in a goroutine so we can send the request headers and
	// wait for a '100 Continue' response before sending the multipart form
	go func() {
		err := uploader.Upload(uploadFn)
		if err == nil {
			err = mw.Close()
		}
		reqW.CloseWithError(err)
	}()

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", err
	}
	defer res.Body.Close()
	if res.StatusCode != http.StatusOK {
		return "", fmt.Errorf("unexpected HTTP status: %s", res.Status)
	}
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	return string(data), nil
615
}
616

617 618
// ErrNoFeedUpdatesFound is returned when Swarm cannot find updates of the given feed
var ErrNoFeedUpdatesFound = errors.New("No updates found for this feed")
619

620
// CreateFeedWithManifest creates a feed manifest, initializing it with the provided
621
// data
622
// Returns the resulting feed manifest address that you can use to include in an ENS Resolver (setContent)
623
// or reference future updates (Client.UpdateFeed)
624
func (c *Client) CreateFeedWithManifest(request *feed.Request) (string, error) {
625
	responseStream, err := c.updateFeed(request, true)
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	if err != nil {
		return "", err
	}
	defer responseStream.Close()

	body, err := ioutil.ReadAll(responseStream)
	if err != nil {
		return "", err
	}

	var manifestAddress string
	if err = json.Unmarshal(body, &manifestAddress); err != nil {
		return "", err
	}
	return manifestAddress, nil
}

643
// UpdateFeed allows you to set a new version of your content
644
func (c *Client) UpdateFeed(request *feed.Request) error {
645
	_, err := c.updateFeed(request, false)
646 647 648
	return err
}

649
func (c *Client) updateFeed(request *feed.Request, createManifest bool) (io.ReadCloser, error) {
650
	URL, err := url.Parse(c.Gateway)
651 652 653
	if err != nil {
		return nil, err
	}
654
	URL.Path = "/bzz-feed:/"
655 656 657 658 659 660
	values := URL.Query()
	body := request.AppendValues(values)
	if createManifest {
		values.Set("manifest", "1")
	}
	URL.RawQuery = values.Encode()
661

662
	req, err := http.NewRequest("POST", URL.String(), bytes.NewBuffer(body))
663 664 665 666 667 668 669 670 671 672 673 674
	if err != nil {
		return nil, err
	}

	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}

	return res.Body, nil
}

675 676
// QueryFeed returns a byte stream with the raw content of the feed update
// manifestAddressOrDomain is the address you obtained in CreateFeedWithManifest or an ENS domain whose Resolver
677
// points to that address
678
func (c *Client) QueryFeed(query *feed.Query, manifestAddressOrDomain string) (io.ReadCloser, error) {
679
	return c.queryFeed(query, manifestAddressOrDomain, false)
680
}
681

682 683
// queryFeed returns a byte stream with the raw content of the feed update
// manifestAddressOrDomain is the address you obtained in CreateFeedWithManifest or an ENS domain whose Resolver
684
// points to that address
685
// meta set to true will instruct the node return feed metainformation instead
686
func (c *Client) queryFeed(query *feed.Query, manifestAddressOrDomain string, meta bool) (io.ReadCloser, error) {
687
	URL, err := url.Parse(c.Gateway)
688 689 690
	if err != nil {
		return nil, err
	}
691
	URL.Path = "/bzz-feed:/" + manifestAddressOrDomain
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	values := URL.Query()
	if query != nil {
		query.AppendValues(values) //adds query parameters
	}
	if meta {
		values.Set("meta", "1")
	}
	URL.RawQuery = values.Encode()
	res, err := http.Get(URL.String())
	if err != nil {
		return nil, err
	}

	if res.StatusCode != http.StatusOK {
		if res.StatusCode == http.StatusNotFound {
707
			return nil, ErrNoFeedUpdatesFound
708 709 710 711 712 713 714 715
		}
		errorMessageBytes, err := ioutil.ReadAll(res.Body)
		var errorMessage string
		if err != nil {
			errorMessage = "cannot retrieve error message: " + err.Error()
		} else {
			errorMessage = string(errorMessageBytes)
		}
716
		return nil, fmt.Errorf("Error retrieving feed updates: %s", errorMessage)
717
	}
718

719
	return res.Body, nil
720 721
}

722
// GetFeedRequest returns a structure that describes the referenced feed status
723
// manifestAddressOrDomain is the address you obtained in CreateFeedWithManifest or an ENS domain whose Resolver
724
// points to that address
725
func (c *Client) GetFeedRequest(query *feed.Query, manifestAddressOrDomain string) (*feed.Request, error) {
726

727
	responseStream, err := c.queryFeed(query, manifestAddressOrDomain, true)
728 729 730 731 732 733 734 735 736 737
	if err != nil {
		return nil, err
	}
	defer responseStream.Close()

	body, err := ioutil.ReadAll(responseStream)
	if err != nil {
		return nil, err
	}

738
	var metadata feed.Request
739 740 741 742 743
	if err := metadata.UnmarshalJSON(body); err != nil {
		return nil, err
	}
	return &metadata, nil
}
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

func GetClientTrace(traceMsg, metricPrefix, ruid string, tn *time.Time) *httptrace.ClientTrace {
	trace := &httptrace.ClientTrace{
		GetConn: func(_ string) {
			log.Trace(traceMsg+" - http get", "event", "GetConn", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".getconn", nil).Update(time.Since(*tn))
		},
		GotConn: func(_ httptrace.GotConnInfo) {
			log.Trace(traceMsg+" - http get", "event", "GotConn", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".gotconn", nil).Update(time.Since(*tn))
		},
		PutIdleConn: func(err error) {
			log.Trace(traceMsg+" - http get", "event", "PutIdleConn", "ruid", ruid, "err", err)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".putidle", nil).Update(time.Since(*tn))
		},
		GotFirstResponseByte: func() {
			log.Trace(traceMsg+" - http get", "event", "GotFirstResponseByte", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".firstbyte", nil).Update(time.Since(*tn))
		},
		Got100Continue: func() {
			log.Trace(traceMsg, "event", "Got100Continue", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".got100continue", nil).Update(time.Since(*tn))
		},
		DNSStart: func(_ httptrace.DNSStartInfo) {
			log.Trace(traceMsg, "event", "DNSStart", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".dnsstart", nil).Update(time.Since(*tn))
		},
		DNSDone: func(_ httptrace.DNSDoneInfo) {
			log.Trace(traceMsg, "event", "DNSDone", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".dnsdone", nil).Update(time.Since(*tn))
		},
		ConnectStart: func(network, addr string) {
			log.Trace(traceMsg, "event", "ConnectStart", "ruid", ruid, "network", network, "addr", addr)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".connectstart", nil).Update(time.Since(*tn))
		},
		ConnectDone: func(network, addr string, err error) {
			log.Trace(traceMsg, "event", "ConnectDone", "ruid", ruid, "network", network, "addr", addr, "err", err)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".connectdone", nil).Update(time.Since(*tn))
		},
		WroteHeaders: func() {
			log.Trace(traceMsg, "event", "WroteHeaders(request)", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".wroteheaders", nil).Update(time.Since(*tn))
		},
		Wait100Continue: func() {
			log.Trace(traceMsg, "event", "Wait100Continue", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".wait100continue", nil).Update(time.Since(*tn))
		},
		WroteRequest: func(_ httptrace.WroteRequestInfo) {
			log.Trace(traceMsg, "event", "WroteRequest", "ruid", ruid)
			metrics.GetOrRegisterResettingTimer(metricPrefix+".wroterequest", nil).Update(time.Since(*tn))
		},
	}
	return trace
}