Date:2010-07-23 15:15:22 (13 years 8 months ago)
Author:jow
Commit:e417063c23716f0dd35f98f1990bb8dad7b963f4
Message:[package] uhttpd: - fix a compile warning - support custom index file names - support custom error pages (or cgi handler) - add option to disable directory listings - add REDIRECT_STATUS for CGI requests, fixes php-cgi

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@22366 3c298f89-4303-0410-b956-a3cf2f4a3e73
Files: package/uhttpd/Makefile (1 diff)
package/uhttpd/files/uhttpd.init (2 diffs)
package/uhttpd/src/uhttpd-cgi.c (1 diff)
package/uhttpd/src/uhttpd-file.c (4 diffs)
package/uhttpd/src/uhttpd-utils.c (2 diffs)
package/uhttpd/src/uhttpd.c (9 diffs)
package/uhttpd/src/uhttpd.h (2 diffs)

Change Details

package/uhttpd/Makefile
88include $(TOPDIR)/rules.mk
99
1010PKG_NAME:=uhttpd
11PKG_RELEASE:=11
11PKG_RELEASE:=12
1212
1313PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
1414
package/uhttpd/files/uhttpd.init
1717    [ -n "$val" -o -n "$def" ] && append UHTTPD_ARGS "$opt ${val:-$def}"
1818}
1919
20append_bool() {
21    local cfg="$1"
22    local var="$2"
23    local opt="$3"
24    local def="$4"
25    local val
26
27    config_get_bool val "$cfg" "$var" "$def"
28    [ "$val" = 1 ] && append UHTTPD_ARGS "$opt"
29}
30
2031generate_keys() {
2132    local cfg="$1"
2233    local key="$2"
...... 
5970    append_arg "$cfg" lua_handler "-L"
6071    append_arg "$cfg" script_timeout "-t"
6172    append_arg "$cfg" network_timeout "-T"
73    append_arg "$cfg" error_page "-E"
74    append_arg "$cfg" index_page "-I"
75
76    append_bool "$cfg" no_symlinks "-S" 0
77    append_bool "$cfg" no_dirlists "-D" 0
6278
6379    config_get http "$cfg" listen_http
6480    for listen in $http; do
package/uhttpd/src/uhttpd-cgi.c
234234                if( pi->info )
235235                    setenv("PATH_INFO", pi->info, 1);
236236
237                /* REDIRECT_STATUS, php-cgi wants it */
238                switch( req->redirect_status )
239                {
240                    case 404:
241                        setenv("REDIRECT_STATUS", "404", 1);
242                        break;
243
244                    default:
245                        setenv("REDIRECT_STATUS", "200", 1);
246                        break;
247                }
237248
238249                /* http version */
239250                if( req->version > 1.0 )
package/uhttpd/src/uhttpd-file.c
2929static const char * uh_file_mime_lookup(const char *path)
3030{
3131    struct mimetype *m = &uh_mime_types[0];
32    char *e;
32    const char *e;
3333
3434    while( m->extn )
3535    {
...... 
275275            strncat(filename, files[i]->d_name,
276276                sizeof(filename) - strlen(files[i]->d_name));
277277
278            if( !stat(filename, &s) && (s.st_mode & S_IFDIR) )
278            if( !stat(filename, &s) &&
279                (s.st_mode & S_IFDIR) && (s.st_mode & S_IXOTH)
280            )
279281                uh_http_sendf(cl, req,
280282                    "<li><strong><a href='%s%s'>%s</a>/</strong><br />"
281283                    "<small>modified: %s<br />directory - %.02f kbyte"
...... 
293295            strncat(filename, files[i]->d_name,
294296                sizeof(filename) - strlen(files[i]->d_name));
295297
296            if( !stat(filename, &s) && !(s.st_mode & S_IFDIR) )
298            if( !stat(filename, &s) &&
299                !(s.st_mode & S_IFDIR) && (s.st_mode & S_IROTH)
300            )
297301                uh_http_sendf(cl, req,
298302                    "<li><strong><a href='%s%s'>%s</a></strong><br />"
299303                    "<small>modified: %s<br />%s - %.02f kbyte<br />"
...... 
369373    }
370374
371375    /* directory */
372    else if( pi->stat.st_mode & S_IFDIR )
376    else if( (pi->stat.st_mode & S_IFDIR) && !cl->server->conf->no_dirlists )
373377    {
374378        /* write status */
375379        uh_file_response_200(cl, req, NULL);
package/uhttpd/src/uhttpd-utils.c
464464    int i = 0;
465465    struct stat s;
466466
467    /* back out early if url is undefined */
468    if ( url == NULL )
469        return NULL;
467470
468471    memset(path_phys, 0, sizeof(path_phys));
469472    memset(path_info, 0, sizeof(path_info));
...... 
550553            memcpy(buffer, path_phys, sizeof(buffer));
551554            pathptr = &buffer[strlen(buffer)];
552555
553            for( i = 0; i < array_size(uh_index_files); i++ )
556            if( cl->server->conf->index_file )
554557            {
555                strncat(buffer, uh_index_files[i], sizeof(buffer));
558                strncat(buffer, cl->server->conf->index_file, sizeof(buffer));
556559
557560                if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
558561                {
559562                    memcpy(path_phys, buffer, sizeof(path_phys));
560563                    memcpy(&p.stat, &s, sizeof(p.stat));
561                    break;
562564                }
565            }
566            else
567            {
568                for( i = 0; i < array_size(uh_index_files); i++ )
569                {
570                    strncat(buffer, uh_index_files[i], sizeof(buffer));
563571
564                *pathptr = 0;
572                    if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
573                    {
574                        memcpy(path_phys, buffer, sizeof(path_phys));
575                        memcpy(&p.stat, &s, sizeof(p.stat));
576                        break;
577                    }
578
579                    *pathptr = 0;
580                }
565581            }
566582
567583            p.root = docroot;
package/uhttpd/src/uhttpd.c
4747    while( waitpid(-1, NULL, WNOHANG) > 0 ) { }
4848}
4949
50static void uh_config_parse(const char *path)
50static void uh_config_parse(struct config *conf)
5151{
5252    FILE *c;
5353    char line[512];
...... 
5555    char *pass = NULL;
5656    char *eol = NULL;
5757
58    if( (c = fopen(path ? path : "/etc/httpd.conf", "r")) != NULL )
58    const char *path = conf->file ? conf->file : "/etc/httpd.conf";
59
60
61    if( (c = fopen(path, "r")) != NULL )
5962    {
6063        memset(line, 0, sizeof(line));
6164
...... 
7477                        "Notice: No password set for user %s, ignoring "
7578                        "authentication on %s\n", user, line
7679                    );
80                }
81            }
82            else if( !strncmp(line, "I:", 2) )
83            {
84                if( !(user = strchr(line, ':')) || (*user++ = 0) ||
85                    !(eol = strchr(user, '\n')) || (*eol++ = 0) )
86                        continue;
7787
78                    break;
79                }
88                conf->index_file = strdup(user);
89            }
90            else if( !strncmp(line, "E404:", 5) )
91            {
92                if( !(user = strchr(line, ':')) || (*user++ = 0) ||
93                    !(eol = strchr(user, '\n')) || (*eol++ = 0) )
94                        continue;
95
96                conf->error_handler = strdup(user);
8097            }
8198        }
8299
...... 
302319        }
303320
304321        /* valid enough */
322        req.redirect_status = 200;
305323        return &req;
306324    }
307325
...... 
505523    }
506524#endif
507525
508    while( (opt = getopt(argc, argv, "fSC:K:p:s:h:c:l:L:d:r:m:x:t:T:")) > 0 )
509    {
526    while( (opt = getopt(argc, argv,
527        "fSDC:K:E:I:p:s:h:c:l:L:d:r:m:x:t:T:")) > 0
528    ) {
510529        switch(opt)
511530        {
512531            /* [addr:]port */
...... 
597616                }
598617                break;
599618
619            /* error handler */
620            case 'E':
621                if( (strlen(optarg) == 0) || (optarg[0] != '/') )
622                {
623                    fprintf(stderr, "Error: Invalid error handler: %s\n",
624                        optarg);
625                    exit(1);
626                }
627                conf.error_handler = optarg;
628                break;
629
630            /* index file */
631            case 'I':
632                if( (strlen(optarg) == 0) || (optarg[0] == '/') )
633                {
634                    fprintf(stderr, "Error: Invalid index page: %s\n",
635                        optarg);
636                    exit(1);
637                }
638                conf.index_file = optarg;
639                break;
640
600641            /* don't follow symlinks */
601642            case 'S':
602643                conf.no_symlinks = 1;
603644                break;
604645
646            /* don't list directories */
647            case 'D':
648                conf.no_dirlists = 1;
649                break;
650
605651#ifdef HAVE_CGI
606652            /* cgi prefix */
607653            case 'x':
...... 
678724                    " -K file ASN.1 server private key file\n"
679725#endif
680726                    " -h directory Specify the document root, default is '.'\n"
727                    " -E string Use given virtual URL as 404 error handler\n"
728                    " -I string Use given filename as index page for directories\n"
681729                    " -S Do not follow symbolic links outside of the docroot\n"
730                    " -D Do not allow directory listings, send 403 instead\n"
682731#ifdef HAVE_LUA
683732                    " -l string URL prefix for Lua handler, default is '/lua'\n"
684733                    " -L file Lua handler script, omit to disable Lua\n"
...... 
727776        conf.realm = "Protected Area";
728777
729778    /* config file */
730    uh_config_parse(conf.file);
779    uh_config_parse(&conf);
731780
732781    /* default network timeout */
733782    if( conf.network_timeout <= 0 )
...... 
913962                        /* 404 */
914963                        else
915964                        {
916                            uh_http_sendhf(cl, 404, "Not Found",
917                                "No such file or directory");
965                            /* Try to invoke an error handler */
966                            pin = uh_path_lookup(cl, conf.error_handler);
967
968                            if( pin && uh_auth_check(cl, req, pin) )
969                            {
970                                req->redirect_status = 404;
971
972#ifdef HAVE_CGI
973                                if( uh_path_match(conf.cgi_prefix, pin->name) )
974                                {
975                                    uh_cgi_request(cl, req, pin);
976                                }
977                                else
978#endif
979                                {
980                                    uh_file_request(cl, req, pin);
981                                }
982                            }
983                            else
984                            {
985                                uh_http_sendhf(cl, 404, "Not Found",
986                                    "No such file or directory");
987                            }
918988                        }
919989                    }
920990
package/uhttpd/src/uhttpd.h
6464    char docroot[PATH_MAX];
6565    char *realm;
6666    char *file;
67    char *index_file;
68    char *error_handler;
6769    int no_symlinks;
70    int no_dirlists;
6871    int network_timeout;
6972#ifdef HAVE_CGI
7073    char *cgi_prefix;
...... 
124127struct http_request {
125128    int method;
126129    float version;
130    int redirect_status;
127131    char *url;
128132    char *headers[UH_LIMIT_HEADERS];
129133    struct auth_realm *realm;

Archive Download the corresponding diff file



interactive