Berry webserver raw content (#17844)

This commit is contained in:
s-hadinger 2023-01-31 23:23:52 +01:00 committed by GitHub
parent 5ea9a95660
commit 293ae8064d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -16,6 +16,7 @@ extern int w_webserver_state(bvm *vm);
extern int w_webserver_check_privileged_access(bvm *vm);
extern int w_webserver_redirect(bvm *vm);
extern int w_webserver_content_start(bvm *vm);
extern int w_webserver_content_open(bvm *vm);
extern int w_webserver_content_send(bvm *vm);
extern int w_webserver_content_response(bvm *vm);
extern int w_webserver_content_send_style(bvm *vm);
@ -42,6 +43,7 @@ module webserver (scope: global) {
content_response, func(w_webserver_content_response)
content_send_style, func(w_webserver_content_send_style)
content_flush, func(w_webserver_content_flush)
content_open, func(w_webserver_content_open)
content_start, func(w_webserver_content_start)
content_stop, func(w_webserver_content_stop)
content_button, func(w_webserver_content_button)

View File

@ -144,7 +144,7 @@ extern "C" {
be_raise(vm, kTypeError, nullptr);
}
// Berry: `webserver.content_start() -> nil`
// Berry: `webserver.content_start(title:string) -> nil`
//
int32_t w_webserver_content_start(struct bvm *vm);
int32_t w_webserver_content_start(struct bvm *vm) {
@ -157,13 +157,36 @@ extern "C" {
be_raise(vm, kTypeError, nullptr);
}
// Berry: `webserver.content_open(http_code:int, mimetype:string) -> nil`
//
int32_t w_webserver_content_open(struct bvm *vm);
int32_t w_webserver_content_open(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 2 && be_isint(vm, 1) && be_isstring(vm, 2)) {
int32_t httpcode = be_toint(vm, 1);
const char * mimetype = be_tostring(vm, 2);
Webserver->client().flush();
WSHeaderSend();
Webserver->setContentLength(CONTENT_LENGTH_UNKNOWN);
Webserver->send(httpcode, mimetype, "");
Web.chunk_buffer = "";
be_return_nil(vm);
}
be_raise(vm, kTypeError, nullptr);
}
// Berry: `webserver.content_send(string) -> nil`
//
int32_t w_webserver_content_send(struct bvm *vm);
int32_t w_webserver_content_send(struct bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments
if (argc >= 1 && be_isstring(vm, 1)) {
const char * html = be_tostring(vm, 1);
if (argc >= 1 && (be_isstring(vm, 1) || be_iscomptr(vm, 1))) {
const char * html;
if (be_isstring(vm, 1)) {
html = be_tostring(vm, 1);
} else {
html = (const char*) be_tocomptr(vm, 1);
}
WSContentSend_P(PSTR("%s"), html);
be_return_nil(vm);
}