blob: 66541faa37354aaec22ba81059f9e7856b7891b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/server/http_connection.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/server/http_server.h"
#include "net/server/web_socket.h"
#include "net/socket/stream_listen_socket.h"
namespace net {
int HttpConnection::last_id_ = 0;
void HttpConnection::Send(const std::string& data) {
if (!socket_.get())
return;
socket_->Send(data);
}
void HttpConnection::Send(const char* bytes, int len) {
if (!socket_.get())
return;
socket_->Send(bytes, len);
}
void HttpConnection::Send(HttpStatusCode status_code,
const std::string& data,
const std::string& content_type) {
if (!socket_.get())
return;
socket_->Send(base::StringPrintf(
"HTTP/1.1 %d %s\r\n"
"Content-Type:%s\r\n"
"Content-Length:%d\r\n"
"\r\n",
status_code,
GetHttpReasonPhrase(status_code),
content_type.c_str(),
static_cast<int>(data.length())));
socket_->Send(data);
}
HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock)
: server_(server),
socket_(sock) {
id_ = last_id_++;
}
HttpConnection::~HttpConnection() {
DetachSocket();
server_->delegate_->OnClose(id_);
}
void HttpConnection::DetachSocket() {
socket_ = NULL;
}
void HttpConnection::Shift(int num_bytes) {
recv_data_ = recv_data_.substr(num_bytes);
}
} // namespace net
|