erickguan commented on code in PR #9442:
URL: https://github.com/apache/opendal/pull/9442#discussion_r4741247864


##########
core/core/src/types/http_transport/mod.rs:
##########
@@ -47,6 +47,11 @@ static DEFAULT_HTTP_TRANSPORTER: OnceLock<HttpTransporter> = 
OnceLock::new();
 /// Implement this trait to provide a custom HTTP backend. A transport must
 /// support 3xx redirection.
 /// It can support [`HttpRedirect`] to let services reuse redirect 
destinations.
+///
+/// See [Set default request headers] for an example that configures a reqwest
+/// client for one operator.
+///
+/// [Set default request headers]: 
crate::docs::performance::http_optimization#set-default-request-headers

Review Comment:
   ```suggestion
   /// Implement this trait to provide a custom HTTP backend, or wrap an 
existing
   /// [`HttpTransporter`] to inject or override request headers for one 
operator.
   /// A transport must support 3xx redirection.
   /// It can support [`HttpRedirect`] to let services reuse redirect 
destinations.
   ///
   /// See [Customize request headers] for examples using a reqwest client's
   /// default headers and a custom `HttpTransport` wrapper.
   ///
   /// [Customize request headers]: 
crate::docs::performance::http_optimization#customize-request-headers
   ```



##########
core/core/src/docs/performance/http_optimization.md:
##########
@@ -28,6 +28,26 @@ fn with_reqwest_client(op: Operator, client: 
reqwest::Client) -> Operator {
 Creating a client or operator per request discards DNS and connection-pool
 state and repeatedly pays connection setup costs.
 
+## Set default request headers
+
+Use `ClientBuilder::default_headers` on the reqwest client you install with
+`with_reqwest_client` to set a default header for one operator.
+
+```rust,ignore
+use reqwest::header::{HeaderMap, HeaderValue};
+
+let mut headers = HeaderMap::new();
+headers.insert("x-client-label", HeaderValue::from_static("example"));
+let client = reqwest::Client::builder().default_headers(headers).build()?;
+let op = with_reqwest_client(op, client);
+```
+
+Reqwest fills in missing headers. Explicit request headers take precedence.
+Use harmless values because defaults can reach other endpoints contacted by
+the same client. Services that sign requests do so before handing them to the
+transport. A header that must participate in the signature needs to be present
+at signing time.

Review Comment:
   ````suggestion
   ## Customize request headers
   
   OpenDAL keeps HTTP-specific header configuration at the `HttpTransport`
   boundary rather than adding header maps to `Op*` options.
   
   ### Set default headers with reqwest
   
   Use `ClientBuilder::default_headers` on the reqwest client you install with
   `with_reqwest_client` to set a default header for one operator:
   
   ```rust,ignore
   use reqwest::header::{HeaderMap, HeaderValue};
   
   let mut headers = HeaderMap::new();
   headers.insert("x-client-label", HeaderValue::from_static("example"));
   let client = reqwest::Client::builder().default_headers(headers).build()?;
   let op = with_reqwest_client(op, client);
   ```
   
   Reqwest fills in missing headers, so explicit request headers take 
precedence.
   Use harmless values because defaults can reach other endpoints contacted by 
the
   same client (such as `reqsign` credential endpoints and redirects).
   
   ### Scope or override headers with an `HttpTransport` wrapper
   
   When you need to restrict a header to specific operations or hosts, or 
override
   a header that a service already sets (such as working around an S3-compatible
   vendor quirk), wrap the operator's `HttpTransporter`.
   
   For example, a gateway that stores zstd-compressed objects may decompress 
them
   on every read unless the client sends `Accept-Encoding: zstd`. Checking
   `opendal::raw::Operation` applies the header only to `Operation::Read`, 
leaving
   `Operation::List` `GET` requests and `reqsign` token requests unchanged:
   
   ```rust,ignore
   use http::{HeaderValue, Request, Response, header::ACCEPT_ENCODING};
   use opendal::raw::Operation;
   use opendal::{Buffer, HttpBody, HttpTransport, HttpTransporter, Operator, 
Result};
   
   struct ReadZstdTransport {
       inner: HttpTransporter,
   }
   
   impl HttpTransport for ReadZstdTransport {
       async fn fetch(&self, mut req: Request<Buffer>) -> 
Result<Response<HttpBody>> {
           if req.extensions().get::<Operation>() == Some(&Operation::Read) {
               req.headers_mut()
                   .insert(ACCEPT_ENCODING, HeaderValue::from_static("zstd"));
           }
           self.inner.fetch(req).await
       }
   }
   
   fn with_read_zstd(op: Operator) -> Operator {
       let inner = op.base_context().http_transport().clone();
       let transport = HttpTransporter::new(ReadZstdTransport { inner });
       let ctx = op.base_context().with_http_transport(transport);
       op.with_context(ctx)
   }
   ```
   
   Services that sign requests (such as S3, OSS, COS, and Azure) do so before
   handing them to the transport. Adding an unsigned header preserves the
   signature, whereas adding a header that must be signed or overriding a signed
   header will fail signature verification on strict servers.
   ````



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to