Skip to content

Commit

Permalink
refactor: remove block endpoint from booster-http (#1075)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored Jan 13, 2023
1 parent 2d731c0 commit 08b1d92
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 91 deletions.
29 changes: 0 additions & 29 deletions cmd/booster-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,6 @@ func TestHttpGzipResponse(t *testing.T) {
require.NoError(t, err)
}

func TestBlockRetrieval(t *testing.T) {
//Create a new mock Http server with custom functions
ctrl := gomock.NewController(t)
mockHttpServer := mocks_booster_http.NewMockHttpServerApi(ctrl)
httpServer := NewHttpServer("", 7779, mockHttpServer)
httpServer.Start(context.Background())

data := []byte("Hello World!")

mockHttpServer.EXPECT().GetBlockByCid(gomock.Any(), gomock.Any()).AnyTimes().Return(data, nil)

// Create a client and make request with Encoding header
client := new(http.Client)
request, err := http.NewRequest("GET", "http://localhost:7779/ipfs/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", nil)
require.NoError(t, err)

response, err := client.Do(request)
require.NoError(t, err)
defer response.Body.Close()
require.Equal(t, "nosniff", response.Header.Get("X-Content-Type-Options"))
require.Equal(t, "application/vnd.ipld.raw", response.Header.Get("Content-Type"))
require.Equal(t, "attachment; filename=\"bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi\"; filename*=UTF-8''bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", response.Header.Get("Content-Disposition"))

out, err := io.ReadAll(response.Body)
require.NoError(t, err)

require.Equal(t, data, out)
}

func TestHttpInfo(t *testing.T) {
var v apiVersion

Expand Down
4 changes: 0 additions & 4 deletions cmd/booster-http/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,3 @@ func (s serverApi) IsUnsealed(ctx context.Context, sectorID abi.SectorNumber, of
func (s serverApi) UnsealSectorAt(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (mount.Reader, error) {
return s.sa.UnsealSectorAt(ctx, sectorID, offset, length)
}

func (s serverApi) GetBlockByCid(ctx context.Context, blockCid cid.Cid) ([]byte, error) {
return s.piecedirectory.BlockstoreGet(ctx, blockCid)
}
59 changes: 1 addition & 58 deletions cmd/booster-http/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -53,7 +52,6 @@ type HttpServerApi interface {
GetPieceDeals(ctx context.Context, pieceCID cid.Cid) ([]model.DealInfo, error)
IsUnsealed(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (bool, error)
UnsealSectorAt(ctx context.Context, sectorID abi.SectorNumber, pieceOffset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (mount.Reader, error)
GetBlockByCid(ctx context.Context, blockCid cid.Cid) ([]byte, error)
}

func NewHttpServer(path string, port int, api HttpServerApi) *HttpServer {
Expand All @@ -64,17 +62,12 @@ func (s *HttpServer) pieceBasePath() string {
return s.path + "/piece/"
}

func (s *HttpServer) blockBasePath() string {
return s.path + "/ipfs/"
}

func (s *HttpServer) Start(ctx context.Context) {
s.ctx, s.cancel = context.WithCancel(ctx)

listenAddr := fmt.Sprintf(":%d", s.port)
handler := http.NewServeMux()
handler.HandleFunc(s.pieceBasePath(), s.handleByPieceCid)
handler.HandleFunc(s.blockBasePath(), s.handleBlockRequest)
handler.HandleFunc("/", s.handleIndex)
handler.HandleFunc("/index.html", s.handleIndex)
handler.HandleFunc("/info", s.handleInfo)
Expand Down Expand Up @@ -113,15 +106,7 @@ const idxPage = `
Download a raw piece by its piece CID
</td>
<td>
<a href="/piece/bafySomePieceCid" > /piece/<piece cid></a>
</td>
</tr>
<tr>
<td>
Download a block by CID
</td>
<td>
<a href="/ipfs/blockCid" > /block/<blockCid></a>
<a href="/piece/bafySomePieceCid">/piece/<piece cid></a>
</td>
</tr>
</tbody>
Expand All @@ -144,48 +129,6 @@ func (s *HttpServer) handleInfo(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(v) //nolint:errcheck
}

func (s *HttpServer) handleBlockRequest(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
ctx, span := tracing.Tracer.Start(r.Context(), "http.block_cid")
defer span.End()

stats.Record(ctx, metrics.HttpBlockByCidRequestCount.M(1))

// Remove the path up to the payload cid
prefixLen := len(s.blockBasePath())
if len(r.URL.Path) <= prefixLen {
msg := fmt.Sprintf("path '%s' is missing block CID", r.URL.Path)
writeError(w, r, http.StatusBadRequest, msg)
stats.Record(ctx, metrics.HttpPayloadByCid400ResponseCount.M(1))
return
}

blockCidStr := r.URL.Path[prefixLen:]
blockCid, err := cid.Parse(blockCidStr)
if err != nil {
msg := fmt.Sprintf("parsing payload CID '%s': %s", blockCidStr, err.Error())
writeError(w, r, http.StatusBadRequest, msg)
stats.Record(ctx, metrics.HttpPayloadByCid400ResponseCount.M(1))
return
}

data, err := s.api.GetBlockByCid(ctx, blockCid)
if err != nil {
msg := fmt.Sprintf("server error getting data for block '%s': %s", blockCidStr, err.Error())
writeError(w, r, http.StatusInternalServerError, msg)
stats.Record(ctx, metrics.HttpBlockByCid500ResponseCount.M(1))
return
}

w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"; filename*=UTF-8''%s", blockCid.String(), blockCid.String()))
w.Header().Set("X-Content-Type-Options", "nosniff") // no funny business in the browsers :^)
b := bytes.NewReader(data)

serveContent(w, r, b, "application/vnd.ipld.raw")
stats.Record(ctx, metrics.HttpBlockByCid200ResponseCount.M(1))
stats.Record(ctx, metrics.HttpBlockByCidRequestDuration.M(float64(time.Since(startTime).Milliseconds())))
}

func (s *HttpServer) handleByPieceCid(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
ctx, span := tracing.Tracer.Start(r.Context(), "http.piece_cid")
Expand Down

0 comments on commit 08b1d92

Please sign in to comment.