diff --git a/include/http_setup.h b/include/http_setup.h index cd55e14..9f94b2f 100644 --- a/include/http_setup.h +++ b/include/http_setup.h @@ -5,12 +5,12 @@ /** * SETUP GET / */ -String http_setup_post_root_content(); +void http_send_setup_post_root_html(); /** * SETUP POST / */ -String http_setup_get_root_content(); +void http_send_setup_get_root_html(); /* 以下は共通 */ diff --git a/include/network/http_utils.h b/include/network/http_utils.h new file mode 100644 index 0000000..ba59dce --- /dev/null +++ b/include/network/http_utils.h @@ -0,0 +1,7 @@ +// HTTP ヘッダ (HTTP 1.1 〜) +// を送信する +void sendHttpHeader(); + +// HTMLヘッダ (<html>~<head/>まで)を送信する。 +// <body>〜</body></html> は呼び出し元で送信する必要がある +void sendHtmlHeader(); \ No newline at end of file diff --git a/src/network/http_setup_web.cpp b/src/network/http_setup_web.cpp index de13d8c..4efce1c 100644 --- a/src/network/http_setup_web.cpp +++ b/src/network/http_setup_web.cpp @@ -12,18 +12,14 @@ #include "embed/style_css.h" #include "network/webserver.h" +#include "network/http_utils.h" + +void http_setup_post_root_error_content(std::vector<std::pair<String, String>> errors) { + + sendHttpHeader(); + sendHtmlHeader(); -String http_setup_post_root_error_content(std::vector<std::pair<String, String>> errors) { String html = ""; - html += "<html>"; - html += "<head>"; - html += "<title>" + product + " setting done. please restart.</title>"; - html += "<meta charset='UTF-8'>"; - html += "<link rel='stylesheet' href='/style.css'>"; - html += "<style>"; - html += " input { width:200px; }"; - html += "</style>"; - html += "</head>"; html += "<body class='setup_err'>"; html += "<h1>" + product + " Settings (" + SETTING_ID + ")</h1>"; html += "<h3>以下の設定値が正しくありません。</h3>"; @@ -39,7 +35,7 @@ String http_setup_post_root_error_content(std::vector<std::pair<String, String>> html += "</body>"; html += "</html>"; - return html; + server.sendContent(html); } /** @@ -55,7 +51,7 @@ void handle_get_root() { mainlog("configNoLoad parameter specified. Skip loading config."); } - http_setup_get_root_content(); + http_send_setup_get_root_html(); // server.send(200, MimeType::HTML, html); } @@ -92,21 +88,13 @@ void handle_post_root() { httplog("[OK] Config save start"); save_config(); httplog("[OK] Sending done HTML"); - html = http_setup_post_root_content(); + http_send_setup_post_root_html(); } else { httplog("Send config error page"); - html = http_setup_post_root_error_content(errors); + http_setup_post_root_error_content(errors); } - - server.send(200, MimeType::HTML, html); } -void handle_get_style_css() { - // httplog(F("style.css accessed")); - server.send(200, MimeType::CSS, STYLE_CSS); -} - - /** * 初期化(設定用Webサーバモード) */ @@ -114,7 +102,6 @@ void setup_http_setup() { httplog(F("HTTP web server initializing")); server.on("/", HTTP_GET, handle_get_root); server.on("/", HTTP_POST, handle_post_root); - server.on("/style.css", HTTP_GET, handle_get_style_css); server.begin(); httplog(F("HTTP web server initialized")); } diff --git a/src/network/http_setup_web_get.cpp b/src/network/http_setup_web_get.cpp index 7793f3f..f512455 100644 --- a/src/network/http_setup_web_get.cpp +++ b/src/network/http_setup_web_get.cpp @@ -11,10 +11,10 @@ #include "global.h" #include "config.h" -#include "embed/style_css.h" #include "http_setup.h" #include "network/webserver.h" +#include "network/http_utils.h" typedef struct { String label; @@ -118,24 +118,13 @@ String _create_radiobuttons(String name, std::vector<LabelValue> choises) { return html; } -String http_setup_get_root_content() { +void http_send_setup_get_root_html() { - server.sendContent("HTTP/1.1 200 OK\r\n"); - server.sendContent("Content-Type: text/html\r\n"); - server.sendContent("Connection: close\r\n"); - server.sendContent("\r\n"); + sendHttpHeader(); + sendHtmlHeader(); String html = ""; - html += "<!doctype html>"; - html += "<html>"; - html += "<head>"; - html += "<meta charset='UTF-8'>"; - html += "<meta name='viewport' content='width=device-width'>"; - html += "<meta name='format-detection' content='telephone=no' />\n"; - html += "<title>" + product + " setting</title>\n"; - html += "<style>"; - html += "</style>\n"; - html += "</head>\n"; + html += "<body>\n"; html += "<h1>" + product + " Settings (" + SETTING_ID + ")</h1>"; html += "<form method='post'>\n"; @@ -310,18 +299,9 @@ String http_setup_get_root_content() { server.sendContent(html); html = ""; - server.sendContent("<style>\n"); - server.sendContent(STYLE_CSS); // String(STYLE_CSS) は使えないので注意(空文字列しか生成されない) - server.sendContent("\n</style>"); - - server.sendContent(html); - html = ""; - html += "</body>\n"; html += "</html>\n"; server.sendContent(html); html = ""; - - return html; } diff --git a/src/network/http_setup_web_post.cpp b/src/network/http_setup_web_post.cpp index 7f2e0c9..9d6077a 100644 --- a/src/network/http_setup_web_post.cpp +++ b/src/network/http_setup_web_post.cpp @@ -12,24 +12,19 @@ #include "config.h" #include "http_setup.h" +#include "network/webserver.h" +#include "network/http_utils.h" /** * Post 設定 ( config の post。 ファイルに設定を保存) */ -String http_setup_post_root_content() { +void http_send_setup_post_root_html() { + + sendHttpHeader(); + sendHtmlHeader(); String html = ""; - html += "<html>"; - html += "<head>"; - html += "<title>" + product + " setting done. please restart.</title>"; - html += "<meta charset='UTF-8'>"; - html += "<link rel='stylesheet' href='/style.css'>"; - html += "<style>"; - html += " input { width:200px; }"; - html += "</style>"; - html += "</head>"; html += "<body class='setup_done'>"; - html += "<h1>" + product + " setting done</h1>"; if (config->get(ConfigNames::OPMODE) == "always") { html += "動作モード:常時稼働モード<br>"; @@ -108,5 +103,5 @@ String http_setup_post_root_content() { html += "</body>"; html += "</html>"; - return html; + server.sendContent(html); } diff --git a/src/network/http_utils.cpp b/src/network/http_utils.cpp new file mode 100644 index 0000000..71cc41c --- /dev/null +++ b/src/network/http_utils.cpp @@ -0,0 +1,33 @@ +#include "global.h" + +#include "network/webserver.h" +#include "embed/style_css.h" + +void sendHttpHeader() { + server.sendContent("HTTP/1.1 200 OK\r\n"); + server.sendContent("Content-Type: text/html\r\n"); + server.sendContent("Connection: close\r\n"); + server.sendContent("\r\n"); +} + +void sendHtmlHeader() { + String html = ""; + html += "<!DOCTYPE html>"; + html += "<html>"; + html += "<head>"; + html += "<meta charset='UTF-8'>"; + html += "<meta name='viewport' content='width=device-width'>"; + html += "<meta name='format-detection' content='telephone=no' />\n"; + html += "<title>" + product + " setting</title>\n"; + + server.sendContent(html); + html = ""; + + server.sendContent("<style>\n"); + server.sendContent(STYLE_CSS); // String(STYLE_CSS) は使えないので注意(空文字列しか生成されない) + server.sendContent("\n</style>"); + + html += "</head>\n"; + + server.sendContent(html); +} \ No newline at end of file