lws_plat_fd add wsi to fops and helpers
authorAndy Green <andy.green@linaro.org>
Fri, 11 Dec 2015 05:12:58 +0000 (13:12 +0800)
committerAndy Green <andy.green@linaro.org>
Sun, 13 Dec 2015 22:43:35 +0000 (06:43 +0800)
Having the lws_context alone doesn't let us track state or act different
by wsi, which is the most interesting usecase.  Eg not only simply track
file position / decompression state per wsi but also act differently
according to wsi authentication state / associated cookies.

Signed-off-by: Andy Green <andy.green@linaro.org>
lib/libwebsockets.c
lib/libwebsockets.h
lib/lws-plat-mbed3.c
lib/lws-plat-unix.c
lib/lws-plat-win.c
lib/output.c
lib/server.c
test-server/test-server-http.c
test-server/test-server.c

index d26e11a..9e5b9cb 100644 (file)
@@ -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");
-               lws_plat_file_close(&context->fops, wsi->u.http.fd);
+               lws_plat_file_close(wsi, 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);
index 6da6d09..b94bea6 100644 (file)
@@ -287,7 +287,7 @@ LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
 #define lwsl_hexdump(a, b)
 
 #endif
-
+struct lws;
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 
 /* api change list for user code to test against */
@@ -443,13 +443,14 @@ struct lws_pollargs {
  * @write:             Write to file *amount is set on exit as amount written
  */
 struct lws_plat_file_ops {
-       lws_filefd_type (*open)(const char *filename, unsigned long *filelen,
-                               int flags);
-       int (*close)(lws_filefd_type fd);
-       unsigned long (*seek_cur)(lws_filefd_type fd, long offset_from_cur_pos);
-       int (*read)(lws_filefd_type fd, unsigned long *amount,
+       lws_filefd_type (*open)(struct lws *wsi, const char *filename,
+                               unsigned long *filelen, int flags);
+       int (*close)(struct lws *wsi, lws_filefd_type fd);
+       unsigned long (*seek_cur)(struct lws *wsi, lws_filefd_type fd,
+                                 long offset_from_cur_pos);
+       int (*read)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                    unsigned char *buf, unsigned long len);
-       int (*write)(lws_filefd_type fd, unsigned long *amount,
+       int (*write)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                     unsigned char *buf, unsigned long len);
 
        /* Add new things just above here ---^
@@ -1637,9 +1638,13 @@ LWS_VISIBLE LWS_EXTERN struct lws_context *
 lws_get_ctx(const struct lws *wsi);
 
 /*
- * File Operations access helpers
+ * Wsi-associated File Operations access helpers
+ *
+ * Use these helper functions if you want to access a file from the perspective
+ * of a specific wsi, which is usually the case.  If you just want contextless
+ * file access, use the fops callbacks directly with NULL wsi instead of these
+ * helpers.
  *
- * usually the first argument will be lws_get_fops(context)
  * If so, then it calls the platform handler or user overrides where present
  * (as defined in info->fops)
  *
@@ -1648,37 +1653,37 @@ lws_get_ctx(const struct lws *wsi);
  */
 
 static inline lws_filefd_type
-lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
+lws_plat_file_open(struct lws *wsi, const char *filename,
                   unsigned long *filelen, int flags)
 {
-       return fops->open(filename, filelen, flags);
+       return lws_get_fops(lws_get_ctx(wsi))->open(wsi, filename,
+                                                   filelen, flags);
 }
 
 static inline int
-lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
+lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
 {
-       return fops->close(fd);
+       return lws_get_fops(lws_get_ctx(wsi))->close(wsi, fd);
 }
 
 static inline unsigned long
-lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
-                      long offset_from_cur_pos)
+lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
 {
-       return fops->seek_cur(fd, offset_from_cur_pos);
+       return lws_get_fops(lws_get_ctx(wsi))->seek_cur(wsi, fd, offset);
 }
 
 static inline int
-lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
-                  unsigned long *amount, unsigned char *buf, unsigned long len)
+lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
+                  unsigned char *buf, unsigned long len)
 {
-       return fops->read(fd, amount, buf, len);
+       return lws_get_fops(lws_get_ctx(wsi))->read(wsi, fd, amount, buf, len);
 }
 
 static inline int
-lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
-                   unsigned long *amount, unsigned char *buf, unsigned long len)
+lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
+                   unsigned char *buf, unsigned long len)
 {
-       return fops->write(fd, amount, buf, len);
+       return lws_get_fops(lws_get_ctx(wsi))->write(wsi, fd, amount, buf, len);
 }
 
 /*
index 1fa7db8..64e4a90 100644 (file)
@@ -159,8 +159,10 @@ delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
 }
 
 static lws_filefd_type
-_lws_plat_file_open(const char *filename, unsigned long *filelen, int flags)
+_lws_plat_file_open(struct lws *wsi, const char *filename,
+                   unsigned long *filelen, int flags)
 {
+       (void)wsi;
        (void)filename;
        (void)filelen;
        (void)flags;
@@ -168,15 +170,17 @@ _lws_plat_file_open(const char *filename, unsigned long *filelen, int flags)
 }
 
 static int
-_lws_plat_file_close(lws_filefd_type fd)
+_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
 {
+       (void)wsi;
        (void)fd;
        return -1;
 }
 
 unsigned long
-_lws_plat_file_seek_cur(lws_filefd_type fd, long offset)
+_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
 {
+       (void)wsi
        (void)fd;
        (void)offset;
 
@@ -184,9 +188,10 @@ _lws_plat_file_seek_cur(lws_filefd_type fd, long offset)
 }
 
 static int
-_lws_plat_file_read(lws_filefd_type fd, unsigned long *amount,
+_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                    unsigned char* buf, unsigned long* len)
 {
+       (void)wsi;
        (void)amount;
        (void)fd;
        (void)buf;
@@ -196,9 +201,10 @@ _lws_plat_file_read(lws_filefd_type fd, unsigned long *amount,
 }
 
 static int
-_lws_plat_file_write(lws_filefd_type fd, unsigned long *amount,
+_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                     unsigned char* buf, unsigned long len)
 {
+       (void)wsi;
        (void)amount;
        (void)fd;
        (void)buf;
index a6898d8..6478e27 100644 (file)
@@ -416,7 +416,8 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
 }
 
 static lws_filefd_type
-_lws_plat_file_open(const char *filename, unsigned long *filelen, int flags)
+_lws_plat_file_open(struct lws *wsi, const char *filename,
+                   unsigned long *filelen, int flags)
 {
        struct stat stat_buf;
        int ret = open(filename, flags, 0664);
@@ -433,19 +434,19 @@ _lws_plat_file_open(const char *filename, unsigned long *filelen, int flags)
 }
 
 static int
-_lws_plat_file_close(lws_filefd_type fd)
+_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
 {
        return close(fd);
 }
 
 unsigned long
-_lws_plat_file_seek_cur(lws_filefd_type fd, long offset)
+_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
 {
        return lseek(fd, offset, SEEK_CUR);
 }
 
 static int
-_lws_plat_file_read(lws_filefd_type fd, unsigned long *amount,
+_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                    unsigned char *buf, unsigned long len)
 {
        long n;
@@ -462,7 +463,7 @@ _lws_plat_file_read(lws_filefd_type fd, unsigned long *amount,
 }
 
 static int
-_lws_plat_file_write(lws_filefd_type fd, unsigned long *amount,
+_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                     unsigned char *buf, unsigned long len)
 {
        long n;
index 8d9d339..2bfbd05 100644 (file)
@@ -411,11 +411,13 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
 }
 
 static lws_filefd_type
-_lws_plat_file_open(const char *filename, unsigned long *filelen, int flags)
+_lws_plat_file_open(struct lws *wsi, const char *filename,
+                   unsigned long *filelen, int flags)
 {
        HANDLE ret;
        WCHAR buf[MAX_PATH];
 
+       (void)wsi;
        MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
        if ((flags & 7) == _O_RDONLY) {
                ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
@@ -433,25 +435,30 @@ _lws_plat_file_open(const char *filename, unsigned long *filelen, int flags)
 }
 
 static int
-_lws_plat_file_close(lws_filefd_type fd)
+_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
 {
+       (void)wsi;
+
        CloseHandle((HANDLE)fd);
 
        return 0;
 }
 
 static unsigned long
-_lws_plat_file_seek_cur(lws_filefd_type fd, long offset)
+_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
 {
+       (void)wsi;
+
        return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
 }
 
 static int
-_lws_plat_file_read(lws_filefd_type fd, unsigned long *amount,
-                  unsigned char* buf, unsigned long len)
+_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
+                   unsigned char* buf, unsigned long len)
 {
        DWORD _amount;
 
+       (void *)wsi;
        if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
                *amount = 0;
 
@@ -464,10 +471,16 @@ _lws_plat_file_read(lws_filefd_type fd, unsigned long *amount,
 }
 
 static int
-_lws_plat_file_write(lws_filefd_type fd, unsigned long *amount,
+_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
                     unsigned char* buf, unsigned long len)
 {
-       lwsl_err("%s: not implemented on this platform\n", __func__);
+       (void)wsi;
+       (void)fd;
+       (void)amount;
+       (void)buf;
+       (void)len;
+
+       lwsl_err("%s: not implemented yet on this platform\n", __func__);
        
        return -1;
 }
index 609da25..1cb1945 100644 (file)
@@ -527,7 +527,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws_context *context,
                if (wsi->u.http.filepos == wsi->u.http.filelen)
                        goto all_sent;
 
-               if (lws_plat_file_read(&context->fops, wsi->u.http.fd, &amount,
+               if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount,
                                       context->service_buffer,
                                       sizeof(context->service_buffer)) < 0)
                        return -1; /* caller will close */
