IHttpServer.h 3.16 KB
Newer Older
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
/*****************************************************************************
 * IHttpServer.h : interface of IHttpServer
 *
 *****************************************************************************
 * Copyright (C) 2016-2016 VideoLAN
 *
 * Authors: Paweł Wegner <pawel.wegner95@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#ifndef IHTTP_SERVER_H
#define IHTTP_SERVER_H

26
#include <functional>
27 28 29 30 31 32 33 34 35 36
#include <memory>
#include <string>
#include <unordered_map>

namespace cloudstorage {

class IHttpServer {
 public:
  using Pointer = std::unique_ptr<IHttpServer>;

37
  enum class Type { Authorization, FileProvider };
38 39 40 41 42 43 44

  virtual ~IHttpServer() = default;

  class IResponse {
   public:
    using Pointer = std::unique_ptr<IResponse>;
    using Headers = std::unordered_map<std::string, std::string>;
45
    using CompletedCallback = std::function<void()>;
46

47 48
    static constexpr int UnknownSize = -1;

49 50 51 52
    class ICallback {
     public:
      using Pointer = std::unique_ptr<ICallback>;

53 54
      static constexpr int Suspend = 0;
      static constexpr int Abort = -1;
55
      static constexpr int End = -2;
56

57 58
      virtual ~ICallback() = default;

59 60 61 62
      virtual int putData(char* buffer, size_t size) = 0;
    };

    virtual ~IResponse() = default;
63 64 65

    virtual void resume() = 0;
    virtual void completed(CompletedCallback) = 0;
66 67
  };

68
  class IRequest {
69
   public:
70
    virtual ~IRequest() = default;
71

72
    virtual const char* get(const std::string& name) const = 0;
73
    virtual const char* header(const std::string& name) const = 0;
74
    virtual std::string method() const = 0;
75
    virtual std::string url() const = 0;
76 77 78 79

    virtual IResponse::Pointer response(
        int code, const IResponse::Headers&, int size,
        IResponse::ICallback::Pointer) const = 0;
80 81 82 83 84 85 86 87
  };

  class ICallback {
   public:
    using Pointer = std::shared_ptr<ICallback>;

    virtual ~ICallback() = default;

88
    virtual IResponse::Pointer handle(const IRequest&) = 0;
89 90
  };

91
  virtual ICallback::Pointer callback() const = 0;
92 93 94 95 96 97 98 99 100
};

class IHttpServerFactory {
 public:
  using Pointer = std::shared_ptr<IHttpServerFactory>;

  virtual ~IHttpServerFactory() = default;

  virtual IHttpServer::Pointer create(IHttpServer::ICallback::Pointer,
101
                                      const std::string& session_id,
102
                                      IHttpServer::Type) = 0;
103 104

  static IHttpServerFactory::Pointer create();
105 106 107 108 109
};

}  // namespace cloudstorage

#endif  // IHTTP_SERVER_H