|
@@ -14,6 +14,7 @@
|
|
|
#include "ucmd_lib.h"
|
|
#include "ucmd_lib.h"
|
|
|
#include "uhttp.h"
|
|
#include "uhttp.h"
|
|
|
#include "uhttp_cookie.h"
|
|
#include "uhttp_cookie.h"
|
|
|
|
|
+#include "img.h"
|
|
|
|
|
|
|
|
#define LINE_MAX_C 320
|
|
#define LINE_MAX_C 320
|
|
|
|
|
|
|
@@ -63,7 +64,7 @@ static const struct uhttp_hooks hooks = {
|
|
|
|
|
|
|
|
#define FETCH_PATH_MAX 32
|
|
#define FETCH_PATH_MAX 32
|
|
|
#define FETCH_POST_MAX 16384u
|
|
#define FETCH_POST_MAX 16384u
|
|
|
-#define FETCH_MAX (2u * 1024u * 1024u)
|
|
|
|
|
|
|
+#define FETCH_MAX (4u * 1024u * 1024u)
|
|
|
|
|
|
|
|
/* Each browser worker gets its own set of /share files; slot -1 keeps the
|
|
/* Each browser worker gets its own set of /share files; slot -1 keeps the
|
|
|
* historical single-helper names for the one-shot __fetch mode. */
|
|
* historical single-helper names for the one-shot __fetch mode. */
|
|
@@ -73,6 +74,7 @@ struct fetch_paths {
|
|
|
char meta[FETCH_PATH_MAX];
|
|
char meta[FETCH_PATH_MAX];
|
|
|
char done[FETCH_PATH_MAX];
|
|
char done[FETCH_PATH_MAX];
|
|
|
char post[FETCH_PATH_MAX];
|
|
char post[FETCH_PATH_MAX];
|
|
|
|
|
+ char px[FETCH_PATH_MAX];
|
|
|
};
|
|
};
|
|
|
static struct fetch_paths fp;
|
|
static struct fetch_paths fp;
|
|
|
|
|
|
|
@@ -123,6 +125,117 @@ static void fetch_paths_init(unsigned int session, int slot)
|
|
|
fetch_path(fp.meta, sizeof(fp.meta), "meta", session, slot);
|
|
fetch_path(fp.meta, sizeof(fp.meta), "meta", session, slot);
|
|
|
fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
|
|
fetch_path(fp.done, sizeof(fp.done), "done", session, slot);
|
|
|
fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
|
|
fetch_path(fp.post, sizeof(fp.post), "post", session, slot);
|
|
|
|
|
+ fetch_path(fp.px, sizeof(fp.px), "px", session, slot);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Optional decode in the fetch helper. The browser's UI thread only has
|
|
|
|
|
+ * to copy the finished RGB565 bitmap; the JPEG IDCT/box filter runs in the
|
|
|
|
|
+ * helper process (and several helpers can decode in parallel). */
|
|
|
|
|
+#define DECODE_ARENA_MAX (24u * 1024u * 1024u)
|
|
|
|
|
+#define DECODE_PX_MAX (8u * 1024u * 1024u)
|
|
|
|
|
+
|
|
|
|
|
+struct fetch_px {
|
|
|
|
|
+ unsigned char *data;
|
|
|
|
|
+ unsigned int len, dw, dh, nw, nh, alpha, format;
|
|
|
|
|
+ unsigned int dec_ms;
|
|
|
|
|
+ int err;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static void fetch_decode(const unsigned char *data, unsigned int len,
|
|
|
|
|
+ unsigned int max_w, unsigned int max_h,
|
|
|
|
|
+ struct fetch_px *out)
|
|
|
|
|
+{
|
|
|
|
|
+ struct img_info info;
|
|
|
|
|
+ struct img_options opt = {0};
|
|
|
|
|
+ struct img_mem mem;
|
|
|
|
|
+ struct img_image im;
|
|
|
|
|
+ unsigned char *arena;
|
|
|
|
|
+ unsigned int est, dw, dh, outlen, n;
|
|
|
|
|
+ int r;
|
|
|
|
|
+
|
|
|
|
|
+ memset(out, 0, sizeof(*out));
|
|
|
|
|
+ if (img_sniff(data, len, &info) != 0) {
|
|
|
|
|
+ out->err = 1;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ est = img_estimate(&info);
|
|
|
|
|
+ if (est == 0xFFFFFFFFu || est > DECODE_ARENA_MAX) {
|
|
|
|
|
+ out->err = 2;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ arena = (unsigned char *)malloc(est);
|
|
|
|
|
+ if (!arena) {
|
|
|
|
|
+ out->err = 3;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ img_mem_init(&mem, arena, est);
|
|
|
|
|
+ opt.max_pixels = 8u * 1024u * 1024u;
|
|
|
|
|
+ opt.max_w = max_w;
|
|
|
|
|
+ opt.max_h = max_h;
|
|
|
|
|
+ r = img_decode(data, len, &opt, &mem, &im);
|
|
|
|
|
+ if (r != 0 || !im.w || !im.h) {
|
|
|
|
|
+ out->err = 4;
|
|
|
|
|
+ free(arena);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ dw = im.w;
|
|
|
|
|
+ dh = im.h;
|
|
|
|
|
+ if (max_w && dw > max_w) {
|
|
|
|
|
+ dh = (unsigned int)((unsigned long long)dh * max_w / dw);
|
|
|
|
|
+ dw = max_w;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (max_h && dh > max_h) {
|
|
|
|
|
+ dw = (unsigned int)((unsigned long long)dw * max_h / dh);
|
|
|
|
|
+ dh = max_h;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!dw)
|
|
|
|
|
+ dw = 1;
|
|
|
|
|
+ if (!dh)
|
|
|
|
|
+ dh = 1;
|
|
|
|
|
+ if (dw > 4096 || dh > 4096) {
|
|
|
|
|
+ out->err = 5;
|
|
|
|
|
+ free(arena);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ outlen = dw * dh * 2u + (im.alpha ? dw * dh : 0);
|
|
|
|
|
+ if (outlen > DECODE_PX_MAX) {
|
|
|
|
|
+ out->err = 5;
|
|
|
|
|
+ free(arena);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ out->data = (unsigned char *)malloc(outlen);
|
|
|
|
|
+ if (!out->data) {
|
|
|
|
|
+ out->err = 6;
|
|
|
|
|
+ free(arena);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dw == im.w && dh == im.h) {
|
|
|
|
|
+ n = dw * dh;
|
|
|
|
|
+ memcpy(out->data, im.px, n * 2u);
|
|
|
|
|
+ if (im.alpha)
|
|
|
|
|
+ memcpy(out->data + n * 2u, im.alpha, n);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ struct img_blit b;
|
|
|
|
|
+
|
|
|
|
|
+ memset(&b, 0, sizeof(b));
|
|
|
|
|
+ b.dst = (unsigned short *)out->data;
|
|
|
|
|
+ b.dst_w = (int)dw;
|
|
|
|
|
+ b.dst_h = (int)dh;
|
|
|
|
|
+ b.clip_w = (int)dw;
|
|
|
|
|
+ b.clip_h = (int)dh;
|
|
|
|
|
+ b.scale = ((unsigned int)dw << 16) / im.w;
|
|
|
|
|
+ b.dst_a = im.alpha ? out->data + dw * dh * 2u : 0;
|
|
|
|
|
+ b.flags = im.alpha ? IMG_BLIT_SAVE_ALPHA : 0;
|
|
|
|
|
+ img_draw(&im, &b);
|
|
|
|
|
+ }
|
|
|
|
|
+ out->len = outlen;
|
|
|
|
|
+ out->dw = dw;
|
|
|
|
|
+ out->dh = dh;
|
|
|
|
|
+ out->nw = info.width;
|
|
|
|
|
+ out->nh = info.height;
|
|
|
|
|
+ out->alpha = im.alpha ? 1 : 0;
|
|
|
|
|
+ out->format = info.format;
|
|
|
|
|
+ free(arena);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void fetch_fail(unsigned int status, int err, const char *ctype,
|
|
static void fetch_fail(unsigned int status, int err, const char *ctype,
|
|
@@ -146,11 +259,12 @@ static void fetch_fail(unsigned int status, int err, const char *ctype,
|
|
|
* the parallel workers. Re-read before each request and written back when
|
|
* the parallel workers. Re-read before each request and written back when
|
|
|
* the response changed it; with several helpers the last writer wins. */
|
|
* the response changed it; with several helpers the last writer wins. */
|
|
|
#define FETCH_JAR_PATH "/share/bcookies"
|
|
#define FETCH_JAR_PATH "/share/bcookies"
|
|
|
|
|
+static char fetch_jar_path[64] = FETCH_JAR_PATH;
|
|
|
static struct uhttp_jar fetch_jar;
|
|
static struct uhttp_jar fetch_jar;
|
|
|
|
|
|
|
|
static void jar_load(void)
|
|
static void jar_load(void)
|
|
|
{
|
|
{
|
|
|
- int n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH,
|
|
|
|
|
|
|
+ int n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path,
|
|
|
(int)&fetch_jar, (int)sizeof(fetch_jar));
|
|
(int)&fetch_jar, (int)sizeof(fetch_jar));
|
|
|
|
|
|
|
|
if (n != (int)sizeof(fetch_jar))
|
|
if (n != (int)sizeof(fetch_jar))
|
|
@@ -203,7 +317,7 @@ static void jar_save(void)
|
|
|
{
|
|
{
|
|
|
int i, n;
|
|
int i, n;
|
|
|
|
|
|
|
|
- n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
|
|
|
|
|
|
|
+ n = u_syscall3(SYS_READ_FILE, (int)fetch_jar_path, (int)&jar_file,
|
|
|
(int)sizeof(jar_file));
|
|
(int)sizeof(jar_file));
|
|
|
if (n != (int)sizeof(jar_file))
|
|
if (n != (int)sizeof(jar_file))
|
|
|
uhttp_jar_init(&jar_file);
|
|
uhttp_jar_init(&jar_file);
|
|
@@ -223,7 +337,7 @@ static void jar_save(void)
|
|
|
if (o->used && !jar_lookup(&fetch_jar, o))
|
|
if (o->used && !jar_lookup(&fetch_jar, o))
|
|
|
jar_replay(o, 1);
|
|
jar_replay(o, 1);
|
|
|
}
|
|
}
|
|
|
- u_syscall3(SYS_WRITE_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
|
|
|
|
|
|
|
+ u_syscall3(SYS_WRITE_FILE, (int)fetch_jar_path, (int)&jar_file,
|
|
|
(int)sizeof(jar_file));
|
|
(int)sizeof(jar_file));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -235,7 +349,7 @@ struct fetch_times {
|
|
|
|
|
|
|
|
static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
|
|
static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
|
|
|
int err, unsigned int body_len, unsigned int body_trunc,
|
|
int err, unsigned int body_len, unsigned int body_trunc,
|
|
|
- const struct fetch_times *t)
|
|
|
|
|
|
|
+ const struct fetch_times *t, const struct fetch_px *px)
|
|
|
{
|
|
{
|
|
|
char meta[1024];
|
|
char meta[1024];
|
|
|
unsigned int n = 0;
|
|
unsigned int n = 0;
|
|
@@ -254,6 +368,16 @@ static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
|
|
|
app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ /* Optional decoded bitmap: dw, dh, native w/h, alpha, px bytes, error,
|
|
|
|
|
+ * format. */
|
|
|
|
|
+ app_dec(meta, &n, px ? px->dw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? px->dh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? px->nw : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? px->nh : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? px->alpha : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? px->len : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? (unsigned int)px->err : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
|
|
+ app_dec(meta, &n, px ? px->dec_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
|
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -375,7 +499,7 @@ static void fetch_once(void)
|
|
|
if (body_len)
|
|
if (body_len)
|
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
|
|
|
(int)body_len);
|
|
(int)body_len);
|
|
|
- write_meta(0, 0, &h, err, body_len, body_trunc, ×);
|
|
|
|
|
|
|
+ write_meta(0, 0, &h, err, body_len, body_trunc, ×, 0);
|
|
|
uhttp_close(&h);
|
|
uhttp_close(&h);
|
|
|
free(buf);
|
|
free(buf);
|
|
|
u_exit(err ? 1 : 0);
|
|
u_exit(err ? 1 : 0);
|
|
@@ -411,6 +535,15 @@ static void fetch_serve(const char *arg)
|
|
|
slot = slot * 10 + (*arg++ - '0');
|
|
slot = slot * 10 + (*arg++ - '0');
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ while (*arg == ' ')
|
|
|
|
|
+ arg++;
|
|
|
|
|
+ if (*arg == '/') {
|
|
|
|
|
+ unsigned int n = 0;
|
|
|
|
|
+
|
|
|
|
|
+ while (*arg && *arg != ' ' && n + 1 < sizeof(fetch_jar_path))
|
|
|
|
|
+ fetch_jar_path[n++] = *arg++;
|
|
|
|
|
+ fetch_jar_path[n] = '\0';
|
|
|
|
|
+ }
|
|
|
fetch_paths_init(session, slot);
|
|
fetch_paths_init(session, slot);
|
|
|
|
|
|
|
|
buf = (unsigned char *)malloc(FETCH_MAX);
|
|
buf = (unsigned char *)malloc(FETCH_MAX);
|
|
@@ -426,7 +559,10 @@ static void fetch_serve(const char *arg)
|
|
|
for (;;) {
|
|
for (;;) {
|
|
|
unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
|
|
unsigned int seq = 0, cap = 0, body_len = 0, body_trunc = 0;
|
|
|
unsigned int post_len = 0, jar_changes;
|
|
unsigned int post_len = 0, jar_changes;
|
|
|
|
|
+ unsigned int dec_w = 0, dec_h = 0;
|
|
|
|
|
+ int decode = 0;
|
|
|
int is_post = 0;
|
|
int is_post = 0;
|
|
|
|
|
+ struct fetch_px px;
|
|
|
char method[12], ctype[96];
|
|
char method[12], ctype[96];
|
|
|
|
|
|
|
|
method[0] = '\0';
|
|
method[0] = '\0';
|
|
@@ -457,9 +593,31 @@ static void fetch_serve(const char *arg)
|
|
|
i++;
|
|
i++;
|
|
|
method[0] = '\0';
|
|
method[0] = '\0';
|
|
|
ctype[0] = '\0';
|
|
ctype[0] = '\0';
|
|
|
|
|
+ /* "<seq> <cap> D <max_w> <max_h> <url>": decode the
|
|
|
|
|
+ * response at the display bounds before publishing it. */
|
|
|
|
|
+ if (!strncmp(req + i, "D ", 2)) {
|
|
|
|
|
+ i += 2;
|
|
|
|
|
+ while (req[i] == ' ')
|
|
|
|
|
+ i++;
|
|
|
|
|
+ while (req[i] >= '0' && req[i] <= '9') {
|
|
|
|
|
+ dec_w = dec_w * 10 +
|
|
|
|
|
+ (unsigned int)(req[i] - '0');
|
|
|
|
|
+ i++;
|
|
|
|
|
+ }
|
|
|
|
|
+ while (req[i] == ' ')
|
|
|
|
|
+ i++;
|
|
|
|
|
+ while (req[i] >= '0' && req[i] <= '9') {
|
|
|
|
|
+ dec_h = dec_h * 10 +
|
|
|
|
|
+ (unsigned int)(req[i] - '0');
|
|
|
|
|
+ i++;
|
|
|
|
|
+ }
|
|
|
|
|
+ while (req[i] == ' ')
|
|
|
|
|
+ i++;
|
|
|
|
|
+ decode = 1;
|
|
|
|
|
+ }
|
|
|
/* "X <method> <body length> <content type or -> <url>":
|
|
/* "X <method> <body length> <content type or -> <url>":
|
|
|
* any method, for the browser's fetch()/XHR. */
|
|
* any method, for the browser's fetch()/XHR. */
|
|
|
- if (!strncmp(req + i, "X ", 2)) {
|
|
|
|
|
|
|
+ if (!decode && !strncmp(req + i, "X ", 2)) {
|
|
|
unsigned int k = 0;
|
|
unsigned int k = 0;
|
|
|
|
|
|
|
|
i += 2;
|
|
i += 2;
|
|
@@ -546,7 +704,23 @@ static void fetch_serve(const char *arg)
|
|
|
if (body_len)
|
|
if (body_len)
|
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
|
|
|
(int)body_len);
|
|
(int)body_len);
|
|
|
- write_meta(seq, 1, &h, err, body_len, body_trunc, ×);
|
|
|
|
|
|
|
+ memset(&px, 0, sizeof(px));
|
|
|
|
|
+ if (decode) {
|
|
|
|
|
+ u_syscall1(SYS_UNLINK, (int)fp.px);
|
|
|
|
|
+ if (body_len && !body_trunc && !err &&
|
|
|
|
|
+ h.resp.status == 200 && !is_post) {
|
|
|
|
|
+ unsigned int td = u_ticks_ms();
|
|
|
|
|
+
|
|
|
|
|
+ fetch_decode(buf, body_len, dec_w, dec_h, &px);
|
|
|
|
|
+ px.dec_ms = u_ticks_ms() - td;
|
|
|
|
|
+ if (!px.err && px.data && px.len)
|
|
|
|
|
+ u_syscall3(SYS_WRITE_FILE, (int)fp.px,
|
|
|
|
|
+ (int)px.data, (int)px.len);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ write_meta(seq, 1, &h, err, body_len, body_trunc, ×, &px);
|
|
|
|
|
+ if (px.data)
|
|
|
|
|
+ free(px.data);
|
|
|
app_dec(done, &dn, seq, sizeof(done));
|
|
app_dec(done, &dn, seq, sizeof(done));
|
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
|
|
u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
|
|
|
|
|
|