1 // Copyright (c) 2009 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.
5 #include "net/tools/flip_server/streamer_interface.h"
9 #include "net/tools/balsa/balsa_frame.h"
10 #include "net/tools/flip_server/constants.h"
11 #include "net/tools/flip_server/flip_config.h"
12 #include "net/tools/flip_server/sm_connection.h"
16 std::string
StreamerSM::forward_ip_header_
;
18 StreamerSM::StreamerSM(SMConnection
* connection
,
19 SMInterface
* sm_other_interface
,
20 EpollServer
* epoll_server
,
21 FlipAcceptor
* acceptor
)
22 : connection_(connection
),
23 sm_other_interface_(sm_other_interface
),
24 epoll_server_(epoll_server
),
27 http_framer_(new BalsaFrame
) {
28 VLOG(2) << ACCEPTOR_CLIENT_IDENT
<< "Creating StreamerSM object";
29 http_framer_
->set_balsa_visitor(this);
30 http_framer_
->set_balsa_headers(&headers_
);
31 http_framer_
->set_is_request(false);
34 StreamerSM::~StreamerSM() {
35 VLOG(1) << ACCEPTOR_CLIENT_IDENT
<< "Destroying StreamerSM object";
40 void StreamerSM::set_is_request() {
42 http_framer_
->set_is_request(true);
45 void StreamerSM::InitSMInterface(SMInterface
* sm_other_interface
,
47 sm_other_interface_
= sm_other_interface
;
50 void StreamerSM::InitSMConnection(SMConnectionPoolInterface
* connection_pool
,
51 SMInterface
* sm_interface
,
52 EpollServer
* epoll_server
,
54 std::string server_ip
,
55 std::string server_port
,
56 std::string remote_ip
,
58 VLOG(2) << ACCEPTOR_CLIENT_IDENT
<< "StreamerSM: Initializing server "
60 connection_
->InitSMConnection(connection_pool
,
70 size_t StreamerSM::ProcessReadInput(const char* data
, size_t len
) {
71 // For now we only want to parse http requests. Just stream responses
73 return http_framer_
->ProcessInput(data
, len
);
75 return sm_other_interface_
->ProcessWriteInput(data
, len
);
79 size_t StreamerSM::ProcessWriteInput(const char* data
, size_t len
) {
80 char* dataPtr
= new char[len
];
81 memcpy(dataPtr
, data
, len
);
82 DataFrame
* df
= new DataFrame
;
83 df
->data
= (const char*)dataPtr
;
85 df
->delete_when_done
= true;
86 connection_
->EnqueueDataFrame(df
);
90 bool StreamerSM::Error() const { return false; }
92 const char* StreamerSM::ErrorAsString() const { return "(none)"; }
94 bool StreamerSM::MessageFullyRead() const {
96 return http_framer_
->MessageFullyRead();
102 void StreamerSM::Reset() {
103 VLOG(1) << ACCEPTOR_CLIENT_IDENT
<< "StreamerSM: Reset";
104 connection_
->Cleanup("Server Reset");
105 http_framer_
->Reset();
108 void StreamerSM::ResetForNewConnection() {
109 http_framer_
->Reset();
110 sm_other_interface_
->Reset();
113 void StreamerSM::Cleanup() {
115 http_framer_
->Reset();
118 int StreamerSM::PostAcceptHook() {
119 if (!sm_other_interface_
) {
120 SMConnection
* server_connection
= SMConnection::NewSMConnection(
121 epoll_server_
, NULL
, NULL
, acceptor_
, "server_conn: ");
122 if (server_connection
== NULL
) {
123 LOG(ERROR
) << "StreamerSM: Could not create server conenction.";
126 VLOG(2) << ACCEPTOR_CLIENT_IDENT
<< "StreamerSM: Creating new server "
128 sm_other_interface_
=
129 new StreamerSM(server_connection
, this, epoll_server_
, acceptor_
);
130 sm_other_interface_
->InitSMInterface(this, 0);
132 // The Streamer interface is used to stream HTTPS connections, so we
133 // will always use the https_server_ip/port here.
134 sm_other_interface_
->InitSMConnection(NULL
,
138 acceptor_
->https_server_ip_
,
139 acceptor_
->https_server_port_
,
146 size_t StreamerSM::SendSynStream(uint32 stream_id
,
147 const BalsaHeaders
& headers
) {
151 size_t StreamerSM::SendSynReply(uint32 stream_id
, const BalsaHeaders
& headers
) {
155 void StreamerSM::ProcessBodyInput(const char* input
, size_t size
) {
156 VLOG(2) << ACCEPTOR_CLIENT_IDENT
157 << "StreamerHttpSM: Process Body Input Data: "
159 sm_other_interface_
->ProcessWriteInput(input
, size
);
162 void StreamerSM::MessageDone() {
163 if (acceptor_
->flip_handler_type_
== FLIP_HANDLER_PROXY
) {
164 VLOG(2) << ACCEPTOR_CLIENT_IDENT
<< "StreamerHttpSM: MessageDone.";
165 // TODO(kelindsay): anything need to be done ehre?
167 VLOG(2) << ACCEPTOR_CLIENT_IDENT
<< "StraemerHttpSM: MessageDone.";
171 void StreamerSM::ProcessHeaders(const BalsaHeaders
& headers
) {
172 VLOG(2) << ACCEPTOR_CLIENT_IDENT
<< "HttpStreamerSM: Process Headers";
173 BalsaHeaders mod_headers
;
174 mod_headers
.CopyFrom(headers
);
175 if (forward_ip_header_
.length()) {
176 LOG(INFO
) << "Adding forward header: " << forward_ip_header_
;
177 mod_headers
.ReplaceOrAppendHeader(forward_ip_header_
,
178 connection_
->client_ip());
180 LOG(INFO
) << "NOT adding forward header.";
185 mod_headers
.WriteHeaderAndEndingToBuffer(&sb
);
186 sb
.GetReadablePtr(&buffer
, &size
);
187 sm_other_interface_
->ProcessWriteInput(buffer
, size
);
190 void StreamerSM::HandleHeaderError(BalsaFrame
* framer
) { HandleError(); }
192 void StreamerSM::HandleChunkingError(BalsaFrame
* framer
) { HandleError(); }
194 void StreamerSM::HandleBodyError(BalsaFrame
* framer
) { HandleError(); }
196 void StreamerSM::HandleError() {
197 VLOG(1) << ACCEPTOR_CLIENT_IDENT
<< "Error detected";