Fix find name function. Pass as argument to cgi callback the rx buffer len.
authorasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Fri, 30 Sep 2011 08:31:00 +0000 (08:31 +0000)
committerasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Fri, 30 Sep 2011 08:31:00 +0000 (08:31 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@5121 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/net/http.c
bertos/net/http.h

index 235401cda382f949aa438471bbba0aaab87b2d51..bd8f3946379886f0393759710ab5ff6623b9de21 100644 (file)
@@ -74,20 +74,42 @@ void http_sendFileNotFound(struct netconn *client)
 }
 
 
-static void get_fileName(char *revc_buf, char *name, size_t len)
+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;
-               for (size_t i = 0; *p != ' '; i++,p++)
+
+               while (p != stop)
                {
-                       if (i > len)
+                       if ((size_t)i == len || (size_t)i >= recv_len)
+                       {
+                               name[i] = '\0';
                                break;
-                       name[i] = *p;
+                       }
+
+                       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';
 }
 
 static http_gci_handler_t cgi_search(const char *name,  HttpCGI *table)
@@ -123,19 +145,20 @@ void http_server(struct netconn *server, struct HttpCGI *table)
        if (rx_buf_conn)
        {
                netbuf_data(rx_buf_conn, (void **)&rx_buf, &len);
+
                if (rx_buf)
                {
                        memset(file_name, 0, sizeof(file_name));
-                       get_fileName(rx_buf, file_name, sizeof(file_name));
+                       get_fileName(rx_buf, len, file_name, sizeof(file_name));
 
                        LOG_INFO("Search %s\n", file_name);
-                       if (strlen(file_name) == 0)
+                       if (file_name[0] == '\0')
                                strcpy(file_name, HTTP_DEFAULT_PAGE);
 
                        http_gci_handler_t cgi = cgi_search(file_name,  table);
                        if (cgi)
                        {
-                               cgi(rx_buf, client);
+                               cgi(client, rx_buf, len);
                        }
                        else if (SD_CARD_PRESENT())
                        {
index 6b5b3bc1721c81654ebdf145c65cc4a73ceb2d76..e280bbaa546b6fd3f5c68bed7dbb0f30ea6915ce 100644 (file)
@@ -52,7 +52,7 @@
 #include <lwip/tcpip.h>
 #include <lwip/dhcp.h>
 
-typedef int (*http_gci_handler_t)(char *revc_buf, struct netconn *client);
+typedef int (*http_gci_handler_t)(struct netconn *client, char *revc_buf, size_t revc_len);
 
 typedef struct HttpCGI
 {