file: support handle large file

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
main^2
Jianhui Zhao 2021-01-19 20:35:50 +08:00
parent f2c55aff77
commit 81db1fa371
5 changed files with 11 additions and 11 deletions

View File

@ -1,5 +1,5 @@
add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE)
add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64)
# The version number.
set(UHTTPD_VERSION_MAJOR 3)

View File

@ -62,7 +62,7 @@ static void conn_send(struct uh_connection *conn, const void *data, ssize_t len)
ev_io_start(conni->srv->loop, &conni->iow);
}
static void conn_send_file(struct uh_connection *conn, const char *path, size_t offset, ssize_t len)
static void conn_send_file(struct uh_connection *conn, const char *path, off_t offset, int64_t len)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
size_t min = 8192;

View File

@ -74,7 +74,7 @@ struct uh_connection_internal {
uint8_t flags;
struct {
int fd;
size_t size;
uint64_t size;
} file;
struct ev_io ior;
struct ev_io iow;

View File

@ -154,7 +154,7 @@ static void file_if_gzip(struct uh_connection *conn, const char *path, const cha
conn->printf(conn, "Content-Encoding: gzip\r\n");
}
static bool file_range(struct uh_connection *conn, size_t size, size_t *start, size_t *end, bool *ranged)
static bool file_range(struct uh_connection *conn, uint64_t size, uint64_t *start, uint64_t *end, bool *ranged)
{
struct uh_connection_internal *conni = (struct uh_connection_internal *)conn;
const struct uh_str hdr = conn->get_header(conn, "Range");
@ -202,7 +202,7 @@ static bool file_range(struct uh_connection *conn, size_t size, size_t *start, s
break;
}
*start = strtoul(buf, NULL, 0);
*start = strtoull(buf, NULL, 0);
i = 0;
@ -213,7 +213,7 @@ static bool file_range(struct uh_connection *conn, size_t size, size_t *start, s
}
buf[i] = '\0';
*end = strtoul(buf, NULL, 0);
*end = strtoull(buf, NULL, 0);
if (*start >= size)
goto err;
@ -237,7 +237,7 @@ err:
conn->send_status_line(conn, HTTP_STATUS_RANGE_NOT_SATISFIABLE, "Content-Type: text/plain\r\nConnection: close\r\n");
conn->printf(conn, "Content-Length: %d\r\n", content_length);
conn->printf(conn, "Content-Range: bytes */%zu\r\n", size);
conn->printf(conn, "Content-Range: bytes */%" PRIu64 "\r\n", size);
conn->send(conn, "\r\n", 2);
@ -258,8 +258,8 @@ void serve_file(struct uh_connection *conn)
const char *docroot = srv->docroot;
const char *index_page = srv->index_page;
static char fullpath[PATH_MAX];
uint64_t start, end;
int docroot_len;
size_t start, end;
const char *mime;
struct stat st;
bool ranged;
@ -334,10 +334,10 @@ void serve_file(struct uh_connection *conn)
mime = file_mime_lookup(fullpath);
conn->printf(conn, "Content-Type: %s\r\n", mime);
conn->printf(conn, "Content-Length: %zu\r\n", end - start + 1);
conn->printf(conn, "Content-Length: %" PRIu64 "\r\n", end - start + 1);
if (ranged)
conn->printf(conn, "Content-Range: bytes %zu-%zu/%" PRIu64 "\r\n", start, end, (uint64_t)st.st_size);
conn->printf(conn, "Content-Range: bytes %" PRIu64 "-%" PRIu64 "/%" PRIu64 "\r\n", start, end, (uint64_t)st.st_size);
else
file_if_gzip(conn, fullpath, mime);

View File

@ -57,7 +57,7 @@ struct uh_connection {
*/
void (*done)(struct uh_connection *conn);
void (*send)(struct uh_connection *conn, const void *data, ssize_t len);
void (*send_file)(struct uh_connection *conn, const char *path, size_t offset, ssize_t len);
void (*send_file)(struct uh_connection *conn, const char *path, off_t offset, int64_t len);
void (*printf)(struct uh_connection *conn, const char *format, ...) __attribute__((format(printf, 2, 3)));
void (*vprintf)(struct uh_connection *conn, const char *format, va_list arg);
void (*send_status_line)(struct uh_connection *conn, int code, const char *extra_headers);