@@ -545,7 +545,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws_context *context,
 
                        if (m != n)
                                /* adjust for what was not sent */
-                               if (lws_plat_file_seek_cur(&context->fops,
+                               if (lws_plat_file_seek_cur(wsi,
                                                           wsi->u.http.fd,
                                                           m - n) ==
                                                             (unsigned long)-1)
@@ -557,7 +557,7 @@ all_sent:
                        wsi->state = WSI_STATE_HTTP;
 
                        /* we might be in keepalive, so close it off here */
-                       lws_plat_file_close(&context->fops, wsi->u.http.fd);
+                       lws_plat_file_close(wsi, wsi->u.http.fd);
                        wsi->u.http.fd = LWS_INVALID_FILE;
 
                        if (wsi->protocol->callback)
index 7e774ef..9fe069e 100644 (file)
@@ -916,7 +916,7 @@ LWS_VISIBLE int lws_serve_http_file(struct lws_context *context,
                             LWS_SEND_BUFFER_PRE_PADDING;
        int ret = 0;
 
-       wsi->u.http.fd = lws_plat_file_open(&context->fops, file,
+       wsi->u.http.fd = lws_plat_file_open(wsi, file,
                                            &wsi->u.http.filelen, O_RDONLY);
 
        if (wsi->u.http.fd == LWS_INVALID_FILE) {
index 1ff6449..81aabd6 100644 (file)
@@ -166,8 +166,7 @@ int callback_http(struct lws_context *context, struct lws *wsi,
                        p = buffer + LWS_SEND_BUFFER_PRE_PADDING;
                        end = p + sizeof(buffer) - LWS_SEND_BUFFER_PRE_PADDING;
 
-                       pss->fd = lws_plat_file_open(lws_get_fops(context),
-                                                    leaf_path, &file_len,
+                       pss->fd = lws_plat_file_open(wsi, leaf_path, &file_len,
                                                     LWS_O_RDONLY);
 
                        if (pss->fd == LWS_INVALID_FILE)
@@ -218,8 +217,7 @@ int callback_http(struct lws_context *context, struct lws *wsi,
                                      LWS_WRITE_HTTP_HEADERS);
 
                        if (n < 0) {
-                               lws_plat_file_close(lws_get_fops(context),
-                                                   pss->fd);
+                               lws_plat_file_close(wsi, pss->fd);
                                return -1;
                        }
                        /*
@@ -325,7 +323,7 @@ int callback_http(struct lws_context *context, struct lws *wsi,
                                /* he couldn't handle that much */
                                n = m;
                        
-                       n = lws_plat_file_read(lws_get_fops(context), pss->fd,
+                       n = lws_plat_file_read(wsi, pss->fd,
                                               &amount, buffer +
                                                LWS_SEND_BUFFER_PRE_PADDING, n);
                        /* problem reading, close conn */
@@ -353,8 +351,7 @@ int callback_http(struct lws_context *context, struct lws *wsi,
                         */
                        if (m != n)
                                /* partial write, adjust */
-                               if (lws_plat_file_seek_cur(lws_get_fops(context),
-                                                          pss->fd, m - n) ==
+                               if (lws_plat_file_seek_cur(wsi, pss->fd, m - n) ==
                                                             (unsigned long)-1)
                                        goto bail;
 
@@ -378,11 +375,11 @@ flush_bail:
                        lws_callback_on_writable(context, wsi);
                        break;
                }
-               lws_plat_file_close(lws_get_fops(context), pss->fd);
+               lws_plat_file_close(wsi, pss->fd);
                goto try_to_reuse;
 
 bail:
-               lws_plat_file_close(lws_get_fops(context), pss->fd);
+               lws_plat_file_close(wsi, pss->fd);
                return -1;
 
        /*
index 0f55b00..e60a7c0 100644 (file)
@@ -104,12 +104,13 @@ static struct lws_protocols protocols[] = {
  * compressed files without decompressing the whole archive)
  */
 static lws_filefd_type
-test_server_fops_open(const char *filename, unsigned long *filelen, int flags)
+test_server_fops_open(struct lws *wsi, const char *filename,
+                     unsigned long *filelen, int flags)
 {
        int n;
 
        /* call through to original platform implementation */
-       n = fops_plat.open(filename, filelen, flags);
+       n = fops_plat.open(wsi, filename, filelen, flags);
 
        lwsl_notice("%s: opening %s, ret %d, len %lu\n", __func__, filename,
                    n, *filelen);