connectrpc 0.1.0

Connect RPC implementation for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::Result;
use crate::b64;
use crate::codec::Codec;
use crate::error::Error;
use crate::header::{
    ACCEPT_ENCODING, CONNECT_PROTOCOL_VERSION, CONNECT_PROTOCOL_VERSION_1, CONNECT_TIMEOUT_MS,
    CONTENT_ENCODING, CONTENT_TYPE,
};
use crate::metadata::Metadata;
use http::uri::{Authority, Scheme};
use http::{HeaderMap, HeaderName, HeaderValue, Method, Uri};
use std::time::Duration;

#[derive(Debug, Clone)]
pub struct Builder {
    scheme: Option<Scheme>,
    authority: Option<Authority>,
    path_prefix: Option<String>,
    service: Option<String>,
    method: Option<String>,
    metadata: HeaderMap,
    message_codec: Option<Codec>,
    timeout_ms: Option<HeaderValue>,
    query: Option<String>,
    content_encoding: Option<String>,
    accept_encodings: Vec<HeaderValue>,
}

impl Default for Builder {
    fn default() -> Self {
        Self {
            scheme: Some(Scheme::HTTPS),
            authority: Some(Authority::from_static("localhost")),
            path_prefix: None,
            service: None,
            method: None,
            metadata: HeaderMap::new(),
            message_codec: None,
            query: None,
            timeout_ms: None,
            content_encoding: None,
            accept_encodings: vec![],
        }
    }
}

