/[lwip]/contrib/apps/httpserver/httpserver-netconn.c
ViewVC logotype

Contents of /contrib/apps/httpserver/httpserver-netconn.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations) (download)
Sun Oct 28 14:17:49 2007 UTC (16 years, 1 month ago) by goldsimon
Branch: MAIN
CVS Tags: STABLE-1_3_1, STABLE-1_3_0, STABLE-1_3_2
Changes since 1.3: +1 -1 lines
File MIME type: text/plain
Use the api function netconn_err() instead of directly dereferencing the struct netconn.

1 #include "lwip/opt.h"
2 #include "lwip/arch.h"
3 #include "lwip/api.h"
4
5 #if LWIP_NETCONN
6
7 const static char http_html_hdr[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
8 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.</body></html>";
9
10 void http_server_serve(struct netconn *conn) {
11 struct netbuf *inbuf;
12 char *buf;
13 u16_t buflen;
14
15 /* Read the data from the port, blocking if nothing yet there.
16 We assume the request (the part we care about) is in one netbuf */
17 inbuf = netconn_recv(conn);
18
19 if (netconn_err(conn) == ERR_OK) {
20 netbuf_data(inbuf, &buf, &buflen);
21
22 /* Is this an HTTP GET command? (only check the first 5 chars, since
23 there are other formats for GET, and we're keeping it very simple )*/
24 if (buflen>=5 &&
25 buf[0]=='G' &&
26 buf[1]=='E' &&
27 buf[2]=='T' &&
28 buf[3]==' ' &&
29 buf[4]=='/' ) {
30
31 /* Send the HTML header
32 * subtract 1 from the size, since we dont send the \0 in the string
33 * NETCONN_NOCOPY: our data is const static, so no need to copy it
34 */
35 netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
36
37 /* Send our HTML page */
38 netconn_write(conn, http_index_html, sizeof(http_index_html)-1, NETCONN_NOCOPY);
39 }
40 }
41 /* Close the connection (server closes in HTTP) */
42 netconn_close(conn);
43
44 /* Delete the buffer (netconn_recv gives us ownership,
45 so we have to make sure to deallocate the buffer) */
46 netbuf_delete(inbuf);
47 }
48
49 int http_server() {
50 struct netconn *conn, *newconn;
51
52 /* Create a new TCP connection handle */
53 conn = netconn_new(NETCONN_TCP);
54 LWIP_ERROR("http_server: invalid conn", (conn != NULL), return -1;);
55
56 /* Bind to port 80 (HTTP) with default IP address */
57 netconn_bind(conn, NULL, 80);
58
59 /* Put the connection into LISTEN state */
60 netconn_listen(conn);
61
62 while(1) {
63 newconn = netconn_accept(conn);
64 http_server_serve(newconn);
65 netconn_delete(newconn);
66 }
67 return 0;
68 }
69
70 #endif /* LWIP_NETCONN*/

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26