2010-01-13 15:56:56 +00:00
2007-10-01 20:43:28 +00:00
# include "lwip/opt.h"
# include "lwip/arch.h"
# include "lwip/api.h"
2010-01-13 15:56:56 +00:00
# include "httpserver-netconn.h"
2007-10-01 20:43:28 +00:00
# if LWIP_NETCONN
2007-07-13 20:03:25 +00:00
const static char http_html_hdr [ ] = " HTTP/1.1 200 OK \r \n Content-type: text/html \r \n \r \n " ;
2010-01-13 15:56:56 +00:00
const static 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, served by httpserver-netconn.</body></html> " ;
2007-07-13 20:03:25 +00:00
2010-01-13 15:56:56 +00:00
/** Serve one HTTP connection accepted in the http thread */
static void
http_server_netconn_serve ( struct netconn * conn )
{
2007-10-01 19:09:28 +00:00
struct netbuf * inbuf ;
char * buf ;
u16_t buflen ;
2010-01-20 07:38:10 +00:00
err_t err ;
2007-10-01 19:09:28 +00:00
/* Read the data from the port, blocking if nothing yet there.
We assume the request ( the part we care about ) is in one netbuf */
2010-01-20 07:38:10 +00:00
err = netconn_recv ( conn , & inbuf ) ;
2007-10-01 19:09:28 +00:00
2010-01-20 07:38:10 +00:00
if ( err = = ERR_OK ) {
2007-10-01 19:09:28 +00:00
netbuf_data ( inbuf , & 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 ] = = ' / ' ) {
/* Send the HTML header
2007-07-13 20:03:25 +00:00
* subtract 1 from the size , since we dont send the \ 0 in the string
* NETCONN_NOCOPY : our data is const static , so no need to copy it
2007-10-01 19:09:28 +00:00
*/
netconn_write ( conn , http_html_hdr , sizeof ( http_html_hdr ) - 1 , NETCONN_NOCOPY ) ;
/* Send our HTML page */
netconn_write ( conn , http_index_html , sizeof ( http_index_html ) - 1 , NETCONN_NOCOPY ) ;
2007-07-13 20:03:25 +00:00
}
2007-10-01 19:09:28 +00:00
}
/* Close the connection (server closes in HTTP) */
netconn_close ( conn ) ;
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer ) */
netbuf_delete ( inbuf ) ;
2007-07-13 20:03:25 +00:00
}
2010-01-13 15:56:56 +00:00
/** The main function, never returns! */
static void
http_server_netconn_thread ( void * arg )
{
2007-10-01 19:09:28 +00:00
struct netconn * conn , * newconn ;
2010-01-20 07:38:10 +00:00
err_t err ;
2010-01-13 15:56:56 +00:00
LWIP_UNUSED_ARG ( arg ) ;
2007-10-01 19:09:28 +00:00
/* Create a new TCP connection handle */
conn = netconn_new ( NETCONN_TCP ) ;
2010-01-13 15:56:56 +00:00
LWIP_ERROR ( " http_server: invalid conn " , ( conn ! = NULL ) , return ; ) ;
2007-10-01 19:09:28 +00:00
/* Bind to port 80 (HTTP) with default IP address */
netconn_bind ( conn , NULL , 80 ) ;
/* Put the connection into LISTEN state */
netconn_listen ( conn ) ;
while ( 1 ) {
2010-01-20 07:38:10 +00:00
err = netconn_accept ( conn , & newconn ) ;
LWIP_ASSERT ( " err == ERR_OK " , err = = ERR_OK ) ;
if ( err = = ERR_OK ) {
http_server_netconn_serve ( newconn ) ;
netconn_delete ( newconn ) ;
}
2007-10-01 19:09:28 +00:00
}
2010-01-13 15:56:56 +00:00
return ;
}
/** Initialize the HTTP server (start its thread) */
void
http_server_netconn_init ( )
{
sys_thread_new ( " http_server_netconn " , http_server_netconn_thread , NULL , DEFAULT_THREAD_STACKSIZE , DEFAULT_THREAD_PRIO ) ;
2007-07-13 20:03:25 +00:00
}
2007-10-01 20:43:28 +00:00
# endif /* LWIP_NETCONN*/