2014-12-15 00:00:16 +01:00
|
|
|
#ifndef _HTTP_SERVER_H
|
|
|
|
#define _HTTP_SERVER_H
|
|
|
|
|
2016-10-12 11:32:24 +02:00
|
|
|
#include <functional>
|
2014-12-15 00:00:16 +01:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "base/buffer.h"
|
|
|
|
#include "net/http_headers.h"
|
|
|
|
#include "thread/executor.h"
|
|
|
|
|
2016-05-25 19:02:38 -07:00
|
|
|
namespace net {
|
|
|
|
class InputSink;
|
|
|
|
class OutputSink;
|
|
|
|
};
|
|
|
|
|
2014-12-15 00:00:16 +01:00
|
|
|
namespace http {
|
|
|
|
|
|
|
|
class Request {
|
|
|
|
public:
|
|
|
|
Request(int fd);
|
|
|
|
~Request();
|
|
|
|
|
|
|
|
const char *resource() const {
|
|
|
|
return header_.resource;
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:05:32 -07:00
|
|
|
RequestHeader::Method Method() const {
|
|
|
|
return header_.method;
|
|
|
|
}
|
|
|
|
|
2014-12-15 00:00:16 +01:00
|
|
|
bool GetParamValue(const char *param_name, std::string *value) const {
|
|
|
|
return header_.GetParamValue(param_name, value);
|
|
|
|
}
|
2016-05-25 22:21:24 -07:00
|
|
|
// Use lowercase.
|
|
|
|
bool GetHeader(const char *name, std::string *value) const {
|
|
|
|
return header_.GetOther(name, value);
|
|
|
|
}
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-05-25 19:02:38 -07:00
|
|
|
net::InputSink *In() const { return in_; }
|
|
|
|
net::OutputSink *Out() const { return out_; }
|
2014-12-15 00:00:16 +01:00
|
|
|
|
|
|
|
// TODO: Remove, in favor of PartialWrite and friends.
|
|
|
|
int fd() const { return fd_; }
|
|
|
|
|
|
|
|
void WritePartial() const;
|
|
|
|
void Write();
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
bool IsOK() const { return fd_ > 0; }
|
|
|
|
|
|
|
|
// If size is negative, no Content-Length: line is written.
|
2016-05-26 18:05:32 -07:00
|
|
|
void WriteHttpResponseHeader(int status, int64_t size = -1, const char *mimeType = nullptr, const char *otherHeaders = nullptr) const;
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-05-25 19:02:38 -07:00
|
|
|
private:
|
|
|
|
net::InputSink *in_;
|
|
|
|
net::OutputSink *out_;
|
|
|
|
RequestHeader header_;
|
|
|
|
int fd_;
|
2014-12-15 00:00:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Register handlers on this class to serve stuff.
|
|
|
|
class Server {
|
2016-07-03 11:32:18 -07:00
|
|
|
public:
|
|
|
|
Server(threading::Executor *executor);
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
typedef std::function<void(const Request &)> UrlHandlerFunc;
|
|
|
|
typedef std::map<std::string, UrlHandlerFunc> UrlHandlerMap;
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
// Runs forever, serving request. If you want to do something else than serve pages,
|
|
|
|
// better put this on a thread. Returns false if failed to start serving, never
|
|
|
|
// returns if successful.
|
|
|
|
bool Run(int port);
|
|
|
|
// May run for (significantly) longer than timeout, but won't wait longer than that
|
|
|
|
// for a new connection to handle.
|
|
|
|
bool RunSlice(double timeout);
|
|
|
|
bool Listen(int port);
|
2016-07-04 07:24:04 -07:00
|
|
|
void Stop();
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
void RegisterHandler(const char *url_path, UrlHandlerFunc handler);
|
|
|
|
void SetFallbackHandler(UrlHandlerFunc handler);
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
// If you want to customize things at a lower level than just a simple path handler,
|
|
|
|
// then inherit and override this. Implementations should forward to HandleRequestDefault
|
|
|
|
// if they don't recognize the url.
|
|
|
|
virtual void HandleRequest(const Request &request);
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
int Port() {
|
|
|
|
return port_;
|
|
|
|
}
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
private:
|
|
|
|
void HandleConnection(int conn_fd);
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
// Things like default 404, etc.
|
|
|
|
void HandleRequestDefault(const Request &request);
|
2016-07-03 10:18:54 -07:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
// Neat built-in handlers that are tied to the server.
|
|
|
|
void HandleListing(const Request &request);
|
|
|
|
void Handle404(const Request &request);
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
int listener_;
|
|
|
|
int port_;
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
UrlHandlerMap handlers_;
|
|
|
|
UrlHandlerFunc fallback_;
|
2014-12-15 00:00:16 +01:00
|
|
|
|
2016-07-03 11:32:18 -07:00
|
|
|
threading::Executor *executor_;
|
2014-12-15 00:00:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace http
|
|
|
|
|
|
|
|
#endif // _HTTP_SERVER_H
|