<commit_before>import ceylon.file {
    Path,
    File,
    parsePath
}
import ceylon.io {
    newOpenFile,
    OpenFile
}
import ceylon.io.buffer {
    newByteBuffer
}
import ceylon.net.http {
    contentType,
    contentLength
}
import ceylon.net.http.server {
    Response,
    Request,
    ServerException
}

"Endpoint for serving static files."
by("<NAME>")
shared void serveStaticFile(
                "Root directory containing files."
                String externalPath, 
                String fileMapper(Request request) => request.path,
                Options options = Options(),
                void onSuccess(Request r) => noop(r),
                void onError(ServerException e, Request r) => noop(e,r))
        (Request request, Response response, void complete()) {
    
    Path filePath = parsePath(externalPath + fileMapper(request));
    if (is File file = filePath.resource) {
        //TODO log
        //print("Serving file: ``filePath.absolutePath.string``");
        
        value openFile = newOpenFile(file);
        response.addHeader(contentLength(file.size.string));
        if (exists type = file.contentType) {
            response.addHeader(contentType(type));
        }
        
        sendFile {
            openFile = openFile;
            response = response;
            options = options;
            void onSuccess() {
                openFile.close();
                response.flush();
                response.close();
                onSuccess(request);
                complete();
            }
            void onError(ServerException exception) {
                openFile.close();
                response.flush();
                response.close();
                onError(exception,request);
                complete();
            }
        };
        
    } else {
        response.responseStatus = 404;
        //TODO log
        print("File ``filePath.absolutePath.string`` does not exist.");
        response.flush();
        response.close();
    }
}

void sendFile(<commit_msg>Further tentative fix for #440<commit_after>import ceylon.file {
    Path,
    File,
    parsePath
}
import ceylon.io {
    newOpenFile,
    OpenFile
}
import ceylon.io.buffer {
    newByteBuffer
}
import ceylon.net.http {
    contentType,
    contentLength
}
import ceylon.net.http.server {
    Response,
    Request,
    ServerException
}

"Endpoint for serving static files."
by("<NAME>")
shared void serveStaticFile(
                "Root directory containing files."
                String externalPath, 
                String fileMapper(Request request) => request.path,
                Options options = Options(),
                void onSuccess(Request r) => noop(r),
                void onError(ServerException e, Request r) => noop(e,r))
        (Request request, Response response, void complete()) {
    
    Path filePath = parsePath(externalPath + fileMapper(request));
    if (is File file = filePath.resource) {
        //TODO log
        //print("Serving file: ``filePath.absolutePath.string``");
        
        value openFile = newOpenFile(file);
        response.addHeader(contentLength(file.size.string));
        if (exists type = file.contentType) {
            response.addHeader(contentType(type));
        }
        
        sendFile {
            openFile = openFile;
            response = response;
            options = options;
            void onSuccess() {
                openFile.close();
                response.flush();
                onSuccess(request);
                complete();
            }
            void onError(ServerException exception) {
                openFile.close();
                response.flush();
                onError(exception,request);
                complete();
            }
        };
        
    } else {
        response.responseStatus = 404;
        //TODO log
        print("File ``filePath.absolutePath.string`` does not exist.");
        response.flush();
        response.close();
    }
}

void sendFile(
{'fraction_non_alphanumeric': 0.10320815717483213, 'fraction_numerical': 0.002238249191743347, 'mean_word_length': 1.734194425560843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '6271110', 'n_tokens_mistral': 1063, 'n_tokens_neox': 1010, 'n_words': 237}