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 "net/server/http_connection.h"
6
7#include "net/server/http_server.h"
8#include "net/server/http_server_response_info.h"
9#include "net/server/web_socket.h"
10#include "net/socket/stream_listen_socket.h"
11
12namespace net {
13
14int HttpConnection::last_id_ = 0;
15
16void HttpConnection::Send(const std::string& data) {
17  if (!socket_.get())
18    return;
19  socket_->Send(data);
20}
21
22void HttpConnection::Send(const char* bytes, int len) {
23  if (!socket_.get())
24    return;
25  socket_->Send(bytes, len);
26}
27
28void HttpConnection::Send(const HttpServerResponseInfo& response) {
29  Send(response.Serialize());
30}
31
32HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock)
33    : server_(server),
34      socket_(sock) {
35  id_ = last_id_++;
36}
37
38HttpConnection::~HttpConnection() {
39  DetachSocket();
40  server_->delegate_->OnClose(id_);
41}
42
43void HttpConnection::DetachSocket() {
44  socket_ = NULL;
45}
46
47void HttpConnection::Shift(int num_bytes) {
48  recv_data_ = recv_data_.substr(num_bytes);
49}
50
51}  // namespace net
52