Decode percent-encoding in url. Add test. Reorder code.
[bertos.git] / bertos / net / http.c
index 1121362dfb17028fe103805a97142ff2ac0aec0c..891c462bbd13356c0920d0803e38c070e43c9785 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"
@@ -53,6 +55,7 @@
 #include <cfg/log.h>
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 
@@ -63,6 +66,83 @@ static const char http_html_hdr_500[] = "HTTP/1.1 500 Internal Server Error\r\nC
 static HttpCGI *cgi_table;
 static http_handler_t http_callback;
 
+
+static char http_hexToAscii(char first, char second)
+{
+       char hex[5], *stop;
+       hex[0] = '0';
+       hex[1] = 'x';
+       hex[2] = first;
+       hex[3] = second;
+       hex[4] = 0;
+       return strtol(hex, &stop, 16);
+}
+
+void http_decodeUri(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len)
+{
+       char value;
+       for (size_t i = 0; i < raw_len; i++)
+       {
+               if (raw_buf[i] == '%')
+               {
+                       if (i + 2 < raw_len)
+                       {
+                               /* convert hex value after % */
+                               value = http_hexToAscii(raw_buf[i + 1], raw_buf[i + 2]);
+                               if (value)
+                               {
+                                       *decodec_buf++ = value;
+                                       i += 2;
+                                       continue;
+                               }
+                       }
+               }
+               *decodec_buf++ = raw_buf[i];
+       }
+}
+
+void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, size_t len)
+{
+       int i = 0;
+       bool str_ok = false;
+
+       if (recv_buf && (recv_len > sizeof("GET /")))
+       {
+               if (*recv_buf++ == 'G' &&
+                       *recv_buf++ == 'E' && *recv_buf++ == 'T')
+                       {
+                               str_ok = true;
+                               /* skip the space and "/" */
+                               recv_buf += 2;
+                       }
+       }
+
+       if (str_ok)
+       {
+               while ((size_t)i < recv_len)
+               {
+                       char ch = *(recv_buf++);
+                       if (ch == ' ' || ch == '\t' || ch == '\n')
+                               break;
+                       if((size_t)i == len - 1)
+                               break;
+                       page_name[i++] = ch;
+               }
+       }
+
+       page_name[i] = '\0';
+}
+
+INLINE const char *get_ext(const char *name)
+{
+       const char *ext = strstr(name, ".");
+       if(ext && (ext + 1))
+               return (ext + 1);
+
+       return NULL;
+}
+
+
 /**
  * Send on \param client socket
  * the 200 Ok http header
@@ -91,53 +171,6 @@ 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)
-{
-       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;
-
-               while (p != stop)
-               {
-                       if ((size_t)i == len || (size_t)i >= recv_len)
-                       {
-                               name[i] = '\0';
-                               break;
-                       }
-
-                       name[i++] = *(p++);
-               }
-       }
-
-       //Trail white space in the string.
-       while ( --i >= 0 )
-               if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n')
-                       break;
-
-       name[i + 1] = '\0';
-}
-
-INLINE const char *get_ext(const char *name)
-{
-       const char *ext = strstr(name, ".");
-       if(ext && (ext + 1))
-               return (ext + 1);
-
-       return NULL;
-}
-
 static http_handler_t cgi_search(const char *name,  HttpCGI *table)
 {
        if (!table)
@@ -154,9 +187,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 +232,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')