-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #505 from ipfs/fix/495-proxy-add
Fix #495: Hijack proxy /add correctly. Disable keep-alives.
- Loading branch information
Showing
10 changed files
with
194 additions
and
326 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Package adderutils provides some utilities for adding content to cluster. | ||
package adderutils | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"mime/multipart" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/ipfs/ipfs-cluster/adder" | ||
"github.com/ipfs/ipfs-cluster/adder/local" | ||
"github.com/ipfs/ipfs-cluster/adder/sharding" | ||
"github.com/ipfs/ipfs-cluster/api" | ||
|
||
rpc "github.com/hsanjuan/go-libp2p-gorpc" | ||
cid "github.com/ipfs/go-cid" | ||
logging "github.com/ipfs/go-log" | ||
) | ||
|
||
var logger = logging.Logger("adder") | ||
|
||
// AddMultipartHTTPHandler is a helper function to add content | ||
// uploaded using a multipart request. | ||
func AddMultipartHTTPHandler( | ||
ctx context.Context, | ||
rpc *rpc.Client, | ||
params *api.AddParams, | ||
reader *multipart.Reader, | ||
w http.ResponseWriter, | ||
) (*cid.Cid, error) { | ||
var dags adder.ClusterDAGService | ||
output := make(chan *api.AddedOutput, 200) | ||
flusher, flush := w.(http.Flusher) | ||
if params.Shard { | ||
dags = sharding.New(rpc, params.PinOptions, output) | ||
} else { | ||
dags = local.New(rpc, params.PinOptions) | ||
} | ||
|
||
enc := json.NewEncoder(w) | ||
// This must be application/json otherwise go-ipfs client | ||
// will break. | ||
w.Header().Add("Content-Type", "application/json") | ||
// Browsers should not cache when streaming content. | ||
w.Header().Add("Cache-Control", "no-cache") | ||
w.WriteHeader(http.StatusOK) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
for v := range output { | ||
err := enc.Encode(v) | ||
if err != nil { | ||
logger.Error(err) | ||
} | ||
if flush { | ||
flusher.Flush() | ||
} | ||
} | ||
}() | ||
|
||
add := adder.New(dags, params, output) | ||
root, err := add.FromMultipart(ctx, reader) | ||
wg.Wait() | ||
return root, err | ||
} |
This file contains 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 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
Oops, something went wrong.