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