| 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/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/profiler/scoped_tracker.h" | 13 #include "base/profiler/scoped_tracker.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 18 #include "net/base/upload_data_stream.h" | 18 #include "net/base/upload_data_stream.h" |
| 19 #include "net/http/http_chunked_decoder.h" | 19 #include "net/http/http_chunked_decoder.h" |
| 20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_request_info.h" | 21 #include "net/http/http_request_info.h" |
| 22 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
| 23 #include "net/http/http_status_line_validator.h" | 23 #include "net/http/http_status_line_validator.h" |
| 24 #include "net/http/http_util.h" | 24 #include "net/http/http_util.h" |
| 25 #include "net/log/net_log_event_type.h" |
| 25 #include "net/socket/client_socket_handle.h" | 26 #include "net/socket/client_socket_handle.h" |
| 26 #include "net/socket/ssl_client_socket.h" | 27 #include "net/socket/ssl_client_socket.h" |
| 27 #include "net/ssl/token_binding.h" | 28 #include "net/ssl/token_binding.h" |
| 28 #include "url/url_canon.h" | 29 #include "url/url_canon.h" |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 enum HttpHeaderParserEvent { | 35 enum HttpHeaderParserEvent { |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 235 |
| 235 int HttpStreamParser::SendRequest(const std::string& request_line, | 236 int HttpStreamParser::SendRequest(const std::string& request_line, |
| 236 const HttpRequestHeaders& headers, | 237 const HttpRequestHeaders& headers, |
| 237 HttpResponseInfo* response, | 238 HttpResponseInfo* response, |
| 238 const CompletionCallback& callback) { | 239 const CompletionCallback& callback) { |
| 239 DCHECK_EQ(STATE_NONE, io_state_); | 240 DCHECK_EQ(STATE_NONE, io_state_); |
| 240 DCHECK(callback_.is_null()); | 241 DCHECK(callback_.is_null()); |
| 241 DCHECK(!callback.is_null()); | 242 DCHECK(!callback.is_null()); |
| 242 DCHECK(response); | 243 DCHECK(response); |
| 243 | 244 |
| 244 net_log_.AddEvent( | 245 net_log_.AddEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 245 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, | 246 base::Bind(&HttpRequestHeaders::NetLogCallback, |
| 246 base::Bind(&HttpRequestHeaders::NetLogCallback, | 247 base::Unretained(&headers), &request_line)); |
| 247 base::Unretained(&headers), | |
| 248 &request_line)); | |
| 249 | 248 |
| 250 DVLOG(1) << __func__ << "() request_line = \"" << request_line << "\"" | 249 DVLOG(1) << __func__ << "() request_line = \"" << request_line << "\"" |
| 251 << " headers = \"" << headers.ToString() << "\""; | 250 << " headers = \"" << headers.ToString() << "\""; |
| 252 response_ = response; | 251 response_ = response; |
| 253 | 252 |
| 254 // Put the peer's IP address and port into the response. | 253 // Put the peer's IP address and port into the response. |
| 255 IPEndPoint ip_endpoint; | 254 IPEndPoint ip_endpoint; |
| 256 int result = connection_->socket()->GetPeerAddress(&ip_endpoint); | 255 int result = connection_->socket()->GetPeerAddress(&ip_endpoint); |
| 257 if (result != OK) | 256 if (result != OK) |
| 258 return result; | 257 return result; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 // Read() must succeed synchronously if not chunked and in memory. | 298 // Read() must succeed synchronously if not chunked and in memory. |
| 300 DCHECK_GT(consumed, 0); | 299 DCHECK_GT(consumed, 0); |
| 301 request_headers_->DidConsume(consumed); | 300 request_headers_->DidConsume(consumed); |
| 302 todo -= consumed; | 301 todo -= consumed; |
| 303 } | 302 } |
| 304 DCHECK(request_->upload_data_stream->IsEOF()); | 303 DCHECK(request_->upload_data_stream->IsEOF()); |
| 305 // Reset the offset, so the buffer can be read from the beginning. | 304 // Reset the offset, so the buffer can be read from the beginning. |
| 306 request_headers_->SetOffset(0); | 305 request_headers_->SetOffset(0); |
| 307 did_merge = true; | 306 did_merge = true; |
| 308 | 307 |
| 309 net_log_.AddEvent( | 308 net_log_.AddEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST_BODY, |
| 310 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY, | 309 base::Bind(&NetLogSendRequestBodyCallback, |
| 311 base::Bind(&NetLogSendRequestBodyCallback, | 310 request_->upload_data_stream->size(), |
| 312 request_->upload_data_stream->size(), | 311 false, /* not chunked */ |
| 313 false, /* not chunked */ | 312 true /* merged */)); |
| 314 true /* merged */)); | |
| 315 } | 313 } |
| 316 | 314 |
| 317 if (!did_merge) { | 315 if (!did_merge) { |
| 318 // If we didn't merge the body with the headers, then |request_headers_| | 316 // If we didn't merge the body with the headers, then |request_headers_| |
| 319 // contains just the HTTP headers. | 317 // contains just the HTTP headers. |
| 320 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); | 318 scoped_refptr<StringIOBuffer> headers_io_buf(new StringIOBuffer(request)); |
| 321 request_headers_ = | 319 request_headers_ = |
| 322 new DrainableIOBuffer(headers_io_buf.get(), headers_io_buf->size()); | 320 new DrainableIOBuffer(headers_io_buf.get(), headers_io_buf->size()); |
| 323 } | 321 } |
| 324 | 322 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 DCHECK_NE(STATE_NONE, io_state_); | 426 DCHECK_NE(STATE_NONE, io_state_); |
| 429 break; | 427 break; |
| 430 case STATE_SEND_REQUEST_READ_BODY_COMPLETE: | 428 case STATE_SEND_REQUEST_READ_BODY_COMPLETE: |
| 431 result = DoSendRequestReadBodyComplete(result); | 429 result = DoSendRequestReadBodyComplete(result); |
| 432 DCHECK_NE(STATE_NONE, io_state_); | 430 DCHECK_NE(STATE_NONE, io_state_); |
| 433 break; | 431 break; |
| 434 case STATE_SEND_REQUEST_COMPLETE: | 432 case STATE_SEND_REQUEST_COMPLETE: |
| 435 result = DoSendRequestComplete(result); | 433 result = DoSendRequestComplete(result); |
| 436 break; | 434 break; |
| 437 case STATE_READ_HEADERS: | 435 case STATE_READ_HEADERS: |
| 438 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS); | 436 net_log_.BeginEvent(NetLogEventType::HTTP_STREAM_PARSER_READ_HEADERS); |
| 439 DCHECK_GE(result, 0); | 437 DCHECK_GE(result, 0); |
| 440 result = DoReadHeaders(); | 438 result = DoReadHeaders(); |
| 441 break; | 439 break; |
| 442 case STATE_READ_HEADERS_COMPLETE: | 440 case STATE_READ_HEADERS_COMPLETE: |
| 443 result = DoReadHeadersComplete(result); | 441 result = DoReadHeadersComplete(result); |
| 444 net_log_.EndEventWithNetErrorCode( | 442 net_log_.EndEventWithNetErrorCode( |
| 445 NetLog::TYPE_HTTP_STREAM_PARSER_READ_HEADERS, result); | 443 NetLogEventType::HTTP_STREAM_PARSER_READ_HEADERS, result); |
| 446 break; | 444 break; |
| 447 case STATE_READ_BODY: | 445 case STATE_READ_BODY: |
| 448 DCHECK_GE(result, 0); | 446 DCHECK_GE(result, 0); |
| 449 result = DoReadBody(); | 447 result = DoReadBody(); |
| 450 break; | 448 break; |
| 451 case STATE_READ_BODY_COMPLETE: | 449 case STATE_READ_BODY_COMPLETE: |
| 452 result = DoReadBodyComplete(result); | 450 result = DoReadBodyComplete(result); |
| 453 break; | 451 break; |
| 454 default: | 452 default: |
| 455 NOTREACHED(); | 453 NOTREACHED(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 if (request_headers_->BytesRemaining() > 0) { | 498 if (request_headers_->BytesRemaining() > 0) { |
| 501 io_state_ = STATE_SEND_HEADERS; | 499 io_state_ = STATE_SEND_HEADERS; |
| 502 return OK; | 500 return OK; |
| 503 } | 501 } |
| 504 | 502 |
| 505 if (request_->upload_data_stream != NULL && | 503 if (request_->upload_data_stream != NULL && |
| 506 (request_->upload_data_stream->is_chunked() || | 504 (request_->upload_data_stream->is_chunked() || |
| 507 // !IsEOF() indicates that the body wasn't merged. | 505 // !IsEOF() indicates that the body wasn't merged. |
| 508 (request_->upload_data_stream->size() > 0 && | 506 (request_->upload_data_stream->size() > 0 && |
| 509 !request_->upload_data_stream->IsEOF()))) { | 507 !request_->upload_data_stream->IsEOF()))) { |
| 510 net_log_.AddEvent( | 508 net_log_.AddEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST_BODY, |
| 511 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_BODY, | 509 base::Bind(&NetLogSendRequestBodyCallback, |
| 512 base::Bind(&NetLogSendRequestBodyCallback, | 510 request_->upload_data_stream->size(), |
| 513 request_->upload_data_stream->size(), | 511 request_->upload_data_stream->is_chunked(), |
| 514 request_->upload_data_stream->is_chunked(), | 512 false /* not merged */)); |
| 515 false /* not merged */)); | |
| 516 io_state_ = STATE_SEND_BODY; | 513 io_state_ = STATE_SEND_BODY; |
| 517 return OK; | 514 return OK; |
| 518 } | 515 } |
| 519 | 516 |
| 520 // Finished sending the request. | 517 // Finished sending the request. |
| 521 io_state_ = STATE_SEND_REQUEST_COMPLETE; | 518 io_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 522 return OK; | 519 return OK; |
| 523 } | 520 } |
| 524 | 521 |
| 525 int HttpStreamParser::DoSendBody() { | 522 int HttpStreamParser::DoSendBody() { |
| (...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1232 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status, | 1229 UMA_HISTOGRAM_ENUMERATION("Net.HttpStatusLineStatus", status, |
| 1233 HttpStatusLineValidator::STATUS_LINE_MAX); | 1230 HttpStatusLineValidator::STATUS_LINE_MAX); |
| 1234 } | 1231 } |
| 1235 | 1232 |
| 1236 bool HttpStreamParser::SendRequestBuffersEmpty() { | 1233 bool HttpStreamParser::SendRequestBuffersEmpty() { |
| 1237 return request_headers_ == nullptr && request_body_send_buf_ == nullptr && | 1234 return request_headers_ == nullptr && request_body_send_buf_ == nullptr && |
| 1238 request_body_send_buf_ == nullptr; | 1235 request_body_send_buf_ == nullptr; |
| 1239 } | 1236 } |
| 1240 | 1237 |
| 1241 } // namespace net | 1238 } // namespace net |
| OLD | NEW |