11package server
22
33import (
4+ "context"
5+ "errors"
46 "fmt"
57 "net/http"
68 "os"
@@ -42,7 +44,7 @@ type InvocationContext interface {
4244}
4345
4446// ServiceMethod is an invocation handler.
45- type ServiceMethod [O ipld.Builder ] func (input invocation.Invocation , context InvocationContext ) (transaction.Transaction [O , ipld.Builder ], error )
47+ type ServiceMethod [O ipld.Builder ] func (context. Context , invocation.Invocation , InvocationContext ) (transaction.Transaction [O , ipld.Builder ], error )
4648
4749// Service is a mapping of service names to handlers, used to define a
4850// service implementation.
@@ -68,7 +70,7 @@ type ServerView interface {
6870 Server
6971 transport.Channel
7072 // Run executes a single invocation and returns a receipt.
71- Run (invocation ServiceInvocation ) (receipt.AnyReceipt , error )
73+ Run (ctx context. Context , invocation ServiceInvocation ) (receipt.AnyReceipt , error )
7274}
7375
7476// ErrorHandlerFunc allows non-result errors generated during handler execution
@@ -102,7 +104,7 @@ func NewServer(id principal.Signer, options ...Option) (ServerView, error) {
102104
103105 validateAuthorization := cfg .validateAuthorization
104106 if validateAuthorization == nil {
105- validateAuthorization = func (auth validator.Authorization [any ]) validator.Revoked {
107+ validateAuthorization = func (context. Context , validator.Authorization [any ]) validator.Revoked {
106108 return nil
107109 }
108110 }
@@ -122,7 +124,7 @@ func NewServer(id principal.Signer, options ...Option) (ServerView, error) {
122124 resolveDIDKey = validator .FailDIDKeyResolution
123125 }
124126
125- ctx := context {id , canIssue , validateAuthorization , resolveProof , parsePrincipal , resolveDIDKey , cfg .authorityProofs , cfg .altAudiences }
127+ ctx := serverContext {id , canIssue , validateAuthorization , resolveProof , parsePrincipal , resolveDIDKey , cfg .authorityProofs , cfg .altAudiences }
126128 svr := & server {id , cfg .service , ctx , codec , catch }
127129 return svr , nil
128130}
@@ -132,7 +134,7 @@ func ParsePrincipal(str string) (principal.Verifier, error) {
132134 return verifier .Parse (str )
133135}
134136
135- type context struct {
137+ type serverContext struct {
136138 id principal.Signer
137139 canIssue validator.CanIssueFunc [any ]
138140 validateAuthorization validator.RevocationCheckerFunc [any ]
@@ -143,36 +145,36 @@ type context struct {
143145 altAudiences []ucan.Principal
144146}
145147
146- func (ctx context ) ID () principal.Signer {
148+ func (ctx serverContext ) ID () principal.Signer {
147149 return ctx .id
148150}
149151
150- func (ctx context ) CanIssue (capability ucan.Capability [any ], issuer did.DID ) bool {
151- return ctx .canIssue (capability , issuer )
152+ func (sctx serverContext ) CanIssue (capability ucan.Capability [any ], issuer did.DID ) bool {
153+ return sctx .canIssue (capability , issuer )
152154}
153155
154- func (ctx context ) ValidateAuthorization (auth validator.Authorization [any ]) validator.Revoked {
155- return ctx .validateAuthorization (auth )
156+ func (sctx serverContext ) ValidateAuthorization (ctx context. Context , auth validator.Authorization [any ]) validator.Revoked {
157+ return sctx .validateAuthorization (ctx , auth )
156158}
157159
158- func (ctx context ) ResolveProof (proof ucan.Link ) (delegation.Delegation , validator.UnavailableProof ) {
159- return ctx .resolveProof (proof )
160+ func (sctx serverContext ) ResolveProof (ctx context. Context , proof ucan.Link ) (delegation.Delegation , validator.UnavailableProof ) {
161+ return sctx .resolveProof (ctx , proof )
160162}
161163
162- func (ctx context ) ParsePrincipal (str string ) (principal.Verifier , error ) {
163- return ctx .parsePrincipal (str )
164+ func (sctx serverContext ) ParsePrincipal (str string ) (principal.Verifier , error ) {
165+ return sctx .parsePrincipal (str )
164166}
165167
166- func (ctx context ) ResolveDIDKey (did did.DID ) (did.DID , validator.UnresolvedDID ) {
167- return ctx .resolveDIDKey (did )
168+ func (sctx serverContext ) ResolveDIDKey (ctx context. Context , did did.DID ) (did.DID , validator.UnresolvedDID ) {
169+ return sctx .resolveDIDKey (ctx , did )
168170}
169171
170- func (ctx context ) AuthorityProofs () []delegation.Delegation {
171- return ctx .authorityProofs
172+ func (sctx serverContext ) AuthorityProofs () []delegation.Delegation {
173+ return sctx .authorityProofs
172174}
173175
174- func (ctx context ) AlternativeAudiences () []ucan.Principal {
175- return ctx .altAudiences
176+ func (sctx serverContext ) AlternativeAudiences () []ucan.Principal {
177+ return sctx .altAudiences
176178}
177179
178180type server struct {
@@ -199,12 +201,12 @@ func (srv *server) Codec() transport.InboundCodec {
199201 return srv .codec
200202}
201203
202- func (srv * server ) Request (request transport.HTTPRequest ) (transport.HTTPResponse , error ) {
203- return Handle (srv , request )
204+ func (srv * server ) Request (ctx context. Context , request transport.HTTPRequest ) (transport.HTTPResponse , error ) {
205+ return Handle (ctx , srv , request )
204206}
205207
206- func (srv * server ) Run (invocation ServiceInvocation ) (receipt.AnyReceipt , error ) {
207- return Run (srv , invocation )
208+ func (srv * server ) Run (ctx context. Context , invocation ServiceInvocation ) (receipt.AnyReceipt , error ) {
209+ return Run (ctx , srv , invocation )
208210}
209211
210212func (srv * server ) Catch (err HandlerExecutionError [any ]) {
@@ -214,7 +216,7 @@ func (srv *server) Catch(err HandlerExecutionError[any]) {
214216var _ transport.Channel = (* server )(nil )
215217var _ ServerView = (* server )(nil )
216218
217- func Handle (server Server , request transport.HTTPRequest ) (transport.HTTPResponse , error ) {
219+ func Handle (ctx context. Context , server Server , request transport.HTTPRequest ) (transport.HTTPResponse , error ) {
218220 selection , aerr := server .Codec ().Accept (request )
219221 if aerr != nil {
220222 return thttp .NewHTTPResponse (aerr .Status (), strings .NewReader (aerr .Error ()), aerr .Headers ()), nil
@@ -225,15 +227,15 @@ func Handle(server Server, request transport.HTTPRequest) (transport.HTTPRespons
225227 return thttp .NewHTTPResponse (http .StatusBadRequest , strings .NewReader ("The server failed to decode the request payload. Please format the payload according to the specified media type." ), nil ), nil
226228 }
227229
228- result , err := Execute (server , msg )
230+ result , err := Execute (ctx , server , msg )
229231 if err != nil {
230232 return nil , err
231233 }
232234
233235 return selection .Encoder ().Encode (result )
234236}
235237
236- func Execute (server Server , msg message.AgentMessage ) (message.AgentMessage , error ) {
238+ func Execute (ctx context. Context , server Server , msg message.AgentMessage ) (message.AgentMessage , error ) {
237239 br , err := blockstore .NewBlockReader (blockstore .WithBlocksIterator (msg .Blocks ()))
238240 if err != nil {
239241 return nil , err
@@ -256,7 +258,7 @@ func Execute(server Server, msg message.AgentMessage) (message.AgentMessage, err
256258 wg .Add (1 )
257259 go func (inv invocation.Invocation ) {
258260 defer wg .Done ()
259- rcpt , err := Run (server , inv )
261+ rcpt , err := Run (ctx , server , inv )
260262 if err != nil {
261263 rerr = err
262264 return
@@ -276,7 +278,7 @@ func Execute(server Server, msg message.AgentMessage) (message.AgentMessage, err
276278 return message .Build (nil , rcpts )
277279}
278280
279- func Run (server Server , invocation ServiceInvocation ) (receipt.AnyReceipt , error ) {
281+ func Run (ctx context. Context , server Server , invocation ServiceInvocation ) (receipt.AnyReceipt , error ) {
280282 caps := invocation .Capabilities ()
281283 // Invocation needs to have one single capability
282284 if len (caps ) != 1 {
@@ -291,8 +293,11 @@ func Run(server Server, invocation ServiceInvocation) (receipt.AnyReceipt, error
291293 return receipt .Issue (server .ID (), result .NewFailure (err ), ran .FromInvocation (invocation ))
292294 }
293295
294- tx , err := handle (invocation , server .Context ())
296+ tx , err := handle (ctx , invocation , server .Context ())
295297 if err != nil {
298+ if errors .Is (err , context .Canceled ) {
299+ return nil , err
300+ }
296301 herr := NewHandlerExecutionError (err , cap )
297302 server .Catch (herr )
298303 return receipt .Issue (server .ID (), result .NewFailure (herr ), ran .FromInvocation (invocation ))
0 commit comments