mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
fops: refactor around lws_fops_fd_t
This commit is contained in:
parent
96b91cc7ec
commit
1789d0a483
15 changed files with 249 additions and 172 deletions
|
@ -347,25 +347,37 @@ and then can use helpers to also leverage these platform-independent
|
|||
file handling apis
|
||||
|
||||
```
|
||||
static inline lws_filefd_type
|
||||
`lws_plat_file_open`(struct lws *wsi, const char *filename, unsigned long *filelen, int flags)
|
||||
|
||||
static inline lws_fop_fd_t
|
||||
`lws_plat_file_open`(struct lws_plat_file_ops *fops, const char *filename,
|
||||
lws_filepos_t *filelen, lws_fop_flags_t *flags)
|
||||
static inline int
|
||||
`lws_plat_file_close`(struct lws *wsi, lws_filefd_type fd)
|
||||
`lws_plat_file_close`(lws_fop_fd_t fop_fd)
|
||||
|
||||
static inline unsigned long
|
||||
`lws_plat_file_seek_cur`(struct lws *wsi, lws_filefd_type fd, long offset_from_cur_pos)
|
||||
`lws_plat_file_seek_cur`(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
|
||||
|
||||
static inline int
|
||||
`lws_plat_file_read`(struct lws *wsi, lws_filefd_type fd, unsigned long *amount, unsigned char *buf, unsigned long len)
|
||||
`lws_plat_file_read`(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
|
||||
static inline int
|
||||
`lws_plat_file_write`(struct lws *wsi, lws_filefd_type fd, unsigned long *amount, unsigned char *buf, unsigned long len)
|
||||
`lws_plat_file_write`(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len )
|
||||
```
|
||||
|
||||
The user code can also override or subclass the file operations, to either
|
||||
wrap or replace them. An example is shown in test server.
|
||||
|
||||
### Changes from v2.1 and before fops
|
||||
|
||||
There are three changes:
|
||||
|
||||
1) Pre-2.2 fops directly used platform file descriptors. Current fops returns and accepts a wrapper type lws_fop_fd_t which is a pointer to a malloc'd struct containing information specific to the filesystem implementation.
|
||||
|
||||
2) Pre-2.2 fops bound the fops to a wsi. This is completely removed, you just give a pointer to the fops struct that applies to this file when you open it. Afterwards, the operations in the fops just need the lws_fop_fd_t returned from the open.
|
||||
|
||||
3) Everything is wrapped in typedefs. See lws-plat-unix.c for examples of how to implement.
|
||||
|
||||
@section ecdh ECDH Support
|
||||
|
||||
ECDH Certs are now supported. Enable the CMake option
|
||||
|
|
|
@ -476,7 +476,7 @@ int lws_http2_do_pps_send(struct lws_context *context, struct lws *wsi)
|
|||
if (wsi->state == LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS) {
|
||||
wsi->state = LWSS_HTTP2_ESTABLISHED;
|
||||
|
||||
wsi->u.http.fd = LWS_INVALID_FILE;
|
||||
wsi->u.http.fop_fd = NULL;
|
||||
|
||||
if (lws_is_ssl(lws_http2_get_network_wsi(wsi))) {
|
||||
lwsl_info("skipping nonexistent ssl upgrade headers\n");
|
||||
|
|
|
@ -229,9 +229,9 @@ lws_close_free_wsi(struct lws *wsi, enum lws_close_status reason)
|
|||
}
|
||||
|
||||
if (wsi->mode == LWSCM_HTTP_SERVING_ACCEPTED &&
|
||||
wsi->u.http.fd != LWS_INVALID_FILE) {
|
||||
lws_plat_file_close(wsi, wsi->u.http.fd);
|
||||
wsi->u.http.fd = LWS_INVALID_FILE;
|
||||
wsi->u.http.fop_fd != NULL) {
|
||||
lws_plat_file_close(wsi->u.http.fop_fd);
|
||||
wsi->u.http.fop_fd = NULL;
|
||||
wsi->vhost->protocols->callback(wsi,
|
||||
LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);
|
||||
wsi->told_user_closed = 1;
|
||||
|
|
|
@ -4233,9 +4233,29 @@ lws_cgi_kill(struct lws *wsi);
|
|||
#define LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP (1 << 24)
|
||||
#define LWS_FOP_FLAG_COMPR_IS_GZIP (1 << 25)
|
||||
|
||||
struct lws_plat_file_ops;
|
||||
struct lws_fop_fd {
|
||||
lws_filefd_type fd;
|
||||
struct lws_plat_file_ops *fops;
|
||||
void *filesystem_priv;
|
||||
};
|
||||
#if defined(WIN32) || defined(_WIN32)
|
||||
/* ... */
|
||||
typedef SSIZE_T ssize_t;
|
||||
/* !!! >:-[ */
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
#endif
|
||||
typedef struct lws_fop_fd *lws_fop_fd_t;
|
||||
typedef size_t lws_filepos_t;
|
||||
typedef ssize_t lws_fileofs_t;
|
||||
typedef uint32_t lws_fop_flags_t;
|
||||
|
||||
struct lws_plat_file_ops {
|
||||
lws_filefd_type (*LWS_FOP_OPEN)(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags);
|
||||
lws_fop_fd_t (*LWS_FOP_OPEN)(struct lws_plat_file_ops *fops,
|
||||
const char *filename,
|
||||
lws_filepos_t *filelen,
|
||||
lws_fop_flags_t *flags);
|
||||
/**< Open file (always binary access if plat supports it)
|
||||
* filelen is filled on exit to be the length of the file
|
||||
* *flags & LWS_FOP_FLAGS_MASK should be set to O_RDONLY or O_RDWR.
|
||||
|
@ -4244,18 +4264,16 @@ struct lws_plat_file_ops {
|
|||
* gzip-compressed, then the open handler should OR
|
||||
* LWS_FOP_FLAG_COMPR_IS_GZIP on to *flags before returning.
|
||||
*/
|
||||
int (*LWS_FOP_CLOSE)(struct lws *wsi, lws_filefd_type fd);
|
||||
int (*LWS_FOP_CLOSE)(lws_fop_fd_t fop_fd);
|
||||
/**< close file */
|
||||
unsigned long (*LWS_FOP_SEEK_CUR)(struct lws *wsi, lws_filefd_type fd,
|
||||
long offset_from_cur_pos);
|
||||
lws_fileofs_t (*LWS_FOP_SEEK_CUR)(lws_fop_fd_t fop_fd,
|
||||
lws_fileofs_t offset_from_cur_pos);
|
||||
/**< seek from current position */
|
||||
int (*LWS_FOP_READ)(struct lws *wsi, lws_filefd_type fd,
|
||||
unsigned long *amount, unsigned char *buf,
|
||||
unsigned long len);
|
||||
int (*LWS_FOP_READ)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len);
|
||||
/**< Read from file, on exit *amount is set to amount actually read */
|
||||
int (*LWS_FOP_WRITE)(struct lws *wsi, lws_filefd_type fd,
|
||||
unsigned long *amount, unsigned char *buf,
|
||||
unsigned long len);
|
||||
int (*LWS_FOP_WRITE)(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len);
|
||||
/**< Write to file, on exit *amount is set to amount actually written */
|
||||
|
||||
/* Add new things just above here ---^
|
||||
|
@ -4274,75 +4292,70 @@ lws_set_fops(struct lws_context *context, struct lws_plat_file_ops *fops);
|
|||
/**
|
||||
* lws_plat_file_open() - file open operations
|
||||
*
|
||||
* \param wsi: connection doing the opening
|
||||
* \param fops: file ops struct that applies to this descriptor
|
||||
* \param filename: filename to open
|
||||
* \param filelen: length of file (filled in by call)
|
||||
* \param flags: open flags
|
||||
* \param flags: pointer to open flags
|
||||
*
|
||||
* returns semi-opaque handle
|
||||
*/
|
||||
static LWS_INLINE lws_filefd_type LWS_WARN_UNUSED_RESULT
|
||||
lws_plat_file_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
static LWS_INLINE lws_fop_fd_t LWS_WARN_UNUSED_RESULT
|
||||
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
|
||||
lws_filepos_t *filelen, lws_fop_flags_t *flags)
|
||||
{
|
||||
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_OPEN(wsi, filename,
|
||||
filelen, flags);
|
||||
return fops->LWS_FOP_OPEN(fops, filename, filelen, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* lws_plat_file_close() - close file
|
||||
*
|
||||
* \param wsi: connection opened by
|
||||
* \param fd: file descriptor
|
||||
* \param fop_fd: file handle to close
|
||||
*/
|
||||
static LWS_INLINE int
|
||||
lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
|
||||
lws_plat_file_close(lws_fop_fd_t fop_fd)
|
||||
{
|
||||
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_CLOSE(wsi, fd);
|
||||
return fop_fd->fops->LWS_FOP_CLOSE(fop_fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* lws_plat_file_seek_cur() - close file
|
||||
*
|
||||
* \param wsi: connection opened by
|
||||
* \param fd: file descriptor
|
||||
*
|
||||
* \param fop_fd: file handle
|
||||
* \param offset: position to seek to
|
||||
*/
|
||||
static LWS_INLINE unsigned long
|
||||
lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
|
||||
static LWS_INLINE lws_fileofs_t
|
||||
lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
|
||||
{
|
||||
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_SEEK_CUR(wsi,
|
||||
fd, offset);
|
||||
return fop_fd->fops->LWS_FOP_SEEK_CUR(fop_fd, offset);
|
||||
}
|
||||
/**
|
||||
* lws_plat_file_read() - read from file
|
||||
*
|
||||
* \param wsi: connection opened by
|
||||
* \param fd: file descriptor
|
||||
* \param fop_fd: file handle
|
||||
* \param amount: how much to read (rewritten by call)
|
||||
* \param buf: buffer to write to
|
||||
* \param len: max length
|
||||
*/
|
||||
static LWS_INLINE int LWS_WARN_UNUSED_RESULT
|
||||
lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_READ(wsi, fd,
|
||||
amount, buf, len);
|
||||
return fop_fd->fops->LWS_FOP_READ(fop_fd, amount, buf, len);
|
||||
}
|
||||
/**
|
||||
* lws_plat_file_write() - write from file
|
||||
*
|
||||
* \param wsi: connection opened by
|
||||
* \param fd: file descriptor
|
||||
* \param fop_fd: file handle
|
||||
* \param amount: how much to write (rewritten by call)
|
||||
* \param buf: buffer to read from
|
||||
* \param len: max length
|
||||
*/
|
||||
static LWS_INLINE int LWS_WARN_UNUSED_RESULT
|
||||
lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
return lws_get_fops(lws_get_context(wsi))->LWS_FOP_WRITE(wsi, fd,
|
||||
amount, buf, len);
|
||||
return fop_fd->fops->LWS_FOP_WRITE(fop_fd, amount, buf, len);
|
||||
}
|
||||
//@}
|
||||
|
||||
|
|
|
@ -395,44 +395,60 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
|||
return inet_ntop(af, src, dst, cnt);
|
||||
}
|
||||
|
||||
static lws_filefd_type
|
||||
_lws_plat_file_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
static lws_fop_fd_t
|
||||
_lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
|
||||
lws_filepos_t *filelen, lws_fop_flags_t *flags)
|
||||
{
|
||||
struct stat stat_buf;
|
||||
|
||||
lws_fop_fd_t fop_fd;
|
||||
int ret = open(filename, *flags, 0664);
|
||||
|
||||
if (ret < 0)
|
||||
return LWS_INVALID_FILE;
|
||||
return NULL;
|
||||
|
||||
if (fstat(ret, &stat_buf) < 0)
|
||||
|
||||
fop_fd = malloc(sizeof(*fop_fd));
|
||||
if (!fop_fd)
|
||||
goto bail;
|
||||
|
||||
fop_fd->fops = fops;
|
||||
fop_fd->fd = ret;
|
||||
fop_fd->filesystem_priv = NULL; /* we don't use it */
|
||||
|
||||
if (fstat(ret, &stat_buf) < 0) {
|
||||
close(ret);
|
||||
return LWS_INVALID_FILE;
|
||||
}
|
||||
*filelen = stat_buf.st_size;
|
||||
return (lws_filefd_type)ret;
|
||||
|
||||
return fop_fd;
|
||||
|
||||
bail:
|
||||
close(ret);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
|
||||
_lws_plat_file_close(lws_fop_fd_t fops_fd)
|
||||
{
|
||||
return close((int)fd);
|
||||
int fd = fops_fd->fd;
|
||||
|
||||
free(fd);
|
||||
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
unsigned long
|
||||
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
|
||||
lws_fileofs_t
|
||||
_lws_plat_file_seek_cur(lws_fop_fd_t fops_fd, lws_fileofs_t offset)
|
||||
{
|
||||
return lseek((int)fd, offset, SEEK_CUR);
|
||||
return lseek(fops_fd->fd, offset, SEEK_CUR);
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
_lws_plat_file_read(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
long n;
|
||||
|
||||
n = read((int)fd, buf, len);
|
||||
n = read(fops_fd->fd, buf, len);
|
||||
if (n == -1) {
|
||||
*amount = 0;
|
||||
return -1;
|
||||
|
@ -444,12 +460,12 @@ _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
|||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
_lws_plat_file_write(lws_fop_fd_t fops_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
long n;
|
||||
|
||||
n = write((int)fd, buf, len);
|
||||
n = write(fops_fd->fd, buf, len);
|
||||
if (n == -1) {
|
||||
*amount = 0;
|
||||
return -1;
|
||||
|
|
|
@ -261,36 +261,37 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
|||
return "lws_plat_inet_ntop";
|
||||
}
|
||||
|
||||
static lws_filefd_type
|
||||
_lws_plat_file_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
static lws_fop_fd_t
|
||||
_lws_plat_file_open(lws_plat_file_open(struct lws_plat_file_ops *fops,
|
||||
const char *filename, lws_filepos_t *filelen,
|
||||
lws_fop_flags_t *flags)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_close(lws_fop_fd_t fop_fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned lws_fileofs_t
|
||||
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long
|
||||
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -609,43 +609,60 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
|||
return inet_ntop(af, src, dst, cnt);
|
||||
}
|
||||
|
||||
static lws_filefd_type
|
||||
_lws_plat_file_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
// lws_get_fops(lws_get_context(wsi))
|
||||
|
||||
static lws_fop_fd_t
|
||||
_lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
|
||||
lws_filepos_t *filelen, lws_fop_flags_t *flags)
|
||||
{
|
||||
struct stat stat_buf;
|
||||
int ret = open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
|
||||
lws_fop_fd_t fop_fd;
|
||||
|
||||
if (ret < 0)
|
||||
return LWS_INVALID_FILE;
|
||||
return NULL;
|
||||
|
||||
if (fstat(ret, &stat_buf) < 0) {
|
||||
close(ret);
|
||||
return LWS_INVALID_FILE;
|
||||
}
|
||||
if (fstat(ret, &stat_buf) < 0)
|
||||
goto bail;
|
||||
|
||||
fop_fd = malloc(sizeof(*fop_fd));
|
||||
if (!fop_fd)
|
||||
goto bail;
|
||||
|
||||
fop_fd->fops = fops;
|
||||
fop_fd->fd = ret;
|
||||
fop_fd->filesystem_priv = NULL; /* we don't use it */
|
||||
*filelen = stat_buf.st_size;
|
||||
return ret;
|
||||
|
||||
return fop_fd;
|
||||
|
||||
bail:
|
||||
close(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
|
||||
_lws_plat_file_close(lws_fop_fd_t fop_fd)
|
||||
{
|
||||
int fd = fop_fd->fd;
|
||||
|
||||
free(fop_fd);
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
unsigned long
|
||||
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
|
||||
lws_fileofs_t
|
||||
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
|
||||
{
|
||||
return lseek(fd, offset, SEEK_CUR);
|
||||
return lseek(fop_fd->fd, offset, SEEK_CUR);
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
long n;
|
||||
|
||||
n = read((int)fd, buf, len);
|
||||
n = read((int)fop_fd->fd, buf, len);
|
||||
if (n == -1) {
|
||||
*amount = 0;
|
||||
return -1;
|
||||
|
@ -657,12 +674,12 @@ _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
|||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char *buf, unsigned long len)
|
||||
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
long n;
|
||||
|
||||
n = write((int)fd, buf, len);
|
||||
n = write((int)fop_fd->fd, buf, len);
|
||||
if (n == -1) {
|
||||
*amount = 0;
|
||||
return -1;
|
||||
|
|
|
@ -521,55 +521,68 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
|||
return ok ? dst : NULL;
|
||||
}
|
||||
|
||||
static lws_filefd_type
|
||||
_lws_plat_file_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
static lws_fop_fd_t
|
||||
_lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
|
||||
lws_filepos_t *filelen, lws_fop_flags_t *flags)
|
||||
{
|
||||
HANDLE ret;
|
||||
WCHAR buf[MAX_PATH];
|
||||
lws_fop_fd_t fop_fd;
|
||||
|
||||
(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,
|
||||
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
} else {
|
||||
lwsl_err("%s: open for write not implemented\n", __func__);
|
||||
*filelen = 0;
|
||||
return LWS_INVALID_FILE;
|
||||
goto bail;
|
||||
}
|
||||
|
||||
if (ret != LWS_INVALID_FILE)
|
||||
*filelen = GetFileSize(ret, NULL);
|
||||
if (ret == LWS_INVALID_FILE)
|
||||
goto bail;
|
||||
|
||||
return ret;
|
||||
fop_fd = malloc(sizeof(*fop_fd));
|
||||
if (!fop_fd)
|
||||
goto bail;
|
||||
|
||||
fop_fd->fops = fops;
|
||||
fop_fd->fd = ret;
|
||||
fop_fd->filesystem_priv = NULL; /* we don't use it */
|
||||
|
||||
*filelen = GetFileSize(ret, NULL);
|
||||
|
||||
return fop_fd;
|
||||
|
||||
bail:
|
||||
*filelen = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
|
||||
_lws_plat_file_close(lws_fop_fd_t fop_fd)
|
||||
{
|
||||
(void)wsi;
|
||||
HANDLE fd = fop_fd->fd;
|
||||
|
||||
free(fop_fd);
|
||||
|
||||
CloseHandle((HANDLE)fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned long
|
||||
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
|
||||
static lws_fileofs_t
|
||||
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
|
||||
{
|
||||
(void)wsi;
|
||||
|
||||
return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
|
||||
return SetFilePointer((HANDLE)fop_fd->fd, offset, NULL, FILE_CURRENT);
|
||||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char* buf, unsigned long len)
|
||||
_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t *buf, lws_filepos_t len)
|
||||
{
|
||||
DWORD _amount;
|
||||
|
||||
if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
|
||||
if (!ReadFile((HANDLE)fop_fd->fd, buf, (DWORD)len, &_amount, NULL)) {
|
||||
*amount = 0;
|
||||
|
||||
return 1;
|
||||
|
@ -581,11 +594,10 @@ _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
|||
}
|
||||
|
||||
static int
|
||||
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
||||
unsigned char* buf, unsigned long len)
|
||||
_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
|
||||
uint8_t* buf, lws_filepos_t len)
|
||||
{
|
||||
(void)wsi;
|
||||
(void)fd;
|
||||
(void)fop_fd;
|
||||
(void)amount;
|
||||
(void)buf;
|
||||
(void)len;
|
||||
|
|
12
lib/output.c
12
lib/output.c
|
@ -558,7 +558,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
|
|||
struct lws_context *context = wsi->context;
|
||||
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
||||
struct lws_process_html_args args;
|
||||
unsigned long amount, poss;
|
||||
lws_filepos_t amount, poss;
|
||||
unsigned char *p;
|
||||
#if defined(LWS_WITH_RANGES)
|
||||
unsigned char finished = 0;
|
||||
|
@ -591,7 +591,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
|
|||
|
||||
lwsl_notice("%s: doing range start %llu\n", __func__, wsi->u.http.range.start);
|
||||
|
||||
if ((long)lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
|
||||
if ((long)lws_plat_file_seek_cur(wsi->u.http.fop_fd,
|
||||
wsi->u.http.range.start -
|
||||
wsi->u.http.filepos) < 0)
|
||||
return -1;
|
||||
|
@ -633,7 +633,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
|
|||
poss -= 10 + 128;
|
||||
}
|
||||
|
||||
if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount, p, poss) < 0)
|
||||
if (lws_plat_file_read(wsi->u.http.fop_fd, &amount, p, poss) < 0)
|
||||
return -1; /* caller will close */
|
||||
|
||||
//lwsl_notice("amount %ld\n", amount);
|
||||
|
@ -700,7 +700,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
|
|||
|
||||
if (m != n) {
|
||||
/* adjust for what was not sent */
|
||||
if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
|
||||
if (lws_plat_file_seek_cur(wsi->u.http.fop_fd,
|
||||
m - n) ==
|
||||
(unsigned long)-1)
|
||||
return -1;
|
||||
|
@ -716,8 +716,8 @@ all_sent:
|
|||
{
|
||||
wsi->state = LWSS_HTTP;
|
||||
/* we might be in keepalive, so close it off here */
|
||||
lws_plat_file_close(wsi, wsi->u.http.fd);
|
||||
wsi->u.http.fd = LWS_INVALID_FILE;
|
||||
lws_plat_file_close(wsi->u.http.fop_fd);
|
||||
wsi->u.http.fop_fd = NULL;
|
||||
|
||||
lwsl_debug("file completed\n");
|
||||
|
||||
|
|
|
@ -1092,9 +1092,9 @@ struct _lws_http_mode_related {
|
|||
#endif
|
||||
unsigned int preamble_rx_len;
|
||||
struct lws *new_wsi_list;
|
||||
unsigned long filepos;
|
||||
unsigned long filelen;
|
||||
lws_filefd_type fd;
|
||||
lws_filepos_t filepos;
|
||||
lws_filepos_t filelen;
|
||||
lws_fop_fd_t fop_fd;
|
||||
|
||||
#if defined(LWS_WITH_RANGES)
|
||||
struct lws_range_parsing range;
|
||||
|
|
12
lib/server.c
12
lib/server.c
|
@ -1249,7 +1249,7 @@ lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
|
|||
|
||||
lws_union_transition(wsi, LWSCM_HTTP_SERVING_ACCEPTED);
|
||||
wsi->state = LWSS_HTTP;
|
||||
wsi->u.http.fd = LWS_INVALID_FILE;
|
||||
wsi->u.http.fop_fd = NULL;
|
||||
|
||||
/* expose it at the same offset as u.hdr */
|
||||
wsi->u.http.ah = ah;
|
||||
|
@ -2166,7 +2166,8 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
|
|||
unsigned char *p = response;
|
||||
unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
|
||||
unsigned long computed_total_content_length;
|
||||
int ret = 0, cclen = 8, n = HTTP_STATUS_OK, fflags = O_RDONLY;
|
||||
int ret = 0, cclen = 8, n = HTTP_STATUS_OK;
|
||||
lws_fop_flags_t fflags = O_RDONLY;
|
||||
#if defined(LWS_WITH_RANGES)
|
||||
int ranges;
|
||||
#endif
|
||||
|
@ -2179,9 +2180,10 @@ lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
|
|||
}
|
||||
|
||||
|
||||
wsi->u.http.fd = lws_plat_file_open(wsi, file, &wsi->u.http.filelen,
|
||||
&fflags);
|
||||
if (wsi->u.http.fd == LWS_INVALID_FILE) {
|
||||
wsi->u.http.fop_fd = lws_plat_file_open(&wsi->context->fops, file,
|
||||
&wsi->u.http.filelen,
|
||||
&fflags);
|
||||
if (!wsi->u.http.fop_fd) {
|
||||
lwsl_err("Unable to open '%s'\n", file);
|
||||
|
||||
return -1;
|
||||
|
|
|
@ -281,6 +281,8 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
/* check for the "send a big file by hand" example case */
|
||||
|
||||
if (!strcmp((const char *)in, "/leaf.jpg")) {
|
||||
lws_fop_flags_t flags = LWS_O_RDONLY;
|
||||
|
||||
if (strlen(resource_path) > sizeof(leaf_path) - 10)
|
||||
return -1;
|
||||
sprintf(leaf_path, "%s/leaf.jpg", resource_path);
|
||||
|
@ -290,10 +292,10 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
p = buffer + LWS_PRE;
|
||||
end = p + sizeof(buffer) - LWS_PRE;
|
||||
|
||||
pss->fd = lws_plat_file_open(wsi, leaf_path, &file_len,
|
||||
LWS_O_RDONLY);
|
||||
pss->fop_fd = lws_plat_file_open(lws_get_fops(lws_get_context(wsi)),
|
||||
leaf_path, &file_len, &flags);
|
||||
|
||||
if (pss->fd == LWS_INVALID_FILE) {
|
||||
if (!pss->fop_fd) {
|
||||
lwsl_err("failed to open file %s\n", leaf_path);
|
||||
return -1;
|
||||
}
|
||||
|
@ -344,7 +346,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
p - (buffer + LWS_PRE),
|
||||
LWS_WRITE_HTTP_HEADERS);
|
||||
if (n < 0) {
|
||||
lws_plat_file_close(wsi, pss->fd);
|
||||
lws_plat_file_close(pss->fop_fd);
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
|
@ -508,7 +510,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
if (pss->client_finished)
|
||||
return -1;
|
||||
|
||||
if (pss->fd == LWS_INVALID_FILE)
|
||||
if (!pss->fop_fd)
|
||||
goto try_to_reuse;
|
||||
|
||||
#ifndef LWS_NO_CLIENT
|
||||
|
@ -553,7 +555,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
|||
/* he couldn't handle that much */
|
||||
n = m;
|
||||
|
||||
n = lws_plat_file_read(wsi, pss->fd,
|
||||
n = lws_plat_file_read(pss->fop_fd,
|
||||
&amount, buffer + LWS_PRE, n);
|
||||
/* problem reading, close conn */
|
||||
if (n < 0) {
|
||||
|
@ -586,12 +588,12 @@ later:
|
|||
lws_callback_on_writable(wsi);
|
||||
break;
|
||||
penultimate:
|
||||
lws_plat_file_close(wsi, pss->fd);
|
||||
pss->fd = LWS_INVALID_FILE;
|
||||
lws_plat_file_close(pss->fop_fd);
|
||||
pss->fop_fd = NULL;
|
||||
goto try_to_reuse;
|
||||
|
||||
bail:
|
||||
lws_plat_file_close(wsi, pss->fd);
|
||||
lws_plat_file_close(pss->fop_fd);
|
||||
|
||||
return -1;
|
||||
|
||||
|
|
|
@ -149,17 +149,17 @@ static const struct lws_extension exts[] = {
|
|||
* to do any of this unless you have a reason (eg, want to serve
|
||||
* compressed files without decompressing the whole archive)
|
||||
*/
|
||||
static lws_filefd_type
|
||||
test_server_fops_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
static lws_fop_fd_t
|
||||
test_server_fops_open(struct lws_plat_file_ops *fops, const char *filename,
|
||||
lws_filepos_t *filelen, lws_fop_flags_t *flags)
|
||||
{
|
||||
lws_filefd_type n;
|
||||
lws_fop_fd_t n;
|
||||
|
||||
/* call through to original platform implementation */
|
||||
n = fops_plat.open(wsi, filename, filelen, flags);
|
||||
n = fops_plat.open(fops, filename, filelen, flags);
|
||||
|
||||
lwsl_notice("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
|
||||
(long)n, *filelen);
|
||||
lwsl_notice("%s: opening %s, ret %p, len %lu\n", __func__, filename,
|
||||
n, *filelen);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
|
|
@ -120,19 +120,21 @@ static struct lws_protocols protocols[] = {
|
|||
* to do any of this unless you have a reason (eg, want to serve
|
||||
* compressed files without decompressing the whole archive)
|
||||
*/
|
||||
static lws_filefd_type
|
||||
test_server_fops_open(struct lws *wsi, const char *filename,
|
||||
unsigned long *filelen, int *flags)
|
||||
static lws_fop_fd_t
|
||||
test_server_fops_open(struct lws_plat_file_ops *fops,
|
||||
const char *filename,
|
||||
lws_filepos_t *filelen,
|
||||
lws_fop_flags_t *flags)
|
||||
{
|
||||
lws_filefd_type n;
|
||||
lws_fop_fd_t fop_fd;
|
||||
|
||||
/* call through to original platform implementation */
|
||||
n = fops_plat.open(wsi, filename, filelen, flags);
|
||||
fop_fd = fops_plat.open(fops, filename, filelen, flags);
|
||||
|
||||
lwsl_info("%s: opening %s, ret %ld, len %lu\n", __func__, filename,
|
||||
(long)n, *filelen);
|
||||
lwsl_info("%s: opening %s, ret %p, len %lu\n", __func__, filename,
|
||||
fop_fd, (long)*filelen);
|
||||
|
||||
return n;
|
||||
return fop_fd;
|
||||
}
|
||||
|
||||
void sighandler(int sig)
|
||||
|
|
|
@ -69,7 +69,7 @@ extern void test_server_unlock(int care);
|
|||
#endif
|
||||
|
||||
struct per_session_data__http {
|
||||
lws_filefd_type fd;
|
||||
lws_fop_fd_t fop_fd;
|
||||
#ifdef LWS_WITH_CGI
|
||||
struct lws_cgi_args args;
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue