aboutsummaryrefslogtreecommitdiffhomepage
path: root/tdnet/td/net/HttpConnectionBase.h
blob: 8c9f6986cd5c485fb2e0749996d7ec0fe51a74c7 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once

#include "td/net/HttpQuery.h"
#include "td/net/HttpReader.h"
#include "td/net/SslStream.h"

#include "td/actor/actor.h"

#include "td/utils/buffer.h"
#include "td/utils/BufferedFd.h"
#include "td/utils/ByteFlow.h"
#include "td/utils/port/IPAddress.h"
#include "td/utils/port/SocketFd.h"
#include "td/utils/Status.h"

namespace td {

namespace detail {

class HttpConnectionBase : public Actor {
 public:
  void write_next_noflush(BufferSlice buffer);
  void write_next(BufferSlice buffer);
  void write_ok();
  void write_error(Status error);

 protected:
  enum class State { Read, Write, Close };
  HttpConnectionBase(State state, BufferedFd<SocketFd> fd, SslStream ssl_stream, size_t max_post_size, size_t max_files,
                     int32 idle_timeout, int32 slow_scheduler_id);

 private:
  State state_;

  BufferedFd<SocketFd> fd_;
  IPAddress peer_address_;
  SslStream ssl_stream_;

  ByteFlowSource read_source_{&fd_.input_buffer()};
  ByteFlowSink read_sink_;

  ChainBufferWriter write_buffer_;
  ChainBufferReader write_buffer_reader_ = write_buffer_.extract_reader();
  ByteFlowSource write_source_{&write_buffer_reader_};
  ByteFlowMoveSink write_sink_{&fd_.output_buffer()};

  size_t max_post_size_;
  size_t max_files_;
  int32 idle_timeout_;
  HttpReader reader_;
  unique_ptr<HttpQuery> current_query_;
  bool close_after_write_ = false;

  int32 slow_scheduler_id_{-1};

  void live_event();

  void start_up() final;
  void tear_down() final;
  void timeout_expired() final;
  void loop() final;

  void on_start_migrate(int32 sched_id) final;
  void on_finish_migrate() final;

  virtual void on_query(unique_ptr<HttpQuery> query) = 0;
  virtual void on_error(Status error) = 0;
};

}  // namespace detail
}  // namespace td