Root/package/uhttpd/src/uhttpd-utils.c

1/*
2 * uhttpd - Tiny single-threaded httpd - Utility functions
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#define _XOPEN_SOURCE 500 /* crypt() */
20#define _BSD_SOURCE /* strcasecmp(), strncasecmp() */
21
22#include "uhttpd.h"
23#include "uhttpd-utils.h"
24
25#ifdef HAVE_TLS
26#include "uhttpd-tls.h"
27#endif
28
29
30static char *uh_index_files[] = {
31    "index.html",
32    "index.htm",
33    "default.html",
34    "default.htm"
35};
36
37
38const char * sa_straddr(void *sa)
39{
40    static char str[INET6_ADDRSTRLEN];
41    struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
42    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)sa;
43
44    if( v4->sin_family == AF_INET )
45        return inet_ntop(AF_INET, &(v4->sin_addr), str, sizeof(str));
46    else
47        return inet_ntop(AF_INET6, &(v6->sin6_addr), str, sizeof(str));
48}
49
50const char * sa_strport(void *sa)
51{
52    static char str[6];
53    snprintf(str, sizeof(str), "%i", sa_port(sa));
54    return str;
55}
56
57int sa_port(void *sa)
58{
59    return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
60}
61
62int sa_rfc1918(void *sa)
63{
64    struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
65    unsigned long a = htonl(v4->sin_addr.s_addr);
66
67    if( v4->sin_family == AF_INET )
68    {
69        return ((a >= 0x0A000000) && (a <= 0x0AFFFFFF)) ||
70               ((a >= 0xAC100000) && (a <= 0xAC1FFFFF)) ||
71               ((a >= 0xC0A80000) && (a <= 0xC0A8FFFF));
72    }
73
74    return 0;
75}
76
77/* Simple strstr() like function that takes len arguments for both haystack and needle. */
78char *strfind(char *haystack, int hslen, const char *needle, int ndlen)
79{
80    int match = 0;
81    int i, j;
82
83    for( i = 0; i < hslen; i++ )
84    {
85        if( haystack[i] == needle[0] )
86        {
87            match = ((ndlen == 1) || ((i + ndlen) <= hslen));
88
89            for( j = 1; (j < ndlen) && ((i + j) < hslen); j++ )
90            {
91                if( haystack[i+j] != needle[j] )
92                {
93                    match = 0;
94                    break;
95                }
96            }
97
98            if( match )
99                return &haystack[i];
100        }
101    }
102
103    return NULL;
104}
105
106/* interruptable select() */
107int select_intr(int n, fd_set *r, fd_set *w, fd_set *e, struct timeval *t)
108{
109    int rv;
110    sigset_t ssn, sso;
111
112    /* unblock SIGCHLD */
113    sigemptyset(&ssn);
114    sigaddset(&ssn, SIGCHLD);
115    sigaddset(&ssn, SIGPIPE);
116    sigprocmask(SIG_UNBLOCK, &ssn, &sso);
117
118    rv = select(n, r, w, e, t);
119
120    /* restore signal mask */
121    sigprocmask(SIG_SETMASK, &sso, NULL);
122
123    return rv;
124}
125
126
127int uh_tcp_send(struct client *cl, const char *buf, int len)
128{
129    fd_set writer;
130    struct timeval timeout;
131
132    FD_ZERO(&writer);
133    FD_SET(cl->socket, &writer);
134
135    timeout.tv_sec = cl->server->conf->network_timeout;
136    timeout.tv_usec = 0;
137
138    if( select(cl->socket + 1, NULL, &writer, NULL, &timeout) > 0 )
139    {
140#ifdef HAVE_TLS
141        if( cl->tls )
142            return cl->server->conf->tls_send(cl, (void *)buf, len);
143        else
144#endif
145            return send(cl->socket, buf, len, 0);
146    }
147
148    return -1;
149}
150
151int uh_tcp_peek(struct client *cl, char *buf, int len)
152{
153    int sz = uh_tcp_recv(cl, buf, len);
154
155    /* store received data in peek buffer */
156    if( sz > 0 )
157    {
158        cl->peeklen = sz;
159        memcpy(cl->peekbuf, buf, sz);
160    }
161
162    return sz;
163}
164
165int uh_tcp_recv(struct client *cl, char *buf, int len)
166{
167    int sz = 0;
168    int rsz = 0;
169
170    /* first serve data from peek buffer */
171    if( cl->peeklen > 0 )
172    {
173        sz = min(cl->peeklen, len);
174        len -= sz; cl->peeklen -= sz;
175
176        memcpy(buf, cl->peekbuf, sz);
177        memmove(cl->peekbuf, &cl->peekbuf[sz], cl->peeklen);
178    }
179
180    /* caller wants more */
181    if( len > 0 )
182    {
183#ifdef HAVE_TLS
184        if( cl->tls )
185            rsz = cl->server->conf->tls_recv(cl, (void *)&buf[sz], len);
186        else
187#endif
188            rsz = recv(cl->socket, (void *)&buf[sz], len, 0);
189
190        if( (sz == 0) || (rsz > 0) )
191            sz += rsz;
192    }
193
194    return sz;
195}
196
197
198int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...)
199{
200    va_list ap;
201
202    char buffer[UH_LIMIT_MSGHEAD];
203    int len;
204
205    len = snprintf(buffer, sizeof(buffer),
206        "HTTP/1.1 %03i %s\r\n"
207        "Connection: close\r\n"
208        "Content-Type: text/plain\r\n"
209        "Transfer-Encoding: chunked\r\n\r\n",
210            code, summary
211    );
212
213    ensure_ret(uh_tcp_send(cl, buffer, len));
214
215    va_start(ap, fmt);
216    len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
217    va_end(ap);
218
219    ensure_ret(uh_http_sendc(cl, buffer, len));
220    ensure_ret(uh_http_sendc(cl, NULL, 0));
221
222    return 0;
223}
224
225
226int uh_http_sendc(struct client *cl, const char *data, int len)
227{
228    char chunk[8];
229    int clen;
230
231    if( len == -1 )
232        len = strlen(data);
233
234    if( len > 0 )
235    {
236         clen = snprintf(chunk, sizeof(chunk), "%X\r\n", len);
237        ensure_ret(uh_tcp_send(cl, chunk, clen));
238        ensure_ret(uh_tcp_send(cl, data, len));
239        ensure_ret(uh_tcp_send(cl, "\r\n", 2));
240    }
241    else
242    {
243        ensure_ret(uh_tcp_send(cl, "0\r\n\r\n", 5));
244    }
245
246    return 0;
247}
248
249int uh_http_sendf(
250    struct client *cl, struct http_request *req, const char *fmt, ...
251) {
252    va_list ap;
253    char buffer[UH_LIMIT_MSGHEAD];
254    int len;
255
256    va_start(ap, fmt);
257    len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
258    va_end(ap);
259
260    if( (req != NULL) && (req->version > 1.0) )
261        ensure_ret(uh_http_sendc(cl, buffer, len));
262    else if( len > 0 )
263        ensure_ret(uh_tcp_send(cl, buffer, len));
264
265    return 0;
266}
267
268int uh_http_send(
269    struct client *cl, struct http_request *req, const char *buf, int len
270) {
271    if( len < 0 )
272        len = strlen(buf);
273
274    if( (req != NULL) && (req->version > 1.0) )
275        ensure_ret(uh_http_sendc(cl, buf, len));
276    else if( len > 0 )
277        ensure_ret(uh_tcp_send(cl, buf, len));
278
279    return 0;
280}
281
282
283int uh_urldecode(char *buf, int blen, const char *src, int slen)
284{
285    int i;
286    int len = 0;
287
288#define hex(x) \
289    (((x) <= '9') ? ((x) - '0') : \
290        (((x) <= 'F') ? ((x) - 'A' + 10) : \
291            ((x) - 'a' + 10)))
292
293    for( i = 0; (i <= slen) && (i <= blen); i++ )
294    {
295        if( src[i] == '%' )
296        {
297            if( ((i+2) <= slen) && isxdigit(src[i+1]) && isxdigit(src[i+2]) )
298            {
299                buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
300                i += 2;
301            }
302            else
303            {
304                buf[len++] = '%';
305            }
306        }
307        else
308        {
309            buf[len++] = src[i];
310        }
311    }
312
313    return len;
314}
315
316int uh_urlencode(char *buf, int blen, const char *src, int slen)
317{
318    int i;
319    int len = 0;
320    const char hex[] = "0123456789abcdef";
321
322    for( i = 0; (i <= slen) && (i <= blen); i++ )
323    {
324        if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
325            (src[i] == '.') || (src[i] == '~') )
326        {
327            buf[len++] = src[i];
328        }
329        else if( (len+3) <= blen )
330        {
331            buf[len++] = '%';
332            buf[len++] = hex[(src[i] >> 4) & 15];
333            buf[len++] = hex[(src[i] & 15) & 15];
334        }
335        else
336        {
337            break;
338        }
339    }
340
341    return len;
342}
343
344int uh_b64decode(char *buf, int blen, const unsigned char *src, int slen)
345{
346    int i = 0;
347    int len = 0;
348
349    unsigned int cin = 0;
350    unsigned int cout = 0;
351
352
353    for( i = 0; (i <= slen) && (src[i] != 0); i++ )
354    {
355        cin = src[i];
356
357        if( (cin >= '0') && (cin <= '9') )
358            cin = cin - '0' + 52;
359        else if( (cin >= 'A') && (cin <= 'Z') )
360            cin = cin - 'A';
361        else if( (cin >= 'a') && (cin <= 'z') )
362            cin = cin - 'a' + 26;
363        else if( cin == '+' )
364            cin = 62;
365        else if( cin == '/' )
366            cin = 63;
367        else if( cin == '=' )
368            cin = 0;
369        else
370            continue;
371
372        cout = (cout << 6) | cin;
373
374        if( (i % 4) == 3 )
375        {
376            if( (len + 3) < blen )
377            {
378                buf[len++] = (char)(cout >> 16);
379                buf[len++] = (char)(cout >> 8);
380                buf[len++] = (char)(cout);
381            }
382            else
383            {
384                break;
385            }
386        }
387    }
388
389    buf[len++] = 0;
390    return len;
391}
392
393static char * canonpath(const char *path, char *path_resolved)
394{
395    char path_copy[PATH_MAX];
396    char *path_cpy = path_copy;
397    char *path_res = path_resolved;
398
399    struct stat s;
400
401
402    /* relative -> absolute */
403    if( *path != '/' )
404    {
405        getcwd(path_copy, PATH_MAX);
406        strncat(path_copy, "/", PATH_MAX - strlen(path_copy));
407        strncat(path_copy, path, PATH_MAX - strlen(path_copy));
408    }
409    else
410    {
411        strncpy(path_copy, path, PATH_MAX);
412    }
413
414    /* normalize */
415    while( (*path_cpy != '\0') && (path_cpy < (path_copy + PATH_MAX - 2)) )
416    {
417        if( *path_cpy == '/' )
418        {
419            /* skip repeating / */
420            if( path_cpy[1] == '/' )
421            {
422                path_cpy++;
423                continue;
424            }
425
426            /* /./ or /../ */
427            else if( path_cpy[1] == '.' )
428            {
429                /* skip /./ */
430                if( (path_cpy[2] == '/') || (path_cpy[2] == '\0') )
431                {
432                    path_cpy += 2;
433                    continue;
434                }
435
436                /* collapse /x/../ */
437                else if( (path_cpy[2] == '.') &&
438                         ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))
439                ) {
440                    while( (path_res > path_resolved) && (*--path_res != '/') )
441                        ;
442
443                    path_cpy += 3;
444                    continue;
445                }
446            }
447        }
448
449        *path_res++ = *path_cpy++;
450    }
451
452    /* remove trailing slash if not root / */
453    if( (path_res > (path_resolved+1)) && (path_res[-1] == '/') )
454        path_res--;
455    else if( path_res == path_resolved )
456        *path_res++ = '/';
457
458    *path_res = '\0';
459
460    /* test access */
461    if( !stat(path_resolved, &s) && (s.st_mode & S_IROTH) )
462        return path_resolved;
463
464    return NULL;
465}
466
467struct path_info * uh_path_lookup(struct client *cl, const char *url)
468{
469    static char path_phys[PATH_MAX];
470    static char path_info[PATH_MAX];
471    static struct path_info p;
472
473    char buffer[UH_LIMIT_MSGHEAD];
474    char *docroot = cl->server->conf->docroot;
475    char *pathptr = NULL;
476
477    int slash = 0;
478    int no_sym = cl->server->conf->no_symlinks;
479    int i = 0;
480    struct stat s;
481
482    /* back out early if url is undefined */
483    if ( url == NULL )
484        return NULL;
485
486    memset(path_phys, 0, sizeof(path_phys));
487    memset(path_info, 0, sizeof(path_info));
488    memset(buffer, 0, sizeof(buffer));
489    memset(&p, 0, sizeof(p));
490
491    /* copy docroot */
492    memcpy(buffer, docroot,
493        min(strlen(docroot), sizeof(buffer) - 1));
494
495    /* separate query string from url */
496    if( (pathptr = strchr(url, '?')) != NULL )
497    {
498        p.query = pathptr[1] ? pathptr + 1 : NULL;
499
500        /* urldecode component w/o query */
501        if( pathptr > url )
502            uh_urldecode(
503                &buffer[strlen(docroot)],
504                sizeof(buffer) - strlen(docroot) - 1,
505                url, (int)(pathptr - url) - 1
506            );
507    }
508
509    /* no query string, decode all of url */
510    else
511    {
512        uh_urldecode(
513            &buffer[strlen(docroot)],
514            sizeof(buffer) - strlen(docroot) - 1,
515            url, strlen(url)
516        );
517    }
518
519    /* create canon path */
520    for( i = strlen(buffer), slash = (buffer[max(0, i-1)] == '/'); i >= 0; i-- )
521    {
522        if( (buffer[i] == 0) || (buffer[i] == '/') )
523        {
524            memset(path_info, 0, sizeof(path_info));
525            memcpy(path_info, buffer, min(i + 1, sizeof(path_info) - 1));
526
527            if( no_sym ? realpath(path_info, path_phys)
528                       : canonpath(path_info, path_phys)
529            ) {
530                memset(path_info, 0, sizeof(path_info));
531                memcpy(path_info, &buffer[i],
532                    min(strlen(buffer) - i, sizeof(path_info) - 1));
533
534                break;
535            }
536        }
537    }
538
539    /* check whether found path is within docroot */
540    if( strncmp(path_phys, docroot, strlen(docroot)) ||
541        ((path_phys[strlen(docroot)] != 0) &&
542         (path_phys[strlen(docroot)] != '/'))
543    ) {
544        return NULL;
545    }
546
547    /* test current path */
548    if( ! stat(path_phys, &p.stat) )
549    {
550        /* is a regular file */
551        if( p.stat.st_mode & S_IFREG )
552        {
553            p.root = docroot;
554            p.phys = path_phys;
555            p.name = &path_phys[strlen(docroot)];
556            p.info = path_info[0] ? path_info : NULL;
557        }
558
559        /* is a directory */
560        else if( (p.stat.st_mode & S_IFDIR) && !strlen(path_info) )
561        {
562            /* ensure trailing slash */
563            if( path_phys[strlen(path_phys)-1] != '/' )
564                path_phys[strlen(path_phys)] = '/';
565
566            /* try to locate index file */
567            memset(buffer, 0, sizeof(buffer));
568            memcpy(buffer, path_phys, sizeof(buffer));
569            pathptr = &buffer[strlen(buffer)];
570
571            /* if requested url resolves to a directory and a trailing slash
572               is missing in the request url, redirect the client to the same
573               url with trailing slash appended */
574            if( !slash )
575            {
576                uh_http_sendf(cl, NULL,
577                    "HTTP/1.1 302 Found\r\n"
578                    "Location: %s%s%s\r\n"
579                    "Connection: close\r\n\r\n",
580                        &path_phys[strlen(docroot)],
581                        p.query ? "?" : "",
582                        p.query ? p.query : ""
583                );
584
585                p.redirected = 1;
586            }
587            else if( cl->server->conf->index_file )
588            {
589                strncat(buffer, cl->server->conf->index_file, sizeof(buffer));
590
591                if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
592                {
593                    memcpy(path_phys, buffer, sizeof(path_phys));
594                    memcpy(&p.stat, &s, sizeof(p.stat));
595                }
596            }
597            else
598            {
599                for( i = 0; i < array_size(uh_index_files); i++ )
600                {
601                    strncat(buffer, uh_index_files[i], sizeof(buffer));
602
603                    if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
604                    {
605                        memcpy(path_phys, buffer, sizeof(path_phys));
606                        memcpy(&p.stat, &s, sizeof(p.stat));
607                        break;
608                    }
609
610                    *pathptr = 0;
611                }
612            }
613
614            p.root = docroot;
615            p.phys = path_phys;
616            p.name = &path_phys[strlen(docroot)];
617        }
618    }
619
620    return p.phys ? &p : NULL;
621}
622
623
624static struct auth_realm *uh_realms = NULL;
625
626struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
627{
628    struct auth_realm *new = NULL;
629    struct passwd *pwd;
630    struct spwd *spwd;
631
632    if((new = (struct auth_realm *)malloc(sizeof(struct auth_realm))) != NULL)
633    {
634        memset(new, 0, sizeof(struct auth_realm));
635
636        memcpy(new->path, path,
637            min(strlen(path), sizeof(new->path) - 1));
638
639        memcpy(new->user, user,
640            min(strlen(user), sizeof(new->user) - 1));
641
642        /* given password refers to a passwd entry */
643        if( (strlen(pass) > 3) && !strncmp(pass, "$p$", 3) )
644        {
645            /* try to resolve shadow entry */
646            if( ((spwd = getspnam(&pass[3])) != NULL) && spwd->sp_pwdp )
647            {
648                memcpy(new->pass, spwd->sp_pwdp,
649                    min(strlen(spwd->sp_pwdp), sizeof(new->pass) - 1));
650            }
651
652            /* try to resolve passwd entry */
653            else if( ((pwd = getpwnam(&pass[3])) != NULL) && pwd->pw_passwd &&
654                (pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0)
655            ) {
656                memcpy(new->pass, pwd->pw_passwd,
657                    min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1));
658            }
659        }
660
661        /* ordinary pwd */
662        else
663        {
664            memcpy(new->pass, pass,
665                min(strlen(pass), sizeof(new->pass) - 1));
666        }
667
668        if( new->pass[0] )
669        {
670            new->next = uh_realms;
671            uh_realms = new;
672
673            return new;
674        }
675
676        free(new);
677    }
678
679    return NULL;
680}
681
682int uh_auth_check(
683    struct client *cl, struct http_request *req, struct path_info *pi
684) {
685    int i, plen, rlen, protected;
686    char buffer[UH_LIMIT_MSGHEAD];
687    char *user = NULL;
688    char *pass = NULL;
689
690    struct auth_realm *realm = NULL;
691
692    plen = strlen(pi->name);
693    protected = 0;
694
695    /* check whether at least one realm covers the requested url */
696    for( realm = uh_realms; realm; realm = realm->next )
697    {
698        rlen = strlen(realm->path);
699
700        if( (plen >= rlen) && !strncasecmp(pi->name, realm->path, rlen) )
701        {
702            req->realm = realm;
703            protected = 1;
704            break;
705        }
706    }
707
708    /* requested resource is covered by a realm */
709    if( protected )
710    {
711        /* try to get client auth info */
712        foreach_header(i, req->headers)
713        {
714            if( !strcasecmp(req->headers[i], "Authorization") &&
715                (strlen(req->headers[i+1]) > 6) &&
716                !strncasecmp(req->headers[i+1], "Basic ", 6)
717            ) {
718                memset(buffer, 0, sizeof(buffer));
719                uh_b64decode(buffer, sizeof(buffer) - 1,
720                    (unsigned char *) &req->headers[i+1][6],
721                    strlen(req->headers[i+1]) - 6);
722
723                if( (pass = strchr(buffer, ':')) != NULL )
724                {
725                    user = buffer;
726                    *pass++ = 0;
727                }
728
729                break;
730            }
731        }
732
733        /* have client auth */
734        if( user && pass )
735        {
736            /* find matching realm */
737            for( realm = uh_realms; realm; realm = realm->next )
738            {
739                rlen = strlen(realm->path);
740
741                if( (plen >= rlen) &&
742                    !strncasecmp(pi->name, realm->path, rlen) &&
743                    !strcmp(user, realm->user)
744                ) {
745                    req->realm = realm;
746                    break;
747                }
748            }
749
750            /* found a realm matching the username */
751            if( realm )
752            {
753                /* is a crypt passwd */
754                if( realm->pass[0] == '$' )
755                    pass = crypt(pass, realm->pass);
756
757                /* check user pass */
758                if( !strcmp(pass, realm->pass) )
759                    return 1;
760            }
761        }
762
763        /* 401 */
764        uh_http_sendf(cl, NULL,
765            "HTTP/%.1f 401 Authorization Required\r\n"
766            "WWW-Authenticate: Basic realm=\"%s\"\r\n"
767            "Content-Type: text/plain\r\n"
768            "Content-Length: 23\r\n\r\n"
769            "Authorization Required\n",
770                req->version, cl->server->conf->realm
771        );
772
773        return 0;
774    }
775
776    return 1;
777}
778
779
780static struct listener *uh_listeners = NULL;
781static struct client *uh_clients = NULL;
782
783struct listener * uh_listener_add(int sock, struct config *conf)
784{
785    struct listener *new = NULL;
786    socklen_t sl;
787
788    if( (new = (struct listener *)malloc(sizeof(struct listener))) != NULL )
789    {
790        memset(new, 0, sizeof(struct listener));
791
792        new->socket = sock;
793        new->conf = conf;
794
795        /* get local endpoint addr */
796        sl = sizeof(struct sockaddr_in6);
797        memset(&(new->addr), 0, sl);
798        getsockname(sock, (struct sockaddr *) &(new->addr), &sl);
799
800        new->next = uh_listeners;
801        uh_listeners = new;
802
803        return new;
804    }
805
806    return NULL;
807}
808
809struct listener * uh_listener_lookup(int sock)
810{
811    struct listener *cur = NULL;
812
813    for( cur = uh_listeners; cur; cur = cur->next )
814        if( cur->socket == sock )
815            return cur;
816
817    return NULL;
818}
819
820
821struct client * uh_client_add(int sock, struct listener *serv)
822{
823    struct client *new = NULL;
824    socklen_t sl;
825
826    if( (new = (struct client *)malloc(sizeof(struct client))) != NULL )
827    {
828        memset(new, 0, sizeof(struct client));
829
830        new->socket = sock;
831        new->server = serv;
832
833        /* get remote endpoint addr */
834        sl = sizeof(struct sockaddr_in6);
835        memset(&(new->peeraddr), 0, sl);
836        getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);
837
838        /* get local endpoint addr */
839        sl = sizeof(struct sockaddr_in6);
840        memset(&(new->servaddr), 0, sl);
841        getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl);
842
843        new->next = uh_clients;
844        uh_clients = new;
845    }
846
847    return new;
848}
849
850struct client * uh_client_lookup(int sock)
851{
852    struct client *cur = NULL;
853
854    for( cur = uh_clients; cur; cur = cur->next )
855        if( cur->socket == sock )
856            return cur;
857
858    return NULL;
859}
860
861void uh_client_remove(int sock)
862{
863    struct client *cur = NULL;
864    struct client *prv = NULL;
865
866    for( cur = uh_clients; cur; prv = cur, cur = cur->next )
867    {
868        if( cur->socket == sock )
869        {
870            if( prv )
871                prv->next = cur->next;
872            else
873                uh_clients = cur->next;
874
875            free(cur);
876            break;
877        }
878    }
879}
880
881
882#ifdef HAVE_CGI
883static struct interpreter *uh_interpreters = NULL;
884
885struct interpreter * uh_interpreter_add(const char *extn, const char *path)
886{
887    struct interpreter *new = NULL;
888
889    if( (new = (struct interpreter *)
890            malloc(sizeof(struct interpreter))) != NULL )
891    {
892        memset(new, 0, sizeof(struct interpreter));
893
894        memcpy(new->extn, extn, min(strlen(extn), sizeof(new->extn)-1));
895        memcpy(new->path, path, min(strlen(path), sizeof(new->path)-1));
896
897        new->next = uh_interpreters;
898        uh_interpreters = new;
899
900        return new;
901    }
902
903    return NULL;
904}
905
906struct interpreter * uh_interpreter_lookup(const char *path)
907{
908    struct interpreter *cur = NULL;
909    const char *e;
910
911    for( cur = uh_interpreters; cur; cur = cur->next )
912    {
913        e = &path[max(strlen(path) - strlen(cur->extn), 0)];
914
915        if( !strcmp(e, cur->extn) )
916            return cur;
917    }
918
919    return NULL;
920}
921#endif
922

Archive Download this file



interactive