1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Proper HTTP client

This commit is contained in:
Quentin Gliech
2022-02-10 15:33:19 +01:00
parent 2df40762a2
commit 8c36e51176
6 changed files with 306 additions and 100 deletions

View File

@ -0,0 +1,73 @@
// Copyright 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use clap::Parser;
use hyper::Uri;
use tokio::io::AsyncWriteExt;
use tower::{Service, ServiceExt};
#[derive(Parser, Debug)]
pub(super) struct Options {
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(Parser, Debug)]
enum Subcommand {
/// Perform an HTTP request with the default HTTP client
Http {
/// Show response headers
#[clap(long, short = 'I')]
show_headers: bool,
/// URI where to perform a GET request
url: Uri,
},
}
impl Options {
#[tracing::instrument(skip_all)]
pub async fn run(&self, _root: &super::Options) -> anyhow::Result<()> {
use Subcommand as SC;
match &self.subcommand {
SC::Http { show_headers, url } => {
let mut client = mas_http::client("cli-debug-http").await?;
let request = hyper::Request::builder()
.uri(url)
.body(hyper::Body::empty())?;
let mut response = client.ready().await?.call(request).await?;
if *show_headers {
let status = response.status();
println!(
"{:?} {} {}",
response.version(),
status.as_str(),
status.canonical_reason().unwrap_or_default()
);
for (header, value) in response.headers() {
println!("{}: {:?}", header, value);
}
println!();
}
let mut body = hyper::body::aggregate(response.body_mut()).await?;
let mut stdout = tokio::io::stdout();
stdout.write_all_buf(&mut body).await?;
Ok(())
}
}
}
}

View File

@ -20,6 +20,7 @@ use mas_config::ConfigurationSection;
mod config;
mod database;
mod debug;
mod manage;
mod server;
mod templates;
@ -40,6 +41,9 @@ enum Subcommand {
/// Templates-related commands
Templates(self::templates::Options),
/// Debug utilities
Debug(self::debug::Options),
}
#[derive(Parser, Debug)]
@ -67,6 +71,7 @@ impl Options {
Some(S::Server(c)) => c.run(self).await,
Some(S::Manage(c)) => c.run(self).await,
Some(S::Templates(c)) => c.run(self).await,
Some(S::Debug(c)) => c.run(self).await,
None => self::server::Options::default().run(self).await,
}
}

View File

@ -27,7 +27,7 @@ use mas_email::{MailTransport, Mailer};
use mas_storage::MIGRATOR;
use mas_tasks::TaskQueue;
use mas_templates::Templates;
use tower::make::Shared;
use tower::{make::Shared, Layer};
use tracing::{error, info};
#[derive(Parser, Debug, Default)]
@ -211,7 +211,7 @@ impl Options {
let warp_service = warp::service(root);
let service = mas_http::server(warp_service);
let service = mas_http::ServerLayer::default().layer(warp_service);
info!("Listening on http://{}", listener.local_addr().unwrap());