<commit_before>package com.wizzardo.http.response;

import com.wizzardo.epoll.readable.ReadableData;
import com.wizzardo.epoll.readable.ReadableFile;
import com.wizzardo.http.request.Header;
import com.wizzardo.http.request.Header;
import com.wizzardo.http.request.Request;
import com.wizzardo.tools.misc.WrappedException;

import java.io.File;
import java.io.IOException;

/**
 * @author: wizzardo
 * Date: 8/4/14
 */
public class RangeResponse extends Response {
    private File file;
    private Range range;
    private boolean valid = true;

    public RangeResponse(Request request, File file) {
        this.file = file;
        setHeader(Header.KEY_ACCEPT_RANGES, Header.VALUE_BYTES);

        String range = request.header(Header.KEY_RANGE);
        if (range != null) {
            this.range = new Range(range, file.length());
            //TODO check range for validity
            valid = this.range.isValid();
            if (!valid) {
                setStatus(Status._416);
                return;
            }

            setStatus(Status._206);
            setHeader(Header.KEY_CONTENT_RANGE, this.range.toString());
            setHeader(Header.KEY_CONTENT_LENGTH, this.range.length());
        } else {
            this.range = new Range(0, file.length() - 1, file.length());
            setHeader(Header.KEY_CONTENT_LENGTH, file.length());
        }
    }

    @Override
    public ReadableData toReadableBytes() {
        if (!valid)
            return super.toReadableBytes();
        else
            try {
                return buildResponse().append(new ReadableFile(file, range.from, range.length()));
            } catch (IOException e) {
                throw new WrappedException(e);
            }
    }

    private static class Range {
        long from;
        long to;
        long total;

        private Range(long from, long to, long total) {
            this.from = from;
            this.to = to;
            this.total = total;
        }<commit_msg>add keep-alive<commit_after>package com.wizzardo.http.response;

import com.wizzardo.epoll.readable.ReadableData;
import com.wizzardo.epoll.readable.ReadableFile;
import com.wizzardo.http.request.Header;
import com.wizzardo.http.request.Request;
import com.wizzardo.tools.misc.WrappedException;

import java.io.File;
import java.io.IOException;

/**
 * @author: wizzardo
 * Date: 8/4/14
 */
public class RangeResponse extends Response {
    private File file;
    private Range range;
    private boolean valid = true;

    public RangeResponse(Request request, File file) {
        this.file = file;
        setHeader(Header.KEY_ACCEPT_RANGES, Header.VALUE_BYTES);

        String range = request.header(Header.KEY_RANGE);
        if (range != null) {
            this.range = new Range(range, file.length());
            //TODO check range for validity
            valid = this.range.isValid();
            if (!valid) {
                setStatus(Status._416);
                return;
            }

            setStatus(Status._206);
            setHeader(Header.KEY_CONTENT_RANGE, this.range.toString());
            setHeader(Header.KEY_CONTENT_LENGTH, this.range.length());
        } else {
            this.range = new Range(0, file.length() - 1, file.length());
            setHeader(Header.KEY_CONTENT_LENGTH, file.length());
        }
        setHeader(Header.KEY_CONNECTION, Header.VALUE_CONNECTION_KEEP_ALIVE);
    }

    @Override
    public ReadableData toReadableBytes() {
        if (!valid)
            return super.toReadableBytes();
        else
            try {
                return buildResponse().append(new ReadableFile(file, range.from, range.length()));
            } catch (IOException e) {
                throw new WrappedException(e);
            }
    }

    private static class Range {
        long from;
        long to;
        long total;

        private Range(long from, long to, long total) {
            this.from = from;
            this.to = to;
            this.total = total;
        }
{'fraction_non_alphanumeric': 0.10630899155489319, 'fraction_numerical': 0.005961251862891207, 'mean_word_length': 2.104857363145721, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2973785', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1130, 'n_words': 251}