1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 // Interfaces for defining middleware for Flight servers. Currently
19 // experimental.
20 
21 #pragma once
22 
23 #include <memory>
24 #include <string>
25 
26 #include "arrow/flight/middleware.h"
27 #include "arrow/flight/visibility.h"  // IWYU pragma: keep
28 #include "arrow/status.h"
29 
30 namespace arrow {
31 namespace flight {
32 
33 /// \brief Server-side middleware for a call, instantiated per RPC.
34 ///
35 /// Middleware should be fast and must be infallible: there is no way
36 /// to reject the call or report errors from the middleware instance.
37 class ARROW_FLIGHT_EXPORT ServerMiddleware {
38  public:
39   virtual ~ServerMiddleware() = default;
40 
41   /// \brief Unique name of middleware, used as alternative to RTTI
42   /// \return the string name of the middleware
43   virtual std::string name() const = 0;
44 
45   /// \brief A callback before headers are sent. Extra headers can be
46   /// added, but existing ones cannot be read.
47   virtual void SendingHeaders(AddCallHeaders* outgoing_headers) = 0;
48 
49   /// \brief A callback after the call has completed.
50   virtual void CallCompleted(const Status& status) = 0;
51 };
52 
53 /// \brief A factory for new middleware instances.
54 ///
55 /// If added to a server, this will be called for each RPC (including
56 /// Handshake) to give the opportunity to intercept the call.
57 ///
58 /// It is guaranteed that all server middleware methods are called
59 /// from the same thread that calls the RPC method implementation.
60 class ARROW_FLIGHT_EXPORT ServerMiddlewareFactory {
61  public:
62   virtual ~ServerMiddlewareFactory() = default;
63 
64   /// \brief A callback for the start of a new call.
65   ///
66   /// Return a non-OK status to reject the call with the given status.
67   ///
68   /// \param info Information about the call.
69   /// \param incoming_headers Headers sent by the client for this call.
70   ///     Do not retain a reference to this object.
71   /// \param[out] middleware The middleware instance for this call. If
72   ///     null, no middleware will be added to this call instance from
73   ///     this factory.
74   /// \return Status A non-OK status will reject the call with the
75   ///     given status. Middleware previously in the chain will have
76   ///     their CallCompleted callback called. Other middleware
77   ///     factories will not be called.
78   virtual Status StartCall(const CallInfo& info, const CallHeaders& incoming_headers,
79                            std::shared_ptr<ServerMiddleware>* middleware) = 0;
80 };
81 
82 }  // namespace flight
83 }  // namespace arrow
84