| 375 | static void CreateFailAllRead() { s_fail_all_read = new FailAllRead; } |
| 376 | |
| 377 | void HttpMessage::SetBodyReader(ProgressiveReader* r) { |
| 378 | if (!_read_body_progressively) { |
| 379 | return r->OnEndOfMessage( |
| 380 | butil::Status(EPERM, "Call SetBodyReader on HttpMessage with" |
| 381 | " read_body_progressively=false")); |
| 382 | } |
| 383 | const int MAX_TRY = 3; |
| 384 | int ntry = 0; |
| 385 | do { |
| 386 | std::unique_lock<butil::Mutex> mu(_body_mutex); |
| 387 | if (_body_reader != NULL) { |
| 388 | mu.unlock(); |
| 389 | return r->OnEndOfMessage( |
| 390 | butil::Status(EPERM, "SetBodyReader is called more than once")); |
| 391 | } |
| 392 | if (_body.empty()) { |
| 393 | if (_stage <= HTTP_ON_BODY) { |
| 394 | _body_reader = r; |
| 395 | return; |
| 396 | } else { // The body is complete and successfully consumed. |
| 397 | mu.unlock(); |
| 398 | return r->OnEndOfMessage(butil::Status()); |
| 399 | } |
| 400 | } else if (_stage <= HTTP_ON_BODY && ++ntry >= MAX_TRY) { |
| 401 | // Stop making _body empty after we've tried several times. |
| 402 | // If _stage is greater than HTTP_ON_BODY, neither OnBody() nor |
| 403 | // OnMessageComplete() will be called in future, we have to spin |
| 404 | // another time to empty _body. |
| 405 | _body_reader = r; |
| 406 | return; |
| 407 | } |
| 408 | butil::IOBuf body_seen = _body.movable(); |
| 409 | mu.unlock(); |
| 410 | for (size_t i = 0; i < body_seen.backing_block_num(); ++i) { |
| 411 | butil::StringPiece blk = body_seen.backing_block(i); |
| 412 | butil::Status st = r->OnReadOnePart(blk.data(), blk.size()); |
| 413 | if (!st.ok()) { |
| 414 | r->OnEndOfMessage(st); |
| 415 | // Make OnBody() or OnMessageComplete() fail on next call to |
| 416 | // close the socket. If the message was already complete, the |
| 417 | // socket will not be closed. |
| 418 | pthread_once(&s_fail_all_read_once, CreateFailAllRead); |
| 419 | r = s_fail_all_read; |
| 420 | ntry = MAX_TRY; |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | } while (true); |
| 425 | } |
| 426 | |
| 427 | const http_parser_settings g_parser_settings = { |
| 428 | &HttpMessage::on_message_begin, |
nothing calls this directly
no test coverage detected