| Date: | 2010-08-18 02:04:52 (2 years 9 months ago) |
|---|---|
| Author: | jow |
| Commit: | 8618e98eabec2b5677698daa631f195cae92c30b |
| Message: | [package] uhttpd: - fix parsing of interpreter entries in the config file, fixes serving of static files as .cgi with X-Wrt - better cope with connection aborts, especially during header transfer - fix return value checking of TLS reads and writes, solves some blocking issues git-svn-id: svn://svn.openwrt.org/openwrt/trunk@22692 3c298f89-4303-0410-b956-a3cf2f4a3e73 |
| Files: |
package/uhttpd/Makefile (1 diff) package/uhttpd/src/uhttpd-cgi.c (12 diffs) package/uhttpd/src/uhttpd-file.c (6 diffs) package/uhttpd/src/uhttpd-tls.c (1 diff) package/uhttpd/src/uhttpd-utils.c (7 diffs) package/uhttpd/src/uhttpd-utils.h (1 diff) package/uhttpd/src/uhttpd.c (5 diffs) package/uhttpd/src/uhttpd.h (2 diffs) |
Change Details
| package/uhttpd/Makefile | ||
|---|---|---|
| 8 | 8 | include $(TOPDIR)/rules.mk |
| 9 | 9 | |
| 10 | 10 | PKG_NAME:=uhttpd |
| 11 | PKG_RELEASE:=15 | |
| 11 | PKG_RELEASE:=16 | |
| 12 | 12 | |
| 13 | 13 | PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) |
| 14 | 14 | PKG_BUILD_DEPENDS := libcyassl liblua |
| package/uhttpd/src/uhttpd-cgi.c | ||
|---|---|---|
| 139 | 139 | struct client *cl, struct http_request *req, |
| 140 | 140 | struct path_info *pi, struct interpreter *ip |
| 141 | 141 | ) { |
| 142 | int i, hdroff, bufoff; | |
| 142 | int i, hdroff, bufoff, rv; | |
| 143 | 143 | int hdrlen = 0; |
| 144 | 144 | int buflen = 0; |
| 145 | 145 | int fd_max = 0; |
| ... | ... | |
| 376 | 376 | |
| 377 | 377 | memset(hdr, 0, sizeof(hdr)); |
| 378 | 378 | |
| 379 | timeout.tv_sec = cl->server->conf->script_timeout; | |
| 380 | timeout.tv_usec = 0; | |
| 381 | ||
| 382 | #define ensure(x) \ | |
| 383 | do { if( x < 0 ) goto out; } while(0) | |
| 384 | ||
| 385 | 379 | /* I/O loop, watch our pipe ends and dispatch child reads/writes from/to socket */ |
| 386 | 380 | while( 1 ) |
| 387 | 381 | { |
| ... | ... | |
| 391 | 385 | FD_SET(rfd[0], &reader); |
| 392 | 386 | FD_SET(wfd[1], &writer); |
| 393 | 387 | |
| 388 | timeout.tv_sec = (header_sent < 1) ? cl->server->conf->script_timeout : 3; | |
| 389 | timeout.tv_usec = 0; | |
| 390 | ||
| 391 | ensure_out(rv = select_intr(fd_max, &reader, | |
| 392 | (content_length > -1) ? &writer : NULL, NULL, &timeout)); | |
| 393 | ||
| 394 | /* timeout */ | |
| 395 | if( rv == 0 ) | |
| 396 | { | |
| 397 | ensure_out(kill(child, 0)); | |
| 398 | } | |
| 399 | ||
| 394 | 400 | /* wait until we can read or write or both */ |
| 395 | if( select_intr(fd_max, &reader, | |
| 396 | (content_length > -1) ? &writer : NULL, NULL, | |
| 397 | (header_sent < 1) ? &timeout : NULL) > 0 | |
| 398 | ) { | |
| 401 | else if( rv > 0 ) | |
| 402 | { | |
| 399 | 403 | /* ready to write to cgi program */ |
| 400 | 404 | if( FD_ISSET(wfd[1], &writer) ) |
| 401 | 405 | { |
| ... | ... | |
| 403 | 407 | if( content_length > 0 ) |
| 404 | 408 | { |
| 405 | 409 | /* read it from socket ... */ |
| 406 | if( (buflen = uh_tcp_recv(cl, buf, min(content_length, sizeof(buf)))) > 0 ) | |
| 410 | ensure_out(buflen = uh_tcp_recv(cl, buf, | |
| 411 | min(content_length, sizeof(buf)))); | |
| 412 | ||
| 413 | if( buflen > 0 ) | |
| 407 | 414 | { |
| 408 | 415 | /* ... and write it to child's stdin */ |
| 409 | 416 | if( write(wfd[1], buf, buflen) < 0 ) |
| ... | ... | |
| 456 | 463 | if( (res = uh_cgi_header_parse(hdr, hdrlen, &hdroff)) != NULL ) |
| 457 | 464 | { |
| 458 | 465 | /* write status */ |
| 459 | ensure(uh_http_sendf(cl, NULL, | |
| 466 | ensure_out(uh_http_sendf(cl, NULL, | |
| 460 | 467 | "HTTP/%.1f %03d %s\r\n" |
| 461 | 468 | "Connection: close\r\n", |
| 462 | 469 | req->version, res->statuscode, |
| ... | ... | |
| 466 | 473 | if( !uh_cgi_header_lookup(res, "Location") && |
| 467 | 474 | !uh_cgi_header_lookup(res, "Content-Type") |
| 468 | 475 | ) { |
| 469 | ensure(uh_http_send(cl, NULL, | |
| 476 | ensure_out(uh_http_send(cl, NULL, | |
| 470 | 477 | "Content-Type: text/plain\r\n", -1)); |
| 471 | 478 | } |
| 472 | 479 | |
| ... | ... | |
| 474 | 481 | if( (req->version > 1.0) && |
| 475 | 482 | !uh_cgi_header_lookup(res, "Transfer-Encoding") |
| 476 | 483 | ) { |
| 477 | ensure(uh_http_send(cl, NULL, | |
| 484 | ensure_out(uh_http_send(cl, NULL, | |
| 478 | 485 | "Transfer-Encoding: chunked\r\n", -1)); |
| 479 | 486 | } |
| 480 | 487 | |
| 481 | 488 | /* write headers from CGI program */ |
| 482 | 489 | foreach_header(i, res->headers) |
| 483 | 490 | { |
| 484 | ensure(uh_http_sendf(cl, NULL, "%s: %s\r\n", | |
| 491 | ensure_out(uh_http_sendf(cl, NULL, "%s: %s\r\n", | |
| 485 | 492 | res->headers[i], res->headers[i+1])); |
| 486 | 493 | } |
| 487 | 494 | |
| 488 | 495 | /* terminate header */ |
| 489 | ensure(uh_http_send(cl, NULL, "\r\n", -1)); | |
| 496 | ensure_out(uh_http_send(cl, NULL, "\r\n", -1)); | |
| 490 | 497 | |
| 491 | 498 | /* push out remaining head buffer */ |
| 492 | 499 | if( hdroff < hdrlen ) |
| 493 | ensure(uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff)); | |
| 500 | ensure_out(uh_http_send(cl, req, &hdr[hdroff], hdrlen - hdroff)); | |
| 494 | 501 | } |
| 495 | 502 | |
| 496 | 503 | /* ... failed and head buffer exceeded */ |
| 497 | 504 | else if( hdrlen >= sizeof(hdr) ) |
| 498 | 505 | { |
| 499 | ensure(uh_cgi_error_500(cl, req, | |
| 506 | ensure_out(uh_cgi_error_500(cl, req, | |
| 500 | 507 | "The CGI program generated an invalid response:\n\n")); |
| 501 | 508 | |
| 502 | ensure(uh_http_send(cl, req, hdr, hdrlen)); | |
| 509 | ensure_out(uh_http_send(cl, req, hdr, hdrlen)); | |
| 503 | 510 | } |
| 504 | 511 | |
| 505 | 512 | /* ... failed but free buffer space, try again */ |
| ... | ... | |
| 510 | 517 | |
| 511 | 518 | /* push out remaining read buffer */ |
| 512 | 519 | if( bufoff < buflen ) |
| 513 | ensure(uh_http_send(cl, req, &buf[bufoff], buflen - bufoff)); | |
| 520 | ensure_out(uh_http_send(cl, req, &buf[bufoff], buflen - bufoff)); | |
| 514 | 521 | |
| 515 | 522 | header_sent = 1; |
| 516 | 523 | continue; |
| ... | ... | |
| 518 | 525 | |
| 519 | 526 | |
| 520 | 527 | /* headers complete, pass through buffer to socket */ |
| 521 | ensure(uh_http_send(cl, req, buf, buflen)); | |
| 528 | ensure_out(uh_http_send(cl, req, buf, buflen)); | |
| 522 | 529 | } |
| 523 | 530 | |
| 524 | 531 | /* looks like eof from child */ |
| ... | ... | |
| 538 | 545 | * build the required headers here. |
| 539 | 546 | */ |
| 540 | 547 | |
| 541 | ensure(uh_http_sendf(cl, NULL, | |
| 548 | ensure_out(uh_http_sendf(cl, NULL, | |
| 542 | 549 | "HTTP/%.1f 200 OK\r\n" |
| 543 | 550 | "Content-Type: text/plain\r\n" |
| 544 | 551 | "%s\r\n", |
| ... | ... | |
| 546 | 553 | ? "Transfer-Encoding: chunked\r\n" : "" |
| 547 | 554 | )); |
| 548 | 555 | |
| 549 | ensure(uh_http_send(cl, req, hdr, hdrlen)); | |
| 556 | ensure_out(uh_http_send(cl, req, hdr, hdrlen)); | |
| 550 | 557 | } |
| 551 | 558 | |
| 552 | 559 | /* send final chunk if we're in chunked transfer mode */ |
| 553 | ensure(uh_http_send(cl, req, "", 0)); | |
| 560 | ensure_out(uh_http_send(cl, req, "", 0)); | |
| 554 | 561 | break; |
| 555 | 562 | } |
| 556 | 563 | } |
| ... | ... | |
| 561 | 568 | { |
| 562 | 569 | if( (errno != EINTR) && ! header_sent ) |
| 563 | 570 | { |
| 564 | ensure(uh_http_sendhf(cl, 504, "Gateway Timeout", | |
| 571 | ensure_out(uh_http_sendhf(cl, 504, "Gateway Timeout", | |
| 565 | 572 | "The CGI script took too long to produce " |
| 566 | 573 | "a response")); |
| 567 | 574 | } |
| 568 | 575 | |
| 569 | 576 | /* send final chunk if we're in chunked transfer mode */ |
| 570 | ensure(uh_http_send(cl, req, "", 0)); | |
| 577 | ensure_out(uh_http_send(cl, req, "", 0)); | |
| 571 | 578 | |
| 572 | 579 | break; |
| 573 | 580 | } |
| package/uhttpd/src/uhttpd-file.c | ||
|---|---|---|
| 97 | 97 | return NULL; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | #define ensure_ret(x) \ | |
| 101 | do { if( x < 0 ) return -1; } while(0) | |
| 102 | 100 | |
| 103 | 101 | static int uh_file_response_ok_hdrs(struct client *cl, struct http_request *req, struct stat *s) |
| 104 | 102 | { |
| ... | ... | |
| 132 | 130 | "Connection: close\r\n", req->version); |
| 133 | 131 | } |
| 134 | 132 | |
| 135 | static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s) | |
| 133 | static int uh_file_if_match(struct client *cl, struct http_request *req, struct stat *s, int *ok) | |
| 136 | 134 | { |
| 137 | 135 | const char *tag = uh_file_mktag(s); |
| 138 | 136 | char *hdr = uh_file_header_lookup(req, "If-Match"); |
| ... | ... | |
| 152 | 150 | } |
| 153 | 151 | else if( !strcmp(p, "*") || !strcmp(p, tag) ) |
| 154 | 152 | { |
| 155 | return 1; | |
| 153 | *ok = 1; | |
| 154 | return *ok; | |
| 156 | 155 | } |
| 157 | 156 | } |
| 158 | 157 | |
| 159 | uh_file_response_412(cl, req); | |
| 160 | return 0; | |
| 158 | *ok = 0; | |
| 159 | ensure_ret(uh_file_response_412(cl, req)); | |
| 160 | return *ok; | |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | return 1; | |
| 163 | *ok = 1; | |
| 164 | return *ok; | |
| 164 | 165 | } |
| 165 | 166 | |
| 166 | static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s) | |
| 167 | static int uh_file_if_modified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok) | |
| 167 | 168 | { |
| 168 | 169 | char *hdr = uh_file_header_lookup(req, "If-Modified-Since"); |
| 170 | *ok = 1; | |
| 169 | 171 | |
| 170 | 172 | if( hdr ) |
| 171 | 173 | { |
| 172 | if( uh_file_date2unix(hdr) < s->st_mtime ) | |
| 174 | if( uh_file_date2unix(hdr) >= s->st_mtime ) | |
| 173 | 175 | { |
| 174 | return 1; | |
| 175 | } | |
| 176 | else | |
| 177 | { | |
| 178 | uh_file_response_304(cl, req, s); | |
| 179 | return 0; | |
| 176 | *ok = 0; | |
| 177 | ensure_ret(uh_file_response_304(cl, req, s)); | |
| 180 | 178 | } |
| 181 | 179 | } |
| 182 | 180 | |
| 183 | return 1; | |
| 181 | return *ok; | |
| 184 | 182 | } |
| 185 | 183 | |
| 186 | static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s) | |
| 184 | static int uh_file_if_none_match(struct client *cl, struct http_request *req, struct stat *s, int *ok) | |
| 187 | 185 | { |
| 188 | 186 | const char *tag = uh_file_mktag(s); |
| 189 | 187 | char *hdr = uh_file_header_lookup(req, "If-None-Match"); |
| 190 | 188 | char *p; |
| 191 | 189 | int i; |
| 190 | *ok = 1; | |
| 192 | 191 | |
| 193 | 192 | if( hdr ) |
| 194 | 193 | { |
| ... | ... | |
| 203 | 202 | } |
| 204 | 203 | else if( !strcmp(p, "*") || !strcmp(p, tag) ) |
| 205 | 204 | { |
| 205 | *ok = 0; | |
| 206 | ||
| 206 | 207 | if( (req->method == UH_HTTP_MSG_GET) || |
| 207 | 208 | (req->method == UH_HTTP_MSG_HEAD) ) |
| 208 | uh_file_response_304(cl, req, s); | |
| 209 | ensure_ret(uh_file_response_304(cl, req, s)); | |
| 209 | 210 | else |
| 210 | uh_file_response_412(cl, req); | |
| 211 | ensure_ret(uh_file_response_412(cl, req)); | |
| 211 | 212 | |
| 212 | return 0; | |
| 213 | break; | |
| 213 | 214 | } |
| 214 | 215 | } |
| 215 | 216 | } |
| 216 | 217 | |
| 217 | return 1; | |
| 218 | return *ok; | |
| 218 | 219 | } |
| 219 | 220 | |
| 220 | static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s) | |
| 221 | static int uh_file_if_range(struct client *cl, struct http_request *req, struct stat *s, int *ok) | |
| 221 | 222 | { |
| 222 | 223 | char *hdr = uh_file_header_lookup(req, "If-Range"); |
| 224 | *ok = 1; | |
| 223 | 225 | |
| 224 | 226 | if( hdr ) |
| 225 | 227 | { |
| 226 | uh_file_response_412(cl, req); | |
| 227 | return 0; | |
| 228 | *ok = 0; | |
| 229 | ensure_ret(uh_file_response_412(cl, req)); | |
| 228 | 230 | } |
| 229 | 231 | |
| 230 | return 1; | |
| 232 | return *ok; | |
| 231 | 233 | } |
| 232 | 234 | |
| 233 | static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s) | |
| 235 | static int uh_file_if_unmodified_since(struct client *cl, struct http_request *req, struct stat *s, int *ok) | |
| 234 | 236 | { |
| 235 | 237 | char *hdr = uh_file_header_lookup(req, "If-Unmodified-Since"); |
| 238 | *ok = 1; | |
| 236 | 239 | |
| 237 | 240 | if( hdr ) |
| 238 | 241 | { |
| 239 | 242 | if( uh_file_date2unix(hdr) <= s->st_mtime ) |
| 240 | 243 | { |
| 241 | uh_file_response_412(cl, req); | |
| 242 | return 0; | |
| 244 | *ok = 0; | |
| 245 | ensure_ret(uh_file_response_412(cl, req)); | |
| 243 | 246 | } |
| 244 | 247 | } |
| 245 | 248 | |
| 246 | return 1; | |
| 249 | return *ok; | |
| 247 | 250 | } |
| 248 | 251 | |
| 249 | 252 | |
| 250 | #define ensure_out(x) \ | |
| 251 | do { if( x < 0 ) goto out; } while(0) | |
| 252 | ||
| 253 | 253 | static int uh_file_scandir_filter_dir(const struct dirent *e) |
| 254 | 254 | { |
| 255 | 255 | return strcmp(e->d_name, ".") ? 1 : 0; |
| ... | ... | |
| 335 | 335 | void uh_file_request(struct client *cl, struct http_request *req, struct path_info *pi) |
| 336 | 336 | { |
| 337 | 337 | int rlen; |
| 338 | int ok = 1; | |
| 338 | 339 | int fd = -1; |
| 339 | 340 | char buf[UH_LIMIT_MSGHEAD]; |
| 340 | 341 | |
| ... | ... | |
| 342 | 343 | if( (pi->stat.st_mode & S_IFREG) && ((fd = open(pi->phys, O_RDONLY)) > 0) ) |
| 343 | 344 | { |
| 344 | 345 | /* test preconditions */ |
| 345 | if( | |
| 346 | uh_file_if_modified_since(cl, req, &pi->stat) && | |
| 347 | uh_file_if_match(cl, req, &pi->stat) && | |
| 348 | uh_file_if_range(cl, req, &pi->stat) && | |
| 349 | uh_file_if_unmodified_since(cl, req, &pi->stat) && | |
| 350 | uh_file_if_none_match(cl, req, &pi->stat) | |
| 351 | ) { | |
| 346 | if(ok) ensure_out(uh_file_if_modified_since(cl, req, &pi->stat, &ok)); | |
| 347 | if(ok) ensure_out(uh_file_if_match(cl, req, &pi->stat, &ok)); | |
| 348 | if(ok) ensure_out(uh_file_if_range(cl, req, &pi->stat, &ok)); | |
| 349 | if(ok) ensure_out(uh_file_if_unmodified_since(cl, req, &pi->stat, &ok)); | |
| 350 | if(ok) ensure_out(uh_file_if_none_match(cl, req, &pi->stat, &ok)); | |
| 351 | ||
| 352 | if( ok > 0 ) | |
| 353 | { | |
| 352 | 354 | /* write status */ |
| 353 | 355 | ensure_out(uh_file_response_200(cl, req, &pi->stat)); |
| 354 | 356 | |
| package/uhttpd/src/uhttpd-tls.c | ||
|---|---|---|
| 70 | 70 | |
| 71 | 71 | int uh_tls_client_recv(struct client *c, void *buf, int len) |
| 72 | 72 | { |
| 73 | return SSL_read(c->tls, buf, len); | |
| 73 | int rv = SSL_read(c->tls, buf, len); | |
| 74 | return (rv > 0) ? rv : -1; | |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | int uh_tls_client_send(struct client *c, void *buf, int len) |
| 77 | 78 | { |
| 78 | return SSL_write(c->tls, buf, len); | |
| 79 | int rv = SSL_write(c->tls, buf, len); | |
| 80 | return (rv > 0) ? rv : -1; | |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | 83 | void uh_tls_client_close(struct client *c) |
| package/uhttpd/src/uhttpd-utils.c | ||
|---|---|---|
| 112 | 112 | /* unblock SIGCHLD */ |
| 113 | 113 | sigemptyset(&ssn); |
| 114 | 114 | sigaddset(&ssn, SIGCHLD); |
| 115 | sigaddset(&ssn, SIGPIPE); | |
| 115 | 116 | sigprocmask(SIG_UNBLOCK, &ssn, &sso); |
| 116 | 117 | |
| 117 | 118 | rv = select(n, r, w, e, t); |
| ... | ... | |
| 193 | 194 | return sz; |
| 194 | 195 | } |
| 195 | 196 | |
| 196 | #define ensure(x) \ | |
| 197 | do { if( x < 0 ) return -1; } while(0) | |
| 198 | 197 | |
| 199 | 198 | int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...) |
| 200 | 199 | { |
| ... | ... | |
| 211 | 210 | code, summary |
| 212 | 211 | ); |
| 213 | 212 | |
| 214 | ensure(uh_tcp_send(cl, buffer, len)); | |
| 213 | ensure_ret(uh_tcp_send(cl, buffer, len)); | |
| 215 | 214 | |
| 216 | 215 | va_start(ap, fmt); |
| 217 | 216 | len = vsnprintf(buffer, sizeof(buffer), fmt, ap); |
| 218 | 217 | va_end(ap); |
| 219 | 218 | |
| 220 | ensure(uh_http_sendc(cl, buffer, len)); | |
| 221 | ensure(uh_http_sendc(cl, NULL, 0)); | |
| 219 | ensure_ret(uh_http_sendc(cl, buffer, len)); | |
| 220 | ensure_ret(uh_http_sendc(cl, NULL, 0)); | |
| 222 | 221 | |
| 223 | 222 | return 0; |
| 224 | 223 | } |
| ... | ... | |
| 235 | 234 | if( len > 0 ) |
| 236 | 235 | { |
| 237 | 236 | clen = snprintf(chunk, sizeof(chunk), "%X\r\n", len); |
| 238 | ensure(uh_tcp_send(cl, chunk, clen)); | |
| 239 | ensure(uh_tcp_send(cl, data, len)); | |
| 240 | ensure(uh_tcp_send(cl, "\r\n", 2)); | |
| 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)); | |
| 241 | 240 | } |
| 242 | 241 | else |
| 243 | 242 | { |
| 244 | ensure(uh_tcp_send(cl, "0\r\n\r\n", 5)); | |
| 243 | ensure_ret(uh_tcp_send(cl, "0\r\n\r\n", 5)); | |
| 245 | 244 | } |
| 246 | 245 | |
| 247 | 246 | return 0; |
| ... | ... | |
| 259 | 258 | va_end(ap); |
| 260 | 259 | |
| 261 | 260 | if( (req != NULL) && (req->version > 1.0) ) |
| 262 | ensure(uh_http_sendc(cl, buffer, len)); | |
| 261 | ensure_ret(uh_http_sendc(cl, buffer, len)); | |
| 263 | 262 | else if( len > 0 ) |
| 264 | ensure(uh_tcp_send(cl, buffer, len)); | |
| 263 | ensure_ret(uh_tcp_send(cl, buffer, len)); | |
| 265 | 264 | |
| 266 | 265 | return 0; |
| 267 | 266 | } |
| ... | ... | |
| 273 | 272 | len = strlen(buf); |
| 274 | 273 | |
| 275 | 274 | if( (req != NULL) && (req->version > 1.0) ) |
| 276 | ensure(uh_http_sendc(cl, buf, len)); | |
| 275 | ensure_ret(uh_http_sendc(cl, buf, len)); | |
| 277 | 276 | else if( len > 0 ) |
| 278 | ensure(uh_tcp_send(cl, buf, len)); | |
| 277 | ensure_ret(uh_tcp_send(cl, buf, len)); | |
| 279 | 278 | |
| 280 | 279 | return 0; |
| 281 | 280 | } |
| ... | ... | |
| 639 | 638 | ) { |
| 640 | 639 | memcpy(new->pass, pwd->pw_passwd, |
| 641 | 640 | min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1)); |
| 642 | } | |
| 641 | } | |
| 643 | 642 | } |
| 644 | 643 | |
| 645 | 644 | /* ordinary pwd */ |
| package/uhttpd/src/uhttpd-utils.h | ||
|---|---|---|
| 36 | 36 | #define fd_cloexec(fd) \ |
| 37 | 37 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) |
| 38 | 38 | |
| 39 | #define ensure_out(x) \ | |
| 40 | do { if((x) < 0) goto out; } while(0) | |
| 41 | ||
| 42 | #define ensure_ret(x) \ | |
| 43 | do { if((x) < 0) return -1; } while(0) | |
| 44 | ||
| 45 | ||
| 39 | 46 | struct path_info { |
| 40 | 47 | char *root; |
| 41 | 48 | char *phys; |
| package/uhttpd/src/uhttpd.c | ||
|---|---|---|
| 96 | 96 | conf->error_handler = strdup(col1); |
| 97 | 97 | } |
| 98 | 98 | #ifdef HAVE_CGI |
| 99 | else if( (line[0] == '.') && (strchr(line, ':') != NULL) ) | |
| 99 | else if( (line[0] == '*') && (strchr(line, ':') != NULL) ) | |
| 100 | 100 | { |
| 101 | if( !(col1 = strchr(line, ':')) || (*col1++ = 0) || | |
| 102 | !(eol = strchr(col1, '\n')) || (*eol++ = 0) ) | |
| 101 | if( !(col1 = strchr(line, '*')) || (*col1++ = 0) || | |
| 102 | !(col2 = strchr(col1, ':')) || (*col2++ = 0) || | |
| 103 | !(eol = strchr(col2, '\n')) || (*eol++ = 0) ) | |
| 103 | 104 | continue; |
| 104 | 105 | |
| 105 | if( !uh_interpreter_add(line, col1) ) | |
| 106 | if( !uh_interpreter_add(col1, col2) ) | |
| 106 | 107 | { |
| 107 | 108 | fprintf(stderr, |
| 108 | 109 | "Unable to add interpreter %s for extension %s: " |
| 109 | "Out of memory\n", col1, line | |
| 110 | "Out of memory\n", col2, col1 | |
| 110 | 111 | ); |
| 111 | 112 | } |
| 112 | 113 | } |
| ... | ... | |
| 126 | 127 | int status; |
| 127 | 128 | int bound = 0; |
| 128 | 129 | |
| 130 | int tcp_ka_idl = 1; | |
| 131 | int tcp_ka_int = 1; | |
| 132 | int tcp_ka_cnt = 3; | |
| 133 | ||
| 129 | 134 | struct listener *l = NULL; |
| 130 | 135 | struct addrinfo *addrs = NULL, *p = NULL; |
| 131 | 136 | |
| ... | ... | |
| 145 | 150 | } |
| 146 | 151 | |
| 147 | 152 | /* "address already in use" */ |
| 148 | if( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1 ) | |
| 153 | if( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) ) | |
| 149 | 154 | { |
| 150 | 155 | perror("setsockopt()"); |
| 151 | 156 | goto error; |
| 152 | 157 | } |
| 153 | 158 | |
| 159 | /* TCP keep-alive */ | |
| 160 | if( setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) || | |
| 161 | setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &tcp_ka_idl, sizeof(tcp_ka_idl)) || | |
| 162 | setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int)) || | |
| 163 | setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &tcp_ka_cnt, sizeof(tcp_ka_cnt)) ) | |
| 164 | { | |
| 165 | fprintf(stderr, "Notice: Unable to enable TCP keep-alive: %s\n", | |
| 166 | strerror(errno)); | |
| 167 | } | |
| 168 | ||
| 154 | 169 | /* required to get parallel v4 + v6 working */ |
| 155 | 170 | if( p->ai_family == AF_INET6 ) |
| 156 | 171 | { |
| ... | ... | |
| 355 | 370 | ssize_t blen = sizeof(buffer)-1; |
| 356 | 371 | ssize_t rlen = 0; |
| 357 | 372 | |
| 358 | ||
| 359 | 373 | memset(buffer, 0, sizeof(buffer)); |
| 360 | 374 | |
| 361 | 375 | while( blen > 0 ) |
| ... | ... | |
| 371 | 385 | if( select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0 ) |
| 372 | 386 | { |
| 373 | 387 | /* receive data */ |
| 374 | rlen = uh_tcp_peek(cl, bufptr, blen); | |
| 388 | ensure_out(rlen = uh_tcp_peek(cl, bufptr, blen)); | |
| 375 | 389 | |
| 376 | if( rlen > 0 ) | |
| 390 | if( (idxptr = strfind(buffer, sizeof(buffer), "\r\n\r\n", 4)) ) | |
| 377 | 391 | { |
| 378 | if( (idxptr = strfind(buffer, sizeof(buffer), "\r\n\r\n", 4)) ) | |
| 379 | { | |
| 380 | blen -= uh_tcp_recv(cl, bufptr, (int)(idxptr - bufptr) + 4); | |
| 392 | ensure_out(rlen = uh_tcp_recv(cl, bufptr, | |
| 393 | (int)(idxptr - bufptr) + 4)); | |
| 381 | 394 | |
| 382 | /* header read complete ... */ | |
| 383 | return uh_http_header_parse(cl, buffer, sizeof(buffer) - blen - 1); | |
| 384 | } | |
| 385 | else | |
| 386 | { | |
| 387 | rlen = uh_tcp_recv(cl, bufptr, rlen); | |
| 388 | blen -= rlen; | |
| 389 | bufptr += rlen; | |
| 390 | } | |
| 395 | /* header read complete ... */ | |
| 396 | blen -= rlen; | |
| 397 | return uh_http_header_parse(cl, buffer, | |
| 398 | sizeof(buffer) - blen - 1); | |
| 391 | 399 | } |
| 392 | 400 | else |
| 393 | 401 | { |
| 394 | /* invalid request (unexpected eof/timeout) */ | |
| 395 | uh_http_response(cl, 408, "Request Timeout"); | |
| 396 | return NULL; | |
| 402 | ensure_out(rlen = uh_tcp_recv(cl, bufptr, rlen)); | |
| 403 | ||
| 404 | blen -= rlen; | |
| 405 | bufptr += rlen; | |
| 397 | 406 | } |
| 398 | 407 | } |
| 399 | 408 | else |
| 400 | 409 | { |
| 401 | 410 | /* invalid request (unexpected eof/timeout) */ |
| 402 | uh_http_response(cl, 408, "Request Timeout"); | |
| 403 | 411 | return NULL; |
| 404 | 412 | } |
| 405 | 413 | } |
| 406 | 414 | |
| 407 | 415 | /* request entity too large */ |
| 408 | 416 | uh_http_response(cl, 413, "Request Entity Too Large"); |
| 417 | ||
| 418 | out: | |
| 409 | 419 | return NULL; |
| 410 | 420 | } |
| 411 | 421 | |
| package/uhttpd/src/uhttpd.h | ||
|---|---|---|
| 28 | 28 | #include <sys/select.h> |
| 29 | 29 | #include <sys/wait.h> |
| 30 | 30 | #include <netinet/in.h> |
| 31 | #include <netinet/tcp.h> | |
| 31 | 32 | #include <arpa/inet.h> |
| 32 | 33 | #include <linux/limits.h> |
| 33 | 34 | #include <netdb.h> |
| ... | ... | |
| 44 | 45 | #include <openssl/ssl.h> |
| 45 | 46 | #endif |
| 46 | 47 | |
| 48 | /* uClibc... */ | |
| 49 | #ifndef SOL_TCP | |
| 50 | #define SOL_TCP 6 | |
| 51 | #endif | |
| 52 | ||
| 47 | 53 | |
| 48 | 54 | #define UH_LIMIT_MSGHEAD 4096 |
| 49 | 55 | #define UH_LIMIT_HEADERS 64 |
