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/server/http_server.h" | 5 #include "net/server/http_server.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" |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 delegate_->OnWebSocketMessage(connection->id(), message); | 225 delegate_->OnWebSocketMessage(connection->id(), message); |
226 if (HasClosedConnection(connection)) | 226 if (HasClosedConnection(connection)) |
227 return ERR_CONNECTION_CLOSED; | 227 return ERR_CONNECTION_CLOSED; |
228 continue; | 228 continue; |
229 } | 229 } |
230 | 230 |
231 HttpServerRequestInfo request; | 231 HttpServerRequestInfo request; |
232 size_t pos = 0; | 232 size_t pos = 0; |
233 if (!ParseHeaders(read_buf->StartOfBuffer(), read_buf->GetSize(), | 233 if (!ParseHeaders(read_buf->StartOfBuffer(), read_buf->GetSize(), |
234 &request, &pos)) { | 234 &request, &pos)) { |
| 235 // An error has occured. Close the connection. |
| 236 Close(connection->id()); |
| 237 return ERR_CONNECTION_CLOSED; |
| 238 } else if (!pos) { |
| 239 // If pos is 0, all the data in read_buf has been consumed, but the |
| 240 // headers have not been fully parsed yet. Continue parsing when more data |
| 241 // rolls in. |
235 break; | 242 break; |
236 } | 243 } |
237 | 244 |
238 // Sets peer address if exists. | 245 // Sets peer address if exists. |
239 connection->socket()->GetPeerAddress(&request.peer); | 246 connection->socket()->GetPeerAddress(&request.peer); |
240 | 247 |
241 if (request.HasHeaderValue("connection", "upgrade")) { | 248 if (request.HasHeaderValue("connection", "upgrade")) { |
242 connection->SetWebSocket(base::MakeUnique<WebSocket>(this, connection)); | 249 connection->SetWebSocket(base::MakeUnique<WebSocket>(this, connection)); |
243 read_buf->DidConsume(pos); | 250 read_buf->DidConsume(pos); |
244 delegate_->OnWebSocketRequest(connection->id(), request); | 251 delegate_->OnWebSocketRequest(connection->id(), request); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 switch (state) { | 405 switch (state) { |
399 case ST_METHOD: | 406 case ST_METHOD: |
400 info->method = buffer; | 407 info->method = buffer; |
401 buffer.clear(); | 408 buffer.clear(); |
402 break; | 409 break; |
403 case ST_URL: | 410 case ST_URL: |
404 info->path = buffer; | 411 info->path = buffer; |
405 buffer.clear(); | 412 buffer.clear(); |
406 break; | 413 break; |
407 case ST_PROTO: | 414 case ST_PROTO: |
408 // TODO(mbelshe): Deal better with parsing protocol. | 415 if (buffer != "HTTP/1.1") { |
409 DCHECK(buffer == "HTTP/1.1"); | 416 LOG(ERROR) << "Cannot handle request with protocol: " << buffer; |
| 417 next_state = ST_ERR; |
| 418 } |
410 buffer.clear(); | 419 buffer.clear(); |
411 break; | 420 break; |
412 case ST_NAME: | 421 case ST_NAME: |
413 header_name = base::ToLowerASCII(buffer); | 422 header_name = base::ToLowerASCII(buffer); |
414 buffer.clear(); | 423 buffer.clear(); |
415 break; | 424 break; |
416 case ST_VALUE: | 425 case ST_VALUE: |
417 base::TrimWhitespaceASCII(buffer, base::TRIM_LEADING, &header_value); | 426 base::TrimWhitespaceASCII(buffer, base::TRIM_LEADING, &header_value); |
418 it = info->headers.find(header_name); | 427 it = info->headers.find(header_name); |
419 // See the second paragraph ("A sender MUST NOT generate multiple | 428 // See the second paragraph ("A sender MUST NOT generate multiple |
(...skipping 21 matching lines...) Expand all Loading... |
441 buffer.append(&ch, 1); | 450 buffer.append(&ch, 1); |
442 break; | 451 break; |
443 case ST_DONE: | 452 case ST_DONE: |
444 DCHECK(input == INPUT_LF); | 453 DCHECK(input == INPUT_LF); |
445 return true; | 454 return true; |
446 case ST_ERR: | 455 case ST_ERR: |
447 return false; | 456 return false; |
448 } | 457 } |
449 } | 458 } |
450 } | 459 } |
451 // No more characters, but we haven't finished parsing yet. | 460 // No more characters, but we haven't finished parsing yet. Signal this to |
452 return false; | 461 // the caller by setting |pos| to zero. |
| 462 pos = 0; |
| 463 return true; |
453 } | 464 } |
454 | 465 |
455 HttpConnection* HttpServer::FindConnection(int connection_id) { | 466 HttpConnection* HttpServer::FindConnection(int connection_id) { |
456 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id); | 467 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id); |
457 if (it == id_to_connection_.end()) | 468 if (it == id_to_connection_.end()) |
458 return NULL; | 469 return NULL; |
459 return it->second; | 470 return it->second; |
460 } | 471 } |
461 | 472 |
462 // This is called after any delegate callbacks are called to check if Close() | 473 // This is called after any delegate callbacks are called to check if Close() |
463 // has been called during callback processing. Using the pointer of connection, | 474 // has been called during callback processing. Using the pointer of connection, |
464 // |connection| is safe here because Close() deletes the connection in next run | 475 // |connection| is safe here because Close() deletes the connection in next run |
465 // loop. | 476 // loop. |
466 bool HttpServer::HasClosedConnection(HttpConnection* connection) { | 477 bool HttpServer::HasClosedConnection(HttpConnection* connection) { |
467 return FindConnection(connection->id()) != connection; | 478 return FindConnection(connection->id()) != connection; |
468 } | 479 } |
469 | 480 |
470 } // namespace net | 481 } // namespace net |
OLD | NEW |