blob: 2ae698b98c5329424e31b2b2bfab616fad85c3ac [file] [log] [blame]
[email protected]a9813302012-04-28 09:29:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a33721a2011-02-03 17:23:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_SERVER_HTTP_SERVER_H_
6#define NET_SERVER_HTTP_SERVER_H_
[email protected]a33721a2011-02-03 17:23:247
[email protected]a33721a2011-02-03 17:23:248#include <map>
byungchul38c3ae72014-08-25 23:27:469#include <string>
[email protected]a33721a2011-02-03 17:23:2410
11#include "base/basictypes.h"
byungchul38c3ae72014-08-25 23:27:4612#include "base/macros.h"
[email protected]8b62f8e2013-09-03 19:03:0613#include "base/memory/scoped_ptr.h"
byungchul38c3ae72014-08-25 23:27:4614#include "base/memory/weak_ptr.h"
[email protected]e67d2172012-12-11 09:44:4115#include "net/http/http_status_code.h"
[email protected]a33721a2011-02-03 17:23:2416
[email protected]b6dbb0a2011-04-18 00:17:4117namespace net {
18
[email protected]86c6a0b52011-08-02 19:49:2519class HttpConnection;
[email protected]a33721a2011-02-03 17:23:2420class HttpServerRequestInfo;
[email protected]c700fd152013-07-23 21:29:1121class HttpServerResponseInfo;
[email protected]7c17b692012-08-09 16:16:3022class IPEndPoint;
byungchul38c3ae72014-08-25 23:27:4623class ServerSocket;
24class StreamSocket;
[email protected]86c6a0b52011-08-02 19:49:2525class WebSocket;
[email protected]a33721a2011-02-03 17:23:2426
byungchul38c3ae72014-08-25 23:27:4627class HttpServer {
[email protected]a33721a2011-02-03 17:23:2428 public:
byungchul38c3ae72014-08-25 23:27:4629 // Delegate to handle http/websocket events. Beware that it is not safe to
30 // destroy the HttpServer in any of these callbacks.
[email protected]a33721a2011-02-03 17:23:2431 class Delegate {
32 public:
33 virtual void OnHttpRequest(int connection_id,
34 const HttpServerRequestInfo& info) = 0;
[email protected]a33721a2011-02-03 17:23:2435 virtual void OnWebSocketRequest(int connection_id,
36 const HttpServerRequestInfo& info) = 0;
[email protected]a33721a2011-02-03 17:23:2437 virtual void OnWebSocketMessage(int connection_id,
38 const std::string& data) = 0;
[email protected]a33721a2011-02-03 17:23:2439 virtual void OnClose(int connection_id) = 0;
[email protected]a33721a2011-02-03 17:23:2440 };
41
byungchul38c3ae72014-08-25 23:27:4642 HttpServer(scoped_ptr<ServerSocket> server_socket,
[email protected]8bf9ce72012-05-23 17:05:3143 HttpServer::Delegate* delegate);
byungchul38c3ae72014-08-25 23:27:4644 ~HttpServer();
[email protected]a33721a2011-02-03 17:23:2445
46 void AcceptWebSocket(int connection_id,
47 const HttpServerRequestInfo& request);
48 void SendOverWebSocket(int connection_id, const std::string& data);
[email protected]a6948f92014-04-19 02:12:4049 // Sends the provided data directly to the given connection. No validation is
50 // performed that data constitutes a valid HTTP response. A valid HTTP
51 // response may be split across multiple calls to SendRaw.
52 void SendRaw(int connection_id, const std::string& data);
byungchul38c3ae72014-08-25 23:27:4653 // TODO(byungchul): Consider replacing function name with SendResponseInfo
[email protected]c700fd152013-07-23 21:29:1154 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
[email protected]e67d2172012-12-11 09:44:4155 void Send(int connection_id,
56 HttpStatusCode status_code,
57 const std::string& data,
58 const std::string& mime_type);
[email protected]a33721a2011-02-03 17:23:2459 void Send200(int connection_id,
60 const std::string& data,
61 const std::string& mime_type);
62 void Send404(int connection_id);
63 void Send500(int connection_id, const std::string& message);
[email protected]e67d2172012-12-11 09:44:4164
[email protected]a33721a2011-02-03 17:23:2465 void Close(int connection_id);
66
byungchul38c3ae72014-08-25 23:27:4667 void SetReceiveBufferSize(int connection_id, int32 size);
68 void SetSendBufferSize(int connection_id, int32 size);
69
[email protected]7c17b692012-08-09 16:16:3070 // Copies the local address to |address|. Returns a network error code.
71 int GetLocalAddress(IPEndPoint* address);
72
[email protected]a9813302012-04-28 09:29:2873 private:
byungchul38c3ae72014-08-25 23:27:4674 friend class HttpServerTest;
75
76 typedef std::map<int, HttpConnection*> IdToConnectionMap;
77
78 void DoAcceptLoop();
79 void OnAcceptCompleted(int rv);
80 int HandleAcceptResult(int rv);
81
82 void DoReadLoop(HttpConnection* connection);
83 void OnReadCompleted(int connection_id, int rv);
84 int HandleReadResult(HttpConnection* connection, int rv);
85
86 void DoWriteLoop(HttpConnection* connection);
87 void OnWriteCompleted(int connection_id, int rv);
88 int HandleWriteResult(HttpConnection* connection, int rv);
[email protected]a9813302012-04-28 09:29:2889
[email protected]a33721a2011-02-03 17:23:2490 // Expects the raw data to be stored in recv_data_. If parsing is successful,
91 // will remove the data parsed from recv_data_, leaving only the unused
92 // recv data.
byungchul38c3ae72014-08-25 23:27:4693 bool ParseHeaders(const char* data,
94 size_t data_len,
[email protected]2053a1e2011-02-04 16:24:3095 HttpServerRequestInfo* info,
[email protected]86c6a0b52011-08-02 19:49:2596 size_t* pos);
[email protected]a33721a2011-02-03 17:23:2497
[email protected]86c6a0b52011-08-02 19:49:2598 HttpConnection* FindConnection(int connection_id);
[email protected]a33721a2011-02-03 17:23:2499
byungchul38c3ae72014-08-25 23:27:46100 // Whether or not Close() has been called during delegate callback processing.
101 bool HasClosedConnection(HttpConnection* connection);
102
103 const scoped_ptr<ServerSocket> server_socket_;
104 scoped_ptr<StreamSocket> accepted_socket_;
105 HttpServer::Delegate* const delegate_;
106
107 int last_id_;
[email protected]a33721a2011-02-03 17:23:24108 IdToConnectionMap id_to_connection_;
byungchul38c3ae72014-08-25 23:27:46109
110 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
[email protected]a33721a2011-02-03 17:23:24111
112 DISALLOW_COPY_AND_ASSIGN(HttpServer);
113};
114
[email protected]b6dbb0a2011-04-18 00:17:41115} // namespace net
116
[email protected]a33721a2011-02-03 17:23:24117#endif // NET_SERVER_HTTP_SERVER_H_