35 #include "lwip/arch.h"
37 #include "../webserver/lwip_fs.h"
39 #include "lpc43xx_dualcore_config.h"
45 #define HTTPD_DEBUG LWIP_DBG_OFF
51 const static char http_html_hdr[] =
"HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
52 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>";
56 void http_server_netconn_init(
void);
63 static int supported_method(
const char *method)
65 if (strncmp(method,
"GET", 3) == 0)
67 if (strncmp(method,
"POST", 4) == 0)
73 static uint32_t get_version(
const char *vstr)
75 int major = 0, minor = 0;
76 sscanf(vstr,
"HTTP/%d.%d", &major, &minor);
77 return (major << 16) | minor;
82 http_server_netconn_serve(
struct netconn *conn)
89 static uint8_t file_buffer[1024];
95 err = netconn_recv(conn, &inbuf);
97 if (err != ERR_OK)
return;
99 netbuf_data(inbuf, (
void**)&buf, &buflen);
100 if (buflen < 5 || strstr(buf, CRLF) ==
NULL) {
101 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Invalid Request Line\r\n"));
105 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, (
"HTTPD: Got URI %s\r\n", buf));
107 tbuf = strchr(buf,
' ');
109 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Parse error in Request Line\r\n"));
114 if (!supported_method(buf)) {
115 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Un-supported method: %s\r\n", buf));
119 tbuf = strchr(buf,
' ');
121 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Version string not found: %s\r\n", buf));
124 req_ver = get_version(tbuf);
125 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, (
"HTTPD: Request version %d.%d\r\n",
126 req_ver >> 16, req_ver & 0xFFFF));
129 tbuf = strchr(buf,
'?');
131 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Arguements %s in URI ignored\r\n", tbuf));
134 if (strlen(buf) == 1 && *buf ==
'/') {
140 netconn_write(conn, http_html_hdr,
sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
149 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Unable to open file[%s]\r\n", buf));
151 netconn_write(conn, file_buffer, len, NETCONN_NOCOPY);
161 netconn_write(conn, fs->
data, fs->
index, NETCONN_NOCOPY);
164 while ((len =
fs_read(fs, (
char *)file_buffer,
sizeof(file_buffer))) > 0) {
165 netconn_write(conn, file_buffer, len, NETCONN_NOCOPY);
175 netbuf_delete(inbuf);
180 http_server_netconn_thread(
void *arg)
182 struct netconn *conn, *newconn;
184 LWIP_UNUSED_ARG(arg);
187 conn = netconn_new(NETCONN_TCP);
188 LWIP_ERROR(
"http_server: invalid conn", (conn !=
NULL),
return;);
191 netconn_bind(conn,
NULL, 80);
194 netconn_listen(conn);
197 err = netconn_accept(conn, &newconn);
202 err = netconn_getaddr(newconn, &remote_ip, &port_num, 0);
208 http_server_netconn_serve(newconn);
209 netconn_delete(newconn);
211 }
while(err == ERR_OK);
212 LWIP_DEBUGF(HTTPD_DEBUG,
213 (
"http_server_netconn_thread: netconn_accept received error %d, shutting down",
216 netconn_delete(conn);
228 http_server_netconn_init(
void)
233 #ifdef __IAR_SYSTEMS_ICC__