Skip to content

Commit b0ceacc

Browse files
authored
Redirect to the web UI on "GET /" request (#394)
1 parent d04c2ec commit b0ceacc

3 files changed

Lines changed: 66 additions & 18 deletions

File tree

src/Devices/EthernetServer_TC.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ void EthernetServer_TC::echo() {
6060

6161
// Handles an HTTP GET request
6262
void EthernetServer_TC::get() {
63-
if (memcmp_P(buffer + 5, F("echo"), 4) == 0) {
63+
if (memcmp_P(buffer + 4, F("/ "), 2) == 0) {
64+
sendHomeRedirect();
65+
state = FINISHED;
66+
} else if (memcmp_P(buffer + 5, F("echo"), 4) == 0) {
6467
echo();
6568
} else if (memcmp_P(buffer + 5, F("api"), 3) == 0) {
6669
getApiHandler();
@@ -496,6 +499,16 @@ void EthernetServer_TC::sendDisplayRedirect() {
496499
client.write(buffer);
497500
}
498501

502+
void EthernetServer_TC::sendHomeRedirect() {
503+
const __FlashStringHelper *response_303 =
504+
F("HTTP/1.1 303 See Other\r\n"
505+
"Location: https://open-acidification.github.io/TankControllerManager/\r\n"
506+
"Access-Control-Allow-Origin: *\r\n"
507+
"\r\n");
508+
strscpy_P(buffer, response_303, sizeof(buffer));
509+
client.write(buffer);
510+
}
511+
499512
void EthernetServer_TC::sendResponse(int code) {
500513
const __FlashStringHelper *response_400 =
501514
F("HTTP/1.1 400 Bad Request\r\n"

src/Devices/EthernetServer_TC.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,28 @@ class EthernetServer_TC : public EthernetServer {
6767

6868
// instance methods: constructor
6969
EthernetServer_TC(uint16_t port);
70-
// instance methods: utility
71-
void sendHeadersWithSize(uint32_t size);
72-
void sendResponse(int);
73-
void sendCurrentRedirect();
74-
void sendDisplayRedirect();
75-
int weekday(int year, int month, int day);
76-
// instance methods: HTTP
77-
void get();
78-
void post();
79-
void put();
80-
void options();
81-
void echo();
82-
void getApiHandler();
70+
71+
// other instance methods
8372
void currentData();
8473
void display();
74+
void echo();
75+
bool fileContinue();
76+
void fileSetup();
77+
void get();
78+
void getApiHandler();
79+
bool isRequestForExistingFile();
8580
void keypress();
86-
void rootdirSetup();
81+
void options();
82+
void post();
83+
void put();
8784
void rootdir();
85+
void rootdirSetup();
86+
void sendCurrentRedirect();
87+
void sendDisplayRedirect();
88+
void sendHeadersWithSize(uint32_t size);
89+
void sendHomeRedirect();
90+
void sendResponse(int);
8891
void testReadSpeed();
8992
void testWriteSpeed();
90-
bool isRequestForExistingFile();
91-
void fileSetup();
92-
bool fileContinue();
93+
int weekday(int year, int month, int day);
9394
};

test/EthernetServerTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,38 @@ unittest(PUT_Kp) {
565565
assertEqual(1000.125, singleton->getKp());
566566
}
567567

568+
unittest(home) {
569+
EthernetServer_TC* server = EthernetServer_TC::instance();
570+
server->setHasClientCalling(true);
571+
delay(1);
572+
server->loop();
573+
EthernetClient_CI client = server->getClient();
574+
const char request[] =
575+
"GET / HTTP/1.1\r\n"
576+
"Host: localhost:80\r\n"
577+
"Accept: text/plain;charset=UTF-8\r\n"
578+
"Accept-Encoding: identity\r\n"
579+
"Accept-Language: en-US\r\n"
580+
"\r\n";
581+
client.pushToReadBuffer(request);
582+
server->loop();
583+
deque<uint8_t>* pBuffer = client.writeBuffer();
584+
assertEqual(129, pBuffer->size());
585+
String response;
586+
while (!pBuffer->empty()) {
587+
response.concat(pBuffer->front());
588+
pBuffer->pop_front();
589+
}
590+
const char expectedResponse[] =
591+
"HTTP/1.1 303 See Other\r\n"
592+
"Location: https://open-acidification.github.io/TankControllerManager/\r\n"
593+
"Access-Control-Allow-Origin: *\r\n"
594+
"\r\n";
595+
assertEqual(expectedResponse, response);
596+
server->loop();
597+
assertEqual(NOT_CONNECTED, server->getState());
598+
client.stop();
599+
server->loop();
600+
}
601+
568602
unittest_main()

0 commit comments

Comments
 (0)