Skip to content

Commit 28df892

Browse files
committed
Update for new libwebsockets "file operatons" (fops) API.
Use LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP to detect new API.
1 parent f77f40a commit 28df892

File tree

3 files changed

+102
-38
lines changed

3 files changed

+102
-38
lines changed

configure.ac

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,20 @@ datadir_relative="share"
150150

151151
AC_SUBST(datadir_relative)
152152

153-
case "${with_libwebsockets}" in
154-
"yes" | "no" | "")
155-
LIBWEBSOCKETS_LIB=-lwebsockets
156-
LIBWEBSOCKETS_INCLUDES=
157-
;;
158-
*)
159-
LIBWEBSOCKETS_LIB="${with_libwebsockets}/lib"
160-
LIBWEBSOCKETS_INCLUDES="${with_libwebsockets}/include"
161-
;;
162-
esac
163-
164153
PKG_PROG_PKG_CONFIG
165154
if test "$with_libwebsockets" != "no"; then
166155
PKG_CHECK_MODULES(JSON_C, json-c)
167156
PKG_CHECK_MODULES(OPENSSL, openssl)
157+
case "${with_libwebsockets}" in
158+
"yes" | "")
159+
PKG_CHECK_MODULES(LIBWEBSOCKETS, libwebsockets)
160+
;;
161+
*)
162+
#LIBWEBSOCKETS_LIBS="-L ${with_libwebsockets}/lib -lwebsockets"
163+
LIBWEBSOCKETS_LIBS="${with_libwebsockets}/lib/libwebsockets.a"
164+
LIBWEBSOCKETS_CFLAGS="-I${with_libwebsockets}/include"
165+
;;
166+
esac
168167
fi
169168
ldomterm_misc_includes=
170169
if (which brew>&/dev/null)
@@ -181,8 +180,8 @@ AC_SUBST(CLASSPATH_SEPARATOR)
181180
AC_SUBST(conf_classpath)
182181
AC_SUBST(JAVA_HOME)
183182
AC_SUBST(JAVA_INCLUDE_SUBDIR)
184-
AC_SUBST(LIBWEBSOCKETS_LIB)
185-
AC_SUBST(LIBWEBSOCKETS_INCLUDES)
183+
AC_SUBST(LIBWEBSOCKETS_LIBS)
184+
AC_SUBST(LIBWEBSOCKETS_CFLAGS)
186185
AC_SUBST(DOMTERM_VERSION)
187186
AC_SUBST(DOMTERM_YEAR)
188187
AC_SUBST(with_closure_compiler)

lws-term/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ LN_H = ln
1010
@rm -f ../bin/ldomterm$(EXEEXT)
1111
cd ../bin && $(LN_H) ../lws-term/ldomterm$(EXEEXT) ldomterm$(EXEEXT)
1212

13-
LIBWEBSOCKETS_LIBARG = @LIBWEBSOCKETS_LIB@
13+
LIBWEBSOCKETS_LIBARG = @LIBWEBSOCKETS_LIBS@
1414
bin_PROGRAMS = ldomterm
1515
ldomterm_SOURCES = server.c utils.c protocol.c http.c io.c whereami.c junzip.c
16-
ldomterm_CFLAGS = $(OPENSSL_CFLAGS) $(JSON_C_CFLAGS) @ldomterm_misc_includes@ -I$(srcdir)/lws-term @LIBWEBSOCKETS_INCLUDES@
16+
ldomterm_CFLAGS = $(OPENSSL_CFLAGS) $(JSON_C_CFLAGS) @ldomterm_misc_includes@ -I$(srcdir)/lws-term @LIBWEBSOCKETS_CFLAGS@
1717
if ENABLE_LD_PRELOAD
1818
ldomterm_CFLAGS += -DENABLE_LD_PRELOAD
1919
endif

lws-term/io.c

Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#include <junzip.h>
55
#include <zlib.h>
66

