Esempio n. 1
0
int Dir_stream_file(FileRecord *file, Connection *conn)
{
    ssize_t sent = 0;
    size_t total = 0;
    off_t offset = 0;
    size_t block_size = MAX_SEND_BUFFER;

    // For the non-sendfile slowpath
    char *file_buffer = NULL;
    int nread = 0;
    int amt = 0;
    int tempfd = -1;

    int rc = Dir_send_header(file, conn);
    check_debug(rc, "Failed to write header to socket.");

    if(conn->ssl == NULL) {
        for(total = 0; fdwait(conn->fd, 'w') == 0 && total < file->sb.st_size;
            total += sent) {
            sent = Dir_send(conn->fd, file->fd, &offset, block_size);
            check_debug(sent > 0, "Failed to sendfile on socket: %d from "
                        "file %d", conn->fd, file->fd);
        }
    }
    else {
        // We have to reopen the file, so we don't get ourselves into seek
        // position trouble
        int tempfd = open((const char *)(file->full_path->data), O_RDONLY);
        check(tempfd >= 0, "Could not reopen open file");

        file_buffer = malloc(MAX_SEND_BUFFER);
        check_mem(file_buffer);

        while((nread = fdread(tempfd, file_buffer, MAX_SEND_BUFFER)) > 0) {
            for(amt = 0, sent = 0; sent < nread; sent += amt) {
                amt = conn->send(conn, file_buffer + sent, nread - sent);
                check_debug(amt > 0, "Failed to send on socket: %d from "
                            "file %d", conn->fd, tempfd);
            }
            total += nread;
        }
        free(file_buffer);
        close(tempfd); tempfd = -1;
    }
    
    check(total <= file->sb.st_size, 
            "Wrote way too much, wrote %d but size was %d",
            (int)total, (int)file->sb.st_size);

    check(total == file->sb.st_size,
            "Sent other than expected, sent: %d, but expected: %d", 
            (int)total, (int)file->sb.st_size);

    return total;

error:
    if(file_buffer) free(file_buffer);
    if(tempfd >= 0) close(tempfd);
    return -1;
}
Esempio n. 2
0
int Dir_serve_file(Dir *dir, Request *req, Connection *conn)
{
    FileRecord *file = NULL;
    bstring resp = NULL;
    bstring path = Request_path(req);
    bstring pattern = req->pattern;
    int rc = 0;
    int is_get = biseq(req->request_method, &HTTP_GET);
    int is_head = is_get ? 0 : biseq(req->request_method, &HTTP_HEAD);

    check(path, "Request had not path. That's weird.");
    req->response_size = 0;

    if(!(is_get || is_head)) {
        req->status_code = 405;
        rc = Response_send_status(conn, &HTTP_405);
        check_debug(rc == blength(&HTTP_405), "Failed to send 405 to client.");
        return -1;
    } else {
        file = Dir_resolve_file(dir, pattern, path);
        resp = Dir_calculate_response(req, file);

        if(resp) {
            rc = Response_send_status(conn, resp);
            check_debug(rc == blength(resp), "Failed to send error response on file serving.");
        } else if(is_get) {
            rc = Dir_stream_file(file, conn);
            req->response_size = rc;
            check_debug(rc == file->sb.st_size, "Didn't send all of the file, sent %d of %s.", rc, bdata(path));
        } else if(is_head) {
            rc = Dir_send_header(file, conn);
            check_debug(rc, "Failed to write header to socket.");
        } else {
            sentinel("How the hell did you get to here. Tell Zed.");
        }

        FileRecord_release(file);
        return 0;
    }

    sentinel("Invalid code branch, Tell Zed you have magic.");
error:
    FileRecord_release(file);
    return -1;
}
Esempio n. 3
0
int Dir_stream_file(FileRecord *file, int sock_fd)
{
    ssize_t sent = 0;
    size_t total = 0;
    off_t offset = 0;
    size_t block_size = MAX_SEND_BUFFER;

    int rc = Dir_send_header(file, sock_fd);
    check_debug(rc, "Failed to write header to socket.");

    for(total = 0; fdwait(sock_fd, 'w') == 0 && total < file->sb.st_size; total += sent) {
        sent = Dir_send(sock_fd, file->fd, &offset, block_size);
        check_debug(sent > 0, "Failed to sendfile on socket: %d from file %d", sock_fd, file->fd);
    }
    
    check(total == file->sb.st_size, "Did not write enough!");
    check(total <= file->sb.st_size, "Wrote way too much, wrote %d but size was %d", (int)total, (int)file->sb.st_size);

    return sent;

error:
    return -1;
}