1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00

Use fread() when serving http files

This change allows compilation on platforms without read().
This commit is contained in:
Patrick Gansterer 2014-02-26 18:24:24 +01:00
parent 86267d94ad
commit c9a64ad250
5 changed files with 24 additions and 27 deletions

View file

@ -219,7 +219,6 @@ http_postbody:
memset(&wsi->u, 0, sizeof(wsi->u));
wsi->mode = LWS_CONNMODE_HTTP_SERVING_ACCEPTED;
wsi->state = WSI_STATE_HTTP;
wsi->u.http.fd = -1;
/* expose it at the same offset as u.hdr */
wsi->u.http.ah = ah;

View file

@ -257,10 +257,10 @@ libwebsocket_close_and_free_session(struct libwebsocket_context *context,
free(wsi->u.http.post_buffer);
wsi->u.http.post_buffer = NULL;
}
if (wsi->u.http.fd >= 0) {
lwsl_debug("closing http fd %d\n", wsi->u.http.fd);
close(wsi->u.http.fd);
wsi->u.http.fd = -1;
if (wsi->u.http.fp) {
lwsl_debug("closing http fp %p\n", wsi->u.http.fp);
fclose(wsi->u.http.fp);
wsi->u.http.fp = NULL;
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
}

View file

@ -629,7 +629,8 @@ send_raw:
LWS_VISIBLE int libwebsockets_serve_http_file_fragment(
struct libwebsocket_context *context, struct libwebsocket *wsi)
{
int n, m;
size_t n;
int m;
while (!lws_send_pipe_choked(wsi)) {
@ -643,9 +644,13 @@ LWS_VISIBLE int libwebsockets_serve_http_file_fragment(
if (wsi->u.http.filepos == wsi->u.http.filelen)
goto all_sent;
n = read(wsi->u.http.fd, context->service_buffer,
sizeof(context->service_buffer));
if (n > 0) {
n = fread(context->service_buffer, 1,
sizeof(context->service_buffer), wsi->u.http.fp);
if (ferror(wsi->u.http.fp))
return -1;
if (n) {
m = libwebsocket_write(wsi, context->service_buffer, n,
LWS_WRITE_HTTP);
if (m < 0)
@ -654,11 +659,9 @@ LWS_VISIBLE int libwebsockets_serve_http_file_fragment(
wsi->u.http.filepos += m;
if (m != n)
/* adjust for what was not sent */
lseek(wsi->u.http.fd, m - n, SEEK_CUR);
fseek(wsi->u.http.fp, m - n, SEEK_CUR);
}
if (n < 0)
return -1; /* caller will close */
all_sent:
if (!wsi->truncated_send_malloc &&
wsi->u.http.filepos == wsi->u.http.filelen) {

View file

@ -344,9 +344,9 @@ struct allocated_headers {
struct _lws_http_mode_related {
struct allocated_headers *ah; /* mirroring _lws_header_related */
int fd;
unsigned long filepos;
unsigned long filelen;
FILE* fp;
long filepos;
long filelen;
int content_length;
int content_length_seen;

View file

@ -508,27 +508,23 @@ LWS_VISIBLE int libwebsockets_serve_http_file(
struct libwebsocket *wsi, const char *file,
const char *content_type, const char *other_headers)
{
struct stat stat_buf;
unsigned char *p = context->service_buffer;
int ret = 0;
int n;
wsi->u.http.fd = open(file, O_RDONLY
#ifdef WIN32
| _O_BINARY
#endif
);
wsi->u.http.fp = fopen(file, "rb");
if (wsi->u.http.fd < 1) {
if (!wsi->u.http.fp) {
lwsl_err("Unable to open '%s'\n", file);
libwebsockets_return_http_status(context, wsi,
HTTP_STATUS_NOT_FOUND, NULL);
wsi->u.http.fd = -1;
return -1;
}
fstat(wsi->u.http.fd, &stat_buf);
wsi->u.http.filelen = stat_buf.st_size;
fseek(wsi->u.http.fp, 0, SEEK_END);
wsi->u.http.filelen = ftell(wsi->u.http.fp);
fseek(wsi->u.http.fp, 0, SEEK_SET);
p += sprintf((char *)p,
"HTTP/1.0 200 OK\x0d\x0aServer: libwebsockets\x0d\x0a""Content-Type: %s\x0d\x0a",
content_type);
@ -538,8 +534,7 @@ LWS_VISIBLE int libwebsockets_serve_http_file(
p += n;
}
p += sprintf((char *)p,
"Content-Length: %u\x0d\x0a\x0d\x0a",
(unsigned int)stat_buf.st_size);
"Content-Length: %ld\x0d\x0a\x0d\x0a", wsi->u.http.filelen);
ret = libwebsocket_write(wsi, context->service_buffer,
p - context->service_buffer, LWS_WRITE_HTTP);