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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [httpd/] [current/] [src/] [httpd.c] - Blame information for rev 791

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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