Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/gdata/test_servers/http_connection.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/stringprintf.h" | |
| 10 #include "chrome/browser/chromeos/gdata/test_servers/http_request.h" | |
| 11 #include "chrome/browser/chromeos/gdata/test_servers/http_response.h" | |
| 12 | |
| 13 namespace gdata { | |
| 14 namespace test_servers { | |
| 15 | |
| 16 HttpConnection::HttpConnection(net::StreamListenSocket* socket, | |
| 17 HttpConnectionDelegate* delegate) : | |
| 18 socket_(socket), | |
| 19 delegate_(delegate) { | |
|
satorux1
2012/10/16 03:12:18
indentation is awkward. please do this:
HttpConne
mtomasz
2012/11/08 13:29:59
Done.
| |
| 20 } | |
| 21 | |
| 22 HttpConnection::~HttpConnection() { | |
| 23 | |
| 24 } | |
| 25 | |
| 26 void HttpConnection::SendResponse(scoped_ptr<HttpResponse> response) { | |
| 27 // TODO(mtomasz): This is just a stub. Replace with a better response sending. | |
| 28 | |
| 29 // Response line with headers. | |
| 30 std::string response_builder; | |
| 31 base::StringAppendF(&response_builder, "HTTP/1.1 %d OK\r\n", response->code); | |
| 32 std::map<std::string, std::string>::iterator it; | |
| 33 base::StringAppendF(&response_builder, | |
| 34 "Content-Length: %lu\r\n", | |
| 35 response->content.length()); | |
|
satorux1
2012/10/16 03:12:18
Is length() 64-bit? If so %lu is not correct. See
mtomasz
2012/11/08 13:29:59
Done.
| |
| 36 base::StringAppendF(&response_builder, | |
| 37 "Content-Type: %s\r\n", | |
| 38 response->content_type.c_str()); | |
| 39 for (it = response->custom_headers.begin(); | |
|
satorux1
2012/10/16 03:12:18
Can you declare it here?
for (std::map<std::stri
mtomasz
2012/11/08 13:29:59
Done.
| |
| 40 it != response->custom_headers.end(); | |
| 41 it++) { | |
| 42 base::StringAppendF(&response_builder, | |
| 43 "%s: %s\r\n", | |
| 44 it->first.c_str(), | |
| 45 it->second.c_str()); | |
| 46 } | |
| 47 base::StringAppendF(&response_builder, "\r\n"); | |
| 48 socket_->Send(response_builder.c_str(), response_builder.length()); | |
| 49 | |
| 50 // Response content. | |
| 51 socket_->Send(response->content.c_str(), response->content.length()); | |
| 52 } | |
| 53 | |
| 54 bool HttpConnection::ReceiveData(const char* data, int length) { | |
| 55 request_parser_.ProcessChunk(data, length); | |
| 56 HttpRequestParser::STATE parser_state; | |
| 57 while ((parser_state = request_parser_.ParseRequest()) == | |
| 58 HttpRequestParser::READY) { | |
| 59 if (!delegate_->HandleRequest(*this, request_parser_.GetRequest().Pass())) { | |
| 60 // Delegate did not accept the request, so we will drop the connection. | |
| 61 return false; | |
| 62 } | |
| 63 } | |
| 64 // If syntax error occured, then we drop the connection. | |
| 65 return parser_state != HttpRequestParser::SYNTAX_ERROR; | |
| 66 } | |
| 67 | |
| 68 } // namespace test_servers | |
| 69 } // namespace gdata | |
| OLD | NEW |