pub fn into_response(self, req: &HttpRequest) -> HttpResponse {
        if self.status_code != StatusCode::OK {
            let mut res = HttpResponse::build(self.status_code);

            if self.flags.contains(Flags::PREFER_UTF8) {
                let ct = equiv_utf8_text(self.content_type.clone());
                res.insert_header((header::CONTENT_TYPE, ct.to_string()));
            } else {
                res.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
            }

            if self.flags.contains(Flags::CONTENT_DISPOSITION) {
                res.insert_header((
                    header::CONTENT_DISPOSITION,
                    self.content_disposition.to_string(),
                ));
            }

            if let Some(current_encoding) = self.encoding {
                res.encoding(current_encoding);
            }

            let reader = ChunkedReadFile::new(self.md.len(), 0, self.file);

            return res.streaming(reader);
        }

        let etag = if self.flags.contains(Flags::ETAG) {
            self.etag()
        } else {
            None
        };

        let last_modified = if self.flags.contains(Flags::LAST_MD) {
            self.last_modified()
        } else {
            None
        };

        // check preconditions
        let precondition_failed = if !any_match(etag.as_ref(), req) {
            true
        } else if let (Some(ref m), Some(header::IfUnmodifiedSince(ref since))) =
            (last_modified, req.get_header())
        {
            let t1: SystemTime = (*m).into();
            let t2: SystemTime = (*since).into();

            match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
                (Ok(t1), Ok(t2)) => t1.as_secs() > t2.as_secs(),
                _ => false,
            }
        } else {
            false
        };

        // check last modified
        let not_modified = if !none_match(etag.as_ref(), req) {
            true
        } else if req.headers().contains_key(header::IF_NONE_MATCH) {
            false
        } else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) =
            (last_modified, req.get_header())
        {
            let t1: SystemTime = (*m).into();
            let t2: SystemTime = (*since).into();

            match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
                (Ok(t1), Ok(t2)) => t1.as_secs() <= t2.as_secs(),
                _ => false,
            }
        } else {
            false
        };

        let mut resp = HttpResponse::build(self.status_code);

        if self.flags.contains(Flags::PREFER_UTF8) {
            let ct = equiv_utf8_text(self.content_type.clone());
            resp.insert_header((header::CONTENT_TYPE, ct.to_string()));
        } else {
            resp.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
        }

        if self.flags.contains(Flags::CONTENT_DISPOSITION) {
            resp.insert_header((
                header::CONTENT_DISPOSITION,
                self.content_disposition.to_string(),
            ));
        }

        // default compressing
        if let Some(current_encoding) = self.encoding {
            resp.encoding(current_encoding);
        }

        if let Some(lm) = last_modified {
            resp.insert_header((header::LAST_MODIFIED, lm.to_string()));
        }

        if let Some(etag) = etag {
            resp.insert_header((header::ETAG, etag.to_string()));
        }

        resp.insert_header((header::ACCEPT_RANGES, "bytes"));

        let mut length = self.md.len();
        let mut offset = 0;

        // check for range header
        if let Some(ranges) = req.headers().get(header::RANGE) {
            if let Ok(ranges_header) = ranges.to_str() {
                if let Ok(ranges) = HttpRange::parse(ranges_header, length) {
                    length = ranges[0].length;
                    offset = ranges[0].start;

                    resp.encoding(ContentEncoding::Identity);
                    resp.insert_header((
                        header::CONTENT_RANGE,
                        format!("bytes {}-{}/{}", offset, offset + length - 1, self.md.len()),
                    ));
                } else {
                    resp.insert_header((header::CONTENT_RANGE, format!("bytes */{}", length)));
                    return resp.status(StatusCode::RANGE_NOT_SATISFIABLE).finish();
                };
            } else {
                return resp.status(StatusCode::BAD_REQUEST).finish();
            };
        };

        if precondition_failed {
            return resp.status(StatusCode::PRECONDITION_FAILED).finish();
        } else if not_modified {
            return resp.status(StatusCode::NOT_MODIFIED).finish();
        }

        let reader = ChunkedReadFile::new(length, offset, self.file);

        if offset != 0 || length != self.md.len() {
            resp.status(StatusCode::PARTIAL_CONTENT);
        }

        resp.body(SizedStream::new(length, reader))
    }
