github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/download_reqeust_header.go (about) 1 package sdk 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "net/http" 7 "strconv" 8 9 "github.com/hitenjain14/fasthttp" 10 ) 11 12 // DownloadRequestHeader download request header 13 type DownloadRequestHeader struct { 14 ClientID string 15 PathHash string 16 BlockNum int64 17 NumBlocks int64 18 ReadMarker []byte 19 AuthToken []byte 20 DownloadMode string 21 VerifyDownload bool 22 ConnectionID string 23 Version string 24 } 25 26 // ToHeader update header 27 func (h *DownloadRequestHeader) ToHeader(req *http.Request) { 28 if h.PathHash != "" { 29 req.Header.Set("X-Path-Hash", h.PathHash) 30 } 31 32 if h.BlockNum > 0 { 33 req.Header.Set("X-Block-Num", strconv.FormatInt(h.BlockNum, 10)) 34 } 35 36 if h.NumBlocks > 0 { 37 req.Header.Set("X-Num-Blocks", strconv.FormatInt(h.NumBlocks, 10)) 38 } 39 40 if h.ReadMarker != nil { 41 req.Header.Set("X-Read-Marker", string(h.ReadMarker)) 42 } 43 44 if h.AuthToken != nil { 45 token := base64.StdEncoding.EncodeToString(h.AuthToken) 46 req.Header.Set("X-Auth-Token", token) 47 } 48 49 if h.DownloadMode != "" { 50 req.Header.Set("X-Mode", h.DownloadMode) 51 } 52 53 if h.ConnectionID != "" { 54 req.Header.Set("X-Connection-ID", h.ConnectionID) 55 } 56 57 if h.Version != "" { 58 req.Header.Set("X-Version", h.Version) 59 } 60 61 req.Header.Set("X-Verify-Download", fmt.Sprint(h.VerifyDownload)) 62 } 63 64 func (h *DownloadRequestHeader) ToFastHeader(req *fasthttp.Request) { 65 if h.PathHash != "" { 66 req.Header.Set("X-Path-Hash", h.PathHash) 67 } 68 69 if h.BlockNum > 0 { 70 req.Header.Set("X-Block-Num", strconv.FormatInt(h.BlockNum, 10)) 71 } 72 73 if h.NumBlocks > 0 { 74 req.Header.Set("X-Num-Blocks", strconv.FormatInt(h.NumBlocks, 10)) 75 } 76 77 if h.ReadMarker != nil { 78 req.Header.Set("X-Read-Marker", string(h.ReadMarker)) 79 } 80 81 if h.AuthToken != nil { 82 token := base64.StdEncoding.EncodeToString(h.AuthToken) 83 req.Header.Set("X-Auth-Token", token) 84 } 85 86 if h.DownloadMode != "" { 87 req.Header.Set("X-Mode", h.DownloadMode) 88 } 89 90 if h.ConnectionID != "" { 91 req.Header.Set("X-Connection-ID", h.ConnectionID) 92 } 93 req.Header.Set("X-Verify-Download", fmt.Sprint(h.VerifyDownload)) 94 }