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/quic/quic_http_stream.h" | 5 #include "net/quic/quic_http_stream.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 QuicHttpStream::~QuicHttpStream() { | 42 QuicHttpStream::~QuicHttpStream() { |
43 Close(false); | 43 Close(false); |
44 } | 44 } |
45 | 45 |
46 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info, | 46 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info, |
47 const BoundNetLog& stream_net_log, | 47 const BoundNetLog& stream_net_log, |
48 const CompletionCallback& callback) { | 48 const CompletionCallback& callback) { |
49 CHECK(stream_); | 49 CHECK(stream_); |
50 | 50 |
| 51 stream_net_log_ = stream_net_log; |
51 request_info_ = request_info; | 52 request_info_ = request_info; |
52 | 53 |
53 return OK; | 54 return OK; |
54 } | 55 } |
55 | 56 |
56 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers, | 57 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers, |
57 HttpResponseInfo* response, | 58 HttpResponseInfo* response, |
58 const CompletionCallback& callback) { | 59 const CompletionCallback& callback) { |
59 CHECK(stream_); | 60 CHECK(stream_); |
60 CHECK(!request_body_stream_); | 61 CHECK(!request_body_stream_); |
61 CHECK(!response_info_); | 62 CHECK(!response_info_); |
62 CHECK(!callback.is_null()); | 63 CHECK(!callback.is_null()); |
63 CHECK(response); | 64 CHECK(response); |
64 | 65 |
65 // Store the serialized request headers. | 66 // Store the serialized request headers. |
66 if (use_spdy_) { | 67 if (use_spdy_) { |
67 SpdyHeaderBlock headers; | 68 SpdyHeaderBlock headers; |
68 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers, | 69 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers, |
69 &headers, 3, /*direct=*/true); | 70 &headers, 3, /*direct=*/true); |
70 size_t len = SpdyFramer::GetSerializedLength(3, &headers); | 71 size_t len = SpdyFramer::GetSerializedLength(3, &headers); |
71 SpdyFrameBuilder builder(len); | 72 SpdyFrameBuilder builder(len); |
72 SpdyFramer::WriteHeaderBlock(&builder, 3, &headers); | 73 SpdyFramer::WriteHeaderBlock(&builder, 3, &headers); |
73 scoped_ptr<SpdyFrame> frame(builder.take()); | 74 scoped_ptr<SpdyFrame> frame(builder.take()); |
74 request_ = std::string(frame->data(), len); | 75 request_ = std::string(frame->data(), len); |
| 76 // Log the actual request with the URL Request's net log. |
| 77 stream_net_log_.AddEvent( |
| 78 NetLog::TYPE_HTTP_TRANSACTION_SPDY_SEND_REQUEST_HEADERS, |
| 79 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers)); |
| 80 // Also log to the QuicSession's net log. |
| 81 stream_->net_log().AddEvent( |
| 82 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS, |
| 83 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers)); |
75 } else { | 84 } else { |
76 std::string path = HttpUtil::PathForRequest(request_info_->url); | 85 std::string path = HttpUtil::PathForRequest(request_info_->url); |
77 std::string first_line = base::StringPrintf("%s %s HTTP/1.1\r\n", | 86 std::string first_line = base::StringPrintf("%s %s HTTP/1.1\r\n", |
78 request_info_->method.c_str(), | 87 request_info_->method.c_str(), |
79 path.c_str()); | 88 path.c_str()); |
80 request_ = first_line + request_headers.ToString(); | 89 request_ = first_line + request_headers.ToString(); |
| 90 // Log the actual request with the URL Request's net log. |
| 91 stream_net_log_.AddEvent( |
| 92 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 93 base::Bind(&HttpRequestHeaders::NetLogCallback, |
| 94 base::Unretained(&request_headers), |
| 95 &first_line)); |
| 96 // Also log to the QuicSession's net log. |
| 97 stream_->net_log().AddEvent( |
| 98 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS, |
| 99 base::Bind(&HttpRequestHeaders::NetLogCallback, |
| 100 base::Unretained(&request_headers), |
| 101 &first_line)); |
81 } | 102 } |
82 | 103 |
83 // Store the request body. | 104 // Store the request body. |
84 request_body_stream_ = request_info_->upload_data_stream; | 105 request_body_stream_ = request_info_->upload_data_stream; |
85 if (request_body_stream_ && (request_body_stream_->size() || | 106 if (request_body_stream_ && (request_body_stream_->size() || |
86 request_body_stream_->is_chunked())) { | 107 request_body_stream_->is_chunked())) { |
87 // Use kMaxPacketSize as the buffer size, since the request | 108 // Use kMaxPacketSize as the buffer size, since the request |
88 // body data is written with this size at a time. | 109 // body data is written with this size at a time. |
89 // TODO(rch): use a smarter value since we can't write an entire | 110 // TODO(rch): use a smarter value since we can't write an entire |
90 // packet due to overhead. | 111 // packet due to overhead. |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 if (len == 0) { | 472 if (len == 0) { |
452 return ERR_IO_PENDING; | 473 return ERR_IO_PENDING; |
453 } | 474 } |
454 | 475 |
455 // Save the remaining received data. | 476 // Save the remaining received data. |
456 size_t delta = read_buf_len - len; | 477 size_t delta = read_buf_len - len; |
457 if (delta > 0) { | 478 if (delta > 0) { |
458 BufferResponseBody(read_buf_->data(), delta); | 479 BufferResponseBody(read_buf_->data(), delta); |
459 } | 480 } |
460 | 481 |
| 482 // The URLRequest logs these headers, so only log to the QuicSession's |
| 483 // net log. |
| 484 stream_->net_log().AddEvent( |
| 485 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS, |
| 486 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers)); |
| 487 |
461 SpdyHeadersToHttpResponse(headers, 3, response_info_); | 488 SpdyHeadersToHttpResponse(headers, 3, response_info_); |
462 // Put the peer's IP address and port into the response. | 489 // Put the peer's IP address and port into the response. |
463 IPEndPoint address = stream_->GetPeerAddress(); | 490 IPEndPoint address = stream_->GetPeerAddress(); |
464 response_info_->socket_address = HostPortPair::FromIPEndPoint(address); | 491 response_info_->socket_address = HostPortPair::FromIPEndPoint(address); |
465 response_info_->vary_data.Init(*request_info_, *response_info_->headers); | 492 response_info_->vary_data.Init(*request_info_, *response_info_->headers); |
466 response_headers_received_ = true; | 493 response_headers_received_ = true; |
467 | 494 |
468 return OK; | 495 return OK; |
469 } | 496 } |
470 int end_offset = HttpUtil::LocateEndOfHeaders(read_buf_->StartOfBuffer(), | 497 int end_offset = HttpUtil::LocateEndOfHeaders(read_buf_->StartOfBuffer(), |
471 read_buf_->offset(), 0); | 498 read_buf_->offset(), 0); |
472 | 499 |
473 if (end_offset == -1) { | 500 if (end_offset == -1) { |
474 return ERR_IO_PENDING; | 501 return ERR_IO_PENDING; |
475 } | 502 } |
476 | 503 |
477 if (!stream_) | 504 if (!stream_) |
478 return ERR_UNEXPECTED; | 505 return ERR_UNEXPECTED; |
479 | 506 |
480 scoped_refptr<HttpResponseHeaders> headers = new HttpResponseHeaders( | 507 scoped_refptr<HttpResponseHeaders> headers = new HttpResponseHeaders( |
481 HttpUtil::AssembleRawHeaders(read_buf_->StartOfBuffer(), end_offset)); | 508 HttpUtil::AssembleRawHeaders(read_buf_->StartOfBuffer(), end_offset)); |
482 | 509 |
483 // Put the peer's IP address and port into the response. | 510 // Put the peer's IP address and port into the response. |
484 IPEndPoint address = stream_->GetPeerAddress(); | 511 IPEndPoint address = stream_->GetPeerAddress(); |
485 response_info_->socket_address = HostPortPair::FromIPEndPoint(address); | 512 response_info_->socket_address = HostPortPair::FromIPEndPoint(address); |
486 response_info_->headers = headers; | 513 response_info_->headers = headers; |
487 response_info_->vary_data.Init(*request_info_, *response_info_->headers); | 514 response_info_->vary_data.Init(*request_info_, *response_info_->headers); |
488 response_headers_received_ = true; | 515 response_headers_received_ = true; |
489 | 516 |
| 517 // The URLRequest logs these headers, so only log to the QuicSession's |
| 518 // net log. |
| 519 stream_->net_log().AddEvent( |
| 520 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS, |
| 521 base::Bind(&HttpResponseHeaders::NetLogCallback, |
| 522 response_info_->headers)); |
| 523 |
490 // Save the remaining received data. | 524 // Save the remaining received data. |
491 int delta = read_buf_->offset() - end_offset; | 525 int delta = read_buf_->offset() - end_offset; |
492 if (delta > 0) { | 526 if (delta > 0) { |
493 BufferResponseBody(read_buf_->data(), delta); | 527 BufferResponseBody(read_buf_->data(), delta); |
494 } | 528 } |
495 | 529 |
496 return OK; | 530 return OK; |
497 } | 531 } |
498 | 532 |
499 void QuicHttpStream::BufferResponseBody(const char* data, int length) { | 533 void QuicHttpStream::BufferResponseBody(const char* data, int length) { |
500 if (length == 0) | 534 if (length == 0) |
501 return; | 535 return; |
502 IOBufferWithSize* io_buffer = new IOBufferWithSize(length); | 536 IOBufferWithSize* io_buffer = new IOBufferWithSize(length); |
503 memcpy(io_buffer->data(), data, length); | 537 memcpy(io_buffer->data(), data, length); |
504 response_body_.push_back(make_scoped_refptr(io_buffer)); | 538 response_body_.push_back(make_scoped_refptr(io_buffer)); |
505 } | 539 } |
506 | 540 |
507 } // namespace net | 541 } // namespace net |
OLD | NEW |