/// Creates an `HttpResponse` with file as a streaming body.
Creates an `HttpResponse` with file as a streaming body.
Creates an ` HttpResponse ` with file as a streaming body .
pub fn into_response(self, req: &HttpRequest) -> HttpResponse {
        if self.status_code != StatusCode::OK {
            let mut res = HttpResponse::build(self.status_code);
            if self.flags.contains(Flags::PREFER_UTF8) {
                let ct = equiv_utf8_text(self.content_type.clone());
                res.insert_header((header::CONTENT_TYPE, ct.to_string()));
            } else {
                res.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
            }
            if self.flags.contains(Flags::CONTENT_DISPOSITION) {
                res.insert_header((
                    header::CONTENT_DISPOSITION,
                    self.content_disposition.to_string(),
                ));
            }
            if let Some(current_encoding) = self.encoding {
                res.encoding(current_encoding);
            }
            let reader = ChunkedReadFile::new(self.md.len(), 0, self.file);
            return res.streaming(reader);
        }
        let etag = if self.flags.contains(Flags::ETAG) {
            self.etag()
        } else {
            None
        };
        let last_modified = if self.flags.contains(Flags::LAST_MD) {
            self.last_modified()
        } else {
            None
        };
        // check preconditions
        let precondition_failed = if !any_match(etag.as_ref(), req) {
            true
        } else if let (Some(ref m), Some(header::IfUnmodifiedSince(ref since))) =
            (last_modified, req.get_header())
        {
            let t1: SystemTime = (*m).into();
            let t2: SystemTime = (*since).into();
            match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
                (Ok(t1), Ok(t2)) => t1.as_secs() > t2.as_secs(),
                _ => false,
            }
        } else {
            false
        };
        // check last modified
        let not_modified = if !none_match(etag.as_ref(), req) {
            true
        } else if req.headers().contains_key(header::IF_NONE_MATCH) {
            false
        } else if let (Some(ref m), Some(header::IfModifiedSince(ref since))) =
            (last_modified, req.get_header())
        {
            let t1: SystemTime = (*m).into();
            let t2: SystemTime = (*since).into();
            match (t1.duration_since(UNIX_EPOCH), t2.duration_since(UNIX_EPOCH)) {
                (Ok(t1), Ok(t2)) => t1.as_secs() <= t2.as_secs(),
                _ => false,
            }
        } else {
            false
        };
        let mut resp = HttpResponse::build(self.status_code);
        if self.flags.contains(Flags::PREFER_UTF8) {
            let ct = equiv_utf8_text(self.content_type.clone());
            resp.insert_header((header::CONTENT_TYPE, ct.to_string()));
        } else {
            resp.insert_header((header::CONTENT_TYPE, self.content_type.to_string()));
        }
        if self.flags.contains(Flags::CONTENT_DISPOSITION) {
            resp.insert_header((
                header::CONTENT_DISPOSITION,
                self.content_disposition.to_string(),
            ));
        }
        // default compressing
        if let Some(current_encoding) = self.encoding {
            resp.encoding(current_encoding);
        }
        if let Some(lm) = last_modified {
            resp.insert_header((header::LAST_MODIFIED, lm.to_string()));
        }
        if let Some(etag) = etag {
            resp.insert_header((header::ETAG, etag.to_string()));
        }
        resp.insert_header((header::ACCEPT_RANGES, "bytes"));
        let mut length = self.md.len();
        let mut offset = 0;
        // check for range header
        if let Some(ranges) = req.headers().get(header::RANGE) {
            if let Ok(ranges_header) = ranges.to_str() {
                if let Ok(ranges) = HttpRange::parse(ranges_header, length) {
                    length = ranges[0].length;
                    offset = ranges[0].start;
                    resp.encoding(ContentEncoding::Identity);
                    resp.insert_header((
                        header::CONTENT_RANGE,
                        format!("bytes {}-{}/{}", offset, offset + length - 1, self.md.len()),
                    ));
                } else {
                    resp.insert_header((header::CONTENT_RANGE, format!("bytes */{}", length)));
                    return resp.status(StatusCode::RANGE_NOT_SATISFIABLE).finish();
                };
            } else {
                return resp.status(StatusCode::BAD_REQUEST).finish();
            };
        };
        if precondition_failed {
            return resp.status(StatusCode::PRECONDITION_FAILED).finish();
        } else if not_modified {
            return resp.status(StatusCode::NOT_MODIFIED).finish();
        }
        let reader = ChunkedReadFile::new(length, offset, self.file);
        if offset != 0 || length != self.md.len() {
            resp.status(StatusCode::PARTIAL_CONTENT);
        }
        resp.body(SizedStream::new(length, reader))
    }
