mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-07 17:35:24 +00:00
Berry integration part 1
* t * fx * split closure handler * setTimeout, setInterval * split berry frm SVM, but I need to move event waiter to separate struct? * fx * fx * split relations * remvoe ode duplciaton * POC - pass value to berry * POC - pass str to berry * berry preps + lfs test * lfs test + bytes * save * remove code duplication * try dpid calls * working test for bytes * tyuamcu to file in eberry * str for berry test * tuyamcu to berry glue * fx1 * fixes * try berry http * fix * tests * test vaar lifespan * test * test * test if * berry in html * #if ENABLE_OBK_BERRY * fx * better * fx * test nested loop * free mem * simpler * split lfs run * arg * fx * fix * #if ENABLE_OBK_BERRY * comamnd hook * vars access, ntp * finalize? * unfinished
This commit is contained in:
@ -233,6 +233,7 @@ int http_fn_index(http_request_t* request) {
|
||||
|
||||
// use ?state URL parameter to only request current state
|
||||
if (!http_getArg(request->url, "state", tmpA, sizeof(tmpA))) {
|
||||
// full update - include header
|
||||
http_html_start(request, NULL);
|
||||
|
||||
poststr(request, "<div id=\"changed\">");
|
||||
@ -347,6 +348,10 @@ int http_fn_index(http_request_t* request) {
|
||||
poststr(request, "<div id=\"state\">"); // replaceable content follows
|
||||
}
|
||||
|
||||
#if ENABLE_OBK_BERRY
|
||||
CMD_Berry_RunEventHandlers_StrInt(CMD_EVENT_ON_HTTP, "state", (int)request);
|
||||
#endif
|
||||
|
||||
if (!CFG_HasFlag(OBK_FLAG_HTTP_NO_ONOFF_WORDS)){
|
||||
poststr(request, "<table>"); //Table default to 100% width in stylesheet
|
||||
for (i = 0; i < CHANNEL_MAX; i++) {
|
||||
@ -3192,6 +3197,11 @@ int http_fn_ota(http_request_t* request) {
|
||||
|
||||
int http_fn_other(http_request_t* request) {
|
||||
http_setup(request, httpMimeTypeHTML);
|
||||
#if ENABLE_OBK_BERRY
|
||||
if (CMD_Berry_RunEventHandlers_StrInt(CMD_EVENT_ON_HTTP, request->url, (int)request)) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
http_html_start(request, "Not found");
|
||||
poststr(request, "Not found.<br/>");
|
||||
poststr(request, htmlFooterReturnToMainPage);
|
||||
|
||||
@ -152,6 +152,7 @@ static int http_rest_get_logconfig(http_request_t* request);
|
||||
#if ENABLE_LITTLEFS
|
||||
static int http_rest_get_lfs_delete(http_request_t* request);
|
||||
static int http_rest_get_lfs_file(http_request_t* request);
|
||||
static int http_rest_run_lfs_file(http_request_t* request);
|
||||
static int http_rest_post_lfs_file(http_request_t* request);
|
||||
#endif
|
||||
|
||||
@ -245,6 +246,9 @@ static int http_rest_get(http_request_t* request) {
|
||||
if (!strncmp(request->url, "api/lfs/", 8)) {
|
||||
return http_rest_get_lfs_file(request);
|
||||
}
|
||||
if (!strncmp(request->url, "api/run/", 8)) {
|
||||
return http_rest_run_lfs_file(request);
|
||||
}
|
||||
if (!strncmp(request->url, "api/del/", 8)) {
|
||||
return http_rest_get_lfs_delete(request);
|
||||
}
|
||||
@ -417,7 +421,127 @@ int EndsWith(const char* str, const char* suffix)
|
||||
return 0;
|
||||
return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
|
||||
}
|
||||
char *my_memmem(const char *haystack, int haystack_len, const char *needle, int needle_len) {
|
||||
if (needle_len == 0 || haystack_len < needle_len)
|
||||
return NULL;
|
||||
|
||||
for (int i = 0; i <= haystack_len - needle_len; i++) {
|
||||
if (memcmp(haystack + i, needle, needle_len) == 0)
|
||||
return (char *)(haystack + i);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
typedef struct berryBuilder_s {
|
||||
|
||||
char berry_buffer[4096];
|
||||
int berry_len;
|
||||
} berryBuilder_t;
|
||||
|
||||
void BB_Start(berryBuilder_t *b)
|
||||
{
|
||||
b->berry_buffer[0] = 0;
|
||||
b->berry_len = 0;
|
||||
}
|
||||
void BB_AddCode(berryBuilder_t *b, const char *start, const char *end) {
|
||||
int len;
|
||||
if (end) {
|
||||
len = end - start;
|
||||
}
|
||||
else {
|
||||
len = strlen(start);
|
||||
}
|
||||
memcpy(&b->berry_buffer[b->berry_len], start, len);
|
||||
b->berry_len += len;
|
||||
}
|
||||
void BB_AddText(berryBuilder_t *b, const char *fname, const char *start, const char *end) {
|
||||
BB_AddCode(b, " echo(\"",0);
|
||||
#if 0
|
||||
BB_AddCode(b, start, end);
|
||||
#else
|
||||
const char *p = start;
|
||||
const char *limit = end ? end : (start + strlen(start));
|
||||
while (p < limit) {
|
||||
char c = *p++;
|
||||
switch (c) {
|
||||
case '\\': BB_AddCode(b, "\\\\", 0); break;
|
||||
case '\"': BB_AddCode(b, "\\\"", 0); break;
|
||||
case '\n': BB_AddCode(b, "\\n", 0); break;
|
||||
case '\r': BB_AddCode(b, "\\r", 0); break;
|
||||
case '\t': BB_AddCode(b, "\\t", 0); break;
|
||||
default:
|
||||
BB_AddCode(b, &c, &c + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
BB_AddCode(b, "\")", 0);
|
||||
}
|
||||
void eval_berry_snippet(const char *s);
|
||||
void Berry_SaveRequest(http_request_t *r);
|
||||
void BB_Run(berryBuilder_t *b)
|
||||
{
|
||||
b->berry_buffer[b->berry_len] = 0;
|
||||
eval_berry_snippet(b->berry_buffer);
|
||||
}
|
||||
int http_runBerryFile(http_request_t *request, const char *fname) {
|
||||
Berry_SaveRequest(request);
|
||||
berryBuilder_t bb;
|
||||
BB_Start(&bb);
|
||||
char *data = (char*)LFS_ReadFile(fname);
|
||||
if (data == 0)
|
||||
return 0;
|
||||
http_setup(request, httpMimeTypeHTML);
|
||||
char *p = data;
|
||||
while (*p) {
|
||||
char *btag = strstr(p, "<?b");
|
||||
if (!btag) {
|
||||
break;
|
||||
}
|
||||
BB_AddText(&bb, fname, p, btag);
|
||||
char *etag = strstr(btag, "?>");
|
||||
|
||||
BB_AddCode(&bb, btag + 3, etag);
|
||||
|
||||
p = etag + 2;
|
||||
}
|
||||
const char *s = p;
|
||||
while (*p)
|
||||
p++;
|
||||
BB_AddText(&bb, fname, s, p);
|
||||
free(data);
|
||||
BB_Run(&bb);
|
||||
return 1;
|
||||
}
|
||||
static int http_rest_run_lfs_file(http_request_t* request) {
|
||||
char* fpath;
|
||||
// don't start LFS just because we're trying to read a file -
|
||||
// it won't exist anyway
|
||||
if (!lfs_present()) {
|
||||
request->responseCode = HTTP_RESPONSE_NOT_FOUND;
|
||||
http_setup(request, httpMimeTypeText);
|
||||
poststr(request, NULL);
|
||||
return 0;
|
||||
}
|
||||
#if ENABLE_OBK_BERRY
|
||||
const char* base = request->url + strlen("api/lfs/");
|
||||
const char* q = strchr(base, '?');
|
||||
size_t len = q ? (size_t)(q - base) : strlen(base);
|
||||
fpath = os_malloc(len + 1);
|
||||
memcpy(fpath, base, len);
|
||||
fpath[len] = '\0';
|
||||
int ran = http_runBerryFile(request, fpath);
|
||||
if (ran==0)
|
||||
#endif
|
||||
{
|
||||
request->responseCode = HTTP_RESPONSE_NOT_FOUND;
|
||||
http_setup(request, httpMimeTypeText);
|
||||
poststr(request, NULL);
|
||||
return 0;
|
||||
}
|
||||
free(fpath);
|
||||
return 0;
|
||||
}
|
||||
static int http_rest_get_lfs_file(http_request_t* request) {
|
||||
char* fpath;
|
||||
char* buff;
|
||||
@ -533,6 +657,9 @@ static int http_rest_get_lfs_file(http_request_t* request) {
|
||||
} while (0);
|
||||
|
||||
http_setup(request, mimetype);
|
||||
//#if ENABLE_OBK_BERRY
|
||||
// http_runBerryFile(request, fpath);
|
||||
//#else
|
||||
do {
|
||||
len = lfs_file_read(&lfs, file, buff, 1024);
|
||||
total += len;
|
||||
@ -540,7 +667,8 @@ static int http_rest_get_lfs_file(http_request_t* request) {
|
||||
//ADDLOG_DEBUG(LOG_FEATURE_API, "%d bytes read", len);
|
||||
postany(request, buff, len);
|
||||
}
|
||||
} while (len > 0);
|
||||
} while (len > 0);
|
||||
//#endif
|
||||
lfs_file_close(&lfs, file);
|
||||
ADDLOG_DEBUG(LOG_FEATURE_API, "%d total bytes read", total);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user