impl Builder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn scheme(mut self, scheme: Scheme) -> Self {
        self.scheme = Some(scheme);
        self
    }

    pub fn authority(mut self, authority: impl Into<String>) -> Result<Self> {
        self.authority = Some(
            authority
                .into()
                .parse()
                .map_err(|e| Error::invalid_request(format!("invalid authority: {}", e)))?,
        );
        Ok(self)
    }

    pub fn path_prefix(mut self, path: impl Into<String>) -> Self {
        self.path_prefix = Some(path.into().trim_matches('/').to_string());
        self
    }

    /// Set the RPC path in the form of `/Service/Method`. The leading '/' will be trimmed if
    /// present.
    pub fn rpc_path(mut self, segment: &str) -> Result<Self> {
        let (svc, method) = segment
            .trim_start_matches('/')
            .rsplit_once('/')
            .ok_or_else(|| {
                Error::invalid_request(format!(
                    "rpc path must be in the form of Service/Method: {segment}"
                ))
            })?;
        self.service = Some(svc.trim_matches('/').to_string());
        self.method = Some(method.trim_matches('/').to_string());
        Ok(self)
    }

    /// Set the query string for the request. The leading '?' will be trimmed if present.
    pub fn query(mut self, query: impl Into<String>) -> Self {
        self.query = Some(query.into().trim_start_matches('?').to_string());
        self
    }

    /// Extends the metadata with the provided headers.
    pub fn append_metadata(mut self, headerrs: HeaderMap) -> Self {
        self.metadata.extend(headerrs);
        self
    }

    /// Add an ASCII metadata entry.
    pub fn append_ascii_metadata(
        mut self,
        key: impl TryInto<HeaderName, Error: Into<Error>>,
        val: impl Into<String>,
    ) -> Result<Self> {
        self.metadata.append_ascii(key, val)?;
        Ok(self)
    }

    /// Add a binary metadata entry.
    pub fn binary_metadata(
        mut self,
        key: impl TryInto<http::header::HeaderName, Error: Into<Error>>,
        val: impl AsRef<[u8]>,
    ) -> Result<Self> {
        self.metadata.append_binary(key, val)?;
        Ok(self)
    }

    /// Set the timeout for the request.
    pub fn timeout(mut self, timeout: Duration) -> Result<Self> {
        let timeout = timeout.as_millis().to_string();
        if timeout.len() > 10 {
            return Err(Error::invalid_request("timeout too large"));
        }
        self.timeout_ms = Some(timeout.try_into().unwrap());
        Ok(self)
    }

    /// Set the content encoding of the request.
    pub fn content_encoding(mut self, encoding: impl Into<String>) -> Self {
        self.content_encoding = Some(encoding.into());
        self
    }

    /// Add an accepted encoding to the request.
    pub fn accept_encodings(mut self, encoding: impl Into<String>) -> Self {
        self.accept_encodings
            .push(encoding.into().try_into().unwrap());
        self
    }

    /// Set the message codec (e.g. proto or json)
    pub fn message_codec(mut self, codec: Codec) -> Self {
        self.message_codec = Some(codec);
        self
    }

    /// assumes validate has been called
    fn request_uri(&mut self) -> Uri {
        let mut path = String::new();
        if let Some(prefix) = &self.path_prefix {
            path.push_str(&format!("/{}", prefix));
        }
        let service = self.service.take().unwrap();
        let method = self.method.take().unwrap();
        path.push_str(&format!("/{}/{}", service, method));
        if let Some(query) = &self.query {
            path.push_str(&format!("?{}", query));
        }

        Uri::builder()
            .scheme(self.scheme.take().unwrap())
            .authority(self.authority.take().unwrap())
            .path_and_query(path)
            .build()
            .unwrap()
    }

    /// Build logic common to all requests.
    fn request_base<T>(&mut self, method: Method, body: T) -> Result<http::Request<T>> {
        let mut req = http::Request::new(body);
        *req.uri_mut() = self.request_uri();
        *req.method_mut() = method;
        let mut headers: HeaderMap = std::mem::take(&mut self.metadata);
        // Connect-Protocol-Version → "connect-protocol-version" "1"
        headers.insert(CONNECT_PROTOCOL_VERSION, CONNECT_PROTOCOL_VERSION_1);
        // Timeout → "connect-timeout-ms" Timeout-Milliseconds
        if let Some(timeout) = self.timeout_ms.take() {
            headers.insert(CONNECT_TIMEOUT_MS, timeout);
        }
        *req.headers_mut() = headers;
        Ok(req)
    }

    fn request_content_encoding(&mut self) -> HeaderValue {
        if let Some(ce) = std::mem::take(&mut self.content_encoding) {
            ce.try_into().unwrap()
        } else {
            HeaderValue::from_static("identity")
        }
    }

    /// Build a unary request with the given message as the body.
    /// POST request will be used.
    ///
    /// The user should ensure that the message is encoded according to the specified codec.
    pub fn unary(mut self, message: Vec<u8>) -> Result<http::Request<Vec<u8>>> {
        self.validate()?;
        let mut req = self.request_base(Method::POST, message)?;
        let headers = req.headers_mut();
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_str(&format!(
                "application/{}",
                self.message_codec.as_ref().unwrap().name()
            ))?,
        );
        headers.insert(CONTENT_ENCODING, self.request_content_encoding());
        // Accept-Encoding → "accept-encoding" Content-Coding [...]
        for value in std::mem::take(&mut self.accept_encodings) {
            headers.append(ACCEPT_ENCODING, value);
        }
        Ok(req)
    }

    fn rebuild_get_query(&mut self, message: &[u8]) {
        let mut query = form_urlencoded::Serializer::new(String::new());
        query
            .append_pair("message", &b64::url_encode(message))
            .append_pair("base64", "1")
            .append_pair("connect", "v1")
            .append_pair("encoding", self.message_codec.as_ref().unwrap().name());
        if let Some(content_encoding) = std::mem::take(&mut self.content_encoding) {
            query.append_pair("compression", &content_encoding);
        }
        self.query = Some(query.finish())
    }

    /// Build a unary GET request with the given message as a base64-encoded query parameter.
    /// GET request will be used.
    ///
    /// The user should ensure that the message is encoded according to the specified codec.
    pub fn unary_get(mut self, message: Vec<u8>) -> Result<http::Request<()>> {
        self.validate()?;
        self.rebuild_get_query(&message);
        let mut req = self.request_base(Method::GET, ())?;
        let headers = req.headers_mut();
        // Accept-Encoding → "accept-encoding" Content-Coding [...]
        for value in std::mem::take(&mut self.accept_encodings) {
            headers.append(ACCEPT_ENCODING, value);
        }
        Ok(req)
    }

    /// Validate that all required fields are set.
    ///
    /// This method will be called automatically by the build methods.
    pub fn validate(&self) -> Result<()> {
        if self.scheme.is_none() {
            return Err(Error::invalid_request(
                "scheme must be set before building request",
            ));
        }
        if self.authority.is_none() {
            return Err(Error::invalid_request(
                "authority must be set before building request",
            ));
        }
        if self.service.is_none() || self.method.is_none() {
            return Err(Error::invalid_request(
                "service and method must be set before building request",
            ));
        }
        if self.message_codec.is_none() {
            return Err(Error::invalid_request(
                "codec must be set before building request",
            ));
        }
        Ok(())
    }
}

