From 436ecb8bec380823b655893ff6dbd6602627b5c6 Mon Sep 17 00:00:00 2001 From: SoapyMan Date: Thu, 10 Dec 2015 09:08:52 +0600 Subject: [PATCH] use lws_plat_file_* instead of compatible_file_* fix linux and mbed --- lib/context.c | 33 ++++++++++ lib/file-callbacks.c | 118 ------------------------------------ lib/libwebsockets.c | 2 +- lib/lws-plat-mbed3.c | 42 ++++++++++--- lib/lws-plat-unix.c | 30 +++++++-- lib/lws-plat-win.c | 43 +++++++++++++ lib/private-libwebsockets.h | 9 +++ test-server/test-server.c | 2 - 8 files changed, 147 insertions(+), 132 deletions(-) delete mode 100644 lib/file-callbacks.c diff --git a/lib/context.c b/lib/context.c index ec70e7be7..5598da681 100644 --- a/lib/context.c +++ b/lib/context.c @@ -21,6 +21,39 @@ #include "private-libwebsockets.h" +/** + * initializes file callbacks for context + */ +LWS_VISIBLE void +lws_context_init_file_callbacks(struct lws_context_creation_info *info, + struct libwebsocket_context *context) +{ + struct libwebsocket_file_callbacks* cb; + + cb = info->file_callbacks; + + if(cb && cb->pfn_open) + context->file_callbacks.pfn_open = cb->pfn_open; + else + context->file_callbacks.pfn_open = lws_plat_file_open; + + if(cb && cb->pfn_close) + context->file_callbacks.pfn_close = cb->pfn_close; + else + context->file_callbacks.pfn_close = lws_plat_file_close; + + if(cb && cb->pfn_seek_cur) + context->file_callbacks.pfn_seek_cur = cb->pfn_seek_cur; + else + context->file_callbacks.pfn_seek_cur = lws_plat_file_seek_cur; + + if(cb && cb->pfn_read) + context->file_callbacks.pfn_read = cb->pfn_read; + else + context->file_callbacks.pfn_read = lws_plat_file_read; + +} + #ifndef LWS_BUILD_HASH #define LWS_BUILD_HASH "unknown-build-hash" #endif diff --git a/lib/file-callbacks.c b/lib/file-callbacks.c deleted file mode 100644 index 87816dd94..000000000 --- a/lib/file-callbacks.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * libwebsockets - small server side websockets and web server implementation - * - * Copyright (C) 2010-2015 Andy Green - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation: - * version 2.1 of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301 USA - */ - -#include "private-libwebsockets.h" -#include "libwebsockets.h" - -#if defined(WIN32) || defined(_WIN32) - -HANDLE compatible_file_open(const char* filename, unsigned long* filelen){ - HANDLE ret; - WCHAR buffer[MAX_PATH]; - - MultiByteToWideChar(CP_UTF8, 0, filename, -1, buffer, - sizeof(buffer) / sizeof(buffer[0])); - ret = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if (ret != LWS_INVALID_FILE) - *filelen = GetFileSize(ret, NULL); - - return ret; -} - -void compatible_file_close(void*fd){ - CloseHandle((HANDLE)fd); -} - -unsigned long compatible_file_seek_cur(void* fd, long offset){ - return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT); -} - -void compatible_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len){ - DWORD _amount; - if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) - *amount = -1; - else - *amount = (unsigned long)_amount; -} - -#else /* not windows --> */ - -int compatible_file_open(const char* filename, unsigned long* filelen){ - struct stat stat_buf; - int ret = open(filename, O_RDONLY); - - if (ret < 0) - return LWS_INVALID_FILE; - - if (fstat(ret, &stat_buf) < 0) { - close(ret); - return LWS_INVALID_FILE; - } - *filelen = stat_buf.st_size; - return ret; -} - -void compatible_file_close(void*fd){ - close((int)fd) -} - -unsigned long compatible_file_seek_cur(void* fd, long offset){ - return lseek((int)fd, offset, SEEK_CUR); -} - -void compatible_file_read(unsigned long amount, void* fd, unsigned char* buf, unsigned long* len){ - *amount = read((int)fd, buf, len); -} - -#endif // WIN32 || _WIN32 - - -LWS_VISIBLE void -lws_context_init_file_callbacks(struct lws_context_creation_info *info, - struct libwebsocket_context *context) -{ - struct libwebsocket_file_callbacks* cb; - - cb = info->file_callbacks; - - if(cb && cb->pfn_open) - context->file_callbacks.pfn_open = cb->pfn_open; - else - context->file_callbacks.pfn_open = compatible_file_open; - - if(cb && cb->pfn_close) - context->file_callbacks.pfn_close = cb->pfn_close; - else - context->file_callbacks.pfn_close = compatible_file_close; - - if(cb && cb->pfn_seek_cur) - context->file_callbacks.pfn_seek_cur = cb->pfn_seek_cur; - else - context->file_callbacks.pfn_seek_cur = compatible_file_seek_cur; - - if(cb && cb->pfn_read) - context->file_callbacks.pfn_read = cb->pfn_read; - else - context->file_callbacks.pfn_read = compatible_file_read; - -} \ No newline at end of file diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 16b50273b..83d37d235 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -73,7 +73,7 @@ lws_close_and_free_session(struct lws_context *context, if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED && wsi->u.http.fd != LWS_INVALID_FILE) { lwsl_debug("closing http file\n"); - compatible_file_close(wsi->u.http.fd); + context->file_callbacks.pfn_close(wsi->u.http.fd); wsi->u.http.fd = LWS_INVALID_FILE; context->protocols[0].callback(context, wsi, LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0); diff --git a/lib/lws-plat-mbed3.c b/lib/lws-plat-mbed3.c index 06d3ca8f5..a968ff4ac 100644 --- a/lib/lws-plat-mbed3.c +++ b/lib/lws-plat-mbed3.c @@ -145,13 +145,6 @@ lws_plat_service_periodic(struct lws_context *context) (void)context; } -LWS_VISIBLE int -lws_plat_open_file(const char* filename, unsigned long* filelen) -{ - (void)filename; - (void)filelen; - return LWS_INVALID_FILE; -} LWS_VISIBLE const char * lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt) @@ -179,4 +172,39 @@ delete_from_fd(struct lws_context *context, lws_sockfd_type fd) (void)fd; return 1; +} + +/* + * Default file callbacks + */ + +LWS_VISIBLE int +lws_plat_file_open(const char* filename, unsigned long* filelen) +{ + (void)filename; + (void)filelen; + return LWS_INVALID_FILE; +} + +LWS_VISIBLE void +lws_plat_file_close(void*fd) +{ + (void)fd; +} + +LWS_VISIBLE unsigned long +lws_plat_file_seek_cur(void* fd, long offset) +{ + (void)fd; + (void)offset; + return 0; +} + +LWS_VISIBLE void +lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len) +{ + (void)amount; + (void)fd; + (void)buf; + (void)len; } \ No newline at end of file diff --git a/lib/lws-plat-unix.c b/lib/lws-plat-unix.c index a087439ca..3ccf3b71d 100644 --- a/lib/lws-plat-unix.c +++ b/lib/lws-plat-unix.c @@ -451,8 +451,18 @@ lws_plat_change_pollfd(struct lws_context *context, return 0; } +LWS_VISIBLE const char * +lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt) +{ + return inet_ntop(af, src, dst, cnt); +} + +/* + * Default file callbacks + */ + LWS_VISIBLE int -lws_plat_open_file(const char* filename, unsigned long* filelen) +lws_plat_file_open(const char* filename, unsigned long* filelen) { struct stat stat_buf; int ret = open(filename, O_RDONLY); @@ -468,8 +478,20 @@ lws_plat_open_file(const char* filename, unsigned long* filelen) return ret; } -LWS_VISIBLE const char * -lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt) +LWS_VISIBLE void +lws_plat_file_close(void*fd) { - return inet_ntop(af, src, dst, cnt); + close((int)fd) +} + +LWS_VISIBLE unsigned long +lws_plat_file_seek_cur(void* fd, long offset) +{ + return lseek((int)fd, offset, SEEK_CUR); +} + +LWS_VISIBLE void +lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len) +{ + *amount = read((int)fd, buf, len); } diff --git a/lib/lws-plat-win.c b/lib/lws-plat-win.c index f4fef9f33..2042fab78 100644 --- a/lib/lws-plat-win.c +++ b/lib/lws-plat-win.c @@ -443,3 +443,46 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt) lws_free(buffer); return ok ? dst : NULL; } + +/* + * Default file callbacks + */ + +LWS_VISIBLE HANDLE +lws_plat_file_open(const char* filename, unsigned long* filelen) +{ + HANDLE ret; + WCHAR buffer[MAX_PATH]; + + MultiByteToWideChar(CP_UTF8, 0, filename, -1, buffer, + sizeof(buffer) / sizeof(buffer[0])); + ret = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ, + NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + + if (ret != LWS_INVALID_FILE) + *filelen = GetFileSize(ret, NULL); + + return ret; +} + +LWS_VISIBLE void +lws_plat_file_close(void*fd) +{ + CloseHandle((HANDLE)fd); +} + +LWS_VISIBLE unsigned long +lws_plat_file_seek_cur(void* fd, long offset) +{ + return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT); +} + +LWS_VISIBLE void +lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len) +{ + DWORD _amount; + if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) + *amount = -1; + else + *amount = (unsigned long)_amount; +} \ No newline at end of file diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index c91ff9928..ff366495d 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -1287,6 +1287,15 @@ time_in_microseconds(void); LWS_EXTERN const char * lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt); +LWS_EXTERN HANDLE +lws_plat_file_open(const char* filename, unsigned long* filelen); +LWS_EXTERN void +lws_plat_file_close(void*fd); +LWS_EXTERN unsigned long +lws_plat_file_seek_cur(void* fd, long offset); +LWS_EXTERN void +lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len); + #ifdef __cplusplus }; #endif diff --git a/test-server/test-server.c b/test-server/test-server.c index 3b9b7c850..2ffe06257 100644 --- a/test-server/test-server.c +++ b/test-server/test-server.c @@ -296,8 +296,6 @@ int main(int argc, char **argv) * which also includes libwebsocket sockets */ - pollfds->events = (0x0100 | 0x0200); - n = poll(pollfds, count_pollfds, 50); if (n < 0) continue;