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; } }
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; }
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; }
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); }
static bool findFileWrapper(CStrRef file, void* ctx) { ResolveIncludeContext* context = (ResolveIncludeContext*)ctx; assert(context->path.isNull()); Stream::Wrapper* w = Stream::getWrapperFromURI(file); if (!dynamic_cast<FileStreamWrapper*>(w)) { if (w->stat(file, context->s) == 0) { context->path = file; return true; } } // handle file:// if (file.substr(0, 7) == s_file_url) { return findFileWrapper(file.substr(7), ctx); } // TranslatePath() will canonicalize the path and also check // whether the file is in an allowed directory. String translatedPath = File::TranslatePathKeepRelative(file); if (file[0] != '/') { if (HPHP::Eval::FileRepository::findFile(translatedPath.get(), context->s)) { context->path = translatedPath; return true; } return false; } if (RuntimeOption::SandboxMode || !RuntimeOption::AlwaysUseRelativePath) { if (HPHP::Eval::FileRepository::findFile(translatedPath.get(), context->s)) { context->path = translatedPath; return true; } } string server_root(SourceRootInfo::GetCurrentSourceRoot()); if (server_root.empty()) { server_root = string(g_vmContext->getCwd()->data()); if (server_root.empty() || server_root[server_root.size() - 1] != '/') { server_root += "/"; } } String rel_path(Util::relativePath(server_root, translatedPath.data())); if (HPHP::Eval::FileRepository::findFile(rel_path.get(), context->s)) { context->path = rel_path; return true; } return false; }
Variant f_json_decode(CStrRef json, bool assoc /* = false */, CVarRef options /* = 0 */) { if (json.empty()) { return uninit_null(); } int64_t json_options = options.toInt64(); if (options.isBoolean() && options.toBooleanVal()) { json_options = k_JSON_FB_LOOSE; } Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, (json_options & k_JSON_FB_LOOSE))) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) return uninit_null(); if (!strcasecmp(json.data(), "true")) return true; } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { return false; } int64_t p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { return p; } else if (type == KindOfDouble) { return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { return json.substr(1, json.size() - 2); } if ((json_options & k_JSON_FB_LOOSE) && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { return json.substr(1, json.size() - 2); } if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */ return uninit_null(); } return json; }
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()); }
Variant f_stristr(CStrRef haystack, CVarRef needle) { Variant ret = f_stripos(haystack, needle); if (same(ret, false)) { return false; } return haystack.substr(ret.toInt32()); }
Variant f_json_decode(CStrRef json, bool assoc /* = false */, bool loose /* = false */) { if (json.empty()) { return null; } Variant z; if (JSON_parser(z, json.data(), json.size(), assoc, loose)) { return z; } if (json.size() == 4) { if (!strcasecmp(json.data(), "null")) return null; if (!strcasecmp(json.data(), "true")) return true; } else if (json.size() == 5 && !strcasecmp(json.data(), "false")) { return false; } int64 p; double d; DataType type = json->isNumericWithVal(p, d, 0); if (type == KindOfInt64) { return p; } else if (type == KindOfDouble) { return d; } char ch0 = json.charAt(0); if (json.size() > 1 && ch0 == '"' && json.charAt(json.size() - 1) == '"') { return json.substr(1, json.size() - 2); } if (loose && json.size() > 1 && ch0 == '\'' && json.charAt(json.size() - 1) == '\'') { return json.substr(1, json.size() - 2); } if (ch0 == '{' || ch0 == '[') { /* invalid JSON string */ return null; } return json; }
// 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; } } }
Array StringUtil::Split(CStrRef str, int split_length /* = 1 */) { if (split_length <= 0) { throw InvalidArgumentException("split_length", "(non-positive)"); } Array ret; int len = str.size(); if (split_length >= len) { ret.append(str); } else { for (int i = 0; i < len; i += split_length) { ret.append(str.substr(i, split_length)); } } return ret; }
Variant StringUtil::Split(CStrRef str, int split_length /* = 1 */) { if (split_length <= 0) { throw_invalid_argument("split_length: (non-positive)"); return false; } Array ret; int len = str.size(); if (split_length >= len) { ret.append(str); } else { for (int i = 0; i < len; i += split_length) { ret.append(str.substr(i, split_length)); } } return ret; }
Variant StringUtil::Split(CStrRef str, int split_length /* = 1 */) { if (split_length <= 0) { throw_invalid_argument( "The length of each segment must be greater than zero" ); return false; } Array ret; int len = str.size(); if (split_length >= len) { ret.append(str); } else { for (int i = 0; i < len; i += split_length) { ret.append(str.substr(i, split_length)); } } return ret; }
Variant f_strcspn(CStrRef str1, CStrRef str2, int start /* = 0 */, int length /* = 0x7FFFFFFF */) { String s = str1.substr(start, length); if (s.isNull()) return false; return string_cspan(s, s.size(), str2, str2.size()); }