Exemplo n.º 1
0
 bool readFile(const req::ptr<File>& file) {
   mode = DESC_FILE;
   childend = dup(file->fd());
   if (childend < 0) {
     raise_warning("unable to dup File-Handle for descriptor %d - %s",
                     index, folly::errnoStr(errno).c_str());
     return false;
   }
   return true;
 }
Exemplo n.º 2
0
SSLSocket::SSLSocket(int sockfd, int type, const req::ptr<StreamContext>& ctx,
                     const char *address /* = NULL */, int port /* = 0 */)
: Socket(std::make_shared<SSLSocketData>(port, type), sockfd, type, address, port),
  m_data(static_cast<SSLSocketData*>(getSocketData()))
{
  if (!ctx) {
    return;
  }
  this->setStreamContext(ctx);
  this->m_context = ctx->getOptions()[s_ssl].toArray();
}
Exemplo n.º 3
0
void find_var_recursive(const TypedValue* tv,
                        const req::ptr<WddxPacket>& wddxPacket) {
  if (tvIsString(tv)) {
    String var_name{tvCastToString(tv)};
    wddxPacket->add_var(var_name, true);
  }
  if (isArrayType(tv->m_type)) {
    for (ArrayIter iter(tv->m_data.parr); iter; ++iter) {
      find_var_recursive(iter.secondRef().asTypedValue(), wddxPacket);
    }
  }
}
Exemplo n.º 4
0
// Check for file descriptors >= FD_SETSIZE
// which can't be returned in an fdset
// This is a little hacky, but necessary given cURL's APIs
int CurlMultiAwait::addHighHandles(req::ptr<CurlMultiResource> multi) {
  int count = 0;
  auto easy_handles = multi->getEasyHandles();
  for (ArrayIter iter(easy_handles); iter; ++iter) {
    Variant easy_handle = iter.second();
    auto easy = dyn_cast_or_null<CurlResource>(easy_handle);
    if (!easy) continue;
    long sock;
    if ((curl_easy_getinfo(easy->get(),
                           CURLINFO_LASTSOCKET, &sock) != CURLE_OK) ||
        (sock < FD_SETSIZE)) {
      continue;
    }
    // No idea which type of event it needs, ask for everything
    addHandle(sock, AsioEventHandler::READ_WRITE);
    ++count;
  }
  return count;
}
Exemplo n.º 5
0
// Ask curl_multi for its handles directly
// This is preferable as we get to know which
// are blocking on reads, and which on writes.
int CurlMultiAwait::addLowHandles(req::ptr<CurlMultiResource> multi) {
  fd_set read_fds, write_fds;
  int max_fd = -1, count = 0;
  FD_ZERO(&read_fds); FD_ZERO(&write_fds);
  if ((CURLM_OK != curl_multi_fdset(multi->get(), &read_fds, &write_fds,
                                    nullptr, &max_fd)) ||
      (max_fd < 0)) {
    return count;
  }
  for (int i = 0 ; i <= max_fd; ++i) {
    int events = 0;
    if (FD_ISSET(i, &read_fds))  events |= AsioEventHandler::READ;
    if (FD_ISSET(i, &write_fds)) events |= AsioEventHandler::WRITE;
    if (events) {
      addHandle(i, events);
      ++count;
    }
  }
  return count;
}
Exemplo n.º 6
0
req::ptr<File>
HttpStreamWrapper::open(const String& filename,
                        const String& mode,
                        int options,
                        const req::ptr<StreamContext>& context) {
  if (RuntimeOption::ServerHttpSafeMode && !is_cli_mode()) {
    return nullptr;
  }

  if (strncmp(filename.data(), "http://",  sizeof("http://")  - 1) &&
      strncmp(filename.data(), "https://", sizeof("https://") - 1)) {
    return nullptr;
  }

  Array headers;
  String method = s_GET;
  String post_data = null_string;
  String proxy_host;
  String proxy_user;
  String proxy_pass;
  int proxy_port = -1;
  int max_redirs = 20;
  int timeout = -1;
  bool ignore_errors = false;

  if (context && !context->getOptions().isNull() &&
      !context->getOptions()[s_http].isNull()) {
    Array opts = context->getOptions()[s_http].toArray();
    if (opts.exists(s_method)) {
      method = opts[s_method].toString();
    }
    if (opts.exists(s_header)) {
      Array lines;
      if (opts[s_header].isString()) {
        lines = StringUtil::Explode(
          opts[s_header].toString(), "\r\n").toArray();
      } else if (opts[s_header].isArray()) {
        lines = opts[s_header];
      }

      for (ArrayIter it(lines); it; ++it) {
        Array parts = StringUtil::Explode(
          it.second().toString(), ":", 2).toArray();
        headers.set(parts.rvalAt(0), parts.rvalAt(1));
      }
    }
    if (opts.exists(s_user_agent) && !headers.exists(s_User_Agent)) {
      headers.set(s_User_Agent, opts[s_user_agent]);
    }
    if (opts.exists(s_max_redirects)) {
      max_redirs = opts[s_max_redirects].toInt64();
    }
    if (opts.exists(s_timeout)) {
      timeout = opts[s_timeout].toInt64();
    }
    if (opts.exists(s_ignore_errors)) {
      ignore_errors = opts[s_ignore_errors].toBoolean();
    }
    if (opts.exists(s_proxy)) {
      Variant host = f_parse_url(opts[s_proxy].toString(), k_PHP_URL_HOST);
      Variant port = f_parse_url(opts[s_proxy].toString(), k_PHP_URL_PORT);
      if (!same(host, false) && !same(port, false)) {
        proxy_host = host.toString();
        proxy_port = port.toInt64();
        Variant user = f_parse_url(opts[s_proxy].toString(), k_PHP_URL_USER);
        Variant pass = f_parse_url(opts[s_proxy].toString(), k_PHP_URL_PASS);
        if (!same(user, false) && !same(pass, false)) {
          proxy_user = user.toString();
          proxy_pass = pass.toString();
        }
      }
    }
    post_data = opts[s_content].toString();
  }

  if (!headers.exists(s_User_Agent)) {
    auto default_user_agent = ThreadInfo::s_threadInfo.getNoCheck()
      ->m_reqInjectionData.getUserAgent();
    if (!default_user_agent.empty()) {
      headers.set(s_User_Agent, default_user_agent);
    }
  }
  auto file = req::make<UrlFile>(method.data(), headers,
                                    post_data, max_redirs,
                                    timeout, ignore_errors);
  file->setStreamContext(context);
  file->setProxy(proxy_host, proxy_port, proxy_user, proxy_pass);
  bool ret = file->open(filename, mode);
  if (!ret) {
    raise_warning("Failed to open %s (%s)", filename.data(),
                  file->getLastError().c_str());
    return nullptr;
  }
  return file;
}
Exemplo n.º 7
0
PDOPgSqlStatement::PDOPgSqlStatement(
  req::ptr<PDOPgSqlResource> conn, PQ::Connection* server
) : m_conn(conn->conn()), m_server(server),
  m_result(), m_isPrepared(false), m_current_row(0) {
  this->dbh = cast<PDOResource>(conn);
}