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 //! Construct the http server using TcpStream. 15 16 use std::sync::mpsc::Receiver; 17 18 pub(crate) struct TcpHandle { 19 pub addr: String, 20 21 // This channel allows the server to notify the client when it has shut down. 22 pub server_shutdown: Receiver<()>, 23 } 24 25 pub(crate) fn format_header_str(key: &str, value: &str) -> String { 26 format!("{}:{}\r\n", key.to_ascii_lowercase(), value) 27 } 28 29 #[macro_export] 30 macro_rules! build_client_request { 31 ( 32 Request: { 33 $(Method: $method: expr,)? 34 $( 35 Version: $version: expr, 36 )? 37 Path: $path: expr, 38 Addr: $addr: expr, 39 $( 40 Header: $req_n: expr, $req_v: expr, 41 )* 42 Body: $req_body: expr, 43 }, 44 ) => {{ 45 Request::builder() 46 $(.method($method))? 47 $(.version($version))? 48 .url(format!("http://{}{}",$addr, $path).as_str()) 49 $(.header($req_n, $req_v))* 50 .body($req_body) 51 .expect("Request build failed") 52 }}; 53 } 54 55 #[macro_export] 56 macro_rules! start_tcp_server { 57 ( 58 Handles: $handle_vec: expr, 59 $(EndWith: $end: expr,)? 60 $( 61 Response: { 62 Status: $status: expr, 63 Version: $version: expr, 64 $( 65 Header: $resp_n: expr, $resp_v: expr, 66 )* 67 Body: $resp_body: expr, 68 }, 69 )? 70 $(Shutdown: $shutdown: expr,)? 71 72 ) => {{ 73 use std::sync::mpsc::channel; 74 use ylong_runtime::net::TcpListener; 75 use ylong_runtime::io::AsyncReadExt; 76 77 let (tx, rx) = channel(); 78 let (tx2, rx2) = channel(); 79 80 ylong_runtime::spawn(async move { 81 82 let server = TcpListener::bind("127.0.0.1:0").await.expect("server is failed to bind a address !"); 83 let addr = server.local_addr().expect("failed to get server address !"); 84 let handle = TcpHandle { 85 addr: addr.to_string(), 86 server_shutdown: rx, 87 }; 88 tx2.send(handle).expect("send TcpHandle out coroutine failed !"); 89 90 let (mut stream, _client) = server.accept().await.expect("failed to build a tcp stream"); 91 92 let mut buf = [0u8; 4096]; 93 let _size = stream.read(&mut buf).await.expect("tcp stream read error !"); 94 95 $( 96 let mut total = _size; 97 while !&buf[..total].ends_with($end.as_bytes()) { 98 let tmp_size = stream.read(&mut buf[total..]).await.expect("tcp stream read error !"); 99 total += tmp_size; 100 } 101 )? 102 $( 103 { 104 let crlf = "\r\n"; 105 let mut resp_str = String::from(format!("{} {} OK\r\n", $version, $status)); 106 $( 107 let header = format_header_str($resp_n, $resp_v); 108 resp_str.push_str(header.as_str()); 109 )* 110 resp_str.push_str(crlf); 111 resp_str.push_str($resp_body); 112 113 stream.write_all(resp_str.as_bytes()).await.expect("server write response failed"); 114 } 115 )? 116 $( 117 stream.shutdown($shutdown).expect("server shutdown failed"); 118 )? 119 120 tx.send(()).expect("server send order failed !"); 121 122 }); 123 124 let handle = rx2.recv().expect("recv server handle failed !"); 125 126 $handle_vec.push(handle); 127 }}; 128 } 129