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/websockets/websocket_handshake_handler.h" | 5 #include "net/websockets/websocket_handshake_handler.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/sha1.h" | 10 #include "base/sha1.h" |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 header_separator_ = std::string(original_.data() + header_size, | 338 header_separator_ = std::string(original_.data() + header_size, |
339 original_header_length_ - header_size); | 339 original_header_length_ - header_size); |
340 return original_header_length_ - old_original_length; | 340 return original_header_length_ - old_original_length; |
341 } | 341 } |
342 | 342 |
343 bool WebSocketHandshakeResponseHandler::HasResponse() const { | 343 bool WebSocketHandshakeResponseHandler::HasResponse() const { |
344 return original_header_length_ > 0 && | 344 return original_header_length_ > 0 && |
345 static_cast<size_t>(original_header_length_) <= original_.size(); | 345 static_cast<size_t>(original_header_length_) <= original_.size(); |
346 } | 346 } |
347 | 347 |
| 348 void ComputeSecWebSocketAccept(const std::string& key, |
| 349 std::string* accept) { |
| 350 DCHECK(accept); |
| 351 |
| 352 std::string hash = |
| 353 base::SHA1HashString(key + websockets::kWebSocketGuid); |
| 354 bool encode_success = base::Base64Encode(hash, accept); |
| 355 DCHECK(encode_success); |
| 356 } |
| 357 |
348 bool WebSocketHandshakeResponseHandler::ParseResponseInfo( | 358 bool WebSocketHandshakeResponseHandler::ParseResponseInfo( |
349 const HttpResponseInfo& response_info, | 359 const HttpResponseInfo& response_info, |
350 const std::string& challenge) { | 360 const std::string& challenge) { |
351 if (!response_info.headers.get()) | 361 if (!response_info.headers.get()) |
352 return false; | 362 return false; |
353 | 363 |
354 // TODO(ricea): Eliminate all the reallocations and string copies. | 364 // TODO(ricea): Eliminate all the reallocations and string copies. |
355 std::string response_message; | 365 std::string response_message; |
356 response_message = response_info.headers->GetStatusLine(); | 366 response_message = response_info.headers->GetStatusLine(); |
357 response_message += "\r\n"; | 367 response_message += "\r\n"; |
358 | 368 |
359 AppendHeader(websockets::kUpgrade, | 369 AppendHeader(websockets::kUpgrade, |
360 websockets::kWebSocketLowercase, | 370 websockets::kWebSocketLowercase, |
361 &response_message); | 371 &response_message); |
362 | 372 |
363 AppendHeader( | 373 AppendHeader( |
364 HttpRequestHeaders::kConnection, websockets::kUpgrade, &response_message); | 374 HttpRequestHeaders::kConnection, websockets::kUpgrade, &response_message); |
365 | 375 |
366 std::string hash = | |
367 base::SHA1HashString(challenge + websockets::kWebSocketGuid); | |
368 std::string websocket_accept; | 376 std::string websocket_accept; |
369 bool encode_success = base::Base64Encode(hash, &websocket_accept); | 377 ComputeSecWebSocketAccept(challenge, &websocket_accept); |
370 DCHECK(encode_success); | |
371 AppendHeader( | 378 AppendHeader( |
372 websockets::kSecWebSocketAccept, websocket_accept, &response_message); | 379 websockets::kSecWebSocketAccept, websocket_accept, &response_message); |
373 | 380 |
374 void* iter = NULL; | 381 void* iter = NULL; |
375 std::string name; | 382 std::string name; |
376 std::string value; | 383 std::string value; |
377 while (response_info.headers->EnumerateHeaderLines(&iter, &name, &value)) { | 384 while (response_info.headers->EnumerateHeaderLines(&iter, &name, &value)) { |
378 AppendHeader(name, value, &response_message); | 385 AppendHeader(name, value, &response_message); |
379 } | 386 } |
380 response_message += "\r\n"; | 387 response_message += "\r\n"; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 491 |
485 std::string WebSocketHandshakeResponseHandler::GetResponse() { | 492 std::string WebSocketHandshakeResponseHandler::GetResponse() { |
486 DCHECK(HasResponse()); | 493 DCHECK(HasResponse()); |
487 DCHECK(!status_line_.empty()); | 494 DCHECK(!status_line_.empty()); |
488 // headers_ might be empty for wrong response from server. | 495 // headers_ might be empty for wrong response from server. |
489 | 496 |
490 return status_line_ + headers_ + header_separator_; | 497 return status_line_ + headers_ + header_separator_; |
491 } | 498 } |
492 | 499 |
493 } // namespace net | 500 } // namespace net |
OLD | NEW |