35 #include "lwip/arch.h"
39 #include "lpc43xx_dualcore_config.h"
40 #include "httpserver-netconn.h"
46 #define HTTPD_DEBUG LWIP_DBG_OFF
50 const static char http_html_hdr[] =
"HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
51 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>";
61 static int supported_method(
const char *method)
63 if (strncmp(method,
"GET", 3) == 0)
65 if (strncmp(method,
"POST", 4) == 0)
71 static uint32_t get_version(
const char *vstr)
73 int major = 0, minor = 0;
74 sscanf(vstr,
"HTTP/%d.%d", &major, &minor);
75 return (major << 16) | minor;
80 http_server_netconn_serve(
struct netconn *conn)
87 static uint8_t file_buffer[1024];
93 err = netconn_recv(conn, &inbuf);
95 if (err != ERR_OK)
return;
97 netbuf_data(inbuf, (
void**)&buf, &buflen);
98 if (buflen < 5 || strstr(buf, CRLF) ==
NULL) {
99 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Invalid Request Line\r\n"));
103 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, (
"HTTPD: Got URI %s\r\n", buf));
105 tbuf = strchr(buf,
' ');
107 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Parse error in Request Line\r\n"));
112 if (!supported_method(buf)) {
113 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Un-supported method: %s\r\n", buf));
117 tbuf = strchr(buf,
' ');
119 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Version string not found: %s\r\n", buf));
122 req_ver = get_version(tbuf);
123 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, (
"HTTPD: Request version %d.%d\r\n",
124 req_ver >> 16, req_ver & 0xFFFF));
127 tbuf = strchr(buf,
'?');
129 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Arguements %s in URI ignored\r\n", tbuf));
132 if (strlen(buf) == 1 && *buf ==
'/') {
138 netconn_write(conn, http_html_hdr,
sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
147 LWIP_DEBUGF(HTTPD_DEBUG, (
"HTTPD: Unable to open file[%s]\r\n", buf));
149 netconn_write(conn, file_buffer, len, NETCONN_NOCOPY);
157 if (fs->http_header_included)
159 netconn_write(conn, fs->data, fs->index, NETCONN_NOCOPY);
162 while ((len =
fs_read(fs, (
char *)file_buffer,
sizeof(file_buffer))) > 0) {
163 netconn_write(conn, file_buffer, len, NETCONN_NOCOPY);
173 netbuf_delete(inbuf);
178 http_server_netconn_thread(
void *arg)
180 struct netconn *conn, *newconn;
182 LWIP_UNUSED_ARG(arg);
185 conn = netconn_new(NETCONN_TCP);
186 LWIP_ERROR(
"http_server: invalid conn", (conn !=
NULL),
return;);
189 netconn_bind(conn,
NULL, 80);
192 netconn_listen(conn);
195 err = netconn_accept(conn, &newconn);
200 err = netconn_getaddr(newconn, &remote_ip, &port_num, 0);
205 http_server_netconn_serve(newconn);
206 netconn_delete(newconn);
208 }
while(err == ERR_OK);
209 LWIP_DEBUGF(HTTPD_DEBUG,
210 (
"http_server_netconn_thread: netconn_accept received error %d, shutting down",
213 netconn_delete(conn);
225 http_server_netconn_init()