| 12
 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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 
 | /** @file
  A brief file description
  @section license License
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
/****************************************************************************
   HttpCacheSM.h
   Description:
 ****************************************************************************/
#pragma once
#include "P_Cache.h"
#include "ProxyConfig.h"
#include "URL.h"
#include "HTTP.h"
#include "HttpConfig.h"
class HttpSM;
class HttpCacheSM;
struct HttpCacheAction : public Action {
  HttpCacheAction();
  void cancel(Continuation *c = nullptr) override;
  void
  init(HttpCacheSM *sm_arg)
  {
    sm = sm_arg;
  };
  HttpCacheSM *sm = nullptr;
};
class HttpCacheSM : public Continuation
{
public:
  HttpCacheSM();
  void
  init(HttpSM *sm_arg, Ptr<ProxyMutex> &amutex)
  {
    master_sm = sm_arg;
    mutex     = amutex;
    captive_action.init(this);
  }
  Action *open_read(const HttpCacheKey *key, URL *url, HTTPHdr *hdr, const OverridableHttpConfigParams *params,
                    time_t pin_in_cache);
  Action *open_write(const HttpCacheKey *key, URL *url, HTTPHdr *request, CacheHTTPInfo *old_info, time_t pin_in_cache, bool retry,
                     bool allow_multiple);
  CacheVConnection *cache_read_vc  = nullptr;
  CacheVConnection *cache_write_vc = nullptr;
  bool read_locked  = false;
  bool write_locked = false;
  // Flag to check whether read-while-write is in progress or not
  bool readwhilewrite_inprogress = false;
  HttpSM *master_sm      = nullptr;
  Action *pending_action = nullptr;
  // Function to set readwhilewrite_inprogress flag
  inline void
  set_readwhilewrite_inprogress(bool value)
  {
    readwhilewrite_inprogress = value;
  }
  // Function to get the readwhilewrite_inprogress flag
  inline bool
  is_readwhilewrite_inprogress()
  {
    return readwhilewrite_inprogress;
  }
  bool
  is_ram_cache_hit()
  {
    return cache_read_vc ? (cache_read_vc->is_ram_cache_hit()) : false;
  }
  bool
  is_compressed_in_ram()
  {
    return cache_read_vc ? (cache_read_vc->is_compressed_in_ram()) : false;
  }
  inline void
  set_open_read_tries(int value)
  {
    open_read_tries = value;
  }
  int
  get_open_read_tries()
  {
    return open_read_tries;
  }
  inline void
  set_open_write_tries(int value)
  {
    open_write_tries = value;
  }
  int
  get_open_write_tries()
  {
    return open_write_tries;
  }
  int
  get_volume_number()
  {
    if (cache_read_vc) {
      return cache_read_vc->get_volume_number();
    } else if (cache_write_vc) {
      return cache_write_vc->get_volume_number();
    }
    return -1;
  }
  const char *
  get_disk_path()
  {
    if (cache_read_vc) {
      return cache_read_vc->get_disk_path();
    } else if (cache_write_vc) {
      return cache_write_vc->get_disk_path();
    }
    return nullptr;
  }
  inline void
  abort_read()
  {
    if (cache_read_vc) {
      HTTP_DECREMENT_DYN_STAT(http_current_cache_connections_stat);
      cache_read_vc->do_io_close(0); // passing zero as aborting read is not an error
      cache_read_vc = nullptr;
    }
  }
  inline void
  abort_write()
  {
    if (cache_write_vc) {
      HTTP_DECREMENT_DYN_STAT(http_current_cache_connections_stat);
      cache_write_vc->do_io_close(0); // passing zero as aborting write is not an error
      cache_write_vc = nullptr;
    }
  }
  inline void
  close_write()
  {
    if (cache_write_vc) {
      HTTP_DECREMENT_DYN_STAT(http_current_cache_connections_stat);
      cache_write_vc->do_io_close();
      cache_write_vc = nullptr;
    }
  }
  inline void
  close_read()
  {
    if (cache_read_vc) {
      HTTP_DECREMENT_DYN_STAT(http_current_cache_connections_stat);
      cache_read_vc->do_io_close();
      cache_read_vc = nullptr;
    }
  }
  inline void
  end_both()
  {
    // We close the read so that cache
    //   records its stats
    close_read();
    abort_write();
  }
private:
  void do_schedule_in();
  Action *do_cache_open_read(const HttpCacheKey &);
  int state_cache_open_read(int event, void *data);
  int state_cache_open_write(int event, void *data);
  HttpCacheAction captive_action;
  bool open_read_cb  = false;
  bool open_write_cb = false;
  // Open read parameters
  int open_read_tries                            = 0;
  HTTPHdr *read_request_hdr                      = nullptr;
  const OverridableHttpConfigParams *http_params = nullptr;
  time_t read_pin_in_cache                       = 0;
  // Open write parameters
  bool retry_write     = true;
  int open_write_tries = 0;
  // Common parameters
  URL *lookup_url = nullptr;
  HttpCacheKey cache_key;
  // to keep track of multiple cache lookups
  int lookup_max_recursive = 0;
  int current_lookup_level = 0;
};
 |