Changeset 5895

Show
Ignore:
Timestamp:
03/21/10 03:27:29 (3 years ago)
Author:
jow
Message:

uhttpd: terminate I/O loops if socket writes fail

Location:
luci/trunk/contrib/package/uhttpd/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/contrib/package/uhttpd/src/uhttpd-cgi.c

    r5894 r5895  
    119119} 
    120120 
    121 static void uh_cgi_error_500(struct client *cl, struct http_request *req, const char *message) 
     121static int uh_cgi_error_500(struct client *cl, struct http_request *req, const char *message) 
    122122{ 
    123     uh_http_sendf(cl, NULL, 
     123    if( uh_http_sendf(cl, NULL, 
    124124        "HTTP/%.1f 500 Internal Server Error\r\n" 
    125125        "Content-Type: text/plain\r\n%s\r\n", 
     
    127127            (req->version > 1.0) 
    128128                ? "Transfer-Encoding: chunked\r\n" : "" 
    129     ); 
    130  
    131     uh_http_send(cl, req, message, -1); 
     129        ) >= 0 
     130    ) { 
     131        return uh_http_send(cl, req, message, -1); 
     132    } 
     133 
     134    return -1; 
    132135} 
    133136 
     
    348351 
    349352            memset(hdr, 0, sizeof(hdr)); 
     353 
     354#define ensure(x) \ 
     355    do { if( x < 0 ) goto out; } while(0) 
    350356 
    351357            /* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */ 
     
    391397 
    392398                        /* there is no more post data, close pipe to child's stdin */ 
    393                         else 
     399                        else if( content_length > -1 ) 
    394400                        { 
    395401                            close(wfd[1]); 
     
    424430                                { 
    425431                                    /* write status */ 
    426                                     uh_http_sendf(cl, NULL, "HTTP/%.1f %03d %s\r\n", 
    427                                         req->version, res->statuscode, res->statusmsg); 
     432                                    ensure(uh_http_sendf(cl, NULL, 
     433                                        "HTTP/%.1f %03d %s\r\n", 
     434                                        req->version, res->statuscode, 
     435                                        res->statusmsg)); 
    428436 
    429437                                    /* add Content-Type if no Location or Content-Type */ 
     
    431439                                        !uh_cgi_header_lookup(res, "Content-Type") 
    432440                                    ) { 
    433                                         uh_http_send(cl, NULL, 
    434                                             "Content-Type: text/plain\r\n", -1); 
     441                                        ensure(uh_http_send(cl, NULL, 
     442                                            "Content-Type: text/plain\r\n", -1)); 
    435443                                    } 
    436444 
     
    439447                                        !uh_cgi_header_lookup(res, "Transfer-Encoding") 
    440448                                    ) { 
    441                                         uh_http_send(cl, NULL, 
    442                                             "Transfer-Encoding: chunked\r\n", -1); 
     449                                        ensure(uh_http_send(cl, NULL, 
     450                                            "Transfer-Encoding: chunked\r\n", -1)); 
    443451                                    } 
    444452 
     
    446454                                    foreach_header(i, res->headers) 
    447455                                    { 
    448                                         uh_http_sendf(cl, NULL, "%s: %s\r\n", 
    449                                             res->headers[i], res->headers[i+1]); 
     456                                        ensure(uh_http_sendf(cl, NULL, "%s: %s\r\n", 
     457                                            res->headers[i], res->headers[i+1])); 
    450458                                    } 
    451459 
    452460                                    /* terminate header */ 
    453                                     uh_http_send(cl, NULL, "\r\n", -1); 
     461                                    ensure(uh_http_send(cl, NULL, "\r\n", -1)); 
    454462 
    455463                                    /* push out remaining head buffer */ 
    456464                                    if( hdroff < hdrlen ) 
    457                                         uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff); 
     465                                        ensure(uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff)); 
    458466                                } 
    459467 
     
    461469                                else if( hdrlen >= sizeof(hdr) ) 
    462470                                { 
    463                                     uh_cgi_error_500(cl, req, 
    464                                         "The CGI program generated an invalid response:\n\n"); 
    465  
    466                                     uh_http_send(cl, req, hdr, hdrlen); 
     471                                    ensure(uh_cgi_error_500(cl, req, 
     472                                        "The CGI program generated an invalid response:\n\n")); 
     473 
     474                                    ensure(uh_http_send(cl, req, hdr, hdrlen)); 
    467475                                } 
    468476 
     
    475483                                /* push out remaining read buffer */ 
    476484                                if( bufoff < buflen ) 
    477                                     uh_http_send(cl, req, &buf[bufoff], buflen - bufoff); 
     485                                    ensure(uh_http_send(cl, req, &buf[bufoff], buflen - bufoff)); 
    478486 
    479487                                header_sent = 1; 
     
    483491 
    484492                            /* headers complete, pass through buffer to socket */ 
    485                             uh_http_send(cl, req, buf, buflen); 
     493                            ensure(uh_http_send(cl, req, buf, buflen)); 
    486494                        } 
    487495 
     
    503511                                 */ 
    504512 
    505                                 uh_http_sendf(cl, NULL, 
     513                                ensure(uh_http_sendf(cl, NULL, 
    506514                                    "HTTP/%.1f 200 OK\r\n" 
    507515                                    "Content-Type: text/plain\r\n" 
     
    509517                                        req->version, (req->version > 1.0) 
    510518                                            ? "Transfer-Encoding: chunked\r\n" : "" 
    511                                 ); 
    512  
    513                                 uh_http_send(cl, req, hdr, hdrlen); 
     519                                )); 
     520 
     521                                ensure(uh_http_send(cl, req, hdr, hdrlen)); 
    514522                            } 
    515523 
    516524                            /* send final chunk if we're in chunked transfer mode */ 
    517                             uh_http_send(cl, req, "", 0); 
     525                            ensure(uh_http_send(cl, req, "", 0)); 
    518526                            break; 
    519527                        } 
     
    524532                else 
    525533                { 
    526                     uh_http_sendhf(cl, 504, "Gateway Timeout", 
    527                         "The CGI script took too long to produce a response"); 
     534                    ensure(uh_http_sendhf(cl, 504, "Gateway Timeout", 
     535                        "The CGI script took too long to produce a response")); 
    528536 
    529537                    break; 
     
    531539            } 
    532540 
     541        out: 
    533542            close(rfd[0]); 
    534543            close(wfd[1]); 
  • luci/trunk/contrib/package/uhttpd/src/uhttpd-file.c

    r5892 r5895  
    350350                while( (rlen = read(fd, buf, sizeof(buf))) > 0 ) 
    351351                { 
    352                     uh_http_send(cl, req, buf, rlen); 
     352                    if( uh_http_send(cl, req, buf, rlen) < 0 ) 
     353                        break; 
    353354                } 
    354355