gunk-dev/the-valley

protocol.go

sourcee381ae98a1ffmain's published tip history
6.2 KiB
1package export
2
3import (
4 "errors"
5 "fmt"
6 "net/http"
7 "strconv"
8)
9
10// The upload's paths, under the receiver's base URL.
11const (
12 // ManifestPath takes step 1's POST of a signed manifest.
13 ManifestPath = "/web-export/v1/manifest"
14)
15
16// BodyPath takes step 2's PUT of one body of the upload with sequence.
17func BodyPath(sequence int64, sha256 string) string {
18 return "/web-export/v1/" + strconv.FormatInt(sequence, 10) + "/" + sha256
19}
20
21// CommitPath takes step 3's POST that commits the upload with sequence.
22func CommitPath(sequence int64) string {
23 return "/web-export/v1/" + strconv.FormatInt(sequence, 10) + "/commit"
24}
25
26// The schemas of the receiver's replies.
27const (
28 InventorySchema = "valley-web-inventory/v1"
29 CommittedSchema = "valley-web-committed/v1"
30 ErrorSchema = "valley-web-error/v1"
31)
32
33// Inventory is the reply to a manifest the receiver admitted: what it
34// already holds, and the bodies it still needs before the upload can be
35// committed.
36type Inventory struct {
37 Schema string `json:"schema"`
38 Sequence int64 `json:"sequence"`
39 // Active is the export the receiver serves, or nil for none.
40 Active *Active `json:"active"`
41 // Holds are, per project, the source tip the receiver holds a verified
42 // repository at.
43 Holds []Held `json:"holds"`
44 // Needs are the bodies the publisher must PUT, export.json first. A
45 // bundle is not needed when the receiver holds its project at the
46 // declared source tip, and no body is needed twice.
47 Needs []Need `json:"needs"`
48}
49
50// Active is the export a receiver serves: its sequence and its content
51// digest. A publisher whose export has this digest, and for which the
52// receiver needs no bundle, need not commit.
53type Active struct {
54 Sequence int64 `json:"sequence"`
55 Digest string `json:"digest"`
56}
57
58// Held is a project the receiver holds a verified repository of, at its
59// source tip.
60type Held struct {
61 Project string `json:"project"`
62 SourceTip string `json:"source_tip"`
63}
64
65// Need is one body the receiver needs, as the manifest declares it.
66type Need struct {
67 Name string `json:"name"`
68 SHA256 string `json:"sha256"`
69 Size int64 `json:"size"`
70}
71
72// Committed is the reply to a commit: the sequence now active.
73type Committed struct {
74 Schema string `json:"schema"`
75 Sequence int64 `json:"sequence"`
76}
77
78// ErrorReply is the reply to a request the receiver refused.
79type ErrorReply struct {
80 Schema string `json:"schema"`
81 Code Code `json:"error"`
82 Message string `json:"message"`
83 // HighWater is the greatest sequence accepted, given with replayed.
84 HighWater int64 `json:"high_water,omitempty"`
85 // Needs are the bodies still needed, given with incomplete.
86 Needs []Need `json:"needs,omitempty"`
87}
88
89// Code is a refusal's stable name. A publisher acts on the code; the
90// message is for people.
91type Code string
92
93const (
94 // CodeTooLarge: a manifest over MaxManifest bytes, or a body declared
95 // larger than the receiver's cap.
96 CodeTooLarge Code = "too-large"
97 // CodeEncoding: a body sent with a Content-Encoding.
98 CodeEncoding Code = "content-encoding"
99 // CodeUnreadable: a request body that could not be read.
100 CodeUnreadable Code = "unreadable"
101 // CodeBadSignature: a manifest that does not open under the pinned key.
102 CodeBadSignature Code = "bad-signature"
103 // CodeBadManifest: a manifest whose text is not a valid manifest.
104 CodeBadManifest Code = "bad-manifest"
105 // CodeWrongInstance and CodeWrongDestination: a manifest signed for
106 // another instance, or for another receiver.
107 CodeWrongInstance Code = "wrong-instance"
108 CodeWrongDestination Code = "wrong-destination"
109 // CodeReplayed: a manifest whose sequence is not greater than the
110 // greatest accepted. It is a replay of an accepted manifest, or older
111 // than one. The publisher waits for its clock to pass HighWater.
112 CodeReplayed Code = "replayed"
113 // CodeOutOfWindow: a manifest signed more than MaxAge ago, or more than
114 // MaxAhead ahead of the receiver's clock.
115 CodeOutOfWindow Code = "out-of-window"
116 // CodeSuperseded: a manifest older than the one staged, or a body or
117 // commit for an upload a newer manifest replaced.
118 CodeSuperseded Code = "superseded"
119 // CodeNoManifest: a body or commit for a sequence nothing is staged
120 // for.
121 CodeNoManifest Code = "no-manifest"
122 // CodeNotDeclared: a body whose hash the staged manifest does not
123 // declare.
124 CodeNotDeclared Code = "not-declared"
125 // CodeSizeMismatch and CodeHashMismatch: a body whose size or SHA-256
126 // is not what the manifest declares.
127 CodeSizeMismatch Code = "size-mismatch"
128 CodeHashMismatch Code = "hash-mismatch"
129 // CodeBadExport: an export.json that is not a valid export, or that
130 // disagrees with the manifest.
131 CodeBadExport Code = "bad-export"
132 // CodeBadBundle: a bundle the acceptance procedure refuses.
133 CodeBadBundle Code = "bad-bundle"
134 // CodeIncomplete: a commit before every needed body has verified.
135 CodeIncomplete Code = "incomplete"
136)
137
138// Status is the HTTP status a refusal is replied with.
139func (c Code) Status() int {
140 switch c {
141 case CodeTooLarge:
142 return http.StatusRequestEntityTooLarge
143 case CodeEncoding:
144 return http.StatusUnsupportedMediaType
145 case CodeBadSignature, CodeWrongInstance, CodeWrongDestination, CodeOutOfWindow:
146 return http.StatusForbidden
147 case CodeReplayed, CodeSuperseded, CodeIncomplete:
148 return http.StatusConflict
149 case CodeNoManifest, CodeNotDeclared:
150 return http.StatusNotFound
151 case CodeHashMismatch, CodeBadExport, CodeBadBundle:
152 return http.StatusUnprocessableEntity
153 }
154 return http.StatusBadRequest
155}
156
157// Error is a refusal: its code, and what was wrong.
158type Error struct {
159 Code Code
160 Message string
161 // HighWater is set with CodeReplayed.
162 HighWater int64
163 // Needs is set with CodeIncomplete.
164 Needs []Need
165}
166
167func (e *Error) Error() string { return string(e.Code) + ": " + e.Message }
168
169// Reply is the refusal as the receiver replies with it.
170func (e *Error) Reply() ErrorReply {
171 return ErrorReply{Schema: ErrorSchema, Code: e.Code, Message: e.Message, HighWater: e.HighWater, Needs: e.Needs}
172}
173
174// CodeOf is the code of the refusal err is or wraps, or "" for none.
175func CodeOf(err error) Code {
176 var e *Error
177 if errors.As(err, &e) {
178 return e.Code
179 }
180 return ""
181}
182
183func refuse(code Code, format string, args ...any) *Error {
184 return &Error{Code: code, Message: fmt.Sprintf(format, args...)}
185}