Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: net/server/http_server.cc

Issue 2314073003: Handle non-HTTP/1.1 requests more gracefully in net::HttpServer. (Closed)
Patch Set: More tests Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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, we read all the data in read_buf, but haven't yet finished
mmenke 2016/09/15 17:40:12 nit: avoid "we" in comments, due to ambiguity ("W
slan 2016/09/16 22:35:30 Done.
240 // parsing the headers. Continue parsing when more data rolls in.
235 break; 241 break;
236 } 242 }
237 243
238 // Sets peer address if exists. 244 // Sets peer address if exists.
239 connection->socket()->GetPeerAddress(&request.peer); 245 connection->socket()->GetPeerAddress(&request.peer);
240 246
241 if (request.HasHeaderValue("connection", "upgrade")) { 247 if (request.HasHeaderValue("connection", "upgrade")) {
242 connection->SetWebSocket(base::MakeUnique<WebSocket>(this, connection)); 248 connection->SetWebSocket(base::MakeUnique<WebSocket>(this, connection));
243 read_buf->DidConsume(pos); 249 read_buf->DidConsume(pos);
244 delegate_->OnWebSocketRequest(connection->id(), request); 250 delegate_->OnWebSocketRequest(connection->id(), request);
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 switch (state) { 404 switch (state) {
399 case ST_METHOD: 405 case ST_METHOD:
400 info->method = buffer; 406 info->method = buffer;
401 buffer.clear(); 407 buffer.clear();
402 break; 408 break;
403 case ST_URL: 409 case ST_URL:
404 info->path = buffer; 410 info->path = buffer;
405 buffer.clear(); 411 buffer.clear();
406 break; 412 break;
407 case ST_PROTO: 413 case ST_PROTO:
408 // TODO(mbelshe): Deal better with parsing protocol. 414 if (buffer != "HTTP/1.1") {
409 DCHECK(buffer == "HTTP/1.1"); 415 LOG(ERROR) << "Cannot handle request with protocol: " << buffer;
416 next_state = ST_ERR;
417 }
410 buffer.clear(); 418 buffer.clear();
411 break; 419 break;
412 case ST_NAME: 420 case ST_NAME:
413 header_name = base::ToLowerASCII(buffer); 421 header_name = base::ToLowerASCII(buffer);
414 buffer.clear(); 422 buffer.clear();
415 break; 423 break;
416 case ST_VALUE: 424 case ST_VALUE:
417 base::TrimWhitespaceASCII(buffer, base::TRIM_LEADING, &header_value); 425 base::TrimWhitespaceASCII(buffer, base::TRIM_LEADING, &header_value);
418 it = info->headers.find(header_name); 426 it = info->headers.find(header_name);
419 // See the second paragraph ("A sender MUST NOT generate multiple 427 // See the second paragraph ("A sender MUST NOT generate multiple
(...skipping 21 matching lines...) Expand all
441 buffer.append(&ch, 1); 449 buffer.append(&ch, 1);
442 break; 450 break;
443 case ST_DONE: 451 case ST_DONE:
444 DCHECK(input == INPUT_LF); 452 DCHECK(input == INPUT_LF);
445 return true; 453 return true;
446 case ST_ERR: 454 case ST_ERR:
447 return false; 455 return false;
448 } 456 }
449 } 457 }
450 } 458 }
451 // No more characters, but we haven't finished parsing yet. 459 // No more characters, but we haven't finished parsing yet. Signal this to
452 return false; 460 // the caller by setting |pos| to zero.
461 pos = 0;
462 return true;
453 } 463 }
454 464
455 HttpConnection* HttpServer::FindConnection(int connection_id) { 465 HttpConnection* HttpServer::FindConnection(int connection_id) {
456 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id); 466 IdToConnectionMap::iterator it = id_to_connection_.find(connection_id);
457 if (it == id_to_connection_.end()) 467 if (it == id_to_connection_.end())
458 return NULL; 468 return NULL;
459 return it->second; 469 return it->second;
460 } 470 }
461 471
462 // This is called after any delegate callbacks are called to check if Close() 472 // 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, 473 // has been called during callback processing. Using the pointer of connection,
464 // |connection| is safe here because Close() deletes the connection in next run 474 // |connection| is safe here because Close() deletes the connection in next run
465 // loop. 475 // loop.
466 bool HttpServer::HasClosedConnection(HttpConnection* connection) { 476 bool HttpServer::HasClosedConnection(HttpConnection* connection) {
467 return FindConnection(connection->id()) != connection; 477 return FindConnection(connection->id()) != connection;
468 } 478 }
469 479
470 } // namespace net 480 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698