aboutsummaryrefslogtreecommitdiffstats
path: root/apps/higet/higet.cc
blob: 194e616ed65105e547e8a923b454faa7985fb645 (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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (c) 2020 Cisco and/or its affiliates.
 * Licensed 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 <hicn/transport/http/client_connection.h>

#include <algorithm>
#include <fstream>
#include <functional>
#include <map>

#define ASIO_STANDALONE
#include <asio.hpp>
#undef ASIO_STANDALONE

#include <thread>

typedef std::chrono::time_point<std::chrono::system_clock> Time;
typedef std::chrono::milliseconds TimeDuration;

Time t1;

#define DEFAULT_BETA 0.99
#define DEFAULT_GAMMA 0.07

namespace http {

typedef struct {
  std::string file_name;
  bool print_headers;
  std::string producer_certificate;
  std::string ipv6_first_word;
} Configuration;

class ReadBytesCallbackImplementation
    : public transport::http::HTTPClientConnection::ReadBytesCallback {
  static std::string chunk_separator;

 public:
  ReadBytesCallbackImplementation(std::string file_name, long yet_downloaded)
      : file_name_(file_name),
        temp_file_name_(file_name_ + ".temp"),
        yet_downloaded_(yet_downloaded),
        byte_downloaded_(yet_downloaded),
        chunked_(false),
        chunk_size_(0),
        work_(std::make_unique<asio::io_service::work>(io_service_)),
        thread_(
            std::make_unique<std::thread>([this]() { io_service_.run(); })) {
    std::streambuf *buf;
    if (file_name_ != "-") {
      of_.open(temp_file_name_, std::ofstream::binary | std::ofstream::app);
      buf = of_.rdbuf();
    } else {
      buf = std::cout.rdbuf();
    }

    out_ = new std::ostream(buf);
  }

  ~ReadBytesCallbackImplementation() {
    if (thread_->joinable()) {
      thread_->join();
    }
  }

  void onBytesReceived(std::unique_ptr<utils::MemBuf> &&buffer) {
    auto buffer_ptr = buffer.release();
    io_service_.post([this, buffer_ptr]() {
      auto buffer = std::unique_ptr<utils::MemBuf>(buffer_ptr);
      std::unique_ptr<utils::MemBuf> payload;
      if (!first_chunk_read_) {
        transport::http::HTTPResponse http_response(std::move(buffer));
        payload = http_response.getPayload();
        auto header = http_response.getHeaders();
        content_size_ = yet_downloaded_;
        std::map<std::string, std::string>::iterator it =
            header.find("Content-Length");
        if (it != header.end()) {
          content_size_ += std::stol(it->second);
        } else {
          it = header.find("Transfer-Encoding");
          if (it != header.end() && it->second.compare("chunked") == 0) {
            chunked_ = true;
          }
        }
        first_chunk_read_ = true;
      } else {
        payload = std::move(buffer);
      }

      if (chunked_) {
        if (chunk_size_ > 0) {
          out_->write((char *)payload->data(), chunk_size_);
          payload->trimStart(chunk_size_);

          if (payload->length() >= chunk_separator.size()) {
            payload->trimStart(chunk_separator.size());
          }
        }

        while (payload->length() > 0) {
          // read next chunk size
          const char *begin = (const char *)payload->data();
          const char *end = (const char *)payload->tail();
          const char *begincrlf2 = (const char *)chunk_separator.c_str();
          const char *endcrlf2 = begincrlf2 + chunk_separator.size();
          auto it = std::search(begin, end, begincrlf2, endcrlf2);
          if (it != end) {
            chunk_size_ = std::stoul(begin, 0, 16);
            content_size_ += chunk_size_;
            payload->trimStart(it + chunk_separator.size() - begin);

            std::size_t to_write;
            if (payload->length() >= chunk_size_) {
              to_write = chunk_size_;
            } else {
              to_write = payload->length();
              chunk_size_ -= payload->length();
            }

            out_->write((char *)payload->data(), to_write);
            byte_downloaded_ += to_write;
            payload->trimStart(to_write);

            if (payload->length() >= chunk_separator.size()) {
              payload->trimStart(chunk_separator.size());
            }
          }
        }
      } else {
        out_->write((char *)payload->data(), payload->length());
        byte_downloaded_ += payload->length();
      }

      if (file_name_ != "-") {
        print_bar(byte_downloaded_, content_size_, false);
      }
    });
  }

  void onSuccess(std::size_t bytes) {
    io_service_.post([this, bytes]() {
      if (file_name_ != "-") {
        of_.close();
        delete out_;
        std::size_t found = file_name_.find_last_of(".");
        std::string name = file_name_.substr(0, found);
        std::string extension = file_name_.substr(found + 1);
        if (!exists_file(file_name_)) {
          std::rename(temp_file_name_.c_str(), file_name_.c_str());
        } else {
          int i = 1;
          std::ostringstream sstream;
          sstream << name << "(" << i << ")." << extension;
          std::string final_name = sstream.str();
          while (exists_file(final_name)) {
            i++;
            sstream.str("");
            sstream << name << "(" << i << ")." << extension;
            final_name = sstream.str();
          }
          std::rename(temp_file_name_.c_str(), final_name.c_str());
        }

        print_bar(100, 100, true);
        std::cout << "\nDownloaded " << bytes << " bytes" << std::endl;
      }
      work_.reset();
    });
  }

  void onError(const std::error_code ec) {
    io_service_.post([this]() {
      of_.close();
      delete out_;
      work_.reset();
    });
  }

 private:
  bool exists_file(const std::string &name) {
    std::ifstream f(name.c_str());
    return f.good();
  }

  void print_bar(long value, long max_value, bool last) {
    float progress = (float)value / max_value;
#ifdef _WIN32
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    int barWidth = csbi.srWindow.Right - csbi.srWindow.Left + 7;
#else
    struct winsize size;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
    int barWidth = size.ws_col - 8;
#endif

    std::cout << "[";
    int pos = barWidth * progress;
    for (int i = 0; i < barWidth; ++i) {
      if (i < pos) {
        std::cout << "=";
      } else if (i == pos) {
        std::cout << ">";
      } else {
        std::cout << " ";
      }
    }
    if (last) {
      std::cout << "] " << int(progress * 100.0) << " %" << std::endl
                << std::endl;
    } else {
      std::cout << "] " << int(progress * 100.0) << " %\r";
      std::cout.flush();
    }
  }

 private:
  std::string file_name_;
  std::string temp_file_name_;
  std::ostream *out_;
  std::ofstream of_;
  long yet_downloaded_;
  long content_size_;
  bool first_chunk_read_ = false;
  long byte_downloaded_ = 0;
  bool chunked_;
  std::size_t chunk_size_;
  asio::io_service io_service_;
  std::unique_ptr<asio::io_service::work> work_;
  std::unique_ptr<std::thread> thread_;
};

std::string ReadBytesCallbackImplementation::chunk_separator = "\r\n";

long checkFileStatus(std::string file_name) {
  struct stat stat_buf;
  std::string temp_file_name_ = file_name + ".temp";
  int rc = stat(temp_file_name_.c_str(), &stat_buf);
  return rc == 0 ? stat_buf.st_size : -1;
}

void usage(char *program_name) {
  std::cerr << "usage:" << std::endl;
  std::cerr << program_name << " [option]... [url]..." << std::endl;
  std::cerr << program_name << " options:" << std::endl;
  std::cerr
      << "-O <out_put_path>            = write documents to <out_put_file>"
      << std::endl;
  std::cerr << "-S                          = print server response"
            << std::endl;
  std::cerr << "-P                          = first word of the ipv6 name of "
               "the response"
            << std::endl;
  std::cerr << "example:" << std::endl;
  std::cerr << "\t" << program_name << " -O - http://origin/index.html"
            << std::endl;
  exit(EXIT_FAILURE);
}

int main(int argc, char **argv) {
#ifdef _WIN32
  WSADATA wsaData = {0};
  WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif

  Configuration conf;
  conf.file_name = "";
  conf.print_headers = false;
  conf.producer_certificate = "";
  conf.ipv6_first_word = "b001";

  std::string name("http://webserver/sintel/mpd");

  int opt;
  while ((opt = getopt(argc, argv, "O:Sc:P:")) != -1) {
    switch (opt) {
      case 'O':
        conf.file_name = optarg;
        break;
      case 'S':
        conf.print_headers = true;
        break;
      case 'c':
        conf.producer_certificate = optarg;
        break;
      case 'P':
        conf.ipv6_first_word = optarg;
        break;
      case 'h':
      default:
        usage(argv[0]);
        break;
    }
  }

  if (!argv[optind]) {
    usage(argv[0]);
  }

  name = argv[optind];
  std::cerr << "Using name " << name << " and name first word "
            << conf.ipv6_first_word << std::endl;

  if (conf.file_name.empty()) {
    conf.file_name = name.substr(1 + name.find_last_of("/"));
  }

  long yetDownloaded = checkFileStatus(conf.file_name);

  std::map<std::string, std::string> headers;
  if (yetDownloaded == -1) {
    headers = {{"Host", "localhost"},
               {"User-Agent", "higet/1.0"},
               {"Connection", "Keep-Alive"}};
  } else {
    std::string range;
    range.append("bytes=");
    range.append(std::to_string(yetDownloaded));
    range.append("-");
    headers = {{"Host", "localhost"},
               {"User-Agent", "higet/1.0"},
               {"Connection", "Keep-Alive"},
               {"Range", range}};
  }

  transport::http::HTTPClientConnection connection;

  if (!conf.producer_certificate.empty()) {
    std::shared_ptr<transport::auth::Verifier> verifier =
        std::make_shared<transport::auth::AsymmetricVerifier>(
            conf.producer_certificate);
    connection.setVerifier(verifier);
  }

  t1 = std::chrono::system_clock::now();

  http::ReadBytesCallbackImplementation readBytesCallback(conf.file_name,
                                                          yetDownloaded);

  connection.get(name, headers, {}, nullptr, &readBytesCallback,
                 conf.ipv6_first_word);

#ifdef _WIN32
  WSACleanup();
#endif

  return EXIT_SUCCESS;
}

}  // end namespace http

int main(int argc, char **argv) { return http::main(argc, argv); }