| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/spdy_stream.h" | 5 #include "net/spdy/spdy_stream.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "net/log/net_log_capture_mode.h" | 23 #include "net/log/net_log_capture_mode.h" |
| 24 #include "net/log/net_log_event_type.h" | 24 #include "net/log/net_log_event_type.h" |
| 25 #include "net/spdy/spdy_buffer_producer.h" | 25 #include "net/spdy/spdy_buffer_producer.h" |
| 26 #include "net/spdy/spdy_http_utils.h" | 26 #include "net/spdy/spdy_http_utils.h" |
| 27 #include "net/spdy/spdy_session.h" | 27 #include "net/spdy/spdy_session.h" |
| 28 | 28 |
| 29 namespace net { | 29 namespace net { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 enum StatusHeader { |
| 34 STATUS_HEADER_NOT_INCLUDED = 0, |
| 35 STATUS_HEADER_DOES_NOT_START_WITH_NUMBER = 1, |
| 36 STATUS_HEADER_IS_NUMBER = 2, |
| 37 STATUS_HEADER_HAS_STATUS_TEXT = 3, |
| 38 STATUS_HEADER_MAX = STATUS_HEADER_HAS_STATUS_TEXT |
| 39 }; |
| 40 |
| 41 StatusHeader ParseStatusHeaderImpl(const SpdyHeaderBlock& response_headers, |
| 42 int* status) { |
| 43 SpdyHeaderBlock::const_iterator it = response_headers.find(":status"); |
| 44 if (it == response_headers.end()) |
| 45 return STATUS_HEADER_NOT_INCLUDED; |
| 46 |
| 47 // Save status in |*status| even if some text follows the status code. |
| 48 base::StringPiece status_string = it->second; |
| 49 base::StringPiece::size_type end = status_string.find(' '); |
| 50 if (!StringToInt(status_string.substr(0, end), status)) |
| 51 return STATUS_HEADER_DOES_NOT_START_WITH_NUMBER; |
| 52 |
| 53 return end == base::StringPiece::npos ? STATUS_HEADER_IS_NUMBER |
| 54 : STATUS_HEADER_HAS_STATUS_TEXT; |
| 55 } |
| 56 |
| 57 StatusHeader ParseStatusHeader(const SpdyHeaderBlock& response_headers, |
| 58 int* status) { |
| 59 StatusHeader status_header = ParseStatusHeaderImpl(response_headers, status); |
| 60 UMA_HISTOGRAM_ENUMERATION("Net.Http2ResponseStatusHeader", status_header, |
| 61 STATUS_HEADER_MAX + 1); |
| 62 return status_header; |
| 63 } |
| 64 |
| 33 std::unique_ptr<base::Value> NetLogSpdyStreamErrorCallback( | 65 std::unique_ptr<base::Value> NetLogSpdyStreamErrorCallback( |
| 34 SpdyStreamId stream_id, | 66 SpdyStreamId stream_id, |
| 35 int status, | 67 int status, |
| 36 const std::string* description, | 68 const std::string* description, |
| 37 NetLogCaptureMode /* capture_mode */) { | 69 NetLogCaptureMode /* capture_mode */) { |
| 38 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 70 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 39 dict->SetInteger("stream_id", static_cast<int>(stream_id)); | 71 dict->SetInteger("stream_id", static_cast<int>(stream_id)); |
| 40 dict->SetInteger("status", status); | 72 dict->SetInteger("status", status); |
| 41 dict->SetString("description", *description); | 73 dict->SetString("description", *description); |
| 42 return std::move(dict); | 74 return std::move(dict); |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 } | 401 } |
| 370 | 402 |
| 371 void SpdyStream::SetRequestTime(base::Time t) { | 403 void SpdyStream::SetRequestTime(base::Time t) { |
| 372 request_time_ = t; | 404 request_time_ = t; |
| 373 } | 405 } |
| 374 | 406 |
| 375 void SpdyStream::OnHeadersReceived(const SpdyHeaderBlock& response_headers, | 407 void SpdyStream::OnHeadersReceived(const SpdyHeaderBlock& response_headers, |
| 376 base::Time response_time, | 408 base::Time response_time, |
| 377 base::TimeTicks recv_first_byte_time) { | 409 base::TimeTicks recv_first_byte_time) { |
| 378 switch (response_state_) { | 410 switch (response_state_) { |
| 379 case READY_FOR_HEADERS: | 411 case READY_FOR_HEADERS: { |
| 380 // No header block has been received yet. | 412 // No header block has been received yet. |
| 381 DCHECK(response_headers_.empty()); | 413 DCHECK(response_headers_.empty()); |
| 382 | 414 int status; |
| 383 { | 415 switch (ParseStatusHeader(response_headers, &status)) { |
| 384 SpdyHeaderBlock::const_iterator it = response_headers.find(":status"); | 416 case STATUS_HEADER_NOT_INCLUDED: { |
| 385 if (it == response_headers.end()) { | |
| 386 const std::string error("Response headers do not include :status."); | 417 const std::string error("Response headers do not include :status."); |
| 387 LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); | 418 LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); |
| 388 session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR, error); | 419 session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR, error); |
| 389 return; | 420 return; |
| 390 } | 421 } |
| 391 | 422 case STATUS_HEADER_DOES_NOT_START_WITH_NUMBER: { |
| 392 int status; | |
| 393 if (!StringToInt(it->second, &status)) { | |
| 394 const std::string error("Cannot parse :status."); | 423 const std::string error("Cannot parse :status."); |
| 395 LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); | 424 LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); |
| 396 session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR, error); | 425 session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR, error); |
| 397 return; | 426 return; |
| 398 } | 427 } |
| 399 | 428 // Intentional fallthrough for the following two cases, |
| 400 // Ignore informational headers. | 429 // to maintain compatibility with broken servers that include |
| 401 // TODO(bnc): Add support for 103 Early Hints, https://crbug.com/671310. | 430 // status text in the response. |
| 402 if (status / 100 == 1) { | 431 case STATUS_HEADER_IS_NUMBER: |
| 403 return; | 432 case STATUS_HEADER_HAS_STATUS_TEXT: |
| 404 } | 433 // Ignore informational headers. |
| 434 // TODO(bnc): Add support for 103 Early Hints, |
| 435 // https://crbug.com/671310. |
| 436 if (status / 100 == 1) { |
| 437 return; |
| 438 } |
| 405 } | 439 } |
| 406 | 440 |
| 407 response_state_ = READY_FOR_DATA_OR_TRAILERS; | 441 response_state_ = READY_FOR_DATA_OR_TRAILERS; |
| 408 | 442 |
| 409 switch (type_) { | 443 switch (type_) { |
| 410 case SPDY_BIDIRECTIONAL_STREAM: | 444 case SPDY_BIDIRECTIONAL_STREAM: |
| 411 case SPDY_REQUEST_RESPONSE_STREAM: | 445 case SPDY_REQUEST_RESPONSE_STREAM: |
| 412 // A bidirectional stream or a request/response stream is ready for | 446 // A bidirectional stream or a request/response stream is ready for |
| 413 // the response headers only after request headers are sent. | 447 // the response headers only after request headers are sent. |
| 414 if (io_state_ == STATE_IDLE) { | 448 if (io_state_ == STATE_IDLE) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 432 break; | 466 break; |
| 433 } | 467 } |
| 434 | 468 |
| 435 DCHECK_NE(io_state_, STATE_IDLE); | 469 DCHECK_NE(io_state_, STATE_IDLE); |
| 436 | 470 |
| 437 response_time_ = response_time; | 471 response_time_ = response_time; |
| 438 recv_first_byte_time_ = recv_first_byte_time; | 472 recv_first_byte_time_ = recv_first_byte_time; |
| 439 SaveResponseHeaders(response_headers); | 473 SaveResponseHeaders(response_headers); |
| 440 | 474 |
| 441 break; | 475 break; |
| 442 | 476 } |
| 443 case READY_FOR_DATA_OR_TRAILERS: | 477 case READY_FOR_DATA_OR_TRAILERS: |
| 444 // Second header block is trailers. | 478 // Second header block is trailers. |
| 445 if (type_ == SPDY_PUSH_STREAM) { | 479 if (type_ == SPDY_PUSH_STREAM) { |
| 446 const std::string error("Trailers not supported for push stream."); | 480 const std::string error("Trailers not supported for push stream."); |
| 447 LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); | 481 LogStreamError(ERR_SPDY_PROTOCOL_ERROR, error); |
| 448 session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR, error); | 482 session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR, error); |
| 449 return; | 483 return; |
| 450 } | 484 } |
| 451 | 485 |
| 452 response_state_ = TRAILERS_RECEIVED; | 486 response_state_ = TRAILERS_RECEIVED; |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 901 description = base::StringPrintf("Unknown state 0x%08X (%u)", state, | 935 description = base::StringPrintf("Unknown state 0x%08X (%u)", state, |
| 902 state); | 936 state); |
| 903 break; | 937 break; |
| 904 } | 938 } |
| 905 return description; | 939 return description; |
| 906 } | 940 } |
| 907 | 941 |
| 908 #undef STATE_CASE | 942 #undef STATE_CASE |
| 909 | 943 |
| 910 } // namespace net | 944 } // namespace net |
| OLD | NEW |