You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
9.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <algorithm>
  28. #include "talk/base/httpcommon-inl.h"
  29. #include "talk/base/asyncsocket.h"
  30. #include "talk/base/common.h"
  31. #include "talk/base/httpserver.h"
  32. #include "talk/base/logging.h"
  33. #include "talk/base/socketstream.h"
  34. #include "talk/base/thread.h"
  35. namespace talk_base {
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // HttpServer
  38. ///////////////////////////////////////////////////////////////////////////////
  39. HttpServer::HttpServer() : next_connection_id_(1), closing_(false) {
  40. }
  41. HttpServer::~HttpServer() {
  42. if (closing_) {
  43. LOG(LS_WARNING) << "HttpServer::CloseAll has not completed";
  44. }
  45. for (ConnectionMap::iterator it = connections_.begin();
  46. it != connections_.end();
  47. ++it) {
  48. StreamInterface* stream = it->second->EndProcess();
  49. delete stream;
  50. delete it->second;
  51. }
  52. }
  53. int
  54. HttpServer::HandleConnection(StreamInterface* stream) {
  55. int connection_id = next_connection_id_++;
  56. ASSERT(connection_id != HTTP_INVALID_CONNECTION_ID);
  57. Connection* connection = new Connection(connection_id, this);
  58. connections_.insert(ConnectionMap::value_type(connection_id, connection));
  59. connection->BeginProcess(stream);
  60. return connection_id;
  61. }
  62. void
  63. HttpServer::Respond(HttpServerTransaction* transaction) {
  64. int connection_id = transaction->connection_id();
  65. if (Connection* connection = Find(connection_id)) {
  66. connection->Respond(transaction);
  67. } else {
  68. delete transaction;
  69. // We may be tempted to SignalHttpComplete, but that implies that a
  70. // connection still exists.
  71. }
  72. }
  73. void
  74. HttpServer::Close(int connection_id, bool force) {
  75. if (Connection* connection = Find(connection_id)) {
  76. connection->InitiateClose(force);
  77. }
  78. }
  79. void
  80. HttpServer::CloseAll(bool force) {
  81. if (connections_.empty()) {
  82. SignalCloseAllComplete(this);
  83. return;
  84. }
  85. closing_ = true;
  86. std::list<Connection*> connections;
  87. for (ConnectionMap::const_iterator it = connections_.begin();
  88. it != connections_.end(); ++it) {
  89. connections.push_back(it->second);
  90. }
  91. for (std::list<Connection*>::const_iterator it = connections.begin();
  92. it != connections.end(); ++it) {
  93. (*it)->InitiateClose(force);
  94. }
  95. }
  96. HttpServer::Connection*
  97. HttpServer::Find(int connection_id) {
  98. ConnectionMap::iterator it = connections_.find(connection_id);
  99. if (it == connections_.end())
  100. return NULL;
  101. return it->second;
  102. }
  103. void
  104. HttpServer::Remove(int connection_id) {
  105. ConnectionMap::iterator it = connections_.find(connection_id);
  106. if (it == connections_.end()) {
  107. ASSERT(false);
  108. return;
  109. }
  110. Connection* connection = it->second;
  111. connections_.erase(it);
  112. SignalConnectionClosed(this, connection_id, connection->EndProcess());
  113. delete connection;
  114. if (closing_ && connections_.empty()) {
  115. closing_ = false;
  116. SignalCloseAllComplete(this);
  117. }
  118. }
  119. ///////////////////////////////////////////////////////////////////////////////
  120. // HttpServer::Connection
  121. ///////////////////////////////////////////////////////////////////////////////
  122. HttpServer::Connection::Connection(int connection_id, HttpServer* server)
  123. : connection_id_(connection_id), server_(server),
  124. current_(NULL), signalling_(false), close_(false) {
  125. }
  126. HttpServer::Connection::~Connection() {
  127. // It's possible that an object hosted inside this transaction signalled
  128. // an event which caused the connection to close.
  129. Thread::Current()->Dispose(current_);
  130. }
  131. void
  132. HttpServer::Connection::BeginProcess(StreamInterface* stream) {
  133. base_.notify(this);
  134. base_.attach(stream);
  135. current_ = new HttpServerTransaction(connection_id_);
  136. if (base_.mode() != HM_CONNECT)
  137. base_.recv(&current_->request);
  138. }
  139. StreamInterface*
  140. HttpServer::Connection::EndProcess() {
  141. base_.notify(NULL);
  142. base_.abort(HE_DISCONNECTED);
  143. return base_.detach();
  144. }
  145. void
  146. HttpServer::Connection::Respond(HttpServerTransaction* transaction) {
  147. ASSERT(current_ == NULL);
  148. current_ = transaction;
  149. if (current_->response.begin() == current_->response.end()) {
  150. current_->response.set_error(HC_INTERNAL_SERVER_ERROR);
  151. }
  152. bool keep_alive = HttpShouldKeepAlive(current_->request);
  153. current_->response.setHeader(HH_CONNECTION,
  154. keep_alive ? "Keep-Alive" : "Close",
  155. false);
  156. close_ = !HttpShouldKeepAlive(current_->response);
  157. base_.send(&current_->response);
  158. }
  159. void
  160. HttpServer::Connection::InitiateClose(bool force) {
  161. bool request_in_progress = (HM_SEND == base_.mode()) || (NULL == current_);
  162. if (!signalling_ && (force || !request_in_progress)) {
  163. server_->Remove(connection_id_);
  164. } else {
  165. close_ = true;
  166. }
  167. }
  168. //
  169. // IHttpNotify Implementation
  170. //
  171. HttpError
  172. HttpServer::Connection::onHttpHeaderComplete(bool chunked, size_t& data_size) {
  173. if (data_size == SIZE_UNKNOWN) {
  174. data_size = 0;
  175. }
  176. ASSERT(current_ != NULL);
  177. bool custom_document = false;
  178. server_->SignalHttpRequestHeader(server_, current_, &custom_document);
  179. if (!custom_document) {
  180. current_->request.document.reset(new MemoryStream);
  181. }
  182. return HE_NONE;
  183. }
  184. void
  185. HttpServer::Connection::onHttpComplete(HttpMode mode, HttpError err) {
  186. if (mode == HM_SEND) {
  187. ASSERT(current_ != NULL);
  188. signalling_ = true;
  189. server_->SignalHttpRequestComplete(server_, current_, err);
  190. signalling_ = false;
  191. if (close_) {
  192. // Force a close
  193. err = HE_DISCONNECTED;
  194. }
  195. }
  196. if (err != HE_NONE) {
  197. server_->Remove(connection_id_);
  198. } else if (mode == HM_CONNECT) {
  199. base_.recv(&current_->request);
  200. } else if (mode == HM_RECV) {
  201. ASSERT(current_ != NULL);
  202. // TODO: do we need this?
  203. //request_.document_->rewind();
  204. HttpServerTransaction* transaction = current_;
  205. current_ = NULL;
  206. server_->SignalHttpRequest(server_, transaction);
  207. } else if (mode == HM_SEND) {
  208. Thread::Current()->Dispose(current_->response.document.release());
  209. current_->request.clear(true);
  210. current_->response.clear(true);
  211. base_.recv(&current_->request);
  212. } else {
  213. ASSERT(false);
  214. }
  215. }
  216. void
  217. HttpServer::Connection::onHttpClosed(HttpError err) {
  218. UNUSED(err);
  219. server_->Remove(connection_id_);
  220. }
  221. ///////////////////////////////////////////////////////////////////////////////
  222. // HttpListenServer
  223. ///////////////////////////////////////////////////////////////////////////////
  224. HttpListenServer::HttpListenServer() {
  225. SignalConnectionClosed.connect(this, &HttpListenServer::OnConnectionClosed);
  226. }
  227. HttpListenServer::~HttpListenServer() {
  228. }
  229. int HttpListenServer::Listen(const SocketAddress& address) {
  230. AsyncSocket* sock =
  231. Thread::Current()->socketserver()->CreateAsyncSocket(address.family(),
  232. SOCK_STREAM);
  233. if (!sock) {
  234. return SOCKET_ERROR;
  235. }
  236. listener_.reset(sock);
  237. listener_->SignalReadEvent.connect(this, &HttpListenServer::OnReadEvent);
  238. if ((listener_->Bind(address) != SOCKET_ERROR) &&
  239. (listener_->Listen(5) != SOCKET_ERROR))
  240. return 0;
  241. return listener_->GetError();
  242. }
  243. bool HttpListenServer::GetAddress(SocketAddress* address) const {
  244. if (!listener_) {
  245. return false;
  246. }
  247. *address = listener_->GetLocalAddress();
  248. return !address->IsNil();
  249. }
  250. void HttpListenServer::StopListening() {
  251. if (listener_) {
  252. listener_->Close();
  253. }
  254. }
  255. void HttpListenServer::OnReadEvent(AsyncSocket* socket) {
  256. ASSERT(socket == listener_.get());
  257. ASSERT(listener_);
  258. AsyncSocket* incoming = listener_->Accept(NULL);
  259. if (incoming) {
  260. StreamInterface* stream = new SocketStream(incoming);
  261. //stream = new LoggingAdapter(stream, LS_VERBOSE, "HttpServer", false);
  262. HandleConnection(stream);
  263. }
  264. }
  265. void HttpListenServer::OnConnectionClosed(HttpServer* server,
  266. int connection_id,
  267. StreamInterface* stream) {
  268. Thread::Current()->Dispose(stream);
  269. }
  270. ///////////////////////////////////////////////////////////////////////////////
  271. } // namespace talk_base