Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/url_request/sdch_dictionary_fetcher.h" | 5 #include "net/url_request/sdch_dictionary_fetcher.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/auto_reset.h" | 11 #include "base/auto_reset.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 17 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 18 #include "net/base/sdch_net_log_params.h" | 18 #include "net/base/sdch_net_log_params.h" |
| 19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 20 #include "net/log/net_log.h" | 20 #include "net/log/net_log.h" |
| 21 #include "net/url_request/redirect_info.h" | |
| 21 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
| 22 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
| 23 #include "net/url_request/url_request_throttler_manager.h" | 24 #include "net/url_request/url_request_throttler_manager.h" |
| 24 | 25 |
| 25 namespace net { | 26 namespace net { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const int kBufferSize = 4096; | 30 const int kBufferSize = 4096; |
| 30 | 31 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 | 137 |
| 137 void SdchDictionaryFetcher::Cancel() { | 138 void SdchDictionaryFetcher::Cancel() { |
| 138 DCHECK(CalledOnValidThread()); | 139 DCHECK(CalledOnValidThread()); |
| 139 | 140 |
| 140 ResetRequest(); | 141 ResetRequest(); |
| 141 next_state_ = STATE_NONE; | 142 next_state_ = STATE_NONE; |
| 142 | 143 |
| 143 fetch_queue_->Clear(); | 144 fetch_queue_->Clear(); |
| 144 } | 145 } |
| 145 | 146 |
| 147 void SdchDictionaryFetcher::OnReceivedRedirect( | |
| 148 URLRequest* request, | |
| 149 const RedirectInfo& redirect_info, | |
| 150 bool* defer_redirect) { | |
| 151 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_PENDING); | |
| 152 | |
| 153 redirect_url_ = redirect_info.new_url; | |
| 154 next_state_ = STATE_RECEIVED_REDIRECT; | |
| 155 | |
| 156 DoLoop(OK); | |
| 157 } | |
| 158 | |
| 146 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { | 159 void SdchDictionaryFetcher::OnResponseStarted(URLRequest* request) { |
| 147 DCHECK(CalledOnValidThread()); | 160 DCHECK(CalledOnValidThread()); |
| 148 DCHECK_EQ(request, current_request_.get()); | 161 DCHECK_EQ(request, current_request_.get()); |
| 149 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_COMPLETE); | 162 DCHECK_EQ(next_state_, STATE_SEND_REQUEST_PENDING); |
| 150 DCHECK(!in_loop_); | 163 DCHECK(!in_loop_); |
| 151 | 164 |
| 152 // Confirm that the response isn't a stale read from the cache (as | 165 // Confirm that the response isn't a stale read from the cache (as |
| 153 // may happen in the reload case). If the response was not retrieved over | 166 // may happen in the reload case). If the response was not retrieved over |
| 154 // HTTP, it is presumed to be fresh. | 167 // HTTP, it is presumed to be fresh. |
| 155 HttpResponseHeaders* response_headers = request->response_headers(); | 168 HttpResponseHeaders* response_headers = request->response_headers(); |
| 156 int result = request->status().error(); | 169 int result = request->status().error(); |
| 157 if (result == OK && response_headers) { | 170 if (result == OK && response_headers) { |
| 158 ValidationType validation_type = response_headers->RequiresValidation( | 171 ValidationType validation_type = response_headers->RequiresValidation( |
| 159 request->response_info().request_time, | 172 request->response_info().request_time, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 DCHECK(!in_loop_); | 234 DCHECK(!in_loop_); |
| 222 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); | 235 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); |
| 223 | 236 |
| 224 do { | 237 do { |
| 225 State state = next_state_; | 238 State state = next_state_; |
| 226 next_state_ = STATE_NONE; | 239 next_state_ = STATE_NONE; |
| 227 switch (state) { | 240 switch (state) { |
| 228 case STATE_SEND_REQUEST: | 241 case STATE_SEND_REQUEST: |
| 229 rv = DoSendRequest(rv); | 242 rv = DoSendRequest(rv); |
| 230 break; | 243 break; |
| 231 case STATE_SEND_REQUEST_COMPLETE: | 244 case STATE_RECEIVED_REDIRECT: |
| 232 rv = DoSendRequestComplete(rv); | 245 rv = DoReceivedRedirect(rv); |
| 246 break; | |
| 247 case STATE_SEND_REQUEST_PENDING: | |
| 248 rv = DoSendRequestPending(rv); | |
| 233 break; | 249 break; |
| 234 case STATE_READ_BODY: | 250 case STATE_READ_BODY: |
| 235 rv = DoReadBody(rv); | 251 rv = DoReadBody(rv); |
| 236 break; | 252 break; |
| 237 case STATE_READ_BODY_COMPLETE: | 253 case STATE_READ_BODY_COMPLETE: |
| 238 rv = DoReadBodyComplete(rv); | 254 rv = DoReadBodyComplete(rv); |
| 239 break; | 255 break; |
| 240 case STATE_REQUEST_COMPLETE: | 256 case STATE_REQUEST_COMPLETE: |
| 241 rv = DoCompleteRequest(rv); | 257 rv = DoCompleteRequest(rv); |
| 242 break; | 258 break; |
| 243 case STATE_NONE: | 259 case STATE_NONE: |
| 244 NOTREACHED(); | 260 NOTREACHED(); |
| 245 } | 261 } |
| 246 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 262 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 247 | 263 |
| 248 return rv; | 264 return rv; |
| 249 } | 265 } |
| 250 | 266 |
| 251 int SdchDictionaryFetcher::DoSendRequest(int rv) { | 267 int SdchDictionaryFetcher::DoSendRequest(int rv) { |
| 252 DCHECK(CalledOnValidThread()); | 268 DCHECK(CalledOnValidThread()); |
| 253 | 269 |
| 254 // |rv| is ignored, as the result from the previous request doesn't | 270 // |rv| is ignored, as the result from the previous request doesn't |
| 255 // affect the next request. | 271 // affect the next request. |
| 256 | 272 |
| 257 if (fetch_queue_->IsEmpty() || current_request_.get()) { | 273 if (fetch_queue_->IsEmpty() || current_request_.get()) { |
| 258 next_state_ = STATE_NONE; | 274 next_state_ = STATE_NONE; |
| 259 return OK; | 275 return OK; |
| 260 } | 276 } |
| 261 | 277 |
| 262 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 278 next_state_ = STATE_SEND_REQUEST_PENDING; |
| 263 | 279 |
| 264 FetchInfo info; | 280 FetchInfo info; |
| 265 bool success = fetch_queue_->Pop(&info); | 281 bool success = fetch_queue_->Pop(&info); |
| 266 DCHECK(success); | 282 DCHECK(success); |
| 267 current_request_ = context_->CreateRequest(info.url, IDLE, this); | 283 current_request_ = context_->CreateRequest(info.url, IDLE, this); |
| 268 int load_flags = LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES; | 284 int load_flags = LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES; |
| 269 if (info.cache_only) | 285 if (info.cache_only) |
| 270 load_flags |= LOAD_ONLY_FROM_CACHE; | 286 load_flags |= LOAD_ONLY_FROM_CACHE; |
| 271 current_request_->SetLoadFlags(load_flags); | 287 current_request_->SetLoadFlags(load_flags); |
| 272 | 288 |
| 273 buffer_ = new IOBuffer(kBufferSize); | 289 buffer_ = new IOBuffer(kBufferSize); |
| 274 current_callback_ = info.callback; | 290 current_callback_ = info.callback; |
| 275 | 291 |
| 276 current_request_->Start(); | 292 current_request_->Start(); |
| 277 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); | 293 current_request_->net_log().AddEvent(NetLog::TYPE_SDCH_DICTIONARY_FETCH); |
| 278 | 294 |
| 279 return ERR_IO_PENDING; | 295 return ERR_IO_PENDING; |
| 280 } | 296 } |
| 281 | 297 |
| 282 int SdchDictionaryFetcher::DoSendRequestComplete(int rv) { | 298 int SdchDictionaryFetcher::DoReceivedRedirect(int rv) { |
| 299 // Don't allow redirect to go cross-origin or cross-port. This test also | |
| 300 // forbids going cross-scheme, which is probably not necessary, but will | |
| 301 // be necessary when/if SDCH is restricted to HTTPS. | |
|
Ryan Sleevi
2016/04/13 00:30:44
The way this comment is worded, I think you're con
Mike West
2016/04/13 15:49:31
How are we not already restricting SDCH to HTTPS?
| |
| 302 if (redirect_url_.GetOrigin() != current_request_->url().GetOrigin()) { | |
|
eroman
2016/04/13 00:12:38
+rsleevi for security perspective, although mkwst
Ryan Sleevi
2016/04/13 00:30:44
Right, Mike is definitely the better reviewer here
Mike West
2016/04/13 10:54:07
1. I agree with Ryan that a spec which explained h
Mike West
2016/04/13 15:49:31
On the narrow question of this code: I'd suggest s
| |
| 303 current_request_->Cancel(); | |
|
eroman
2016/04/13 00:12:38
I think this is redundant -- calling ResetRequest(
Randy Smith (Not in Mondays)
2016/04/13 18:18:46
Yeah, I think you're right. Good point.
| |
| 304 ResetRequest(); | |
| 305 next_state_ = STATE_SEND_REQUEST; | |
|
eroman
2016/04/13 00:12:38
Shouldn't there be a return statement here, and er
Randy Smith (Not in Mondays)
2016/04/13 18:18:46
Ooops. Yes, there should. That suggests my tests
| |
| 306 } | |
| 307 next_state_ = STATE_SEND_REQUEST_PENDING; | |
| 308 return ERR_IO_PENDING; | |
| 309 } | |
| 310 | |
| 311 int SdchDictionaryFetcher::DoSendRequestPending(int rv) { | |
| 283 DCHECK(CalledOnValidThread()); | 312 DCHECK(CalledOnValidThread()); |
| 284 | 313 |
| 285 // If there's been an error, abort the current request. | 314 // If there's been an error, abort the current request. |
| 286 if (rv != OK) { | 315 if (rv != OK) { |
| 287 current_request_.reset(); | 316 current_request_.reset(); |
| 288 buffer_ = NULL; | 317 buffer_ = NULL; |
| 289 next_state_ = STATE_SEND_REQUEST; | 318 next_state_ = STATE_SEND_REQUEST; |
| 290 | 319 |
| 291 return OK; | 320 return OK; |
| 292 } | 321 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 current_request_->net_log(), | 377 current_request_->net_log(), |
| 349 current_request_->was_cached()); | 378 current_request_->was_cached()); |
| 350 } | 379 } |
| 351 | 380 |
| 352 ResetRequest(); | 381 ResetRequest(); |
| 353 next_state_ = STATE_SEND_REQUEST; | 382 next_state_ = STATE_SEND_REQUEST; |
| 354 return OK; | 383 return OK; |
| 355 } | 384 } |
| 356 | 385 |
| 357 } // namespace net | 386 } // namespace net |
| OLD | NEW |