• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! This is a simple asynchronous HTTP client example in concurrent scenarios
15 //! using the ylong_http_client crate. It demonstrates creating a client, making
16 //! a request, and reading the response asynchronously.
17 #[cfg(feature = "tokio_base")]
18 use std::sync::Arc;
19 
20 #[cfg(feature = "tokio_base")]
21 use ylong_http_client::async_impl::{Body, ClientBuilder};
22 #[cfg(feature = "tokio_base")]
23 use ylong_http_client::{HttpClientError, RequestBuilder, StatusCode, TextBody, Version};
24 #[cfg(feature = "tokio_base")]
main() -> Result<(), HttpClientError>25 fn main() -> Result<(), HttpClientError> {
26     let rt = tokio::runtime::Builder::new_multi_thread()
27         .worker_threads(1)
28         .enable_all()
29         .build()
30         .expect("Build runtime failed.");
31 
32     let client = ClientBuilder::new().http2_prior_knowledge().build()?;
33 
34     let client_interface = Arc::new(client);
35     let mut shut_downs = vec![];
36 
37     for i in 0..5 {
38         let client = client_interface.clone();
39         let handle = rt.spawn(async move {
40             let body_text = format!("hello {i}");
41             let request = RequestBuilder::new()
42                 .version("HTTP/2.0")
43                 .url("127.0.0.1:5678")
44                 .method("GET")
45                 .header("host", "127.0.0.1")
46                 .body(TextBody::from_bytes(body_text.as_bytes()))?;
47 
48             let mut response = client.request(request).await?;
49             assert_eq!(response.version(), &Version::HTTP2);
50             assert_eq!(response.status(), &StatusCode::OK);
51 
52             let mut buf = [0u8; 4096];
53             let mut size = 0;
54 
55             loop {
56                 let read = response
57                     .body_mut()
58                     .data(&mut buf[size..])
59                     .await
60                     .expect("Response body read failed");
61                 if read == 0 {
62                     break;
63                 }
64                 size += read;
65             }
66             assert_eq!(
67                 &buf[..size],
68                 "hello world".as_bytes(),
69                 "Assert response body failed"
70             );
71         });
72 
73         shut_downs.push(handle);
74     }
75 
76     for shut_down in shut_downs {
77         rt.block_on(shut_down)
78             .expect("Runtime wait for server shutdown failed");
79     }
80     Ok(())
81 }
82