git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@8339 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
Giovanni Di Sirio 2015-10-01 09:12:33 +00:00
parent f011b5da61
commit e319439395
4 changed files with 105 additions and 17 deletions

View File

@ -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;

View File

@ -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);

View File

@ -132,7 +132,7 @@ caddr_t _sbrk_r(struct _reent *r, int incr)
#if CH_CFG_USE_MEMCORE
void *p;
chDbgCheck(incr > 0);
chDbgCheck(incr >= 0);
p = chCoreAlloc((size_t)incr);
if (p == NULL) {

View File

@ -116,6 +116,8 @@
- HAL: Introduced support for TIM21 and TIM22 in STM32 ST driver.
- HAL: Updated STM32F0xx headers to STM32CubeF0 version 1.3.0. Added support
for STM32F030xC, STM32F070x6, STM32F070xB devices.
- VAR: Fixed _sbrk_r with incr == 0 should be valid (bug #645)(backported to
3.0.3 and 2.6.10).
- RT: Fixed issues in CMSIS RTOS interface (bug #644)(backported to 3.0.3).
- HAL: Fixed RT dependency in STM32 SDCv1 driver (bug #643)(backported
to 3.0.2).