OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [httpd/] [v2_0/] [src/] [httpd.c] - Blame information for rev 174

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.