Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 116 additions & 4 deletions openapi/Swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ paths:
description: Default response
head:
summary: Retrieve headers containing the content type and length for the reference
description: >
Identical to GET on the same reference except that no content is sent,
so Range and conditional request headers are honored the same way.
tags:
- Bytes
parameters:
Expand All @@ -255,32 +258,77 @@ paths:
$ref: "SwarmCommon.yaml#/components/schemas/SwarmAddress"
required: true
description: Swarm address of chunk
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmCache"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmRedundancyStrategyParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmRedundancyFallbackModeParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmRedundancyLevelParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmChunkRetrievalTimeoutParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmActTimestamp"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmActPublisher"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmActHistoryAddress"
responses:
"200":
description: The chunk exists.
description: Headers for the content at the reference.
headers:
Content-Type:
description: The MIME type of the resource (e.g., application/octet-stream).
schema:
type: string
example: application/octet-stream
Content-Length:
description: The size of the chunk in bytes.
description: The size of the retrievable content in bytes.
schema:
type: integer
example: 1024
Accept-Ranges:
description: Indicates that ranged requests are supported for the reference.
schema:
type: string
example: bytes
ETag:
description: The reference, as an entity tag.
schema:
type: string
Last-Modified:
description: The time the response was generated.
schema:
type: string
Access-Control-Expose-Headers:
description: Headers exposed for CORS.
schema:
type: string
example: Accept-Ranges, Content-Encoding
example: Content-Disposition
"206":
description: Headers for the range requested via the Range header.
headers:
Content-Range:
description: The range covered, and the total size of the content.
schema:
type: string
example: bytes 0-1023/4096
Content-Length:
description: The size of the requested range in bytes.
schema:
type: integer
example: 1024
"304":
description: The entity tag in If-None-Match matches the reference.
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"
"412":
description: A conditional request header was not satisfied.
"416":
description: The requested range cannot be satisfied.
headers:
Content-Range:
description: The total size of the content.
schema:
type: string
example: bytes */4096
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response

Expand Down Expand Up @@ -469,6 +517,9 @@ paths:
description: Default response
head:
summary: Retrieve headers with content type and length for the reference
description: >
Identical to GET on the same reference except that no content is sent,
so Range and conditional request headers are honored the same way.
tags:
- BZZ
parameters:
Expand All @@ -478,16 +529,77 @@ paths:
$ref: "SwarmCommon.yaml#/components/schemas/SwarmAddress"
required: true
description: Swarm address of chunk
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmCache"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmRedundancyStrategyParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmRedundancyFallbackModeParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmRedundancyLevelParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmChunkRetrievalTimeoutParameter"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmActTimestamp"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmActPublisher"
- $ref: "SwarmCommon.yaml#/components/parameters/SwarmActHistoryAddress"
responses:
"200":
description: Chunk exists
description: Headers for the content at the reference.
headers:
Content-Type:
description: The MIME type of the resource.
schema:
type: string
example: application/octet-stream
Content-Length:
description: The size of the retrievable content in bytes.
schema:
type: integer
example: 1024
Accept-Ranges:
description: Indicates that ranged requests are supported for the reference.
schema:
type: string
example: bytes
ETag:
description: The reference, as an entity tag.
schema:
type: string
Last-Modified:
description: The time the response was generated.
schema:
type: string
Access-Control-Expose-Headers:
description: Headers exposed for CORS.
schema:
type: string
example: Content-Disposition
"206":
description: Headers for the range requested via the Range header.
headers:
Content-Range:
description: The range covered, and the total size of the content.
schema:
type: string
example: bytes 0-1023/4096
Content-Length:
description: The size of the requested range in bytes.
schema:
type: integer
example: 1024
"304":
description: The entity tag in If-None-Match matches the reference.
"400":
$ref: "SwarmCommon.yaml#/components/responses/400"
"404":
$ref: "SwarmCommon.yaml#/components/responses/404"
"412":
description: A conditional request header was not satisfied.
"416":
description: The requested range cannot be satisfied.
headers:
Content-Range:
description: The total size of the content.
schema:
type: string
example: bytes */4096
"500":
$ref: "SwarmCommon.yaml#/components/responses/500"
default:
description: Default response

Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ const (
ContentTypeHeader = "Content-Type"
ContentDispositionHeader = "Content-Disposition"
ContentLengthHeader = "Content-Length"
ContentRangeHeader = "Content-Range"
AcceptRangesHeader = "Accept-Ranges"
RangeHeader = "Range"
OriginHeader = "Origin"
AccessControlExposeHeaders = "Access-Control-Expose-Headers"
Expand Down
26 changes: 5 additions & 21 deletions pkg/api/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
package api

import (
"encoding/binary"
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/ethersphere/bee/v2/pkg/accesscontrol"
"github.com/ethersphere/bee/v2/pkg/cac"
"github.com/ethersphere/bee/v2/pkg/file/redundancy"
"github.com/ethersphere/bee/v2/pkg/jsonhttp"
"github.com/ethersphere/bee/v2/pkg/postage"
Expand Down Expand Up @@ -209,24 +206,11 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) {
address = v
}

getter := s.storer.Download(true)
ch, err := getter.Get(r.Context(), address)
if err != nil {
logger.Debug("get root chunk failed", "chunk_address", address, "error", err)
logger.Error(nil, "get root chunk failed")
w.WriteHeader(http.StatusNotFound)
return
additionalHeaders := http.Header{
ContentTypeHeader: {"application/octet-stream"},
}

w.Header().Add(AccessControlExposeHeaders, "Accept-Ranges, Content-Encoding")
w.Header().Add(ContentTypeHeader, "application/octet-stream")
var span int64

if cac.Valid(ch) {
span = int64(binary.LittleEndian.Uint64(ch.Data()[:swarm.SpanSize]))
} else {
span = int64(len(ch.Data()))
}
w.Header().Set(ContentLengthHeader, strconv.FormatInt(span, 10))
w.WriteHeader(http.StatusOK) // HEAD requests do not write a body
// share the GET path: the joiner strips the redundancy level encoded in the
// root chunk span and splits an encrypted reference into address and key.
s.downloadHandler(logger, w, r, address, additionalHeaders, true, true, nil)
}
Loading
Loading