Add other string search method for cgi table.
[bertos.git] / bertos / net / http.c
index 1121362dfb17028fe103805a97142ff2ac0aec0c..8941105232539e0afcebfab0cdef99d9b0f7aaab 100644 (file)
@@ -38,6 +38,8 @@
  * the cases where SD is not present or page not found, using embedded pages.
  * Quering from browser the /status page, the server return a json dictionary where are store
  * some board status info, like board temperature, up-time, etc.
+ *
+ * notest: avr
  */
 
 #include "http.h"
@@ -91,42 +93,36 @@ void http_sendInternalErr(struct netconn *client)
        netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY);
 }
 
-static void get_fileName(const char *revc_buf, size_t recv_len, char *name, size_t len)
+void http_getPageName(const char *revc_buf, size_t recv_len, char *page_name, size_t len)
 {
        int i = 0;
-       char *p = strstr(revc_buf, "GET");
-       if (p)
-       {
-               //Find the end of the page request.
-               char *stop = strstr(revc_buf, "HTTP");
-               if (!stop)
-               {
-                       LOG_ERR("Bad GET request\n");
-                       name[0] = '\0';
-                       return;
-               }
-
-               //skip the "/" in get string request
-               p += sizeof("GET") + 1;
+       bool str_ok = false;
 
-               while (p != stop)
-               {
-                       if ((size_t)i == len || (size_t)i >= recv_len)
+       if (revc_buf && (recv_len > sizeof("GET /")))
+       {
+               if (*revc_buf++ == 'G' &&
+                       *revc_buf++ == 'E' && *revc_buf++ == 'T')
                        {
-                               name[i] = '\0';
-                               break;
+                               str_ok = true;
+                               /* skip the space and "/" */
+                               revc_buf += 2;
                        }
+       }
 
-                       name[i++] = *(p++);
+       if (str_ok)
+       {
+               while ((size_t)i < recv_len)
+               {
+                       char ch = *(revc_buf++);
+                       if (ch == ' ' || ch == '\t' || ch == '\n')
+                               break;
+                       if((size_t)i == len - 1)
+                               break;
+                       page_name[i++] = ch;
                }
        }
 
-       //Trail white space in the string.
-       while ( --i >= 0 )
-               if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
-                       break;
-
-       name[i + 1] = '\0';
+       page_name[i] = '\0';
 }
 
 INLINE const char *get_ext(const char *name)
@@ -154,9 +150,15 @@ static http_handler_t cgi_search(const char *name,  HttpCGI *table)
                        if (!strcmp(table[i].name, ext))
                                break;
                }
-               else /* (table[i].type == CGI_MATCH_NAME) */
+               else if (table[i].type == CGI_MATCH_NAME)
                {
                        LOG_INFO("Match all name %s\n", name);
+                       if (strstr(name, table[i].name) != NULL)
+                               break;
+               }
+               else /* (table[i].type == CGI_MATCH_WORD) */
+               {
+                       LOG_INFO("Match all word %s\n", name);
                        if (!strcmp(table[i].name, name))
                                break;
                }
@@ -193,7 +195,7 @@ void http_poll(struct netconn *server)
                if (rx_buf)
                {
                        memset(req_string, 0, sizeof(req_string));
-                       get_fileName(rx_buf, len, req_string, sizeof(req_string));
+                       http_getPageName(rx_buf, len, req_string, sizeof(req_string));
 
                        LOG_INFO("Search %s\n", req_string);
                        if (req_string[0] == '\0')