This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
HTTP API for mutable resources #204
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
3d92c93
swarm/storage: Simplify code, correct content hashing
nolash cf191d3
swarm/storage: Remove signatures from non-validated resources
nolash 13543f4
swarm: Add base api for mutable resources
nolash 81ec1f9
swarm/api: Add all lookup types + public getblock
nolash 1bf4f4a
swarm/api: Add raw form to api+server WIP
nolash 249e1a8
swarm, cmd/swarm, ethclient: Fullstack mut.rsrc. w api
nolash a16d0af
swarm/, cmd/swarm: Amend comments from @lmars PR 204
nolash 11c3b4c
swarm/api: Add contenttype contingent resolve manifest -> rsrc
nolash 80f539c
swarm/api: Rename Db -> Resource
nolash aff40c4
swarm/storage, ethclient: Move signature to end of chunk data
nolash 703051b
swarm: Cleanup after rebase on swarm-mutableresources-extsign
nolash c2e0be6
swarm/storage, swarm/api: Change noparam fmt.Errorf -> errors.New
nolash 400b7d6
swarm/api/http: Test result data
nolash 0455925
swarm: Amend comments from @lmars PR 204 second review, part I
nolash a32681c
swarm/storage: Correct channel for waiting on chunk put
nolash 94303aa
swarm/storage: Implement resourcehandler hashers as sync.Pool
nolash edacd4b
swarm/api: Remove faulty manifest handling + add rsrc create keycheck
nolash 690522b
swarm/api: Amend @gbalint comments PR 204 + args dep test loglvl
nolash 88ed2d4
swarm, ethclient: Add version test for http api
nolash 4a7b5b8
swarm/api, swarm/storage: Delinting
nolash add8971
swarm/api, swarm/storage: Fullstack contexts
nolash 8d7bf3f
swarm/fuse: Update swarm/api:Api.NewAPI constructor
nolash de74542
swarm: Add missing privatekey from swarm config w/o swap
nolash f38859d
swarm/storage: Delint
nolash ef5eeb6
swarm: Remove privkey log entry
nolash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ package http | |
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
@@ -290,6 +291,95 @@ func (s *Server) HandleDelete(w http.ResponseWriter, r *Request) { | |
| fmt.Fprint(w, newKey) | ||
| } | ||
|
|
||
| func (s *Server) HandlePostResource(w http.ResponseWriter, r *Request) { | ||
| var outdata string | ||
| if r.uri.Path != "" { | ||
| frequency, err := strconv.ParseUint(r.uri.Path, 10, 64) | ||
| if err != nil { | ||
| s.BadRequest(w, r, fmt.Sprintf("Cannot parse frequency parameter: %v", err)) | ||
| return | ||
| } | ||
| key, err := s.api.ResourceCreate(r.Context(), r.uri.Addr, frequency) | ||
| if err != nil { | ||
| s.Error(w, r, fmt.Errorf("Resource creation failed: %v", err)) | ||
| return | ||
| } | ||
| outdata = key.Hex() | ||
| } | ||
|
|
||
| data, err := ioutil.ReadAll(r.Body) | ||
| if err != nil { | ||
| s.Error(w, r, err) | ||
| return | ||
| } | ||
| _, _, _, err = s.api.ResourceUpdate(r.Context(), r.uri.Addr, data) | ||
| if err != nil { | ||
| s.Error(w, r, fmt.Errorf("Update resource failed: %v", err)) | ||
| return | ||
| } | ||
|
|
||
| if outdata != "" { | ||
| w.Header().Add("Content-type", "text/plain") | ||
| w.WriteHeader(http.StatusOK) | ||
| fmt.Fprint(w, outdata) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusOK) | ||
| } | ||
|
|
||
| // Retrieve mutable resource updates: | ||
| // bzz-resource://<id> - get latest update | ||
| // bzz-resource://<id>/<n> - get latest update on period n | ||
| // bzz-resource://<id>/<n>/<m> - get update version m of period n | ||
| // <id> = ens name or hash | ||
| func (s *Server) HandleGetResource(w http.ResponseWriter, r *Request) { | ||
| s.handleGetResource(w, r, r.uri.Addr) | ||
| } | ||
|
|
||
| func (s *Server) handleGetResource(w http.ResponseWriter, r *Request, name string) { | ||
| var params []string | ||
| if len(r.uri.Path) > 0 { | ||
| params = strings.Split(r.uri.Path, "/") | ||
| } | ||
| var updateKey storage.Key | ||
| var period uint64 | ||
| var version uint64 | ||
| var data []byte | ||
| var err error | ||
| now := time.Now() | ||
| log.Debug("handlegetdb", "name", name) | ||
| switch len(params) { | ||
| case 0: | ||
| updateKey, data, err = s.api.ResourceLookup(r.Context(), name, 0, 0) | ||
| case 2: | ||
| version, err = strconv.ParseUint(params[1], 10, 32) | ||
| if err != nil { | ||
| break | ||
| } | ||
|
||
| period, err = strconv.ParseUint(params[0], 10, 32) | ||
| if err != nil { | ||
| break | ||
| } | ||
| updateKey, data, err = s.api.ResourceLookup(r.Context(), name, uint32(period), uint32(version)) | ||
| case 1: | ||
| period, err = strconv.ParseUint(params[0], 10, 32) | ||
| if err != nil { | ||
| break | ||
| } | ||
| updateKey, data, err = s.api.ResourceLookup(r.Context(), name, uint32(period), uint32(version)) | ||
| default: | ||
| s.BadRequest(w, r, "Invalid mutable resource request") | ||
| return | ||
| } | ||
| if err != nil { | ||
| s.Error(w, r, fmt.Errorf("Mutable resource lookup failed: %v", err)) | ||
| return | ||
| } | ||
| log.Debug("Found update", "key", updateKey) | ||
| w.Header().Set("Content-Type", "application/octet-stream") | ||
| http.ServeContent(w, &r.Request, "", now, bytes.NewReader(data)) | ||
| } | ||
|
|
||
| // HandleGet handles a GET request to | ||
| // - bzz-raw://<key> and responds with the raw content stored at the | ||
| // given storage key | ||
|
|
@@ -335,7 +425,7 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *Request) { | |
| return api.SkipManifest | ||
| }) | ||
| if entry == nil { | ||
| s.NotFound(w, r, fmt.Errorf("Manifest entry could not be loaded")) | ||
| s.NotFound(w, r, errors.New("Manifest entry could not be loaded")) | ||
| return | ||
| } | ||
| key = storage.Key(common.Hex2Bytes(entry.Hash)) | ||
|
|
@@ -357,7 +447,6 @@ func (s *Server) HandleGet(w http.ResponseWriter, r *Request) { | |
| contentType = typ | ||
| } | ||
| w.Header().Set("Content-Type", contentType) | ||
|
|
||
| http.ServeContent(w, &r.Request, "", time.Now(), reader) | ||
| case r.uri.Hash(): | ||
| w.Header().Set("Content-Type", "text/plain") | ||
|
|
@@ -604,6 +693,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| case "POST": | ||
| if uri.Raw() || uri.DeprecatedRaw() { | ||
| s.HandlePostRaw(w, req) | ||
| } else if uri.Resource() { | ||
| s.HandlePostResource(w, req) | ||
| } else { | ||
| s.HandlePostFiles(w, req) | ||
| } | ||
|
|
@@ -629,6 +720,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| s.HandleDelete(w, req) | ||
|
|
||
| case "GET": | ||
|
|
||
| if uri.Resource() { | ||
| s.HandleGetResource(w, req) | ||
| return | ||
| } | ||
|
|
||
| if uri.Raw() || uri.Hash() || uri.DeprecatedRaw() { | ||
| s.HandleGet(w, req) | ||
| return | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's not call it Db anything please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 221b084