1
1
use async_trait:: async_trait;
2
2
use bytes:: Bytes ;
3
- use reqwest:: header:: { HeaderValue , ACCEPT_RANGES , RANGE } ;
4
- use reqwest:: { Client , IntoUrl , Method , Request , Url } ;
3
+ use reqwest:: header:: { HeaderValue , RANGE } ;
4
+ use reqwest:: { Client , IntoUrl , Method , Request , StatusCode , Url } ;
5
5
6
6
use crate :: async_reader:: AsyncBackend ;
7
7
use crate :: error:: { Error , HttpError } ;
@@ -20,8 +20,6 @@ impl HttpBackend {
20
20
}
21
21
}
22
22
23
- static VALID_ACCEPT_RANGES : HeaderValue = HeaderValue :: from_static ( "bytes" ) ;
24
-
25
23
#[ async_trait]
26
24
impl AsyncBackend for HttpBackend {
27
25
async fn read_exact ( & self , offset : usize , length : usize ) -> Result < Bytes , Error > {
@@ -35,23 +33,19 @@ impl AsyncBackend for HttpBackend {
35
33
}
36
34
37
35
async fn read ( & self , offset : usize , length : usize ) -> Result < Bytes , Error > {
38
- let mut req = Request :: new ( Method :: GET , self . pmtiles_url . clone ( ) ) ;
39
- let range_header = req
40
- . headers_mut ( )
41
- . entry ( RANGE )
42
- . or_insert ( HeaderValue :: from_static ( "" ) ) ;
43
36
let end = offset + length - 1 ;
44
- // This .unwrap() should be safe, since ` offset` and ` end` will always be valid.
45
- * range_header = HeaderValue :: from_str ( format ! ( "bytes={offset}-{end}" ) . as_str ( ) ) . unwrap ( ) ;
37
+ let range = format ! ( "bytes={ offset}-{ end}" ) ;
38
+ let range = HeaderValue :: try_from ( range ) . map_err ( HttpError :: from ) ? ;
46
39
47
- let response = self . client . execute ( req) . await ?. error_for_status ( ) ?;
40
+ let mut req = Request :: new ( Method :: GET , self . pmtiles_url . clone ( ) ) ;
41
+ req. headers_mut ( ) . insert ( RANGE , range) ;
48
42
49
- if response. headers ( ) . get ( ACCEPT_RANGES ) != Some ( & VALID_ACCEPT_RANGES ) {
43
+ let response = self . client . execute ( req) . await ?. error_for_status ( ) ?;
44
+ if response. status ( ) != StatusCode :: PARTIAL_CONTENT {
50
45
return Err ( HttpError :: RangeRequestsUnsupported . into ( ) ) ;
51
46
}
52
47
53
48
let response_bytes = response. bytes ( ) . await ?;
54
-
55
49
if response_bytes. len ( ) > length {
56
50
Err ( HttpError :: ResponseBodyTooLong ( response_bytes. len ( ) , length) . into ( ) )
57
51
} else {
0 commit comments