1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/spdy/spdy_stream.h"
#include <limits>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/thread_task_runner_handle.h"
#include "base/values.h"
#include "net/spdy/spdy_buffer_producer.h"
#include "net/spdy/spdy_http_utils.h"
#include "net/spdy/spdy_session.h"
namespace net {
namespace {
scoped_ptr<base::Value> NetLogSpdyStreamErrorCallback(
SpdyStreamId stream_id,
int status,
const std::string* description,
NetLogCaptureMode /* capture_mode */) {
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
dict->SetInteger("stream_id", static_cast<int>(stream_id));
dict->SetInteger("status", status);
dict->SetString("description", *description);
return dict.Pass();
}
scoped_ptr<base::Value> NetLogSpdyStreamWindowUpdateCallback(
SpdyStreamId stream_id,
int32_t delta,
int32_t window_size,
NetLogCaptureMode /* capture_mode */) {
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
dict->SetInteger("stream_id", stream_id);
dict->SetInteger("delta", delta);
dict->SetInteger("window_size", window_size);
return dict.Pass();
}
bool ContainsUppercaseAscii(const std::string& str) {
for (std::string::const_iterator i(str.begin()); i != str.end(); ++i) {
if (*i >= 'A' && *i <= 'Z') {
return true;
}
}
return false;
}
} // namespace
void SpdyStream::Delegate::OnTrailers(const SpdyHeaderBlock& trailers) {}
// A wrapper around a stream that calls into ProduceSynStreamFrame().
class SpdyStream::SynStreamBufferProducer : public SpdyBufferProducer {
public:
SynStreamBufferProducer(const base::WeakPtr<SpdyStream>& stream)
: stream_(stream) {
DCHECK(stream_.get());
}
~SynStreamBufferProducer() override {}
scoped_ptr<SpdyBuffer> ProduceBuffer() override {
if (!stream_.get()) {
NOTREACHED();
return scoped_ptr<SpdyBuffer>();
}
DCHECK_GT(stream_->stream_id(), 0u);
return scoped_ptr<SpdyBuffer>(
new SpdyBuffer(stream_->ProduceSynStreamFrame()));
}
private:
const base::WeakPtr<SpdyStream> stream_;
};
SpdyStream::SpdyStream(SpdyStreamType type,
const base::WeakPtr<SpdySession>& session,
const GURL& url,
RequestPriority priority,
int32_t initial_send_window_size,
int32_t max_recv_window_size,
const BoundNetLog& net_log)
: type_(type),
stream_id_(0),
url_(url),
priority_(priority),
send_stalled_by_flow_control_(false),
send_window_size_(initial_send_window_size),
max_recv_window_size_(max_recv_window_size),
recv_window_size_(max_recv_window_size),
unacked_recv_window_bytes_(0),
session_(session),
delegate_(NULL),
pending_send_status_(MORE_DATA_TO_SEND),
request_time_(base::Time::Now()),
response_headers_status_(RESPONSE_HEADERS_ARE_INCOMPLETE),
io_state_(STATE_IDLE),
response_status_(OK),
net_log_(net_log),
raw_received_bytes_(0),
raw_sent_bytes_(0),
send_bytes_(0),
recv_bytes_(0),
write_handler_guard_(false),
weak_ptr_factory_(this) {
CHECK(type_ == SPDY_BIDIRECTIONAL_STREAM ||
type_ == SPDY_REQUEST_RESPONSE_STREAM ||
type_ == SPDY_PUSH_STREAM);
CHECK_GE(priority_, MINIMUM_PRIORITY);
CHECK_LE(priority_, MAXIMUM_PRIORITY);
}
SpdyStream::~SpdyStream() {
CHECK(!write_handler_guard_);
UpdateHistograms();
}
void SpdyStream::SetDelegate(Delegate* delegate) {
CHECK(!delegate_);
CHECK(delegate);
delegate_ = delegate;
CHECK(io_state_ == STATE_IDLE ||
io_state_ == STATE_HALF_CLOSED_LOCAL_UNCLAIMED ||
io_state_ == STATE_RESERVED_REMOTE);
if (io_state_ == STATE_HALF_CLOSED_LOCAL_UNCLAIMED) {
DCHECK_EQ(type_, SPDY_PUSH_STREAM);
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&SpdyStream::PushedStreamReplay, GetWeakPtr()));
}
}
void SpdyStream::PushedStreamReplay() {
DCHECK_EQ(type_, SPDY_PUSH_STREAM);
DCHECK_NE(stream_id_, 0u);
CHECK_EQ(stream_id_ % 2, 0u);
CHECK_EQ(io_state_, STATE_HALF_CLOSED_LOCAL_UNCLAIMED);
io_state_ = STATE_HALF_CLOSED_LOCAL;
// The delegate methods called below may delete |this|, so use
// |weak_this| to detect that.
base::WeakPtr<SpdyStream> weak_this = GetWeakPtr();
CHECK(delegate_);
SpdyResponseHeadersStatus status =
delegate_->OnResponseHeadersUpdated(response_headers_);
if (status == RESPONSE_HEADERS_ARE_INCOMPLETE) {
// Since RESPONSE_HEADERS_ARE_INCOMPLETE was returned, we must not
// have been closed. Since we don't have complete headers, assume
// we're waiting for another HEADERS frame, and we had better not
// have any pending data frames.
CHECK(weak_this);
if (!pending_recv_data_.empty()) {
LogStreamError(ERR_SPDY_PROTOCOL_ERROR,
"Data received with incomplete headers.");
session_->CloseActiveStream(stream_id_, ERR_SPDY_PROTOCOL_ERROR);
}
return;
}
// OnResponseHeadersUpdated() may have closed |this|.
if (!weak_this)
return;
response_headers_status_ = RESPONSE_HEADERS_ARE_COMPLETE;
while (!pending_recv_data_.empty()) {
// Take ownership of the first element of |pending_recv_data_|.
scoped_ptr<SpdyBuffer> buffer = std::move(pending_recv_data_.at(0));
pending_recv_data_.erase(pending_recv_data_.begin());
bool eof = (buffer == NULL);
CHECK(delegate_);
delegate_->OnDataReceived(buffer.Pass());
// OnDataReceived() may have closed |this|.
if (!weak_this)
return;
if (eof) {
DCHECK(pending_recv_data_.empty());
session_->CloseActiveStream(stream_id_, OK);
DCHECK(!weak_this);
// |pending_recv_data_| is invalid at this point.
break;
}
}
}
scoped_ptr<SpdyFrame> SpdyStream::ProduceSynStreamFrame() {
CHECK_EQ(io_state_, STATE_IDLE);
CHECK(request_headers_);
CHECK_GT(stream_id_, 0u);
SpdyControlFlags flags =
(pending_send_status_ == NO_MORE_DATA_TO_SEND) ?
CONTROL_FLAG_FIN : CONTROL_FLAG_NONE;
scoped_ptr<SpdyFrame> frame(session_->CreateSynStream(
stream_id_, priority_, flags, *request_headers_));
send_time_ = base::TimeTicks::Now();
return frame.Pass();
}
void SpdyStream::DetachDelegate() {
DCHECK(!IsClosed());
delegate_ = NULL;
Cancel();
}
void SpdyStream::AdjustSendWindowSize(int32_t delta_window_size) {
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
if (IsClosed())
return;
// Check for wraparound.
if (send_window_size_ > 0) {
DCHECK_LE(delta_window_size,
std::numeric_limits<int32_t>::max() - send_window_size_);
}
if (send_window_size_ < 0) {
DCHECK_GE(delta_window_size,
std::numeric_limits<int32_t>::min() - send_window_size_);
}
send_window_size_ += delta_window_size;
PossiblyResumeIfSendStalled();
}
void SpdyStream::OnWriteBufferConsumed(
size_t frame_payload_size,
size_t consume_size,
SpdyBuffer::ConsumeSource consume_source) {
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
if (consume_source == SpdyBuffer::DISCARD) {
// If we're discarding a frame or part of it, increase the send
// window by the number of discarded bytes. (Although if we're
// discarding part of a frame, it's probably because of a write
// error and we'll be tearing down the stream soon.)
size_t remaining_payload_bytes = std::min(consume_size, frame_payload_size);
DCHECK_GT(remaining_payload_bytes, 0u);
IncreaseSendWindowSize(static_cast<int32_t>(remaining_payload_bytes));
}
// For consumed bytes, the send window is increased when we receive
// a WINDOW_UPDATE frame.
}
void SpdyStream::IncreaseSendWindowSize(int32_t delta_window_size) {
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
DCHECK_GE(delta_window_size, 1);
// Ignore late WINDOW_UPDATEs.
if (IsClosed())
return;
if (send_window_size_ > 0) {
// Check for overflow.
int32_t max_delta_window_size =
std::numeric_limits<int32_t>::max() - send_window_size_;
if (delta_window_size > max_delta_window_size) {
std::string desc = base::StringPrintf(
"Received WINDOW_UPDATE [delta: %d] for stream %d overflows "
"send_window_size_ [current: %d]", delta_window_size, stream_id_,
send_window_size_);
session_->ResetStream(stream_id_, RST_STREAM_FLOW_CONTROL_ERROR, desc);
return;
}
}
send_window_size_ += delta_window_size;
net_log_.AddEvent(
NetLog::TYPE_HTTP2_STREAM_UPDATE_SEND_WINDOW,
base::Bind(&NetLogSpdyStreamWindowUpdateCallback, stream_id_,
delta_window_size, send_window_size_));
PossiblyResumeIfSendStalled();
}
void SpdyStream::DecreaseSendWindowSize(int32_t delta_window_size) {
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
if (IsClosed())
return;
// We only call this method when sending a frame. Therefore,
// |delta_window_size| should be within the valid frame size range.
DCHECK_GE(delta_window_size, 1);
DCHECK_LE(delta_window_size, kMaxSpdyFrameChunkSize);
// |send_window_size_| should have been at least |delta_window_size| for
// this call to happen.
DCHECK_GE(send_window_size_, delta_window_size);
send_window_size_ -= delta_window_size;
net_log_.AddEvent(
NetLog::TYPE_HTTP2_STREAM_UPDATE_SEND_WINDOW,
base::Bind(&NetLogSpdyStreamWindowUpdateCallback, stream_id_,
-delta_window_size, send_window_size_));
}
void SpdyStream::OnReadBufferConsumed(
size_t consume_size,
SpdyBuffer::ConsumeSource consume_source) {
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
DCHECK_GE(consume_size, 1u);
DCHECK_LE(consume_size,
static_cast<size_t>(std::numeric_limits<int32_t>::max()));
IncreaseRecvWindowSize(static_cast<int32_t>(consume_size));
}
void SpdyStream::IncreaseRecvWindowSize(int32_t delta_window_size) {
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
// By the time a read is processed by the delegate, this stream may
// already be inactive.
if (!session_->IsStreamActive(stream_id_))
return;
DCHECK_GE(unacked_recv_window_bytes_, 0);
DCHECK_GE(recv_window_size_, unacked_recv_window_bytes_);
DCHECK_GE(delta_window_size, 1);
// Check for overflow.
DCHECK_LE(delta_window_size,
std::numeric_limits<int32_t>::max() - recv_window_size_);
recv_window_size_ += delta_window_size;
net_log_.AddEvent(
NetLog::TYPE_HTTP2_STREAM_UPDATE_RECV_WINDOW,
base::Bind(&NetLogSpdyStreamWindowUpdateCallback, stream_id_,
delta_window_size, recv_window_size_));
unacked_recv_window_bytes_ += delta_window_size;
if (unacked_recv_window_bytes_ > max_recv_window_size_ / 2) {
session_->SendStreamWindowUpdate(
stream_id_, static_cast<uint32_t>(unacked_recv_window_bytes_));
unacked_recv_window_bytes_ = 0;
}
}
void SpdyStream::DecreaseRecvWindowSize(int32_t delta_window_size) {
DCHECK(session_->IsStreamActive(stream_id_));
DCHECK_GE(session_->flow_control_state(), SpdySession::FLOW_CONTROL_STREAM);
DCHECK_GE(delta_window_size, 1);
// The receiving window size as the peer knows it is
// |recv_window_size_ - unacked_recv_window_bytes_|, if more data are sent by
// the peer, that means that the receive window is not being respected.
if (delta_window_size > recv_window_size_ - unacked_recv_window_bytes_) {
session_->ResetStream(
stream_id_, RST_STREAM_FLOW_CONTROL_ERROR,
"delta_window_size is " + base::IntToString(delta_window_size) +
" in DecreaseRecvWindowSize, which is larger than the receive " +
"window size of " + base::IntToString(recv_window_size_));
return;
}
recv_window_size_ -= delta_window_size;
net_log_.AddEvent(
NetLog::TYPE_HTTP2_STREAM_UPDATE_RECV_WINDOW,
base::Bind(&NetLogSpdyStreamWindowUpdateCallback, stream_id_,
-delta_window_size, recv_window_size_));
}
int SpdyStream::GetPeerAddress(IPEndPoint* address) const {
return session_->GetPeerAddress(address);
}
int SpdyStream::GetLocalAddress(IPEndPoint* address) const {
return session_->GetLocalAddress(address);
}
bool SpdyStream::WasEverUsed() const {
return session_->WasEverUsed();
}
base::Time SpdyStream::GetRequestTime() const {
return request_time_;
}
void SpdyStream::SetRequestTime(base::Time t) {
request_time_ = t;
}
int SpdyStream::OnInitialResponseHeadersReceived(
const SpdyHeaderBlock& initial_response_headers,
base::Time response_time,
base::TimeTicks recv_first_byte_time) {
// SpdySession guarantees that this is called at most once.
CHECK(response_headers_.empty());
// Check to make sure that we don't receive the response headers
// before we're ready for it.
switch (type_) {
case SPDY_BIDIRECTIONAL_STREAM:
// For a bidirectional stream, we're ready for the response
// headers once we've finished sending the request headers.
if (io_state_ == STATE_IDLE) {
session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Response received before request sent");
return ERR_SPDY_PROTOCOL_ERROR;
}
break;
case SPDY_REQUEST_RESPONSE_STREAM:
// For a request/response stream, we're ready for the response
// headers once we've finished sending the request headers.
if (io_state_ == STATE_IDLE) {
session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Response received before request sent");
return ERR_SPDY_PROTOCOL_ERROR;
}
break;
case SPDY_PUSH_STREAM:
// Push streams transition to a locally half-closed state upon headers.
// We must continue to buffer data while waiting for a call to
// SetDelegate() (which may not ever happen).
CHECK_EQ(io_state_, STATE_RESERVED_REMOTE);
if (!delegate_) {
io_state_ = STATE_HALF_CLOSED_LOCAL_UNCLAIMED;
} else {
io_state_ = STATE_HALF_CLOSED_LOCAL;
}
break;
}
DCHECK_NE(io_state_, STATE_IDLE);
response_time_ = response_time;
recv_first_byte_time_ = recv_first_byte_time;
return MergeWithResponseHeaders(initial_response_headers);
}
int SpdyStream::OnAdditionalResponseHeadersReceived(
const SpdyHeaderBlock& additional_response_headers) {
if (type_ == SPDY_REQUEST_RESPONSE_STREAM) {
if (response_headers_status_ != RESPONSE_HEADERS_ARE_COMPLETE) {
session_->ResetStream(
stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Additional headers received for request/response stream");
return ERR_SPDY_PROTOCOL_ERROR;
}
response_headers_status_ = TRAILERS_RECEIVED;
delegate_->OnTrailers(additional_response_headers);
return OK;
}
if (type_ == SPDY_BIDIRECTIONAL_STREAM) {
DCHECK_EQ(RESPONSE_HEADERS_ARE_COMPLETE, response_headers_status_);
response_headers_status_ = TRAILERS_RECEIVED;
delegate_->OnTrailers(additional_response_headers);
return OK;
}
if (type_ == SPDY_PUSH_STREAM &&
response_headers_status_ == RESPONSE_HEADERS_ARE_COMPLETE) {
session_->ResetStream(
stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Additional headers received for push stream");
return ERR_SPDY_PROTOCOL_ERROR;
}
return MergeWithResponseHeaders(additional_response_headers);
}
void SpdyStream::OnPushPromiseHeadersReceived(const SpdyHeaderBlock& headers) {
CHECK(!request_headers_.get());
CHECK_EQ(io_state_, STATE_IDLE);
CHECK_EQ(type_, SPDY_PUSH_STREAM);
DCHECK(!delegate_);
io_state_ = STATE_RESERVED_REMOTE;
request_headers_.reset(new SpdyHeaderBlock(headers));
}
void SpdyStream::OnDataReceived(scoped_ptr<SpdyBuffer> buffer) {
DCHECK(session_->IsStreamActive(stream_id_));
// If we're still buffering data for a push stream, we will do the
// check for data received with incomplete headers in
// PushedStreamReplayData().
if (io_state_ == STATE_HALF_CLOSED_LOCAL_UNCLAIMED) {
DCHECK_EQ(type_, SPDY_PUSH_STREAM);
// It should be valid for this to happen in the server push case.
// We'll return received data when delegate gets attached to the stream.
if (buffer) {
pending_recv_data_.push_back(std::move(buffer));
} else {
pending_recv_data_.push_back(NULL);
// Note: we leave the stream open in the session until the stream
// is claimed.
}
return;
}
if (response_headers_status_ == TRAILERS_RECEIVED && buffer) {
// TRAILERS_RECEIVED is only used in SPDY_REQUEST_RESPONSE_STREAM and
// SPDY_BIDIRECTIONAL_STREAM.
DCHECK(type_ == SPDY_REQUEST_RESPONSE_STREAM ||
type_ == SPDY_BIDIRECTIONAL_STREAM);
session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Data received after trailers");
return;
}
// If we have response headers but the delegate has indicated that
// it's still incomplete, then that's a protocol error.
if (response_headers_status_ == RESPONSE_HEADERS_ARE_INCOMPLETE) {
LogStreamError(ERR_SPDY_PROTOCOL_ERROR,
"Data received with incomplete headers.");
session_->CloseActiveStream(stream_id_, ERR_SPDY_PROTOCOL_ERROR);
return;
}
CHECK(!IsClosed());
if (!buffer) {
if (io_state_ == STATE_OPEN) {
io_state_ = STATE_HALF_CLOSED_REMOTE;
} else if (io_state_ == STATE_HALF_CLOSED_LOCAL) {
io_state_ = STATE_CLOSED;
// Deletes |this|.
session_->CloseActiveStream(stream_id_, OK);
} else {
NOTREACHED() << io_state_;
}
return;
}
size_t length = buffer->GetRemainingSize();
DCHECK_LE(length, session_->GetDataFrameMaximumPayload());
if (session_->flow_control_state() >= SpdySession::FLOW_CONTROL_STREAM) {
base::WeakPtr<SpdyStream> weak_this = GetWeakPtr();
// May close the stream.
DecreaseRecvWindowSize(static_cast<int32_t>(length));
if (!weak_this)
return;
buffer->AddConsumeCallback(
base::Bind(&SpdyStream::OnReadBufferConsumed, GetWeakPtr()));
}
// Track our bandwidth.
recv_bytes_ += length;
recv_last_byte_time_ = base::TimeTicks::Now();
// May close |this|.
delegate_->OnDataReceived(buffer.Pass());
}
void SpdyStream::OnPaddingConsumed(size_t len) {
if (session_->flow_control_state() >= SpdySession::FLOW_CONTROL_STREAM) {
// Decrease window size because padding bytes are received.
// Increase window size because padding bytes are consumed (by discarding).
// Net result: |unacked_recv_window_bytes_| increases by |len|,
// |recv_window_size_| does not change.
base::WeakPtr<SpdyStream> weak_this = GetWeakPtr();
// May close the stream.
DecreaseRecvWindowSize(static_cast<int32_t>(len));
if (!weak_this)
return;
IncreaseRecvWindowSize(static_cast<int32_t>(len));
}
}
void SpdyStream::OnFrameWriteComplete(SpdyFrameType frame_type,
size_t frame_size) {
DCHECK_NE(type_, SPDY_PUSH_STREAM);
CHECK(frame_type == SYN_STREAM ||
frame_type == DATA) << frame_type;
int result = (frame_type == SYN_STREAM) ?
OnRequestHeadersSent() : OnDataSent(frame_size);
if (result == ERR_IO_PENDING) {
// The write operation hasn't completed yet.
return;
}
if (pending_send_status_ == NO_MORE_DATA_TO_SEND) {
if(io_state_ == STATE_OPEN) {
io_state_ = STATE_HALF_CLOSED_LOCAL;
} else if(io_state_ == STATE_HALF_CLOSED_REMOTE) {
io_state_ = STATE_CLOSED;
} else {
NOTREACHED() << io_state_;
}
}
// Notify delegate of write completion. Must not destroy |this|.
CHECK(delegate_);
{
base::WeakPtr<SpdyStream> weak_this = GetWeakPtr();
write_handler_guard_ = true;
if (frame_type == SYN_STREAM) {
delegate_->OnRequestHeadersSent();
} else {
delegate_->OnDataSent();
}
CHECK(weak_this);
write_handler_guard_ = false;
}
if (io_state_ == STATE_CLOSED) {
// Deletes |this|.
session_->CloseActiveStream(stream_id_, OK);
}
}
int SpdyStream::OnRequestHeadersSent() {
CHECK_EQ(io_state_, STATE_IDLE);
CHECK_NE(stream_id_, 0u);
io_state_ = STATE_OPEN;
return OK;
}
int SpdyStream::OnDataSent(size_t frame_size) {
CHECK(io_state_ == STATE_OPEN ||
io_state_ == STATE_HALF_CLOSED_REMOTE) << io_state_;
size_t frame_payload_size = frame_size - session_->GetDataFrameMinimumSize();
CHECK_GE(frame_size, session_->GetDataFrameMinimumSize());
CHECK_LE(frame_payload_size, session_->GetDataFrameMaximumPayload());
send_bytes_ += frame_payload_size;
// If more data is available to send, dispatch it and
// return that the write operation is still ongoing.
pending_send_data_->DidConsume(frame_payload_size);
if (pending_send_data_->BytesRemaining() > 0) {
QueueNextDataFrame();
return ERR_IO_PENDING;
} else {
pending_send_data_ = NULL;
return OK;
}
}
SpdyMajorVersion SpdyStream::GetProtocolVersion() const {
return session_->GetProtocolVersion();
}
void SpdyStream::LogStreamError(int status, const std::string& description) {
net_log_.AddEvent(NetLog::TYPE_HTTP2_STREAM_ERROR,
base::Bind(&NetLogSpdyStreamErrorCallback, stream_id_,
status, &description));
}
void SpdyStream::OnClose(int status) {
// In most cases, the stream should already be CLOSED. The exception is when a
// SpdySession is shutting down while the stream is in an intermediate state.
io_state_ = STATE_CLOSED;
response_status_ = status;
Delegate* delegate = delegate_;
delegate_ = NULL;
if (delegate)
delegate->OnClose(status);
// Unset |stream_id_| last so that the delegate can look it up.
stream_id_ = 0;
}
void SpdyStream::Cancel() {
// We may be called again from a delegate's OnClose().
if (io_state_ == STATE_CLOSED)
return;
if (stream_id_ != 0) {
session_->ResetStream(stream_id_, RST_STREAM_CANCEL, std::string());
} else {
session_->CloseCreatedStream(GetWeakPtr(), RST_STREAM_CANCEL);
}
// |this| is invalid at this point.
}
void SpdyStream::Close() {
// We may be called again from a delegate's OnClose().
if (io_state_ == STATE_CLOSED)
return;
if (stream_id_ != 0) {
session_->CloseActiveStream(stream_id_, OK);
} else {
session_->CloseCreatedStream(GetWeakPtr(), OK);
}
// |this| is invalid at this point.
}
base::WeakPtr<SpdyStream> SpdyStream::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
int SpdyStream::SendRequestHeaders(scoped_ptr<SpdyHeaderBlock> request_headers,
SpdySendStatus send_status) {
CHECK_NE(type_, SPDY_PUSH_STREAM);
CHECK_EQ(pending_send_status_, MORE_DATA_TO_SEND);
CHECK(!request_headers_);
CHECK(!pending_send_data_.get());
CHECK_EQ(io_state_, STATE_IDLE);
request_headers_ = request_headers.Pass();
pending_send_status_ = send_status;
session_->EnqueueStreamWrite(
GetWeakPtr(), SYN_STREAM,
scoped_ptr<SpdyBufferProducer>(
new SynStreamBufferProducer(GetWeakPtr())));
return ERR_IO_PENDING;
}
void SpdyStream::SendData(IOBuffer* data,
int length,
SpdySendStatus send_status) {
CHECK_NE(type_, SPDY_PUSH_STREAM);
CHECK_EQ(pending_send_status_, MORE_DATA_TO_SEND);
CHECK(io_state_ == STATE_OPEN ||
io_state_ == STATE_HALF_CLOSED_REMOTE) << io_state_;
CHECK(!pending_send_data_.get());
pending_send_data_ = new DrainableIOBuffer(data, length);
pending_send_status_ = send_status;
QueueNextDataFrame();
}
bool SpdyStream::GetSSLInfo(SSLInfo* ssl_info,
bool* was_npn_negotiated,
NextProto* protocol_negotiated) {
return session_->GetSSLInfo(
ssl_info, was_npn_negotiated, protocol_negotiated);
}
void SpdyStream::PossiblyResumeIfSendStalled() {
if (IsLocallyClosed()) {
return;
}
if (send_stalled_by_flow_control_ && !session_->IsSendStalled() &&
send_window_size_ > 0) {
net_log_.AddEvent(NetLog::TYPE_HTTP2_STREAM_FLOW_CONTROL_UNSTALLED,
NetLog::IntCallback("stream_id", stream_id_));
send_stalled_by_flow_control_ = false;
QueueNextDataFrame();
}
}
bool SpdyStream::IsClosed() const {
return io_state_ == STATE_CLOSED;
}
bool SpdyStream::IsLocallyClosed() const {
return io_state_ == STATE_HALF_CLOSED_LOCAL_UNCLAIMED ||
io_state_ == STATE_HALF_CLOSED_LOCAL ||
io_state_ == STATE_CLOSED;
}
bool SpdyStream::IsIdle() const {
return io_state_ == STATE_IDLE;
}
bool SpdyStream::IsOpen() const {
return io_state_ == STATE_OPEN;
}
bool SpdyStream::IsReservedRemote() const {
return io_state_ == STATE_RESERVED_REMOTE;
}
NextProto SpdyStream::GetProtocol() const {
return session_->protocol();
}
void SpdyStream::AddRawReceivedBytes(size_t received_bytes) {
raw_received_bytes_ += received_bytes;
}
void SpdyStream::AddRawSentBytes(size_t sent_bytes) {
raw_sent_bytes_ += sent_bytes;
}
bool SpdyStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
if (stream_id_ == 0)
return false;
return session_->GetLoadTimingInfo(stream_id_, load_timing_info);
}
GURL SpdyStream::GetUrlFromHeaders() const {
if (!request_headers_)
return GURL();
return GetUrlFromHeaderBlock(
*request_headers_, GetProtocolVersion(), type_ == SPDY_PUSH_STREAM);
}
bool SpdyStream::HasUrlFromHeaders() const {
return !GetUrlFromHeaders().is_empty();
}
void SpdyStream::UpdateHistograms() {
// We need at least the receive timers to be filled in, as otherwise
// metrics can be bogus.
if (recv_first_byte_time_.is_null() || recv_last_byte_time_.is_null())
return;
base::TimeTicks effective_send_time;
if (type_ == SPDY_PUSH_STREAM) {
// Push streams shouldn't have |send_time_| filled in.
DCHECK(send_time_.is_null());
effective_send_time = recv_first_byte_time_;
} else {
// For non-push streams, we also need |send_time_| to be filled
// in.
if (send_time_.is_null())
return;
effective_send_time = send_time_;
}
UMA_HISTOGRAM_TIMES("Net.SpdyStreamTimeToFirstByte",
recv_first_byte_time_ - effective_send_time);
UMA_HISTOGRAM_TIMES("Net.SpdyStreamDownloadTime",
recv_last_byte_time_ - recv_first_byte_time_);
UMA_HISTOGRAM_TIMES("Net.SpdyStreamTime",
recv_last_byte_time_ - effective_send_time);
UMA_HISTOGRAM_COUNTS("Net.SpdySendBytes", send_bytes_);
UMA_HISTOGRAM_COUNTS("Net.SpdyRecvBytes", recv_bytes_);
}
void SpdyStream::QueueNextDataFrame() {
// Until the request has been completely sent, we cannot be sure
// that our stream_id is correct.
CHECK(io_state_ == STATE_OPEN ||
io_state_ == STATE_HALF_CLOSED_REMOTE) << io_state_;
CHECK_GT(stream_id_, 0u);
CHECK(pending_send_data_.get());
// Only the final fame may have a length of 0.
if (pending_send_status_ == NO_MORE_DATA_TO_SEND) {
CHECK_GE(pending_send_data_->BytesRemaining(), 0);
} else {
CHECK_GT(pending_send_data_->BytesRemaining(), 0);
}
SpdyDataFlags flags =
(pending_send_status_ == NO_MORE_DATA_TO_SEND) ?
DATA_FLAG_FIN : DATA_FLAG_NONE;
scoped_ptr<SpdyBuffer> data_buffer(
session_->CreateDataBuffer(stream_id_,
pending_send_data_.get(),
pending_send_data_->BytesRemaining(),
flags));
// We'll get called again by PossiblyResumeIfSendStalled().
if (!data_buffer)
return;
if (session_->flow_control_state() >= SpdySession::FLOW_CONTROL_STREAM) {
DCHECK_GE(data_buffer->GetRemainingSize(),
session_->GetDataFrameMinimumSize());
size_t payload_size =
data_buffer->GetRemainingSize() - session_->GetDataFrameMinimumSize();
DCHECK_LE(payload_size, session_->GetDataFrameMaximumPayload());
// Send window size is based on payload size, so nothing to do if this is
// just a FIN with no payload.
if (payload_size != 0) {
DecreaseSendWindowSize(static_cast<int32_t>(payload_size));
// This currently isn't strictly needed, since write frames are
// discarded only if the stream is about to be closed. But have it
// here anyway just in case this changes.
data_buffer->AddConsumeCallback(
base::Bind(&SpdyStream::OnWriteBufferConsumed,
GetWeakPtr(), payload_size));
}
}
session_->EnqueueStreamWrite(
GetWeakPtr(), DATA,
scoped_ptr<SpdyBufferProducer>(
new SimpleBufferProducer(data_buffer.Pass())));
}
int SpdyStream::MergeWithResponseHeaders(
const SpdyHeaderBlock& new_response_headers) {
if (new_response_headers.find("transfer-encoding") !=
new_response_headers.end()) {
session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Received transfer-encoding header");
return ERR_SPDY_PROTOCOL_ERROR;
}
for (SpdyHeaderBlock::const_iterator it = new_response_headers.begin();
it != new_response_headers.end(); ++it) {
// Disallow uppercase headers.
if (ContainsUppercaseAscii(it->first.as_string())) {
session_->ResetStream(
stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Upper case characters in header: " + it->first.as_string());
return ERR_SPDY_PROTOCOL_ERROR;
}
SpdyHeaderBlock::iterator it2 =
response_headers_.find(it->first.as_string());
// Disallow duplicate headers. This is just to be conservative.
if (it2 != response_headers_.end()) {
session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Duplicate header: " + it->first.as_string());
return ERR_SPDY_PROTOCOL_ERROR;
}
response_headers_.insert(*it);
}
// If delegate_ is not yet attached, we'll call
// OnResponseHeadersUpdated() after the delegate gets attached to
// the stream.
if (delegate_) {
// The call to OnResponseHeadersUpdated() below may delete |this|,
// so use |weak_this| to detect that.
base::WeakPtr<SpdyStream> weak_this = GetWeakPtr();
SpdyResponseHeadersStatus status =
delegate_->OnResponseHeadersUpdated(response_headers_);
if (status == RESPONSE_HEADERS_ARE_INCOMPLETE) {
// Since RESPONSE_HEADERS_ARE_INCOMPLETE was returned, we must not
// have been closed.
CHECK(weak_this);
// Incomplete headers are OK only for push streams.
if (type_ != SPDY_PUSH_STREAM) {
session_->ResetStream(stream_id_, RST_STREAM_PROTOCOL_ERROR,
"Incomplete headers");
return ERR_INCOMPLETE_SPDY_HEADERS;
}
} else if (weak_this) {
response_headers_status_ = RESPONSE_HEADERS_ARE_COMPLETE;
}
}
return OK;
}
#define STATE_CASE(s) \
case s: \
description = base::StringPrintf("%s (0x%08X)", #s, s); \
break
std::string SpdyStream::DescribeState(State state) {
std::string description;
switch (state) {
STATE_CASE(STATE_IDLE);
STATE_CASE(STATE_OPEN);
STATE_CASE(STATE_HALF_CLOSED_LOCAL_UNCLAIMED);
STATE_CASE(STATE_HALF_CLOSED_LOCAL);
STATE_CASE(STATE_CLOSED);
default:
description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
state);
break;
}
return description;
}
#undef STATE_CASE
} // namespace net
|