| 492 | } |
| 493 | |
| 494 | void WebUI_Server::synchronousCommand( |
| 495 | AsyncWebServerRequest* request, const char* cmd, bool silent, AuthenticationLevel auth_level, bool allowedInMotion) { |
| 496 | // Can we do this with async? |
| 497 | if (http_block_during_motion->get() && inMotionState() && !allowedInMotion) { // ESP800 is to allow a cached paged reload on webui3 |
| 498 | request->send(503, "text/plain", "Try again when not moving\n"); |
| 499 | return; |
| 500 | } |
| 501 | char line[256]; |
| 502 | strncpy(line, cmd, 255); |
| 503 | AsyncWebServerResponse* response; |
| 504 | if (request->method() == HTTP_GET) { |
| 505 | WebClient* webClient = new WebClient(); |
| 506 | webClient->attachWS(silent); |
| 507 | webClient->executeCommandBackground(line); |
| 508 | response = request->beginChunkedResponse("", [webClient, request](uint8_t* buffer, size_t maxLen, size_t total) mutable -> size_t { |
| 509 | // The method can change before the end... not good |
| 510 | //if(request->method() != HTTP_GET) |
| 511 | // return 0; |
| 512 | auto ret = webClient->copyBufferSafe(buffer, min((int)maxLen, 1024), total); |
| 513 | return ret; |
| 514 | }); |
| 515 | // onDisconnect MUST always happen, otherwise commands being processed will wait indefinitly to be read |
| 516 | // by the callback that may never happen if the client is dead |
| 517 | // We rely on AsyncWebServer to take care of that |
| 518 | request->onDisconnect([webClient]() { |
| 519 | webClient->detachWS(); |
| 520 | allChannels.kill(webClient); |
| 521 | // Should not delete, kill() takes care of that |
| 522 | //delete webClient; |
| 523 | }); |
| 524 | } else |
| 525 | response = request->beginResponse(200, "", ""); |
| 526 | response->addHeader(T_Cache_Control, T_no_cache); |
| 527 | request->send(response); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | std::string getSession(AsyncClient* client) { |
| 532 | return (std::to_string(IPAddress(client->getRemoteAddress())) + ":" + std::to_string(client->getRemotePort())); |
nothing calls this directly
no test coverage detected