<commit_before>/*

This file is part of VROOM.

Copyright (c) 2015-2021, <NAME>.
All rights reserved (see LICENSE).

*/

#include <asio.hpp>
#include <asio/ssl.hpp>

#include "routing/http_wrapper.h"

using asio::ip::tcp;

namespace vroom {
namespace routing {

const std::string HttpWrapper::HTTPS_PORT = "443";

HttpWrapper::HttpWrapper(const std::string& profile,
                         const Server& server,
                         const std::string& matrix_service,
                         const std::string& matrix_durations_key,
                         const std::string& route_service,
                         const std::string& extra_args)
  : Wrapper(profile),
    _server(server),
    _matrix_service(matrix_service),
    _matrix_durations_key(matrix_durations_key),
    _route_service(route_service),
    _extra_args(extra_args) {
}

std::string HttpWrapper::send_then_receive(const std::string& query) const {
  std::string response;

  try {
    asio::io_service io_service;

    tcp::resolver r(io_service);

    tcp::resolver::query q(_server.host, _server.port);

    tcp::socket s(io_service);
    asio::connect(s, r.resolve(q));

    asio::write(s, asio::buffer(query));

    char buf[512];
    std::error_code error;
    // Using Valhalla seems to hang on s.read_some below even with
    // full response retrieved, so we never reach the asio::error::eof
    // check as expected. Instead, we spot the content start and
    // retrieve Content-Length as soon as it's been read, then stop
    // based on the content length.
    std::size_t json_start = 0;
    std::size_t content_length = 0;
    for (;;) {
      std::size_t len = s.read_some(asio::buffer(buf), error);
      response.append(buf, len);

      if (json_start == 0) {
        auto start = response.find("{");
        if (start != std::string::npos) {
          json_start = start;

          std::string cl = "Content-Length: ";
          auto cl_start = response.find(cl);
          assert(cl_start != std::string::npos);
          cl_start += cl.size();

          auto cl_end = response.find('\n', cl_start);
          std::string content = response.substr(cl_start, cl_end - cl_start);

          content_length =
            std::stoul(response.substr(cl_start, cl_end - cl_start));
        }
      }

      if (error == asio::error::eof) {
        // Connection closed cleanly.
        break;
      } else {
        if (error) {
          throw std::system_error(error);
        }
      }

      if (content_length == response.size() - json_start) {
        // All expected json content received.
        break;
      }
    }
  } catch (std::system_error& e) {
    throw Exception(ERROR::ROUTING,
                    "Failed to connect to " + _server.host + ":" +
                      _server.port);
  }<commit_msg>Revert "Hack based on Content-Length to spot end of response with Valhalla."<commit_after>/*

This file is part of VROOM.

Copyright (c) 2015-2021, <NAME>.
All rights reserved (see LICENSE).

*/

#include <asio.hpp>
#include <asio/ssl.hpp>

#include "routing/http_wrapper.h"

using asio::ip::tcp;

namespace vroom {
namespace routing {

const std::string HttpWrapper::HTTPS_PORT = "443";

HttpWrapper::HttpWrapper(const std::string& profile,
                         const Server& server,
                         const std::string& matrix_service,
                         const std::string& matrix_durations_key,
                         const std::string& route_service,
                         const std::string& extra_args)
  : Wrapper(profile),
    _server(server),
    _matrix_service(matrix_service),
    _matrix_durations_key(matrix_durations_key),
    _route_service(route_service),
    _extra_args(extra_args) {
}

std::string HttpWrapper::send_then_receive(const std::string& query) const {
  std::string response;

  try {
    asio::io_service io_service;

    tcp::resolver r(io_service);

    tcp::resolver::query q(_server.host, _server.port);

    tcp::socket s(io_service);
    asio::connect(s, r.resolve(q));

    asio::write(s, asio::buffer(query));

    char buf[512];
    std::error_code error;
    for (;;) {
      std::size_t len = s.read_some(asio::buffer(buf), error);
      response.append(buf, len);
      if (error == asio::error::eof) {
        // Connection closed cleanly.
        break;
      } else {
        if (error) {
          throw std::system_error(error);
        }
      }
    }
  } catch (std::system_error& e) {
    throw Exception(ERROR::ROUTING,
                    "Failed to connect to " + _server.host + ":" +
                      _server.port);
  }
{'fraction_non_alphanumeric': 0.12635457303857825, 'fraction_numerical': 0.006718682271348071, 'mean_word_length': 2.4569288389513106, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1335243', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1399, 'n_words': 327}