annotate packages/net/httpd/current/src/httpd.c @ 1240:eeb14eae974b

* src/httpd.c (cyg_httpd_server): Removed unused variable. * doc/httpd.sgml: Updated documentation for previous change. * include/httpd.h: changed prototype of cyg_httpd_send_html and cyg_httpd_send_data to match implementation.
author asl
date Tue, 23 Sep 2003 11:40:14 +0000
parents 657f409c5dfe
children 970d06598f83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
1 /* =================================================================
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
2 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
3 * httpd.c
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
4 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
5 * A simple embedded HTTP server
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
6 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
7 * =================================================================
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
8 * ####ECOSGPLCOPYRIGHTBEGIN####
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
9 * -------------------------------------------
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
10 * This file is part of eCos, the Embedded Configurable Operating
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
11 * System.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
12 * Copyright (C) 2002 Nick Garnett.
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
13 * Copyright (C) 2003 Andrew Lunn.
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
14 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
15 * eCos is free software; you can redistribute it and/or modify it
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
16 * under the terms of the GNU General Public License as published by
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
17 * the Free Software Foundation; either version 2 or (at your option)
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
18 * any later version.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
19 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
20 * eCos is distributed in the hope that it will be useful, but
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
23 * General Public License for more details.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
24 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
25 * You should have received a copy of the GNU General Public License
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
26 * along with eCos; if not, write to the Free Software Foundation,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
27 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
28 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
29 * As a special exception, if other files instantiate templates or
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
30 * use macros or inline functions from this file, or you compile this
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
31 * file and link it with other works to produce a work based on this
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
32 * file, this file does not by itself cause the resulting work to be
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
33 * covered by the GNU General Public License. However the source code
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
34 * for this file must still be made available in accordance with
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
35 * section (3) of the GNU General Public License.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
36 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
37 * This exception does not invalidate any other reasons why a work
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
38 * based on this file might be covered by the GNU General Public
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
39 * License.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
40 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
41 * -------------------------------------------
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
42 * ####ECOSGPLCOPYRIGHTEND####
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
43 * =================================================================
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
44 * #####DESCRIPTIONBEGIN####
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
45 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
46 * Author(s): nickg@calivar.com
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
47 * Contributors: nickg@calivar.com, Andrew.lunn@ascom.ch
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
48 * Date: 2002-10-14
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
49 * Purpose:
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
50 * Description:
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
51 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
52 * ####DESCRIPTIONEND####
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
53 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
54 * =================================================================
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
55 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
56
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
57 #include <pkgconf/system.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
58 #include <pkgconf/isoinfra.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
59 #include <pkgconf/httpd.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
60
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
61 #include <cyg/infra/cyg_trac.h> /* tracing macros */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
62 #include <cyg/infra/cyg_ass.h> /* assertion macros */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
63
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
64 #include <unistd.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
65 #include <fcntl.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
66 #include <sys/stat.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
67 #include <stdio.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
68 #include <errno.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
69 #include <string.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
70
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
71 #include <network.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
72 #include <arpa/inet.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
73
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
74 #include <cyg/httpd/httpd.h>
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
75
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
76 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
77
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
78 #if 0
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
79 #define HTTPD_DIAG diag_printf
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
80 #else
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
81 #define HTTPD_DIAG(...)
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
82 #endif
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
83
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
84 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
85 /* Server socket address and file descriptor.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
86 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
87
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
88 static struct sockaddr_in server_address;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
89
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
90 static int server_socket = -1;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
91 #ifdef CYGPKG_NET_INET6
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
92 static int server_socket6 = -1;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
93 static struct sockaddr_in6 server_address6;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
94 #endif
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
95
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
96 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
97 /* Thread stacks, etc.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
98 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
99
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
100 static cyg_uint8 httpd_stacks[CYGNUM_HTTPD_THREAD_COUNT]
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
101 [CYGNUM_HAL_STACK_SIZE_MINIMUM+
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
102 CYGNUM_HTTPD_SERVER_BUFFER_SIZE+
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
103 CYGNUM_HTTPD_THREAD_STACK_SIZE];
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
104
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
105 static cyg_handle_t httpd_thread[CYGNUM_HTTPD_THREAD_COUNT];
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
106
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
107 static cyg_thread httpd_thread_object[CYGNUM_HTTPD_THREAD_COUNT];
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
108
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
109 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
110 /* Filename lookup table
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
111 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
112
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
113 CYG_HAL_TABLE_BEGIN( cyg_httpd_table, httpd_table );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
114 CYG_HAL_TABLE_END( cyg_httpd_table_end, httpd_table );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
115
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
116 __externC cyg_httpd_table_entry cyg_httpd_table[];
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
117 __externC cyg_httpd_table_entry cyg_httpd_table_end[];
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
118
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
119 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
120 /* Page not found message
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
121 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
122
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
123 static char cyg_httpd_not_found[] =
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
124 "<head><title>Page Not found</title></head>\n"
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
125 "<body><h2>The requested URL was not found on this server.</h2></body>\n";
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
126
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
127 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
128 /* Simple pattern matcher for filenames
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
129 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
130 * This performs a simple pattern match between the given name and the
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
131 * pattern. At present the only matching supported is either exact, or
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
132 * if the pattern ends in * then that matches all remaining
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
133 * characters. At some point we might want to implement a more
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
134 * complete regular expression parser here.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
135 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
136
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
137 static cyg_bool match( char *name, char *pattern )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
138 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
139 while( *name != 0 && *pattern != 0 && *name == *pattern )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
140 name++, pattern++;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
141
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
142 if( *name == 0 && *pattern == 0 )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
143 return true;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
144
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
145 if( *pattern == '*' )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
146 return true;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
147
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
148 return false;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
149 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
150
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
151
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
152 /* ================================================================= */
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
153 /* Main processing function */
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
154 /* */
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
155 /* Reads the HTTP header, look it up in the table and calls the */
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
156 /* handler. */
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
157
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
158 static void cyg_httpd_process( int client_socket, struct sockaddr *client_address )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
159 {
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
160 int calen = sizeof(*client_address);
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
161 int nlc = 0;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
162 char request[CYGNUM_HTTPD_SERVER_BUFFER_SIZE];
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
163 FILE *client;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
164 cyg_httpd_table_entry *entry = cyg_httpd_table;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
165 char *filename;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
166 char *formdata = NULL;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
167 char *p;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
168 cyg_bool success = false;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
169 char name[64];
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
170 char port[10];
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
171
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
172 getnameinfo(client_address, calen, name, sizeof(name),
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
173 port, sizeof(port), NI_NUMERICHOST|NI_NUMERICSERV);
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
174 HTTPD_DIAG("Connection from %s[%s]\n",name,port);
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
175
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
176 /* Convert the file descriptor to a C library FILE object so
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
177 * we can use fprintf() and friends on it.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
178 */
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
179 client = fdopen( client_socket, "r+");
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
180
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
181 /* We are really only interested in the first line.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
182 */
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
183 fgets( request, sizeof(request), client );
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
184
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
185 HTTPD_DIAG("Request >%s<\n", request );
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
186
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
187 /* Absorb the rest of the header. We nibble it away a
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
188 * character at a time like this to avoid having to define
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
189 * another buffer to read lines into. If we ever need to take
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
190 * more interest in the header fields, we will need to be a
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
191 * lot more sophisticated than this.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
192 */
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
193 do{
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
194 int c = getc( client );
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
195 HTTPD_DIAG("%c",c);
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
196 if( c == '\n' )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
197 nlc++;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
198 else if( c != '\r' )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
199 nlc = 0;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
200 } while(nlc < 2);
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
201
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
202 /* Extract the filename and any form data being returned.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
203 * We know that the "GET " request takes 4 bytes.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
204 * TODO: handle POST type requests as well as GET's.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
205 */
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
206
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
207 filename = p = request+4;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
208
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
209 /* Now scan the filename until we hit a space or a '?'. If we
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
210 * end on a '?' then the rest is a form request. Put NULs at
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
211 * the end of each string.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
212 */
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
213 while( *p != ' ' && *p != '?' )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
214 p++;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
215 if( *p == '?' )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
216 formdata = p+1;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
217 *p = 0;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
218
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
219 if( formdata != NULL )
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
220 {
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
221 while( *p != ' ' )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
222 p++;
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
223 *p = 0;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
224 }
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
225
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
226 HTTPD_DIAG("Request filename >%s< formdata >%s<\n",filename,formdata?formdata:"-NULL-");
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
227
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
228 HTTPD_DIAG("table: %08x...%08x\n",cyg_httpd_table, cyg_httpd_table_end);
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
229
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
230 /* Now scan the table for a matching entry. If we find one
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
231 * call the handler routine. If that returns true then we
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
232 * terminate the scan, otherwise we keep looking.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
233 */
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
234 while( entry != cyg_httpd_table_end )
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
235 {
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
236 HTTPD_DIAG("try %08x: %s\n", entry, entry->pattern);
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
237
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
238 if( match( filename, entry->pattern ) )
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
239 {
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
240 if( (success = entry->handler( client, filename, formdata, entry->arg )) )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
241 break;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
242 }
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
243
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
244 entry++;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
245 }
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
246
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
247 /* If we failed to find a match in the table, send a "not
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
248 * found" response.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
249 * TODO: add an optional fallback to go look for files in
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
250 * some filesystem, somewhere.
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
251 */
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
252 if( !success )
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
253 cyg_httpd_send_html( client, NULL, NULL, cyg_httpd_not_found );
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
254
1053
657f409c5dfe * src/httpd.c: Fixed a problem with closing the client socket --
nickg
parents: 1008
diff changeset
255 fclose(client);
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
256 }
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
257
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
258 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
259 /* Main HTTP server
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
260 *
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
261 * This just loops, collects client connections, and calls the main
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
262 * process function on the connects*/
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
263
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
264 static void cyg_httpd_server( cyg_addrword_t arg )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
265 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
266 do
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
267 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
268 int client_socket;
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
269 struct sockaddr client_address;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
270 int calen = sizeof(client_address);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
271 fd_set readfds;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
272 int n;
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
273
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
274 /* Wait for a connection.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
275 */
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
276 FD_ZERO(&readfds);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
277 FD_SET(server_socket, &readfds);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
278 #ifdef CYGPKG_NET_INET6
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
279 FD_SET(server_socket6, &readfds);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
280 n = (server_socket > server_socket6 ? server_socket : server_socket6) + 1;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
281 #else
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
282 n = server_socket + 1;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
283 #endif
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
284 select(n,&readfds,NULL,NULL,NULL);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
285 if (FD_ISSET(server_socket, &readfds)) {
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
286 client_socket = accept( server_socket, &client_address, &calen );
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
287 cyg_httpd_process(client_socket, &client_address);
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
288 }
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
289 #ifdef CYGPKG_NET_INET6
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
290 if (FD_ISSET(server_socket6, &readfds)) {
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
291 client_socket = accept( server_socket6, &client_address, &calen );
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
292 cyg_httpd_process(client_socket, &client_address);
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
293 }
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
294 #endif
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
295 } while(1);
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
296 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
297
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
298 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
299 /* Initialization thread
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
300 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
301 * Optionally delay for a time before getting the network
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
302 * running. Then create and bind the server socket and put it into
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
303 * listen mode. Spawn any further server threads, then enter server
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
304 * mode.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
305 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
306
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
307 static void cyg_httpd_init(cyg_addrword_t arg)
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
308 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
309 int i;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
310 int err = 0;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
311
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
312 /* Delay for a configurable length of time to give the application
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
313 * a chance to get going, or even complete, without interference
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
314 * from the HTTPD.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
315 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
316 if( CYGNUM_HTTPD_SERVER_DELAY > 0 )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
317 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
318 cyg_thread_delay( CYGNUM_HTTPD_SERVER_DELAY );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
319 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
320
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
321 server_address.sin_family = AF_INET;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
322 server_address.sin_len = sizeof(server_address);
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
323 server_address.sin_addr.s_addr = INADDR_ANY;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
324 server_address.sin_port = htons(CYGNUM_HTTPD_SERVER_PORT);
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
325 #ifdef CYGPKG_NET_INET6
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
326 server_address6.sin6_family = AF_INET6;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
327 server_address6.sin6_len = sizeof(server_address6);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
328 server_address6.sin6_addr = in6addr_any;
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
329 server_address6.sin6_port = htons(CYGNUM_HTTPD_SERVER_PORT);
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
330 #endif
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
331 /* Get the network going. This is benign if the application has
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
332 * already done this.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
333 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
334 init_all_network_interfaces();
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
335
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
336 /* Create and bind the server socket.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
337 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
338 server_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
339 CYG_ASSERT( server_socket > 0, "Socket create failed");
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
340
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
341 err = bind( server_socket, (struct sockaddr *)&server_address,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
342 sizeof(server_address) );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
343 CYG_ASSERT( err == 0, "bind() returned error");
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
344
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
345 err = listen( server_socket, SOMAXCONN );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
346 CYG_ASSERT( err == 0, "listen() returned error" );
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
347 #ifdef CYGPKG_NET_INET6
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
348 server_socket6 = socket( AF_INET6, SOCK_STREAM, IPPROTO_TCP );
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
349 CYG_ASSERT( server_socket6 > 0, "Socket AF_INET6 create failed");
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
350
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
351 err = bind( server_socket6, (struct sockaddr *)&server_address6,
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
352 sizeof(server_address6) );
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
353 CYG_ASSERT( err == 0, "bind(AF_INET6) returned error");
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
354
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
355 err = listen( server_socket6, SOMAXCONN );
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
356 CYG_ASSERT( err == 0, "listen(AF_INET6) returned error" );
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
357 #endif
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
358 /* If we are configured to have more than one server thread,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
359 * create them now.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
360 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
361 for( i = 1; i < CYGNUM_HTTPD_THREAD_COUNT; i++ )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
362 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
363 cyg_thread_create( CYGNUM_HTTPD_THREAD_PRIORITY,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
364 cyg_httpd_server,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
365 0,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
366 "HTTPD",
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
367 &httpd_stacks[i][0],
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
368 sizeof(httpd_stacks[i]),
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
369 &httpd_thread[i],
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
370 &httpd_thread_object[i]
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
371 );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
372
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
373 cyg_thread_resume( httpd_thread[i] );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
374 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
375
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
376 /* Now go be a server ourself.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
377 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
378 cyg_httpd_server(arg);
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
379 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
380
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
381 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
382 /* System initializer
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
383 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
384 * This is called from the static constructor in init.cxx. It spawns
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
385 * the main server thread and makes it ready to run.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
386 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
387
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
388 __externC void cyg_httpd_startup(void)
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
389 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
390 cyg_thread_create( CYGNUM_HTTPD_THREAD_PRIORITY,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
391 cyg_httpd_init,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
392 0,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
393 "HTTPD",
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
394 &httpd_stacks[0][0],
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
395 sizeof(httpd_stacks[0]),
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
396 &httpd_thread[0],
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
397 &httpd_thread_object[0]
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
398 );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
399
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
400 cyg_thread_resume( httpd_thread[0] );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
401 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
402
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
403 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
404 /* HTTP protocol handling
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
405 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
406 * cyg_http_start() generates an HTTP header with the given content
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
407 * type and, if non-zero, length.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
408 * cyg_http_finish() just adds a couple of newlines for luck and
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
409 * flushes the stream.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
410 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
411
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
412 __externC void cyg_http_start( FILE *client, char *content_type,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
413 int content_length )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
414 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
415 fputs( "HTTP/1.1 200 OK\n"
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
416 "Server: " CYGDAT_HTTPD_SERVER_ID,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
417 client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
418
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
419 if( content_type != NULL )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
420 fprintf( client,"Content-type: %s\n", content_type );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
421
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
422 if( content_length != 0 )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
423 fprintf( client, "Content-length: %d\n", content_length );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
424
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
425 fputs( "Connection: close\n"
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
426 "\n",
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
427 client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
428 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
429
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
430 __externC void cyg_http_finish( FILE *client )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
431 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
432 fputs( "\n\n", client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
433 fflush( client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
434 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
435
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
436
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
437 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
438 /* HTML tag generation
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
439 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
440 * These functions generate standard HTML begin and end tags. By using
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
441 * these rather than direct printf()s we help to reduce the number of
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
442 * distinct strings present in the executable.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
443 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
444
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
445 __externC void cyg_html_tag_begin( FILE *client, char *tag, char *attr )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
446 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
447 char *pad = "";
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
448
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
449 if( attr == NULL )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
450 attr = pad;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
451 else if( attr[0] != 0 )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
452 pad = " ";
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
453
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
454 fprintf(client, "<%s%s%s>\n",tag,pad,attr);
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
455 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
456
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
457 __externC void cyg_html_tag_end( FILE *client, char *tag )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
458 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
459 fprintf( client, "<%s%s%s>\n","/",tag,"");
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
460 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
461
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
462 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
463 /* Parse form request data
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
464 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
465 * Given a form response string, we parse it into an argv/environment
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
466 * style array of "name=value" strings. We also convert any '+'
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
467 * separators back into spaces.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
468 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
469 * TODO: also translate any %xx escape sequences back into real
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
470 * characters.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
471 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
472
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
473 __externC void cyg_formdata_parse( char *data, char *list[], int size )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
474 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
475 char *p = data;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
476 int i = 0;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
477
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
478 list[i] = p;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
479
1008
4ca8a5f30c03 * src/monitor.c (cyg_monitor_network): Added IPv6 information to
asl
parents: 468
diff changeset
480 while( p && *p != 0 && i < size-1 )
468
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
481 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
482 if( *p == '&' )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
483 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
484 *p++ = 0;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
485 list[++i] = p;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
486 continue;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
487 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
488 if( *p == '+' )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
489 *p = ' ';
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
490 p++;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
491 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
492
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
493 list[++i] = 0;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
494 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
495
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
496 /* ----------------------------------------------------------------- */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
497 /* Search for a form response value
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
498 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
499 * Search a form response list generated by cyg_formdata_parse() for
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
500 * the named element. If it is found a pointer to the value part is
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
501 * returned. If it is not found a NULL pointer is returned.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
502 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
503
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
504 __externC char *cyg_formlist_find( char *list[], char *name )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
505 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
506 while( *list != 0 )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
507 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
508 char *p = *list;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
509 char *q = name;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
510
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
511 while( *p == *q )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
512 p++, q++;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
513
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
514 if( *q == 0 && *p == '=' )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
515 return p+1;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
516
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
517 list++;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
518 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
519
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
520 return 0;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
521 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
522
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
523 /* ================================================================= */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
524 /* Predefined page handlers
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
525 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
526
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
527 /* ----------------------------------------------------------------- */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
528 /* Send an HTML page from a single string
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
529 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
530 * This just sends the string passed as the argument with an HTTP
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
531 * header that describes it as HTML. This is useful for sending
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
532 * straightforward static web content.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
533 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
534
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
535 __externC cyg_bool cyg_httpd_send_html( FILE *client, char *filename,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
536 char *request, void *arg )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
537 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
538 html_begin( client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
539
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
540 fwrite( arg, 1, strlen((char *)arg), client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
541
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
542 html_end( client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
543
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
544 return true;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
545 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
546
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
547 /* ----------------------------------------------------------------- */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
548 /* Send arbitrary data
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
549 *
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
550 * This takes a pointer to a cyg_httpd_data structure as the argument
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
551 * and sends the data therein after a header that uses the content
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
552 * type and size from the structure. This is useful for non-HTML data
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
553 * such a images.
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
554 */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
555
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
556 __externC cyg_bool cyg_httpd_send_data( FILE *client, char *filename,
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
557 char *request, void *arg )
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
558 {
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
559 cyg_httpd_data *data = (cyg_httpd_data *)arg;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
560
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
561 cyg_http_start( client, data->content_type, data->content_length );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
562
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
563 fwrite( data->data, 1, data->content_length, client );
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
564
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
565 return true;
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
566 }
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
567
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
568 /* ----------------------------------------------------------------- */
ca4f29d41b18 Created new HTTPD package.
nickg
parents:
diff changeset
569 /* end of httpd.c */