7+
#ifdef LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP
8+
#define USE_NEW_FOPS 1
9+
#else
10+
#define USE_NEW_FOPS 0
11+
#endif
12+
713
struct junzip_mem_handle {
814
JZFile handle;
915
int fd;
@@ -17,11 +23,16 @@ struct open_mem_file {
1723
long length;
1824
long position;
1925
};
26+
#if USE_NEW_FOPS
27+
#define lws_filefd_to_mem_file(FD) \
28+
((struct open_mem_file*) (FD)->filesystem_priv)
29+
#else
2030
#define MAX_OPEN_MEM_FILES 32
2131
struct open_mem_file open_mem_files[MAX_OPEN_MEM_FILES];
2232
#define lws_filefd_to_mem_file(FD) \
2333
((FD) >= 1000 ? &open_mem_files[(FD)-1000] : NULL)
2434
#define mem_file_to_lws_filefd(MEM) (((MEM)-&open_mem_files[0])+1000)
35+
#endif
2536

2637
struct lws_plat_file_ops fops_plat;
2738

@@ -55,17 +66,21 @@ static int zipRecordCallback(JZFile *zip, int idx, JZFileHeader *header) {
5566

5667
static char domterm_resource_prefix[] = "domterm:/";
5768

69+
#if USE_NEW_FOPS
70+
static lws_fop_fd_t
71+
domserver_fops_open(struct lws_plat_file_ops *fops,
72+
const char *filename,
73+
lws_filepos_t *filelen, lws_fop_flags_t *flags)
74+
#else
5875
static lws_filefd_type
5976
domserver_fops_open(struct lws *wsi, const char *filename,
60-
unsigned long *filelen,
61-
#ifdef LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP
62-
int *flags
63-
#else
64-
int flags
77+
unsigned long *filelen, int flags)
6578
#endif
66-
)
6779
{
6880
lws_filefd_type n;
81+
#if USE_NEW_FOPS
82+
lws_fop_fd_t fop_fd;
83+
#endif
6984
if (strncmp(filename, domterm_resource_prefix,
7085
sizeof(domterm_resource_prefix)-1) == 0) {
7186
JZFile *zip = &junzip_handler.handle;
@@ -78,8 +93,14 @@ domserver_fops_open(struct lws *wsi, const char *filename,
7893
&& memcmp(entry_name, zip->start + entry->fileNameStart,
7994
fnlength) == 0) {
8095
uint32_t uncompressedSize = entry->uncompressedSize;
81-
int j = MAX_OPEN_MEM_FILES;
8296
struct open_mem_file *mem;
97+
#if USE_NEW_FOPS
98+
fop_fd = xmalloc(sizeof(*fop_fd)+sizeof(struct open_mem_file));
99+
mem = (struct open_mem_file*) (fop_fd+1);
100+
fop_fd->filesystem_priv = mem;
101+
fop_fd->fops = fops;
102+
#else
103+
int j = MAX_OPEN_MEM_FILES;
83104
for (;;) {
84105
if (--j == 0) {
85106
errno = EMFILE;
@@ -89,6 +110,7 @@ domserver_fops_open(struct lws *wsi, const char *filename,
89110
if (mem->handle == NULL)
90111
break;
91112
}
113+
#endif
92114
mem->handle = &junzip_handler;
93115
mem->index = i;
94116
mem->position = 0;
@@ -113,10 +135,10 @@ domserver_fops_open(struct lws *wsi, const char *filename,
113135
for (int k = 4; --k >= 0; ) *ptr++ = 0; // MTIME = 0
114136
*ptr++ = 0; // Extra Flags. should get from generalPurposeBitFlag
115137
*ptr++ = 3; // OS=Unix. Could use versionMadeBy
116-
memcpy(ptr, junzip_handler.start + offset, compressedSize);
138+
memcpy(ptr, zip->start + offset, compressedSize);
117139
ptr += compressedSize;
118140
// write 8-byte footer
119-
uint32_t val = entry->info.crc32;
141+
uint32_t val = entry->crc32;
120142
for (int k = 4; --k >= 0; ) {
121143
*ptr++ = val & 0xFF; val >>= 8;
122144
}
@@ -136,24 +158,44 @@ domserver_fops_open(struct lws *wsi, const char *filename,
136158
entry, data) != Z_OK) {
137159
fprintf(stderr, "Couldn't read file data!");
138160
free(data);
139-
return -1;
161+
#if USE_NEW_FOPS
162+
return NULL;
163+
#else
164+
return LWS_INVALID_FILE;
165+
#endif
140166
}
141167
}
142168
mem->length = rsize;
143169
*filelen = rsize;
170+
#if USE_NEW_FOPS
171+
return fop_fd;
172+
#else
144173
return mem_file_to_lws_filefd(mem);
174+
#endif
145175
}
146176
}
147177
errno = EMFILE;
178+
#if USE_NEW_FOPS
179+
return NULL;
180+
#else
148181
return LWS_INVALID_FILE;
182+
#endif
149183
}
150184

151185
/* call through to original platform implementation */
152-
return fops_plat.open(wsi, filename, filelen, flags);
186+
#if USE_NEW_FOPS
187+
return fops_plat.open(fops, filename, filelen, flags);
188+
#else
189+
return fops_plat.open(wsi, filename, filelen, flags);
190+
#endif
153191
}
154192

155193
static int
194+
#if USE_NEW_FOPS
195+
domserver_fops_close(lws_fop_fd_t fd)
196+
#else
156197
domserver_fops_close(struct lws *wsi, lws_filefd_type fd)
198+
#endif
157199
{
158200
struct open_mem_file *mem = lws_filefd_to_mem_file(fd);
159201
if (mem != NULL) {
@@ -164,39 +206,62 @@ domserver_fops_close(struct lws *wsi, lws_filefd_type fd)
164206
mem->index = 0;
165207
return 0;
166208
}
209+
#if USE_NEW_FOPS
210+
return fops_plat.close(fd);
211+
#else
167212
return fops_plat.close(wsi, fd);
213+
#endif
168214
}
169215

216+
#if USE_NEW_FOPS
217+
static lws_fileofs_t
218+
domserver_fops_seek_cur(lws_fop_fd_t fd,
219+
lws_fileofs_t offset_from_cur_pos)
220+
#else
170221
static unsigned long
171222
domserver_fops_seek_cur(struct lws *wsi, lws_filefd_type fd,
172223
long offset_from_cur_pos)
224+
#endif
173225
{
174226
struct open_mem_file *mem = lws_filefd_to_mem_file(fd);
227+
#if !USE_NEW_FOPS
175228
if (mem != NULL) {
176-
long new_position = mem->position + offset_from_cur_pos;
177-
if (new_position < 0 || new_position > mem->length)
178-
return (off_t) (-1);
179-
mem->position = new_position;
180-
return new_position;
229+
#endif
230+
long new_position = mem->position + offset_from_cur_pos;
231+
if (new_position < 0 || new_position > mem->length)
232+
return (off_t) (-1);
233+
mem->position = new_position;
234+
return new_position;
235+
#if !USE_NEW_FOPS
181236
}
182237
return fops_plat.seek_cur(wsi, fd, offset_from_cur_pos);
238+
#endif
183239
}
184240

185241
static int
242+
#if USE_NEW_FOPS
243+
domserver_fops_read(lws_fop_fd_t fd, lws_filepos_t *amount,
244+
uint8_t *buf, lws_filepos_t len)
245+
#else
186246
domserver_fops_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
187247
unsigned char *buf, unsigned long len)
248+
#endif
188249
{
189250
struct open_mem_file *mem = lws_filefd_to_mem_file(fd);
251+
#if !USE_NEW_FOPS
190252
if (mem != NULL) {
191-
unsigned long avail = mem->length - mem->position;
192-
if (len > avail)
193-
len = avail;
194-
memcpy(buf, mem->data + mem->position, len);
195-
mem->position += len;
196-
*amount = len;
197-
return 0;
253+
#endif
254+
unsigned long avail = mem->length - mem->position;
255+
if (len > avail)
256+
len = avail;
257+
memcpy(buf, mem->data + mem->position, len);
258+
mem->position += len;
259+
*amount = len;
260+
return 0;
261+
#if !USE_NEW_FOPS
198262
}
199263
return fops_plat.read(wsi, fd, amount, buf, len);
264+
#endif
200265
}
201266

202267
void

0 commit comments

Comments
 (0)