Skip to content

Commit f7d3678

Browse files
nonsensegbalint
authored andcommitted
swarm/api/http: http package refactoring 1/5 and 2/5 (#17164)
1 parent facf1bc commit f7d3678

6 files changed

Lines changed: 579 additions & 381 deletions

File tree

‎swarm/api/api.go‎

Lines changed: 243 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package api
1818

1919
import (
20+
"archive/tar"
2021
"context"
2122
"fmt"
2223
"io"
@@ -41,22 +42,32 @@ import (
4142
)
4243

4344
var (
44-
apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil)
45-
apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil)
46-
apiPutCount = metrics.NewRegisteredCounter("api.put.count", nil)
47-
apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil)
48-
apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil)
49-
apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil)
50-
apiGetHTTP300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
51-
apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil)
52-
apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil)
53-
apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil)
54-
apiAddFileFail = metrics.NewRegisteredCounter("api.addfile.fail", nil)
55-
apiRmFileCount = metrics.NewRegisteredCounter("api.removefile.count", nil)
56-
apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil)
57-
apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil)
58-
apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil)
59-
apiGetInvalid = metrics.NewRegisteredCounter("api.get.invalid", nil)
45+
apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil)
46+
apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil)
47+
apiPutCount = metrics.NewRegisteredCounter("api.put.count", nil)
48+
apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil)
49+
apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil)
50+
apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil)
51+
apiGetHTTP300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
52+
apiManifestUpdateCount = metrics.NewRegisteredCounter("api.manifestupdate.count", nil)
53+
apiManifestUpdateFail = metrics.NewRegisteredCounter("api.manifestupdate.fail", nil)
54+
apiManifestListCount = metrics.NewRegisteredCounter("api.manifestlist.count", nil)
55+
apiManifestListFail = metrics.NewRegisteredCounter("api.manifestlist.fail", nil)
56+
apiDeleteCount = metrics.NewRegisteredCounter("api.delete.count", nil)
57+
apiDeleteFail = metrics.NewRegisteredCounter("api.delete.fail", nil)
58+
apiGetTarCount = metrics.NewRegisteredCounter("api.gettar.count", nil)
59+
apiGetTarFail = metrics.NewRegisteredCounter("api.gettar.fail", nil)
60+
apiUploadTarCount = metrics.NewRegisteredCounter("api.uploadtar.count", nil)
61+
apiUploadTarFail = metrics.NewRegisteredCounter("api.uploadtar.fail", nil)
62+
apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil)
63+
apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil)
64+
apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil)
65+
apiAddFileFail = metrics.NewRegisteredCounter("api.addfile.fail", nil)
66+
apiRmFileCount = metrics.NewRegisteredCounter("api.removefile.count", nil)
67+
apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil)
68+
apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil)
69+
apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil)
70+
apiGetInvalid = metrics.NewRegisteredCounter("api.get.invalid", nil)
6071
)
6172

6273
// Resolver interface resolve a domain name to a hash using ENS
@@ -424,6 +435,185 @@ func (a *API) Get(ctx context.Context, manifestAddr storage.Address, path string
424435
return
425436
}
426437

438+
func (a *API) Delete(ctx context.Context, addr string, path string) (storage.Address, error) {
439+
apiDeleteCount.Inc(1)
440+
uri, err := Parse("bzz:/" + addr)
441+
if err != nil {
442+
apiDeleteFail.Inc(1)
443+
return nil, err
444+
}
445+
key, err := a.Resolve(ctx, uri)
446+
447+
if err != nil {
448+
return nil, err
449+
}
450+
newKey, err := a.UpdateManifest(ctx, key, func(mw *ManifestWriter) error {
451+
log.Debug(fmt.Sprintf("removing %s from manifest %s", path, key.Log()))
452+
return mw.RemoveEntry(path)
453+
})
454+
if err != nil {
455+
apiDeleteFail.Inc(1)
456+
return nil, err
457+
}
458+
459+
return newKey, nil
460+
}
461+
462+
// GetDirectoryTar fetches a requested directory as a tarstream
463+
// it returns an io.Reader and an error. Do not forget to Close() the returned ReadCloser
464+
func (a *API) GetDirectoryTar(ctx context.Context, uri *URI) (io.ReadCloser, error) {
465+
apiGetTarCount.Inc(1)
466+
addr, err := a.Resolve(ctx, uri)
467+
if err != nil {
468+
return nil, err
469+
}
470+
walker, err := a.NewManifestWalker(ctx, addr, nil)
471+
if err != nil {
472+
apiGetTarFail.Inc(1)
473+
return nil, err
474+
}
475+
476+
piper, pipew := io.Pipe()
477+
478+
tw := tar.NewWriter(pipew)
479+
480+
go func() {
481+
err := walker.Walk(func(entry *ManifestEntry) error {
482+
// ignore manifests (walk will recurse into them)
483+
if entry.ContentType == ManifestType {
484+
return nil
485+
}
486+
487+
// retrieve the entry's key and size
488+
reader, _ := a.Retrieve(ctx, storage.Address(common.Hex2Bytes(entry.Hash)))
489+
size, err := reader.Size(nil)
490+
if err != nil {
491+
return err
492+
}
493+
494+
// write a tar header for the entry
495+
hdr := &tar.Header{
496+
Name: entry.Path,
497+
Mode: entry.Mode,
498+
Size: size,
499+
ModTime: entry.ModTime,
500+
Xattrs: map[string]string{
501+
"user.swarm.content-type": entry.ContentType,
502+
},
503+
}
504+
505+
if err := tw.WriteHeader(hdr); err != nil {
506+
return err
507+
}
508+
509+
// copy the file into the tar stream
510+
n, err := io.Copy(tw, io.LimitReader(reader, hdr.Size))
511+
if err != nil {
512+
return err
513+
} else if n != size {
514+
return fmt.Errorf("error writing %s: expected %d bytes but sent %d", entry.Path, size, n)
515+
}
516+
517+
return nil
518+
})
519+
if err != nil {
520+
apiGetTarFail.Inc(1)
521+
pipew.CloseWithError(err)
522+
} else {
523+
pipew.Close()
524+
}
525+
}()
526+
527+
return piper, nil
528+
}
529+
530+
// GetManifestList lists the manifest entries for the specified address and prefix
531+
// and returns it as a ManifestList
532+
func (a *API) GetManifestList(ctx context.Context, addr storage.Address, prefix string) (list ManifestList, err error) {
533+
apiManifestListCount.Inc(1)
534+
walker, err := a.NewManifestWalker(ctx, addr, nil)
535+
if err != nil {
536+
apiManifestListFail.Inc(1)
537+
return ManifestList{}, err
538+
}
539+
540+
err = walker.Walk(func(entry *ManifestEntry) error {
541+
// handle non-manifest files
542+
if entry.ContentType != ManifestType {
543+
// ignore the file if it doesn't have the specified prefix
544+
if !strings.HasPrefix(entry.Path, prefix) {
545+
return nil
546+
}
547+
548+
// if the path after the prefix contains a slash, add a
549+
// common prefix to the list, otherwise add the entry
550+
suffix := strings.TrimPrefix(entry.Path, prefix)
551+
if index := strings.Index(suffix, "/"); index > -1 {
552+
list.CommonPrefixes = append(list.CommonPrefixes, prefix+suffix[:index+1])
553+
return nil
554+
}
555+
if entry.Path == "" {
556+
entry.Path = "/"
557+
}
558+
list.Entries = append(list.Entries, entry)
559+
return nil
560+
}
561+
562+
// if the manifest's path is a prefix of the specified prefix
563+
// then just recurse into the manifest by returning nil and
564+
// continuing the walk
565+
if strings.HasPrefix(prefix, entry.Path) {
566+
return nil
567+
}
568+
569+
// if the manifest's path has the specified prefix, then if the
570+
// path after the prefix contains a slash, add a common prefix
571+
// to the list and skip the manifest, otherwise recurse into
572+
// the manifest by returning nil and continuing the walk
573+
if strings.HasPrefix(entry.Path, prefix) {
574+
suffix := strings.TrimPrefix(entry.Path, prefix)
575+
if index := strings.Index(suffix, "/"); index > -1 {
576+
list.CommonPrefixes = append(list.CommonPrefixes, prefix+suffix[:index+1])
577+
return ErrSkipManifest
578+
}
579+
return nil
580+
}
581+
582+
// the manifest neither has the prefix or needs recursing in to
583+
// so just skip it
584+
return ErrSkipManifest
585+
})
586+
587+
if err != nil {
588+
apiManifestListFail.Inc(1)
589+
return ManifestList{}, err
590+
}
591+
592+
return list, nil
593+
}
594+
595+
func (a *API) UpdateManifest(ctx context.Context, addr storage.Address, update func(mw *ManifestWriter) error) (storage.Address, error) {
596+
apiManifestUpdateCount.Inc(1)
597+
mw, err := a.NewManifestWriter(ctx, addr, nil)
598+
if err != nil {
599+
apiManifestUpdateFail.Inc(1)
600+
return nil, err
601+
}
602+
603+
if err := update(mw); err != nil {
604+
apiManifestUpdateFail.Inc(1)
605+
return nil, err
606+
}
607+
608+
addr, err = mw.Store()
609+
if err != nil {
610+
apiManifestUpdateFail.Inc(1)
611+
return nil, err
612+
}
613+
log.Debug(fmt.Sprintf("generated manifest %s", addr))
614+
return addr, nil
615+
}
616+
427617
// Modify loads manifest and checks the content hash before recalculating and storing the manifest.
428618
func (a *API) Modify(ctx context.Context, addr storage.Address, path, contentHash, contentType string) (storage.Address, error) {
429619
apiModifyCount.Inc(1)
@@ -501,6 +691,43 @@ func (a *API) AddFile(ctx context.Context, mhash, path, fname string, content []
501691
return fkey, newMkey.String(), nil
502692
}
503693

694+
func (a *API) UploadTar(ctx context.Context, bodyReader io.ReadCloser, manifestPath string, mw *ManifestWriter) (storage.Address, error) {
695+
apiUploadTarCount.Inc(1)
696+
var contentKey storage.Address
697+
tr := tar.NewReader(bodyReader)
698+
defer bodyReader.Close()
699+
for {
700+
hdr, err := tr.Next()
701+
if err == io.EOF {
702+
break
703+
} else if err != nil {
704+
apiUploadTarFail.Inc(1)
705+
return nil, fmt.Errorf("error reading tar stream: %s", err)
706+
}
707+
708+
// only store regular files
709+
if !hdr.FileInfo().Mode().IsRegular() {
710+
continue
711+
}
712+
713+
// add the entry under the path from the request
714+
manifestPath := path.Join(manifestPath, hdr.Name)
715+
entry := &ManifestEntry{
716+
Path: manifestPath,
717+
ContentType: hdr.Xattrs["user.swarm.content-type"],
718+
Mode: hdr.Mode,
719+
Size: hdr.Size,
720+
ModTime: hdr.ModTime,
721+
}
722+
contentKey, err = mw.AddEntry(ctx, tr, entry)
723+
if err != nil {
724+
apiUploadTarFail.Inc(1)
725+
return nil, fmt.Errorf("error adding manifest entry from tar stream: %s", err)
726+
}
727+
}
728+
return contentKey, nil
729+
}
730+
504731
// RemoveFile removes a file entry in a manifest.
505732
func (a *API) RemoveFile(ctx context.Context, mhash string, path string, fname string, nameresolver bool) (string, error) {
506733
apiRmFileCount.Inc(1)

‎swarm/api/client/client_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
func serverFunc(api *api.API) testutil.TestServer {
34-
return swarmhttp.NewServer(api)
34+
return swarmhttp.NewServer(api, "")
3535
}
3636

3737
// TestClientUploadDownloadRaw test uploading and downloading raw data to swarm

‎swarm/api/http/error_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
)
3030

3131
func TestError(t *testing.T) {
32-
3332
srv := testutil.NewTestSwarmServer(t, serverFunc)
3433
defer srv.Close()
3534

0 commit comments

Comments
 (0)