| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "net/server/http_connection.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "net/base/listen_socket.h" |
| 10 #include "net/server/http_server.h" |
| 11 #include "net/server/web_socket.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 int HttpConnection::lastId_ = 0; |
| 16 |
| 17 void HttpConnection::Send(const std::string& data) { |
| 18 if (!socket_) |
| 19 return; |
| 20 socket_->Send(data); |
| 21 } |
| 22 |
| 23 void HttpConnection::Send(const char* bytes, int len) { |
| 24 if (!socket_) |
| 25 return; |
| 26 socket_->Send(bytes, len); |
| 27 } |
| 28 |
| 29 void HttpConnection::Send200(const std::string& data, |
| 30 const std::string& content_type) { |
| 31 if (!socket_) |
| 32 return; |
| 33 socket_->Send(base::StringPrintf( |
| 34 "HTTP/1.1 200 OK\r\n" |
| 35 "Content-Type:%s\r\n" |
| 36 "Content-Length:%d\r\n" |
| 37 "\r\n", |
| 38 content_type.c_str(), |
| 39 static_cast<int>(data.length()))); |
| 40 socket_->Send(data); |
| 41 } |
| 42 |
| 43 void HttpConnection::Send404() { |
| 44 if (!socket_) |
| 45 return; |
| 46 socket_->Send( |
| 47 "HTTP/1.1 404 Not Found\r\n" |
| 48 "Content-Length: 0\r\n" |
| 49 "\r\n"); |
| 50 } |
| 51 |
| 52 void HttpConnection::Send500(const std::string& message) { |
| 53 if (!socket_) |
| 54 return; |
| 55 socket_->Send(base::StringPrintf( |
| 56 "HTTP/1.1 500 Internal Error\r\n" |
| 57 "Content-Type:text/html\r\n" |
| 58 "Content-Length:%d\r\n" |
| 59 "\r\n" |
| 60 "%s", |
| 61 static_cast<int>(message.length()), |
| 62 message.c_str())); |
| 63 } |
| 64 |
| 65 HttpConnection::HttpConnection(HttpServer* server, ListenSocket* sock) |
| 66 : server_(server), |
| 67 socket_(sock) { |
| 68 id_ = lastId_++; |
| 69 } |
| 70 |
| 71 HttpConnection::~HttpConnection() { |
| 72 DetachSocket(); |
| 73 server_->delegate_->OnClose(id_); |
| 74 } |
| 75 |
| 76 void HttpConnection::DetachSocket() { |
| 77 socket_ = NULL; |
| 78 } |
| 79 |
| 80 void HttpConnection::Shift(int num_bytes) { |
| 81 recv_data_ = recv_data_.substr(num_bytes); |
| 82 } |
| 83 |
| 84 } // namespace net |
| OLD | NEW |