OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/md5.h" | 7 #include "base/md5.h" |
8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 original_length_ = input.size(); | 167 original_length_ = input.size(); |
168 return true; | 168 return true; |
169 } | 169 } |
170 | 170 |
171 size_t WebSocketHandshakeRequestHandler::original_length() const { | 171 size_t WebSocketHandshakeRequestHandler::original_length() const { |
172 return original_length_; | 172 return original_length_; |
173 } | 173 } |
174 | 174 |
175 void WebSocketHandshakeRequestHandler::AppendHeaderIfMissing( | 175 void WebSocketHandshakeRequestHandler::AppendHeaderIfMissing( |
176 const std::string& name, const std::string& value) { | 176 const std::string& name, const std::string& value) { |
177 DCHECK(headers_.size() > 0); | 177 DCHECK(!headers_.empty()); |
178 HttpUtil::AppendHeaderIfMissing(name.c_str(), value, &headers_); | 178 HttpUtil::AppendHeaderIfMissing(name.c_str(), value, &headers_); |
179 } | 179 } |
180 | 180 |
181 void WebSocketHandshakeRequestHandler::RemoveHeaders( | 181 void WebSocketHandshakeRequestHandler::RemoveHeaders( |
182 const char* const headers_to_remove[], | 182 const char* const headers_to_remove[], |
183 size_t headers_to_remove_len) { | 183 size_t headers_to_remove_len) { |
184 DCHECK(headers_.size() > 0); | 184 DCHECK(!headers_.empty()); |
185 headers_ = FilterHeaders( | 185 headers_ = FilterHeaders( |
186 headers_, headers_to_remove, headers_to_remove_len); | 186 headers_, headers_to_remove, headers_to_remove_len); |
187 } | 187 } |
188 | 188 |
189 HttpRequestInfo WebSocketHandshakeRequestHandler::GetRequestInfo( | 189 HttpRequestInfo WebSocketHandshakeRequestHandler::GetRequestInfo( |
190 const GURL& url, std::string* challenge) { | 190 const GURL& url, std::string* challenge) { |
191 HttpRequestInfo request_info; | 191 HttpRequestInfo request_info; |
192 request_info.url = url; | 192 request_info.url = url; |
193 base::StringPiece method = status_line_.data(); | 193 base::StringPiece method = status_line_.data(); |
194 size_t method_end = base::StringPiece( | 194 size_t method_end = base::StringPiece( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 260 |
261 challenge->clear(); | 261 challenge->clear(); |
262 GetKeyNumber(key1, challenge); | 262 GetKeyNumber(key1, challenge); |
263 GetKeyNumber(key2, challenge); | 263 GetKeyNumber(key2, challenge); |
264 challenge->append(key3_); | 264 challenge->append(key3_); |
265 | 265 |
266 return true; | 266 return true; |
267 } | 267 } |
268 | 268 |
269 std::string WebSocketHandshakeRequestHandler::GetRawRequest() { | 269 std::string WebSocketHandshakeRequestHandler::GetRawRequest() { |
270 DCHECK(status_line_.size() > 0); | 270 DCHECK(!status_line_.empty()); |
271 DCHECK(headers_.size() > 0); | 271 DCHECK(!headers_.empty()); |
272 DCHECK_EQ(kRequestKey3Size, key3_.size()); | 272 DCHECK_EQ(kRequestKey3Size, key3_.size()); |
273 std::string raw_request = status_line_ + headers_ + "\r\n" + key3_; | 273 std::string raw_request = status_line_ + headers_ + "\r\n" + key3_; |
274 raw_length_ = raw_request.size(); | 274 raw_length_ = raw_request.size(); |
275 return raw_request; | 275 return raw_request; |
276 } | 276 } |
277 | 277 |
278 size_t WebSocketHandshakeRequestHandler::raw_length() const { | 278 size_t WebSocketHandshakeRequestHandler::raw_length() const { |
279 DCHECK_GT(raw_length_, 0); | 279 DCHECK_GT(raw_length_, 0); |
280 return raw_length_; | 280 return raw_length_; |
281 } | 281 } |
282 | 282 |
283 WebSocketHandshakeResponseHandler::WebSocketHandshakeResponseHandler() | 283 WebSocketHandshakeResponseHandler::WebSocketHandshakeResponseHandler() |
284 : original_header_length_(0) { | 284 : original_header_length_(0) { |
285 } | 285 } |
286 | 286 |
287 WebSocketHandshakeResponseHandler::~WebSocketHandshakeResponseHandler() {} | 287 WebSocketHandshakeResponseHandler::~WebSocketHandshakeResponseHandler() {} |
288 | 288 |
289 size_t WebSocketHandshakeResponseHandler::ParseRawResponse( | 289 size_t WebSocketHandshakeResponseHandler::ParseRawResponse( |
290 const char* data, int length) { | 290 const char* data, int length) { |
291 DCHECK_GT(length, 0); | 291 DCHECK_GT(length, 0); |
292 if (HasResponse()) { | 292 if (HasResponse()) { |
293 DCHECK(status_line_.size() > 0); | 293 DCHECK(!status_line_.empty()); |
294 DCHECK(headers_.size() > 0); | 294 DCHECK(!headers_.empty()); |
295 DCHECK_EQ(kResponseKeySize, key_.size()); | 295 DCHECK_EQ(kResponseKeySize, key_.size()); |
296 return 0; | 296 return 0; |
297 } | 297 } |
298 | 298 |
299 size_t old_original_length = original_.size(); | 299 size_t old_original_length = original_.size(); |
300 | 300 |
301 original_.append(data, length); | 301 original_.append(data, length); |
302 // TODO(ukai): fail fast when response gives wrong status code. | 302 // TODO(ukai): fail fast when response gives wrong status code. |
303 original_header_length_ = HttpUtil::LocateEndOfHeaders( | 303 original_header_length_ = HttpUtil::LocateEndOfHeaders( |
304 original_.data(), original_.size(), 0); | 304 original_.data(), original_.size(), 0); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 390 |
391 return ParseRawResponse(response_message.data(), | 391 return ParseRawResponse(response_message.data(), |
392 response_message.size()) == response_message.size(); | 392 response_message.size()) == response_message.size(); |
393 } | 393 } |
394 | 394 |
395 void WebSocketHandshakeResponseHandler::GetHeaders( | 395 void WebSocketHandshakeResponseHandler::GetHeaders( |
396 const char* const headers_to_get[], | 396 const char* const headers_to_get[], |
397 size_t headers_to_get_len, | 397 size_t headers_to_get_len, |
398 std::vector<std::string>* values) { | 398 std::vector<std::string>* values) { |
399 DCHECK(HasResponse()); | 399 DCHECK(HasResponse()); |
400 DCHECK(status_line_.size() > 0); | 400 DCHECK(!status_line_.empty()); |
401 DCHECK(headers_.size() > 0); | 401 DCHECK(!headers_.empty()); |
402 DCHECK_EQ(kResponseKeySize, key_.size()); | 402 DCHECK_EQ(kResponseKeySize, key_.size()); |
403 | 403 |
404 FetchHeaders(headers_, headers_to_get, headers_to_get_len, values); | 404 FetchHeaders(headers_, headers_to_get, headers_to_get_len, values); |
405 } | 405 } |
406 | 406 |
407 void WebSocketHandshakeResponseHandler::RemoveHeaders( | 407 void WebSocketHandshakeResponseHandler::RemoveHeaders( |
408 const char* const headers_to_remove[], | 408 const char* const headers_to_remove[], |
409 size_t headers_to_remove_len) { | 409 size_t headers_to_remove_len) { |
410 DCHECK(HasResponse()); | 410 DCHECK(HasResponse()); |
411 DCHECK(status_line_.size() > 0); | 411 DCHECK(!status_line_.empty()); |
412 DCHECK(headers_.size() > 0); | 412 DCHECK(!headers_.empty()); |
413 DCHECK_EQ(kResponseKeySize, key_.size()); | 413 DCHECK_EQ(kResponseKeySize, key_.size()); |
414 | 414 |
415 headers_ = FilterHeaders(headers_, headers_to_remove, headers_to_remove_len); | 415 headers_ = FilterHeaders(headers_, headers_to_remove, headers_to_remove_len); |
416 } | 416 } |
417 | 417 |
418 std::string WebSocketHandshakeResponseHandler::GetRawResponse() const { | 418 std::string WebSocketHandshakeResponseHandler::GetRawResponse() const { |
419 DCHECK(HasResponse()); | 419 DCHECK(HasResponse()); |
420 return std::string(original_.data(), | 420 return std::string(original_.data(), |
421 original_header_length_ + kResponseKeySize); | 421 original_header_length_ + kResponseKeySize); |
422 } | 422 } |
423 | 423 |
424 std::string WebSocketHandshakeResponseHandler::GetResponse() { | 424 std::string WebSocketHandshakeResponseHandler::GetResponse() { |
425 DCHECK(HasResponse()); | 425 DCHECK(HasResponse()); |
426 DCHECK(status_line_.size() > 0); | 426 DCHECK(!status_line_.empty()); |
427 // headers_ might be empty for wrong response from server. | 427 // headers_ might be empty for wrong response from server. |
428 DCHECK_EQ(kResponseKeySize, key_.size()); | 428 DCHECK_EQ(kResponseKeySize, key_.size()); |
429 | 429 |
430 return status_line_ + headers_ + header_separator_ + key_; | 430 return status_line_ + headers_ + header_separator_ + key_; |
431 } | 431 } |
432 | 432 |
433 } // namespace net | 433 } // namespace net |
OLD | NEW |