Exemplo n.º 1
0
bool Transport::splitHeader(CStrRef header, String &name, const char *&value) {
  int pos = header.find(':');

  if (pos != String::npos) {
    name = header.substr(0, pos);
    value = header.data() + pos;

    do {
      value++;
    } while (*value == ' ');

    return true;
  }

  // header("HTTP/1.0 404 Not Found");
  // header("HTTP/1.0 404");
  if (strncasecmp(header.data(), "http/", 5) == 0) {
    int pos1 = header.find(' ');
    if (pos1 != String::npos) {
      int pos2 = header.find(' ', pos1 + 1);
      if (pos2 == String::npos) pos2 = header.size();
      if (pos2 - pos1 > 1) {
        setResponse(atoi(header.data() + pos1),
                    getResponseInfo().empty() ? "splitHeader"
                                              : getResponseInfo().c_str()
                   );
        return false;
      }
    }
  }

  throw InvalidArgumentException("header", header.c_str());
}
Exemplo n.º 2
0
Variant f_substr_count(CStrRef haystack, CStrRef needle, int offset /* = 0 */,
                       int length /* = 0x7FFFFFFF */) {
  int lenNeedle = needle.size();
  if (lenNeedle == 0) {
    throw_invalid_argument("needle: (empty)");
    return false;
  }

  if (offset < 0 || offset > haystack.size()) {
    throw_invalid_argument("offset: (out of range)");
    return false;
  }
  if (length == 0x7FFFFFFF) {
    length = haystack.size() - offset;
  } else if (length <= 0 || length > haystack.size() - offset) {
    throw_invalid_argument("length: (out of range)");
    return false;
  }

  int count = 0;
  int posMax = offset + length - lenNeedle;
  for (int pos = haystack.find(needle, offset);
       pos != -1 && pos <= posMax;
       pos = haystack.find(needle, pos + lenNeedle)) {
    ++count;
  }
  return count;
}
Exemplo n.º 3
0
Variant f_stripos(CStrRef haystack, CVarRef needle, int offset /* = 0 */) {
  int pos;
  if (needle.isString()) {
    pos = haystack.find(needle.toString(), offset, false);
  } else {
    pos = haystack.find(needle.toByte(), offset, false);
  }
  if (pos >= 0) return pos;
  return false;
}
Exemplo n.º 4
0
Variant StringUtil::Explode(CStrRef input, CStrRef delimiter,
                            int limit /* = 0x7FFFFFFF */) {
  if (delimiter.empty()) {
    throw_invalid_argument("delimiter: (empty)");
    return false;
  }

  Array ret;
  int pos = input.find(delimiter);
  if (limit >= 0) {
    if (pos < 0) {
      ret.append(input);
    } else {
      int len = delimiter.size();
      int pos0 = 0;
      do {
        ret.append(input.substr(pos0, pos - pos0));
        pos += len;
        pos0 = pos;
      } while ((pos = input.find(delimiter, pos)) >= 0 && --limit > 1);

      if (pos0 <= input.size()) {
        ret.append(input.substr(pos0));
      }
    }
  } else if (pos >= 0) {
    vector<int> positions;
    int len = delimiter.size();
    int pos0 = 0;
    int found = 0;
    do {
      positions.push_back(pos0);
      positions.push_back(pos - pos0);
      pos += len;
      pos0 = pos;
      found++;
    } while ((pos = input.find(delimiter, pos)) >= 0);

    if (pos0 <= input.size()) {
      positions.push_back(pos0);
      positions.push_back(input.size() - pos0);
      found++;
    }
    int iMax = (found + limit) << 1;
    for (int i = 0; i < iMax; i += 2) {
      ret.append(input.substr(positions[i], positions[i+1]));
    }

  } // else we have negative limit and delimiter not found, returning empty arr

  return ret;
}
Exemplo n.º 5
0
Variant f_strpos(CStrRef haystack, CVarRef needle, int offset /* = 0 */) {
  int pos;
  if (needle.isString()) {
    String n(needle.toString());
    if (n.length() == 0) {
      raise_warning("Empty delimiter");
      return false;
    }
    pos = haystack.find(n, offset);
  } else {
    pos = haystack.find(needle.toByte(), offset);
  }
  if (pos >= 0) return pos;
  return false;
}
Exemplo n.º 6
0
bool f_putenv(CStrRef setting) {
  int pos = setting.find('=');
  if (pos >= 0) {
    String name = setting.substr(0, pos);
    String value = setting.substr(pos + 1);
    g_context->setenv(name, value);
    return true;
  }
  return false;
}
Exemplo n.º 7
0
static void parse_host(CStrRef address, String &host, int &port) {
  int pos = address.find(':');
  if (pos >= 0) {
    host = address.substr(0, pos);
    port = address.substr(pos + 1).toInt16();
  } else {
    host = address;
    port = 0;
  }
}
Exemplo n.º 8
0
static void parse_socket(CStrRef socket, String &protocol, String &host,
                         int &port) {
  String address;
  int pos = socket.find("://");
  if (pos >= 0) {
    protocol = socket.substr(0, pos);
    address = socket.substr(pos + 3);
  } else {
    protocol = "tcp";
    address = socket;
  }

  parse_host(address, host, port);
}
Exemplo n.º 9
0
// Extract host:port pair.
// 192.168.1.1:80 -> 192.168.1.0 80
// [2a03:2880::1]:80 -> [2a03:2880::1] 80
static void parse_host(CStrRef address, String &host, int &port) {

  if (address.find('[') != std::string::npos) {
    auto epos = address.rfind(']');
    if (epos != std::string::npos) {
      auto cpos = address.rfind(':', epos);
      if (cpos != std::string::npos) {
        port = address.substr(cpos + 1).toInt16();
      }
    }
    host = address.substr(0, epos + 1);
  } else {
    auto cpos = address.rfind(':');
    if (cpos != std::string::npos) {
      host = address.substr(0, cpos);
      port = address.substr(cpos + 1).toInt16();
    } else {
      host = address;
      port = 0;
    }
  }
}
Exemplo n.º 10
0
bool File::IsPlainFilePath(CStrRef filename) {
  return filename.find("://") == String::npos;
}