/// Parts of a request, used for decomposing and composing requests.
pub struct Parts<T>
where
    T: Send + Sync,
{
    pub metadata: HeaderMap,
    pub body: T,
}

/// Options for configuring expected response based on the request.
#[derive(Debug, Clone)]
pub struct RequestResponseOptions {
    pub message_codec: Codec,
    pub accept_encodings: Vec<String>,
}

impl Default for RequestResponseOptions {
    fn default() -> Self {
        Self {
            message_codec: Codec::Proto,
            accept_encodings: vec!["identity".to_string()],
        }
    }
}

#[derive(Debug, Clone)]
pub struct UnaryRequest<T>
where
    T: Send + Sync,
{
    metadata: HeaderMap,
    message: T,
}

impl<T> UnaryRequest<T>
where
    T: Send + Sync,
{
    pub fn new(message: T) -> Self {
        Self {
            metadata: HeaderMap::new(),
            message,
        }
    }

    pub fn metadata(&self) -> &HeaderMap {
        &self.metadata
    }

    pub fn metadata_mut(&mut self) -> &mut HeaderMap {
        &mut self.metadata
    }

    pub fn into_parts(self) -> Parts<T> {
        Parts {
            metadata: self.metadata,
            body: self.message,
        }
    }

    pub fn from_parts(parts: Parts<T>) -> Self {
        Self {
            metadata: parts.metadata,
            message: parts.body,
        }
    }

    pub fn into_message(self) -> T {
        self.message
    }

    pub fn message(&self) -> &T {
        &self.message
    }
}

#[cfg(feature = "client")]
pub(crate) fn get_timeout<T>(req: &http::Request<T>) -> Option<Duration> {
    req.headers()
        .get(CONNECT_TIMEOUT_MS)
        .and_then(|hv| hv.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok())
        .map(Duration::from_millis)
}

#[cfg(test)]
mod tests {
    use super::*;
    use prost::Message;
    use serde::{Deserialize, Serialize};

    #[derive(Serialize, Deserialize, PartialEq, Message)]
    struct HelloRequest {
        #[prost(string, tag = "1")]
        name: String,
    }

    #[test]
    fn test_builder_unary() {
        for codec in [Codec::Proto, Codec::Json] {
            let request = HelloRequest {
                name: "world".to_string(),
            };
            let body = codec.encode(&request);

            let req = Builder::new()
                .scheme(Scheme::HTTPS)
                .authority("example.com")
                .unwrap()
                .rpc_path("/helloworld.Greeter/SayHello")
                .unwrap()
                .message_codec(codec)
                .unary(body.clone())
                .unwrap();
            assert_eq!(req.method(), Method::POST);
            assert_eq!(
                req.uri(),
                &"https://example.com/helloworld.Greeter/SayHello"
            );
            assert_eq!(
                req.headers().get(CONTENT_TYPE).unwrap(),
                &format!("application/{}", codec.name())
            );
            assert_eq!(req.headers().get(CONTENT_ENCODING).unwrap(), "identity");
            assert_eq!(req.into_body(), body);
        }
    }

    #[test]
    fn test_builder_unary_get() {
        for codec in [Codec::Proto, Codec::Json] {
            let request = HelloRequest {
                name: "world".to_string(),
            };
            let body = codec.encode(&request);

            let req = Builder::new()
                .scheme(Scheme::HTTPS)
                .authority("example.com")
                .unwrap()
                .rpc_path("/helloworld.Greeter/SayHello")
                .unwrap()
                .message_codec(codec)
                .unary_get(body.clone())
                .unwrap();
            assert_eq!(req.method(), Method::GET);
            assert_eq!(req.headers().get(CONTENT_TYPE), None);
            assert_eq!(req.headers().get(CONTENT_ENCODING), None);
            assert_eq!(req.uri().scheme_str(), Some("https"));
            assert_eq!(req.uri().authority().unwrap().as_str(), "example.com");
            assert_eq!(req.uri().path(), "/helloworld.Greeter/SayHello");
            let query = form_urlencoded::parse(req.uri().query().unwrap().as_bytes());
            let query_map: std::collections::HashMap<_, _> = query.into_owned().collect();
            assert_eq!(query_map.get("message").unwrap(), &b64::url_encode(&body));
            assert_eq!(query_map.get("base64").unwrap(), "1");
            assert_eq!(query_map.get("connect").unwrap(), "v1");
            assert_eq!(query_map.get("encoding").unwrap(), codec.name());
        }
    }
}