~mort/coffeepaste

92795c856c6227d334635538d5176f6fe34de988 — Martin Dørum 9 days ago 656c067 master
respond properly to range requests
1 files changed, 14 insertions(+), 14 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +14 -14
@@ 129,22 129,22 @@ async fn serve_file(path: &Path, mime: &str, req: &Request<Body>)-> Result<Respo
            Ok(range) => range,
        };

        let end = match end {
            Some(e) => e,
            None => meta.len() - 1,
        };

        file.seek(SeekFrom::Start(start)).await?;

        if let Some(end) = end {
            let len = (end - start + 1) as usize;
            return Ok(rb
                .status(206)
                .header(header::CONTENT_LENGTH, &len.to_string())
                .body(Body::wrap_stream(FramedRead::new(file, BytesCodec::new()).take(len)))
                .unwrap());
        } else {
            return Ok(rb
                .status(206)
                .header(header::CONTENT_LENGTH, &(meta.len() - start).to_string())
                .body(Body::wrap_stream(FramedRead::new(file, BytesCodec::new())))
                .unwrap());
        }
        let content_range = format!("bytes {}-{}/{}", start, end, meta.len());

        let len = (end - start + 1) as usize;
        return Ok(rb
            .status(206)
            .header(header::CONTENT_LENGTH, &len.to_string())
            .header(header::CONTENT_RANGE, &content_range.to_string())
            .body(Body::wrap_stream(FramedRead::new(file, BytesCodec::new()).take(len)))
            .unwrap());
    } else {
        return Ok(rb
            .status(200)