MCPcopy Create free account
hub / github.com/Ray-D-Song/lexe / build_request

Function build_request

modules/llrt_http/src/fetch.rs:167–216  ·  view source on GitHub ↗
(
    ctx: &Ctx<'_>,
    method: &hyper::Method,
    uri: &Uri,
    headers: Option<&Headers>,
    body: Option<&BodyBytes>,
    prev_status: &u16,
    initial_uri: &Uri,
)

Source from the content-addressed store, hash-verified

165}
166
167fn build_request(
168 ctx: &Ctx<'_>,
169 method: &hyper::Method,
170 uri: &Uri,
171 headers: Option<&Headers>,
172 body: Option<&BodyBytes>,
173 prev_status: &u16,
174 initial_uri: &Uri,
175) -> Result<Request<BoxBody<Bytes, Infallible>>> {
176 let same_origin = is_same_origin(uri, initial_uri);
177
178 let change_method = should_change_method(*prev_status, method);
179
180 let (method_to_use, body) = if change_method {
181 (Method::GET, None)
182 } else {
183 (method.clone(), body)
184 };
185
186 let mut req = Request::builder().method(method_to_use).uri(uri.clone());
187
188 let mut detected_headers = HashSet::new();
189
190 if let Some(headers) = headers {
191 for (header_name, value) in headers.iter() {
192 detected_headers.insert(header_name);
193 if change_method && is_request_body_header_name(header_name) {
194 continue;
195 }
196 if !same_origin && is_cors_non_wildcard_request_header_name(header_name) {
197 continue;
198 }
199 req = req.header(header_name, value)
200 }
201 }
202
203 if !detected_headers.contains("user-agent") {
204 req = req.header("user-agent", ["llrt ", VERSION].concat());
205 }
206 if !detected_headers.contains("accept-encoding") {
207 req = req.header("accept-encoding", "zstd, br, gzip, deflate");
208 }
209 if !detected_headers.contains("accept") {
210 req = req.header("accept", "*/*");
211 }
212 req.body(BoxBody::new(
213 body.map(|b| b.body.clone()).unwrap_or_default(),
214 ))
215 .or_throw(ctx)
216}
217
218fn is_same_origin(uri: &Uri, initial_uri: &Uri) -> bool {
219 is_same_scheme(uri, initial_uri)

Callers 1

initFunction · 0.85

Calls 10

should_change_methodFunction · 0.85
methodMethod · 0.80
concatMethod · 0.80
or_throwMethod · 0.80
is_same_originFunction · 0.70
cloneMethod · 0.45
iterMethod · 0.45
bodyMethod · 0.45

Tested by

no test coverage detected