pub fn into_response ( self , req : & HttpRequest ) -> HttpResponse { if self . status_code != StatusCode :: OK { let mut res = HttpResponse :: build ( self . status_code ) ; if self . flags . contains ( Flags :: PREFER_UTF8 ) { let ct = equiv_utf8_text ( self . content_type . clone ( ) ) ; res . insert_header ( ( header :: CONTENT_TYPE , ct . to_string ( ) ) ) ; } else { res . insert_header ( ( header :: CONTENT_TYPE , self . content_type . to_string ( ) ) ) ; } if self . flags . contains ( Flags :: CONTENT_DISPOSITION ) { res . insert_header ( ( header :: CONTENT_DISPOSITION , self . content_disposition . to_string ( ) , ) ) ; } if let Some ( current_encoding ) = self . encoding { res . encoding ( current_encoding ) ; } let reader = ChunkedReadFile :: new ( self . md . len ( ) , 0 , self . file ) ; return res . streaming ( reader ) ; } let etag = if self . flags . contains ( Flags :: ETAG ) { self . etag ( ) } else { None } ; let last_modified = if self . flags . contains ( Flags :: LAST_MD ) { self . last_modified ( ) } else { None } ; // check preconditions let precondition_failed = if ! any_match ( etag . as_ref ( ) , req ) { true } else if let ( Some ( ref m ) , Some ( header :: IfUnmodifiedSince ( ref since ) ) ) = ( last_modified , req . get_header ( ) ) { let t1 : SystemTime = ( * m ) . into ( ) ; let t2 : SystemTime = ( * since ) . into ( ) ; match ( t1 . duration_since ( UNIX_EPOCH ) , t2 . duration_since ( UNIX_EPOCH ) ) { ( Ok ( t1 ) , Ok ( t2 ) ) => t1 . as_secs ( ) > t2 . as_secs ( ) , _ => false , } } else { false } ; // check last modified let not_modified = if ! none_match ( etag . as_ref ( ) , req ) { true } else if req . headers ( ) . contains_key ( header :: IF_NONE_MATCH ) { false } else if let ( Some ( ref m ) , Some ( header :: IfModifiedSince ( ref since ) ) ) = ( last_modified , req . get_header ( ) ) { let t1 : SystemTime = ( * m ) . into ( ) ; let t2 : SystemTime = ( * since ) . into ( ) ; match ( t1 . duration_since ( UNIX_EPOCH ) , t2 . duration_since ( UNIX_EPOCH ) ) { ( Ok ( t1 ) , Ok ( t2 ) ) => t1 . as_secs ( ) <= t2 . as_secs ( ) , _ => false , } } else { false } ; let mut resp = HttpResponse :: build ( self . status_code ) ; if self . flags . contains ( Flags :: PREFER_UTF8 ) { let ct = equiv_utf8_text ( self . content_type . clone ( ) ) ; resp . insert_header ( ( header :: CONTENT_TYPE , ct . to_string ( ) ) ) ; } else { resp . insert_header ( ( header :: CONTENT_TYPE , self . content_type . to_string ( ) ) ) ; } if self . flags . contains ( Flags :: CONTENT_DISPOSITION ) { resp . insert_header ( ( header :: CONTENT_DISPOSITION , self . content_disposition . to_string ( ) , ) ) ; } // default compressing if let Some ( current_encoding ) = self . encoding { resp . encoding ( current_encoding ) ; } if let Some ( lm ) = last_modified { resp . insert_header ( ( header :: LAST_MODIFIED , lm . to_string ( ) ) ) ; } if let Some ( etag ) = etag { resp . insert_header ( ( header :: ETAG , etag . to_string ( ) ) ) ; } resp . insert_header ( ( header :: ACCEPT_RANGES , " " ) ) ; let mut length = self . md . len ( ) ; let mut offset = 0 ; // check for range header if let Some ( ranges ) = req . headers ( ) . get ( header :: RANGE ) { if let Ok ( ranges_header ) = ranges . to_str ( ) { if let Ok ( ranges ) = HttpRange :: parse ( ranges_header , length ) { length = ranges [ 0 ] . length ; offset = ranges [ 0 ] . start ; resp . encoding ( ContentEncoding :: Identity ) ; resp . insert_header ( ( header :: CONTENT_RANGE , format ! ( " " offset offset length 1 self md len ( ) ) , ) ) ; } else { resp . insert_header ( ( header :: CONTENT_RANGE , format ! ( " " length ) ) ) ; return resp . status ( StatusCode :: RANGE_NOT_SATISFIABLE ) . finish ( ) ; } ; } else { return resp . status ( StatusCode :: BAD_REQUEST ) . finish ( ) ; } ; } ; if precondition_failed { return resp . status ( StatusCode :: PRECONDITION_FAILED ) . finish ( ) ; } else if not_modified { return resp . status ( StatusCode :: NOT_MODIFIED ) . finish ( ) ; } let reader = ChunkedReadFile :: new ( length , offset , self . file ) ; if offset != 0 || length != self . md . len ( ) { resp . status ( StatusCode :: PARTIAL_CONTENT ) ; } resp . body ( SizedStream :: new ( length , reader ) ) }
Creates an `HttpResponse` with file as a streaming body.
Creates an ` HttpResponse ` with file as a streaming body .
self
req
&HttpRequest
self
req
&HttpRequest