Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (7 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/networking/httpd.c

    r3232 r3621  
    126126
    127127#include "libbb.h"
     128#include "common_bufsiz.h"
    128129#if ENABLE_PAM
    129130/* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
     
    134135# include <security/pam_misc.h>
    135136#endif
    136 #if ENABLE_FEATURE_HTTPD_USE_SENDFILE
     137#if ENABLE_FEATURE_USE_SENDFILE
    137138# include <sys/sendfile.h>
    138139#endif
     
    308309#endif
    309310    char *iobuf;            /* [IOBUF_SIZE] */
    310 #define hdr_buf bb_common_bufsiz1
     311#define        hdr_buf bb_common_bufsiz1
     312#define sizeof_hdr_buf COMMON_BUFSIZE
    311313    char *hdr_ptr;
    312314    int hdr_cnt;
     
    369371#endif
    370372#define INIT_G() do { \
     373    setup_common_bufsiz(); \
    371374    SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
    372375    IF_FEATURE_HTTPD_BASIC_AUTH(g_realm = "Web Server Authentication";) \
     
    698701            }
    699702            *host_port++ = '\0';
    700             if (strncmp(host_port, "http://", 7) == 0)
     703            if (is_prefixed_with(host_port, "http://"))
    701704                host_port += 7;
    702705            if (*host_port == '\0') {
     
    968971#endif
    969972    if (responseNum == HTTP_MOVED_TEMPORARILY) {
    970         len += sprintf(iobuf + len, "Location: %s/%s%s\r\n",
     973        /* Responding to "GET /dir" with
     974         * "HTTP/1.0 302 Found" "Location: /dir/"
     975         * - IOW, asking them to repeat with a slash.
     976         * Here, overflow IS possible, can't use sprintf:
     977         * mkdir test
     978         * python -c 'print("get /test?" + ("x" * 8192))' | busybox httpd -i -h .
     979         */
     980        len += snprintf(iobuf + len, IOBUF_SIZE-3 - len,
     981                "Location: %s/%s%s\r\n",
    971982                found_moved_temporarily,
    972983                (g_query ? "?" : ""),
    973984                (g_query ? g_query : ""));
     985        if (len > IOBUF_SIZE-3)
     986            len = IOBUF_SIZE-3;
    974987    }
    975988
    976989#if ENABLE_FEATURE_HTTPD_ERROR_PAGES
    977990    if (error_page && access(error_page, R_OK) == 0) {
    978         strcat(iobuf, "\r\n");
    979         len += 2;
    980 
    981         if (DEBUG)
     991        iobuf[len++] = '\r';
     992        iobuf[len++] = '\n';
     993        if (DEBUG) {
     994            iobuf[len] = '\0';
    982995            fprintf(stderr, "headers: '%s'\n", iobuf);
     996        }
    983997        full_write(STDOUT_FILENO, iobuf, len);
    984998        if (DEBUG)
     
    10221036                responseNum, responseString, infoString);
    10231037    }
    1024     if (DEBUG)
     1038    if (DEBUG) {
     1039        iobuf[len] = '\0';
    10251040        fprintf(stderr, "headers: '%s'\n", iobuf);
     1041    }
    10261042    if (full_write(STDOUT_FILENO, iobuf, len) != len) {
    10271043        if (verbose > 1)
     
    10541070    while (1) {
    10551071        if (hdr_cnt <= 0) {
    1056             hdr_cnt = safe_read(STDIN_FILENO, hdr_buf, sizeof(hdr_buf));
     1072            hdr_cnt = safe_read(STDIN_FILENO, hdr_buf, sizeof_hdr_buf);
    10571073            if (hdr_cnt <= 0)
    10581074                break;
     
    11051121    /* NB: breaking out of this loop jumps to log_and_exit() */
    11061122    out_cnt = 0;
     1123    pfd[FROM_CGI].fd = fromCgi_rd;
     1124    pfd[FROM_CGI].events = POLLIN;
     1125    pfd[TO_CGI].fd = toCgi_wr;
    11071126    while (1) {
    1108         memset(pfd, 0, sizeof(pfd));
    1109 
    1110         pfd[FROM_CGI].fd = fromCgi_rd;
    1111         pfd[FROM_CGI].events = POLLIN;
    1112 
    1113         if (toCgi_wr) {
    1114             pfd[TO_CGI].fd = toCgi_wr;
    1115             if (hdr_cnt > 0) {
    1116                 pfd[TO_CGI].events = POLLOUT;
    1117             } else if (post_len > 0) {
    1118                 pfd[0].events = POLLIN;
     1127        /* Note: even pfd[0].events == 0 won't prevent
     1128         * revents == POLLHUP|POLLERR reports from closed stdin.
     1129         * Setting fd to -1 works: */
     1130        pfd[0].fd = -1;
     1131        pfd[0].events = POLLIN;
     1132        pfd[0].revents = 0; /* probably not needed, paranoia */
     1133
     1134        /* We always poll this fd, thus kernel always sets revents: */
     1135        /*pfd[FROM_CGI].events = POLLIN; - moved out of loop */
     1136        /*pfd[FROM_CGI].revents = 0; - not needed */
     1137
     1138        /* gcc-4.8.0 still doesnt fill two shorts with one insn :( */
     1139        /* http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47059 */
     1140        /* hopefully one day it will... */
     1141        pfd[TO_CGI].events = POLLOUT;
     1142        pfd[TO_CGI].revents = 0; /* needed! */
     1143
     1144        if (toCgi_wr && hdr_cnt <= 0) {
     1145            if (post_len > 0) {
     1146                /* Expect more POST data from network */
     1147                pfd[0].fd = 0;
    11191148            } else {
    11201149                /* post_len <= 0 && hdr_cnt <= 0:
     
    11281157
    11291158        /* Now wait on the set of sockets */
    1130         count = safe_poll(pfd, toCgi_wr ? TO_CGI+1 : FROM_CGI+1, -1);
     1159        count = safe_poll(pfd, hdr_cnt > 0 ? TO_CGI+1 : FROM_CGI+1, -1);
    11311160        if (count <= 0) {
    11321161#if 0
     
    11451174
    11461175        if (pfd[TO_CGI].revents) {
    1147             /* hdr_cnt > 0 here due to the way pfd[TO_CGI].events set */
     1176            /* hdr_cnt > 0 here due to the way poll() called */
    11481177            /* Have data from peer and can write to CGI */
    11491178            count = safe_write(toCgi_wr, hdr_ptr, hdr_cnt);
     
    11661195             * and there *is* data to read from the peer
    11671196             * (POSTDATA) */
    1168             //count = post_len > (int)sizeof(hdr_buf) ? (int)sizeof(hdr_buf) : post_len;
     1197            //count = post_len > (int)sizeof_hdr_buf ? (int)sizeof_hdr_buf : post_len;
    11691198            //count = safe_read(STDIN_FILENO, hdr_buf, count);
    1170             count = safe_read(STDIN_FILENO, hdr_buf, sizeof(hdr_buf));
     1199            count = safe_read(STDIN_FILENO, hdr_buf, sizeof_hdr_buf);
    11711200            if (count > 0) {
    11721201                hdr_cnt = count;
     
    12101239                count = 0;
    12111240                /* "Status" header format is: "Status: 302 Redirected\r\n" */
    1212                 if (out_cnt >= 8 && memcmp(rbuf, "Status: ", 8) == 0) {
     1241                if (out_cnt >= 7 && memcmp(rbuf, "Status:", 7) == 0) {
    12131242                    /* send "HTTP/1.0 " */
    12141243                    if (full_write(STDOUT_FILENO, HTTP_200, 9) != 9)
    12151244                        break;
    1216                     rbuf += 8; /* skip "Status: " */
    1217                     count = out_cnt - 8;
     1245                    rbuf += 7; /* skip "Status:" */
     1246                    count = out_cnt - 7;
    12181247                    out_cnt = -1; /* buffering off */
    12191248                } else if (out_cnt >= 4) {
     
    16121641    if (what & SEND_HEADERS)
    16131642        send_headers(HTTP_OK);
    1614 #if ENABLE_FEATURE_HTTPD_USE_SENDFILE
     1643#if ENABLE_FEATURE_USE_SENDFILE
    16151644    {
    16161645        off_t offset = range_start;
     
    16421671    }
    16431672    if (count < 0) {
    1644  IF_FEATURE_HTTPD_USE_SENDFILE(fin:)
     1673 IF_FEATURE_USE_SENDFILE(fin:)
    16451674        if (verbose > 1)
    16461675            bb_perror_msg("error");
     
    17101739            break;
    17111740        case PAM_ERROR_MSG:
    1712             case PAM_TEXT_INFO:
    1713                 s = "";
     1741        case PAM_TEXT_INFO:
     1742            s = "";
    17141743            break;
    17151744        default:
     
    18821911    Htaccess_Proxy *p;
    18831912    for (p = proxy; p; p = p->next) {
    1884         if (strncmp(url, p->url_from, strlen(p->url_from)) == 0)
     1913        if (is_prefixed_with(url, p->url_from))
    18851914            return p;
    18861915    }
     
    19651994
    19661995    /* Determine type of request (GET/POST) */
    1967     urlp = strpbrk(iobuf, " \t");
     1996    // rfc2616: method and URI is separated by exactly one space
     1997    //urlp = strpbrk(iobuf, " \t"); - no, tab isn't allowed
     1998    urlp = strchr(iobuf, ' ');
    19681999    if (urlp == NULL)
    19692000        send_headers_and_exit(HTTP_BAD_REQUEST);
     
    19832014        send_headers_and_exit(HTTP_NOT_IMPLEMENTED);
    19842015#endif
    1985     urlp = skip_whitespace(urlp);
     2016    // rfc2616: method and URI is separated by exactly one space
     2017    //urlp = skip_whitespace(urlp); - should not be necessary
    19862018    if (urlp[0] != '/')
    19872019        send_headers_and_exit(HTTP_BAD_REQUEST);
     
    21682200                /* We know only bytes=NNN-[MMM] */
    21692201                char *s = skip_whitespace(iobuf + sizeof("Range:")-1);
    2170                 if (strncmp(s, "bytes=", 6) == 0) {
     2202                if (is_prefixed_with(s, "bytes=") == 0) {
    21712203                    s += sizeof("bytes=")-1;
    21722204                    range_start = BB_STRTOOFF(s, &s, 10);
     
    22542286
    22552287#if ENABLE_FEATURE_HTTPD_CGI
    2256     if (strncmp(tptr, "cgi-bin/", 8) == 0) {
     2288    if (is_prefixed_with(tptr, "cgi-bin/")) {
    22572289        if (tptr[8] == '\0') {
    22582290            /* protect listing "cgi-bin/" */
     
    23372369
    23382370        /* set the KEEPALIVE option to cull dead connections */
    2339         setsockopt(n, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
     2371        setsockopt_keepalive(n);
    23402372
    23412373        if (fork() == 0) {
     
    23802412
    23812413        /* set the KEEPALIVE option to cull dead connections */
    2382         setsockopt(n, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1));
     2414        setsockopt_keepalive(n);
    23832415
    23842416        if (vfork() == 0) {
Note: See TracChangeset for help on using the changeset viewer.