File: dispatch.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (272 lines) | stat: -rw-r--r-- 7,253 bytes parent folder | download | duplicates (2)
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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/** @file

  Multiplexes request to other origins.

  @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.
 */
#include <cinttypes>
#include <sys/time.h>

#include "dispatch.h"
#include "fetcher.h"
#include "original-request.h"

#ifndef PLUGIN_TAG
#error Please define a PLUGIN_TAG before including this file.
#endif

extern Statistics statistics;

size_t timeout;

Request::Request(const std::string &h, const TSMBuffer b, const TSMLoc l) : host(h), length(0), io(new ats::io::IO())
{
  assert(!host.empty());
  assert(b != nullptr);
  assert(l != nullptr);
  assert(io.get() != nullptr);
  TSHttpHdrPrint(b, l, io->buffer);
  length = TSIOBufferReaderAvail(io->reader);
  assert(length > 0);
  /*
   * TSHttpHdrLengthGet returns the size with possible "internal" headers
   * which are not printed by TSHttpHdrPrint.
   * Therefore the greater than or equal comparison
   */
  assert(TSHttpHdrLengthGet(b, l) >= length);
}

Request::Request(Request &&that) : host(std::move(that.host)), length(that.length), io(std::move(that.io))
{
  assert(!host.empty());
  assert(length > 0);
  assert(io.get() != nullptr);
}

Request &
Request::operator=(const Request &r)
{
  host   = r.host;
  length = r.length;
  io.reset(const_cast<Request &>(r).io.release());
  assert(!host.empty());
  assert(length > 0);
  assert(io.get() != nullptr);
  assert(r.io.get() == nullptr);
  return *this;
}

uint64_t
copy(const TSIOBufferReader &r, const TSIOBuffer b)
{
  assert(r != nullptr);
  assert(b != nullptr);
  TSIOBufferBlock block = TSIOBufferReaderStart(r);

  uint64_t length = 0;

  for (; block; block = TSIOBufferBlockNext(block)) {
    int64_t size              = 0;
    const void *const pointer = TSIOBufferBlockReadStart(block, r, &size);

    if (pointer != nullptr && size > 0) {
      auto const num_written = TSIOBufferWrite(b, pointer, size);
      if (num_written != size) {
        TSError("[" PLUGIN_TAG "] did not write the expected number of body bytes. "
                "Wrote: %" PRId64 ", expected: %" PRId64,
                num_written, size);
      }
      length += num_written;
    }
  }

  return length;
}

uint64_t
read(const TSIOBufferReader &r, std::string &o, int64_t l = 0)
{
  assert(r != nullptr);
  TSIOBufferBlock block = TSIOBufferReaderStart(r);

  assert(l >= 0);
  if (l == 0) {
    l = TSIOBufferReaderAvail(r);
    assert(l >= 0);
  }

  uint64_t length = 0;

  for (; block && l > 0; block = TSIOBufferBlockNext(block)) {
    int64_t size              = 0;
    const char *const pointer = TSIOBufferBlockReadStart(block, r, &size);
    if (pointer != nullptr && size > 0) {
      size = std::min(size, l);
      o.append(pointer, size);
      length += size;
      l -= size;
    }
  }

  return length;
}

uint64_t
read(const TSIOBuffer &b, std::string &o, const int64_t l = 0)
{
  TSIOBufferReader reader = TSIOBufferReaderAlloc(b);
  const uint64_t length   = read(reader, o);
  TSIOBufferReaderFree(reader);
  return length;
}

class Handler
{
  int64_t length;
  struct timeval start;
  std::string response;

public:
  const std::string url;

  Handler(std::string u) : length(0)
  {
    assert(!u.empty());
    const_cast<std::string &>(url).swap(u);
    gettimeofday(&start, nullptr);
  }

  void
  error()
  {
    TSError("[" PLUGIN_TAG "] error when communicating with \"%s\"\n", url.c_str());
    TSStatIntIncrement(statistics.failures, 1);
  }

  void
  timeout()
  {
    TSError("[" PLUGIN_TAG "] timeout when communicating with \"%s\"\n", url.c_str());
    TSStatIntIncrement(statistics.timeouts, 1);
  }

  void
  header(const TSMBuffer b, const TSMLoc l)
  {
    if (TSIsDebugTagSet(PLUGIN_TAG) > 0) {
      const TSIOBuffer buffer = TSIOBufferCreate();
      TSHttpHdrPrint(b, l, buffer);
      std::string buf;
      read(buffer, buf);
      TSDebug(PLUGIN_TAG, "Response header for \"%s\" was:\n%s", url.c_str(), buf.c_str());
      TSIOBufferDestroy(buffer);
    }
  }

  void
  data(const TSIOBufferReader r, const int64_t l)
  {
    length += l;
    if (TSIsDebugTagSet(PLUGIN_TAG) > 0) {
      std::string buffer;
      const uint64_t length = read(r, buffer, l);
      response += buffer;
      TSDebug(PLUGIN_TAG, "Receiving response chunk \"%s\" of %" PRIu64 " bytes", buffer.c_str(), length);
    }
  }

  void
  done()
  {
    struct timeval end;

    gettimeofday(&end, nullptr);

    if (TSIsDebugTagSet(PLUGIN_TAG) > 0) {
      TSDebug(PLUGIN_TAG, "Response for \"%s\" was:\n%s", url.c_str(), response.c_str());
    }

    const long diff = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);

    TSStatIntIncrement(statistics.hits, 1);
    TSStatIntIncrement(statistics.time, diff);
    TSStatIntIncrement(statistics.size, length);
  }
};

void
generateRequests(const Origins &o, const TSMBuffer buffer, const TSMLoc location, Requests &r)
{
  assert(!o.empty());
  assert(buffer != nullptr);
  assert(location != nullptr);

  Origins::const_iterator iterator  = o.begin();
  const Origins::const_iterator end = o.end();

  OriginalRequest request(buffer, location);
  request.urlScheme("");
  request.urlHost("");
  request.xMultiplexerHeader("copy");

  for (; iterator != end; ++iterator) {
    const std::string &host = *iterator;
    assert(!host.empty());
    request.hostHeader(host);
    r.push_back(Request(host, buffer, location));
  }
}

void
addBody(Requests &r, const TSIOBufferReader re)
{
  assert(re != nullptr);
  Requests::iterator iterator  = r.begin();
  const Requests::iterator end = r.end();
  const int64_t length         = TSIOBufferReaderAvail(re);
  if (length == 0) {
    return;
  }
  assert(length > 0);
  for (; iterator != end; ++iterator) {
    assert(iterator->io.get() != nullptr);
    const int64_t size = copy(re, iterator->io->buffer);
    assert(size == length);
    iterator->length += size;
  }
}

void
dispatch(Requests &r, const int t)
{
  Requests::iterator iterator  = r.begin();
  const Requests::iterator end = r.end();
  for (; iterator != end; ++iterator) {
    assert(iterator->io.get() != nullptr);
    if (TSIsDebugTagSet(PLUGIN_TAG) > 0) {
      TSDebug(PLUGIN_TAG, "Dispatching %i bytes to \"%s\"", iterator->length, iterator->host.c_str());
      std::string b;
      read(iterator->io->reader, b);
      assert(b.size() == static_cast<uint64_t>(iterator->length));
      TSDebug(PLUGIN_TAG, "%s", b.c_str());
    }
    // forwarding iterator->io pointer ownership
    ats::get(iterator->io.release(), iterator->length, Handler(iterator->host), t);
  }
}