@@ -258,6 +258,15 @@ pub(crate) async fn http_cache_cleanup(context: &Context) -> Result<()> {
258258 Ok ( ( ) )
259259}
260260
261+ /// Returns the request target in origin form, i.e. the path and query of `url`.
262+ ///
263+ /// The absolute form is only for proxy requests and
264+ /// nginx rejects it if the host starts contains an underscore.
265+ fn origin_form ( url : & hyper:: Uri ) -> & str {
266+ url. path_and_query ( )
267+ . map_or ( "/" , |path_and_query| path_and_query. as_str ( ) )
268+ }
269+
261270/// Fetches URL and updates the cache.
262271///
263272/// URL is fetched regardless of whether there is an existing result in the cache.
@@ -276,7 +285,7 @@ async fn fetch_url(context: &Context, original_url: &str, strict_tls: bool) -> R
276285 . context ( "URL has no authority" ) ?
277286 . clone ( ) ;
278287
279- let req = hyper:: Request :: builder ( ) . uri ( parsed_url) ;
288+ let req = hyper:: Request :: builder ( ) . uri ( origin_form ( & parsed_url) ) ;
280289
281290 // OSM usage policy requires
282291 // that User-Agent is set for HTTP GET requests
@@ -409,7 +418,7 @@ pub(crate) async fn post_empty(context: &Context, url: &str) -> Result<(String,
409418 . authority ( )
410419 . context ( "URL has no authority" ) ?
411420 . clone ( ) ;
412- let req = hyper:: Request :: post ( parsed_url)
421+ let req = hyper:: Request :: post ( origin_form ( & parsed_url) )
413422 . header ( hyper:: header:: HOST , authority. as_str ( ) )
414423 . body ( http_body_util:: Empty :: < Bytes > :: new ( ) ) ?;
415424
@@ -432,6 +441,20 @@ mod tests {
432441 use crate :: test_utils:: TestContext ;
433442 use crate :: tools:: SystemTime ;
434443
444+ #[ test]
445+ fn test_origin_form ( ) {
446+ let url = "https://_cm0.localchat/autoconfig?emailaddress=x%40_cm0.localchat"
447+ . parse ( )
448+ . unwrap ( ) ;
449+ assert_eq ! (
450+ origin_form( & url) ,
451+ "/autoconfig?emailaddress=x%40_cm0.localchat"
452+ ) ;
453+
454+ let url = "https://example.org" . parse ( ) . unwrap ( ) ;
455+ assert_eq ! ( origin_form( & url) , "/" ) ;
456+ }
457+
435458 #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
436459 async fn test_http_cache ( ) -> Result < ( ) > {
437460 let t = & TestContext :: new ( ) . await ;
0 commit comments