From c41b558b888250771c7a7e80e9b75a0c325992b1 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Fri, 28 Feb 2014 13:52:37 +0100 Subject: [PATCH 1/6] constructor EthernetClient(MAX_SOCK_NUM) is undocumented and equivalent to EthernetClient() anyway --- WebServer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WebServer.h b/WebServer.h index 02a9a85..6025264 100644 --- a/WebServer.h +++ b/WebServer.h @@ -367,7 +367,7 @@ class WebServer: public Print WebServer::WebServer(const char *urlPrefix, int port) : m_server(port), - m_client(MAX_SOCK_NUM), + m_client(), m_urlPrefix(urlPrefix), m_pushbackDepth(0), m_contentLength(0), From b2f650fb3bdcdb774dd55c9de6ef44240a96aeae Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Sun, 16 Mar 2014 15:45:34 +0000 Subject: [PATCH 2/6] Reduce size and clarify type of some integer vars Trim down the size of a couple of variables as they didn't need to be as large, clarify the type of some others, and change port from int to uint16_t to match EthernetServer. --- WebServer.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/WebServer.h b/WebServer.h index 6025264..7cb5c7c 100644 --- a/WebServer.h +++ b/WebServer.h @@ -183,7 +183,7 @@ class WebServer: public Print bool tail_complete); // constructor for webserver object - WebServer(const char *urlPrefix = "", int port = 80); + WebServer(const char *urlPrefix = "", uint16_t port = 80); // start listening for connections void begin(); @@ -365,7 +365,7 @@ class WebServer: public Print * IMPLEMENTATION ********************************************************************/ -WebServer::WebServer(const char *urlPrefix, int port) : +WebServer::WebServer(const char *urlPrefix, uint16_t port) : m_server(port), m_client(), m_urlPrefix(urlPrefix), @@ -509,10 +509,10 @@ bool WebServer::dispatchCommand(ConnectionType requestType, char *verb, // if the first character is a slash, there's more after it. if (verb[0] == '/') { - unsigned char i; + uint8_t i; char *qm_loc; - unsigned int verb_len; - int qm_offset; + uint16_t verb_len; + uint8_t qm_offset; // Skip over the leading "/", because it makes the code more // efficient and easier to understand. verb++; @@ -539,7 +539,7 @@ bool WebServer::dispatchCommand(ConnectionType requestType, char *verb, { // Initialize with null bytes, so number of parts can be determined. char *url_path[WEBDUINO_URL_PATH_COMMAND_LENGTH] = {0}; - int part = 0; + uint8_t part = 0; // URL path should be terminated with null byte. *(verb + verb_len) = 0; From f95122d70309f343740be6264853c9274fdef766 Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Sun, 23 Mar 2014 16:58:12 +0000 Subject: [PATCH 3/6] Put flash strings into separate sections Change the definition of the P() macro under the AVR architecture to store the strings in sections named after the variables. This allows unused strings to be removed by the linker, saving space in the flash memory. Also change the variable name for all but one of the instances of failMsg to give the maximum benefit from the above. --- WebServer.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/WebServer.h b/WebServer.h index 7cb5c7c..e88ce74 100644 --- a/WebServer.h +++ b/WebServer.h @@ -134,7 +134,7 @@ extern "C" unsigned long millis(void); // declare a static string #ifdef __AVR__ -#define P(name) static const unsigned char name[] PROGMEM +#define P(name) static const unsigned char name[] __attribute__(( section(".progmem." #name) )) #else #define P(name) static const unsigned char name[] #endif @@ -691,7 +691,7 @@ void WebServer::favicon(ConnectionType type) void WebServer::httpUnauthorized() { - P(failMsg) = + P(unauthMsg) = "HTTP/1.0 401 Authorization Required" CRLF WEBDUINO_SERVER_HEADER "Content-Type: text/html" CRLF @@ -699,19 +699,19 @@ void WebServer::httpUnauthorized() CRLF WEBDUINO_AUTH_MESSAGE; - printP(failMsg); + printP(unauthMsg); } void WebServer::httpServerError() { - P(failMsg) = + P(servErrMsg) = "HTTP/1.0 500 Internal Server Error" CRLF WEBDUINO_SERVER_HEADER "Content-Type: text/html" CRLF CRLF WEBDUINO_SERVER_ERROR_MESSAGE; - printP(failMsg); + printP(servErrMsg); } void WebServer::httpNoContent() From 6b974fd4e9245e8e3029d7cf93456dfc9d592da3 Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Sun, 23 Mar 2014 17:54:28 +0000 Subject: [PATCH 4/6] Save flash by only storing the server header once Modify the functions which send headers to re-use one string containing the server name instead of including it inline in all of the different headers. --- WebServer.h | 86 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/WebServer.h b/WebServer.h index e88ce74..dadc763 100644 --- a/WebServer.h +++ b/WebServer.h @@ -40,12 +40,6 @@ #define WEBDUINO_VERSION 1007 #define WEBDUINO_VERSION_STRING "1.7" -#if WEBDUINO_SUPRESS_SERVER_HEADER -#define WEBDUINO_SERVER_HEADER "" -#else -#define WEBDUINO_SERVER_HEADER "Server: Webduino/" WEBDUINO_VERSION_STRING CRLF -#endif - // standard END-OF-LINE marker in HTTP #define CRLF "\r\n" @@ -379,6 +373,8 @@ WebServer::WebServer(const char *urlPrefix, uint16_t port) : { } +P(webServerHeader) = "Server: Webduino/" WEBDUINO_VERSION_STRING CRLF; + void WebServer::begin() { m_server.begin(); @@ -651,14 +647,19 @@ bool WebServer::checkCredentials(const char authCredentials[45]) void WebServer::httpFail() { - P(failMsg) = - "HTTP/1.0 400 Bad Request" CRLF - WEBDUINO_SERVER_HEADER + P(failMsg1) = "HTTP/1.0 400 Bad Request" CRLF; + printP(failMsg1); + +#ifndef WEBDUINO_SUPRESS_SERVER_HEADER + printP(webServerHeader); +#endif + + P(failMsg2) = "Content-Type: text/html" CRLF CRLF WEBDUINO_FAIL_MESSAGE; - printP(failMsg); + printP(failMsg2); } void WebServer::defaultFailCmd(WebServer &server, @@ -691,50 +692,70 @@ void WebServer::favicon(ConnectionType type) void WebServer::httpUnauthorized() { - P(unauthMsg) = - "HTTP/1.0 401 Authorization Required" CRLF - WEBDUINO_SERVER_HEADER + P(unauthMsg1) = "HTTP/1.0 401 Authorization Required" CRLF; + printP(unauthMsg1); + +#ifndef WEBDUINO_SUPRESS_SERVER_HEADER + printP(webServerHeader); +#endif + + P(unauthMsg2) = "Content-Type: text/html" CRLF "WWW-Authenticate: Basic realm=\"" WEBDUINO_AUTH_REALM "\"" CRLF CRLF WEBDUINO_AUTH_MESSAGE; - printP(unauthMsg); + printP(unauthMsg2); } void WebServer::httpServerError() { - P(servErrMsg) = - "HTTP/1.0 500 Internal Server Error" CRLF - WEBDUINO_SERVER_HEADER + P(servErrMsg1) = "HTTP/1.0 500 Internal Server Error" CRLF; + printP(servErrMsg1); + +#ifndef WEBDUINO_SUPRESS_SERVER_HEADER + printP(webServerHeader); +#endif + + P(servErrMsg2) = "Content-Type: text/html" CRLF CRLF WEBDUINO_SERVER_ERROR_MESSAGE; - printP(servErrMsg); + printP(servErrMsg2); } void WebServer::httpNoContent() { - P(noContentMsg) = - "HTTP/1.0 204 NO CONTENT" CRLF - WEBDUINO_SERVER_HEADER + P(noContentMsg1) = "HTTP/1.0 204 NO CONTENT" CRLF; + printP(noContentMsg1); + +#ifndef WEBDUINO_SUPRESS_SERVER_HEADER + printP(webServerHeader); +#endif + + P(noContentMsg2) = CRLF CRLF; - printP(noContentMsg); + printP(noContentMsg2); } void WebServer::httpSuccess(const char *contentType, const char *extraHeaders) { - P(successMsg1) = - "HTTP/1.0 200 OK" CRLF - WEBDUINO_SERVER_HEADER + P(successMsg1) = "HTTP/1.0 200 OK" CRLF; + printP(successMsg1); + +#ifndef WEBDUINO_SUPRESS_SERVER_HEADER + printP(webServerHeader); +#endif + + P(successMsg2) = "Access-Control-Allow-Origin: *" CRLF "Content-Type: "; - printP(successMsg1); + printP(successMsg2); print(contentType); printCRLF(); if (extraHeaders) @@ -744,12 +765,15 @@ void WebServer::httpSuccess(const char *contentType, void WebServer::httpSeeOther(const char *otherURL) { - P(seeOtherMsg) = - "HTTP/1.0 303 See Other" CRLF - WEBDUINO_SERVER_HEADER - "Location: "; + P(seeOtherMsg1) = "HTTP/1.0 303 See Other" CRLF; + printP(seeOtherMsg1); + +#ifndef WEBDUINO_SUPRESS_SERVER_HEADER + printP(webServerHeader); +#endif - printP(seeOtherMsg); + P(seeOtherMsg2) = "Location: "; + printP(seeOtherMsg2); print(otherURL); printCRLF(); printCRLF(); From 4ee01d3cd37422a5edf74c00c47a4e59ceb1d1ef Mon Sep 17 00:00:00 2001 From: mkldon Date: Thu, 10 Apr 2014 16:44:09 +0400 Subject: [PATCH 5/6] Add WEBDUINO_COMMANDS_COUNT macro Add macro for configuring maximum number of commands that can be defined. This macro replaces hardcoded "8" number. --- WebServer.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/WebServer.h b/WebServer.h index dadc763..6360460 100644 --- a/WebServer.h +++ b/WebServer.h @@ -53,6 +53,10 @@ #define WEBDUINO_READ_TIMEOUT_IN_MS 1000 #endif +#ifndef WEBDUINO_COMMANDS_COUNT +#define WEBDUINO_COMMANDS_COUNT 8 +#endif + #ifndef WEBDUINO_URL_PATH_COMMAND_LENGTH #define WEBDUINO_URL_PATH_COMMAND_LENGTH 8 #endif @@ -329,7 +333,7 @@ class WebServer: public Print { const char *verb; Command *cmd; - } m_commands[8]; + } m_commands[WEBDUINO_COMMANDS_COUNT]; unsigned char m_cmdCount; UrlPathCommand *m_urlPathCmd; From a68507ef6d99045fd55b21b4d64df84f3b5422fa Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Thu, 17 Jul 2014 19:13:58 +0100 Subject: [PATCH 6/6] Fix compiler warnings about narrowing conversion The latest 1.5.7 beta release of Arduino includes an updated toolchain which now issues warnings about narrowing of ints to chars inside curly braces becoming ill-formed in C++11. Explicitly cast the values to char to prevent this. --- WebServer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WebServer.h b/WebServer.h index 6360460..c8c43c3 100644 --- a/WebServer.h +++ b/WebServer.h @@ -992,7 +992,7 @@ bool WebServer::readPOSTparam(char *name, int nameLen, int ch2 = read(); if (ch1 == -1 || ch2 == -1) return false; - char hex[3] = { ch1, ch2, 0 }; + char hex[3] = { (char)ch1, (char)ch2, '\0' }; ch = strtoul(hex, NULL, 16); }