Skip to content

Commit

Permalink
[Enhance] Use sendContent in HTTP
Browse files Browse the repository at this point in the history
Fixes #96
  • Loading branch information
yakumo-saki committed Apr 2, 2021
1 parent 0d0e096 commit 7c38869
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 62 deletions.
4 changes: 2 additions & 2 deletions include/http_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/* 以下は共通 */

Expand Down
7 changes: 7 additions & 0 deletions include/network/http_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// HTTP ヘッダ (HTTP 1.1 〜)
// を送信する
void sendHttpHeader();

// HTMLヘッダ (<html>~<head/>まで)を送信する。
// <body>〜</body></html> は呼び出し元で送信する必要がある
void sendHtmlHeader();
33 changes: 10 additions & 23 deletions src/network/http_setup_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>";
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
Expand Down Expand Up @@ -92,29 +88,20 @@ 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サーバモード)
*/
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"));
}
Expand Down
30 changes: 5 additions & 25 deletions src/network/http_setup_web_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
19 changes: 7 additions & 12 deletions src/network/http_setup_web_post.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>";
Expand Down Expand Up @@ -108,5 +103,5 @@ String http_setup_post_root_content() {
html += "</body>";
html += "</html>";

return html;
server.sendContent(html);
}
33 changes: 33 additions & 0 deletions src/network/http_utils.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 7c38869

Please sign in to comment.