diff options
Diffstat (limited to 'demos/STM32')
| -rw-r--r-- | demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.c | 102 | ||||
| -rw-r--r-- | demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.h | 16 | 
2 files changed, 102 insertions, 16 deletions
| diff --git a/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.c b/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.c index 1907fc633..c0e717b0f 100644 --- a/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.c +++ b/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.c @@ -26,6 +26,8 @@   * @{
   */
 +#include <ctype.h>
 +
  #include "ch.h"
  #include "lwip/opt.h"
 @@ -36,6 +38,81 @@  #if LWIP_NETCONN
 +static char url_buffer[WEB_MAX_PATH_SIZE];
 +
 +#define HEXTOI(x) (isdigit(x) ? (x) - '0' : (x) - 'a' + 10)
 +
 +/**
 + * @brief   Decodes an URL sting.
 + * @note    The string is terminated by a zero or a separator.
 + *
 + * @param[in] url       encoded URL string
 + * @param[out] buf      buffer for the processed string
 + * @param[in] max       max number of chars to copy into the buffer
 + * @return              The conversion status.
 + * @retval false        string converted.
 + * @retval true         the string was not valid or the buffer overflowed
 + *
 + * @notapi
 + */
 +static bool decode_url(const char *url, char *buf, size_t max) {
 +
 +  while (true) {
 +    int h, l;
 +    unsigned c = *url++;
 +
 +    switch (c) {
 +    case 0:
 +    case '\r':
 +    case '\n':
 +    case '\t':
 +    case ' ':
 +    case '?':
 +      *buf = 0;
 +      return false;
 +    case '.':
 +      if (max <= 1)
 +        return true;
 +
 +      h = *(url + 1);
 +      if (h == '.')
 +        return true;
 +
 +      break;
 +    case '%':
 +      if (max <= 1)
 +        return true;
 +
 +      h = tolower((int)*url++);
 +      if (h == 0)
 +        return true;
 +      if (!isxdigit(h))
 +        return true;
 +
 +      l = tolower((int)*url++);
 +      if (l == 0)
 +        return true;
 +      if (!isxdigit(l))
 +        return true;
 +
 +      c = (char)((HEXTOI(h) << 4) | HEXTOI(l));
 +      break;
 +    default:
 +      if (max <= 1)
 +        return true;
 +
 +      if (!isalnum(c) && (c != '_') && (c != '-') && (c != '+') &&
 +          (c != '/'))
 +        return true;
 +
 +      break;
 +    }
 +
 +    *buf++ = c;
 +    max--;
 +  }
 +}
 +
  static const char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
  static const char http_index_html[] = "<html><head><title>Congrats!</title></head><body><h1>Welcome to our lwIP HTTP server!</h1><p>This is a small test page.</body></html>";
 @@ -46,20 +123,25 @@ static void http_server_serve(struct netconn *conn) {    err_t err;
    /* Read the data from the port, blocking if nothing yet there.
 -   We assume the request (the part we care about) is in one netbuf */
 +     We assume the request (the part we care about) is in one netbuf.*/
    err = netconn_recv(conn, &inbuf);
    if (err == ERR_OK) {
      netbuf_data(inbuf, (void **)&buf, &buflen);
      /* Is this an HTTP GET command? (only check the first 5 chars, since
 -    there are other formats for GET, and we're keeping it very simple )*/
 -    if (buflen>=5 &&
 -        buf[0]=='G' &&
 -        buf[1]=='E' &&
 -        buf[2]=='T' &&
 -        buf[3]==' ' &&
 -        buf[4]=='/' ) {
 +       there are other formats for GET, and we're keeping it very simple ).*/
 +    if (buflen >= 5 &&
 +        buf[0] == 'G' &&
 +        buf[1] == 'E' &&
 +        buf[2] == 'T' &&
 +        buf[3] == ' ' &&
 +        buf[4] == '/') {
 +
 +      if (decode_url(buf + 4, url_buffer, WEB_MAX_PATH_SIZE)) {
 +        /* Invalid URL handling.*/
 +        return;
 +      }
        /* Send the HTML header
               * subtract 1 from the size, since we dont send the \0 in the string
 @@ -80,12 +162,12 @@ static void http_server_serve(struct netconn *conn) {  }
  /**
 - * Stack area for the http thread.
 + * @brief   Stack area for the http thread.
   */
  THD_WORKING_AREA(wa_http_server, WEB_THREAD_STACK_SIZE);
  /**
 - * HTTP server thread.
 + * @brief   HTTP server thread.
   */
  THD_FUNCTION(http_server, p) {
    struct netconn *conn, *newconn;
 diff --git a/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.h b/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.h index 902bf9be4..946576b8d 100644 --- a/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.h +++ b/demos/STM32/RT-STM32F746G-DISCOVERY-LWIP-FATFS-USB/web/web.h @@ -24,16 +24,20 @@  #ifndef _WEB_H_
  #define _WEB_H_
 -#ifndef WEB_THREAD_STACK_SIZE
 -#define WEB_THREAD_STACK_SIZE   1024
 +#if !defined(WEB_THREAD_STACK_SIZE)
 +#define WEB_THREAD_STACK_SIZE           1024
  #endif
 -#ifndef WEB_THREAD_PORT
 -#define WEB_THREAD_PORT         80
 +#if !defined(WEB_THREAD_PORT)
 +#define WEB_THREAD_PORT                 80
  #endif
 -#ifndef WEB_THREAD_PRIORITY
 -#define WEB_THREAD_PRIORITY     (LOWPRIO + 2)
 +#if !defined(WEB_THREAD_PRIORITY)
 +#define WEB_THREAD_PRIORITY             (LOWPRIO + 2)
 +#endif
 +
 +#if !defined(WEB_MAX_PATH_SIZE)
 +#define WEB_MAX_PATH_SIZE               128
  #endif
  extern THD_WORKING_AREA(wa_http_server, WEB_THREAD_STACK_SIZE);
 | 
