result_t net_base::connect(exlib::string url, int32_t timeout, obj_ptr<Stream_base>& retVal, AsyncEvent* ac) { if (!qstrcmp(url.c_str(), "ssl:", 4)) return ssl_base::connect(url, timeout, retVal, ac); if (qstrcmp(url.c_str(), "tcp:", 4)) return CHECK_ERROR(CALL_E_INVALIDARG); if (ac->isSync()) return CHECK_ERROR(CALL_E_NOSYNC); obj_ptr<Url> u = new Url(); result_t hr = u->parse(url); if (hr < 0) return hr; if (u->m_port.length() == 0) return CHECK_ERROR(CALL_E_INVALIDARG); int32_t nPort = atoi(u->m_port.c_str()); int32_t family = u->m_ipv6 ? net_base::_AF_INET6 : net_base::_AF_INET; obj_ptr<Socket_base> socket; hr = Socket_base::_new(family, net_base::_SOCK_STREAM, socket); if (hr < 0) return hr; socket->set_timeout(timeout); retVal = socket; return socket->connect(u->m_hostname, nPort, ac); }
result_t fs_base::rename(exlib::string from, exlib::string to, AsyncEvent *ac) { if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); if (::rename(from.c_str(), to.c_str())) return CHECK_ERROR(LastError()); return 0; }
result_t querystring_base::parse(exlib::string str, exlib::string sep, exlib::string eq, v8::Local<v8::Object> opt, obj_ptr<HttpCollection_base>& retVal) { result_t hr; obj_ptr<HttpCollection> c = new HttpCollection(); hr = c->parse(str, sep.c_str(), eq.c_str()); if (hr < 0) return hr; retVal = c; return 0; }
result_t path_base::extname(exlib::string path, exlib::string &retVal) { char ch; const char *p1 = NULL; const char* c_str = path.c_str(); if (*c_str == '.') c_str++; while (*c_str) { ch = *c_str++; if (isPathSlash(ch)) { if (*c_str == '.') c_str++; p1 = NULL; } else if (ch == '.') p1 = c_str - 1; } if (p1) retVal.assign(p1, (int32_t) (c_str - p1)); return 0; }
result_t path_base::fullpath(exlib::string path, exlib::string &retVal) { #ifdef _WIN32 exlib::wstring str = utf8to16String(path); exlib::wchar utf16_buffer[MAX_PATH]; DWORD utf16_len = GetFullPathNameW(str.c_str(), MAX_PATH, utf16_buffer, NULL); if (!utf16_len) return CHECK_ERROR(LastError()); retVal = utf16to8String(utf16_buffer, (int32_t)utf16_len); return 0; #else if (isPathSlash(path.c_str()[0])) return normalize(path, retVal); exlib::string str; process_base::cwd(str); str.append(1, PATH_SLASH); str.append(path); return normalize(str, retVal); #endif }
result_t dns_base::resolve(exlib::string name, obj_ptr<NArray>& retVal, AsyncEvent* ac) { if (ac->isSync()) return CHECK_ERROR(CALL_E_LONGSYNC); addrinfo hints = { 0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, 0, 0, 0 }; addrinfo* result = NULL; addrinfo* ptr = NULL; int res = getaddrinfo(name.c_str(), NULL, &hints, &result); if (res) return CHECK_ERROR(Runtime::setError(gai_strerror(res))); obj_ptr<NArray> arr = new NArray(); for (ptr = result; ptr != NULL; ptr = ptr->ai_next) { inetAddr addr_info; addr_info.init(ptr->ai_addr); arr->append(addr_info.str()); } freeaddrinfo(result); retVal = arr; return 0; }
result_t net_base::isIPv6(exlib::string ip, bool& retVal) { result_t hr; retVal = true; const char* src = ip.c_str(); int len; char tmp[INET6_ADDRSTRLEN], *s, *p; unsigned char dst[sizeof(struct in6_addr)]; s = (char*)src; p = strchr(s, '%'); if (p != NULL) { s = tmp; len = (int32_t)(p - src); if (len > INET6_ADDRSTRLEN - 1) { retVal = false; return 0; } memcpy(s, src, len); s[len] = '\0'; } hr = inet_pton6(s, dst); if (hr != 0) retVal = false; return 0; }
result_t HttpCollection::parseCookie(exlib::string &str) { const char *pstr = str.c_str(); int32_t nSize = (int32_t) str.length(); const char *pstrTemp; exlib::string strKey, strValue; while (nSize) { while (nSize && *pstr == ' ') { pstr++; nSize--; } pstrTemp = pstr; while (nSize && *pstr != '=' && *pstr != ';') { pstr++; nSize--; } if (pstr > pstrTemp) Url::decodeURI(pstrTemp, (int32_t) (pstr - pstrTemp), strKey, true); else strKey.clear(); if (nSize && *pstr == '=') { nSize--; pstr++; } pstrTemp = pstr; while (nSize && *pstr != ';') { pstr++; nSize--; } if (!strKey.empty()) { if (pstr > pstrTemp) Url::decodeURI(pstrTemp, (int32_t) (pstr - pstrTemp), strValue, true); else strValue.clear(); } if (nSize) { nSize--; pstr++; } if (!strKey.empty()) add(strKey, strValue); } return 0; }
result_t XmlParser::parse(XmlDocument* doc, exlib::string source) { XmlParser parser(doc, true); parser.m_now = doc; parser.m_list.push_back(doc); XML_Parser xml_parser = XML_ParserCreate(NULL); XML_SetParamEntityParsing(xml_parser, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); XML_SetUserData(xml_parser, &parser); XML_SetXmlDeclHandler(xml_parser, XmlDeclHandler); XML_SetElementHandler(xml_parser, StartElementHandler, EndElementHandler); XML_SetCharacterDataHandler(xml_parser, CharacterDataHandler); XML_SetProcessingInstructionHandler(xml_parser, ProcessingInstructionHandler); XML_SetCommentHandler(xml_parser, CommentHandler); XML_SetCdataSectionHandler(xml_parser, StartCdataSectionHandler, EndCdataSectionHandler); XML_SetStartDoctypeDeclHandler(xml_parser, StartDoctypeDeclHandler); if (XML_Parse(xml_parser, source.c_str(), (int32_t)source.length(), true) != XML_STATUS_OK) { char msg[128]; sprintf(msg, "XmlParser: error on line %lu at column %lu: %s", XML_GetCurrentLineNumber(xml_parser), XML_GetCurrentColumnNumber(xml_parser) + 1, XML_ErrorString(XML_GetErrorCode(xml_parser))); XML_ParserFree(xml_parser); return CHECK_ERROR(Runtime::setError(msg)); } XML_ParserFree(xml_parser); return 0; }
result_t fs_base::exists(exlib::string path, bool &retVal, AsyncEvent *ac) { if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); retVal = access(path.c_str(), F_OK) == 0; return 0; }
result_t fs_base::rmdir(exlib::string path, AsyncEvent *ac) { if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); if (::rmdir(path.c_str())) return CHECK_ERROR(LastError()); return 0; }
result_t fs_base::chmod(exlib::string path, int32_t mode, AsyncEvent *ac) { if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); if (::chmod(path.c_str(), mode)) return CHECK_ERROR(LastError()); return 0; }
result_t Stat::getStat(exlib::string path) { struct stat64 st; if (::stat64(path.c_str(), &st)) return CHECK_ERROR(LastError()); fill(path, st); return 0; }
result_t SQLite::execute(exlib::string sql, obj_ptr<DBResult_base> &retVal, AsyncEvent *ac) { if (!m_db) return CHECK_ERROR(CALL_E_INVALID_CALL); if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); return execute(sql.c_str(), (int32_t) sql.length(), retVal); }
result_t MongoCollection::runCommand(exlib::string cmd, exlib::string cmd1, exlib::string arg, v8::Local<v8::Object>& retVal) { obj_ptr<MongoDB> db(m_db); if (!db) return CHECK_ERROR(CALL_E_INVALID_CALL); bson bbq; bson_init(&bbq); bson_append_string(&bbq, cmd.c_str(), m_name.c_str()); if (!cmd1.empty()) bson_append_string(&bbq, cmd1.c_str(), arg.c_str()); bson_finish(&bbq); return db->bsonHandler(&bbq, retVal); }
result_t X509Crl::load(exlib::string pemCrl) { int32_t ret; ret = mbedtls_x509_crl_parse(&m_crl, (const unsigned char *)pemCrl.c_str(), pemCrl.length() + 1); if (ret != 0) return CHECK_ERROR(_ssl::setError(ret)); return 0; }
result_t MongoDB::runCommand(exlib::string cmd, v8::Local<v8::Value> arg, v8::Local<v8::Object> &retVal) { bson bbq; bson_init(&bbq); encodeValue(holder(), &bbq, cmd.c_str(), arg); bson_finish(&bbq); return bsonHandler(&bbq, retVal); }
result_t HttpCollection::set(exlib::string name, Variant value) { int32_t i; bool bFound = false; for (i = 0; i < m_count; i++) { pair &_pair = m_map[i]; if (!qstricmp(_pair.first.c_str(), name.c_str())) { exlib::string s; value.toString(s); _pair.second = s; bFound = true; break; } } if (bFound) { int32_t p = ++i; for (; i < m_count; i++) { pair &_pair = m_map[i]; if (qstricmp(_pair.first.c_str(), name.c_str())) { if (i != p) m_map[p] = _pair; p++; } } m_count = p; } else return add(name, value); return 0; }
result_t net_base::isIPv4(exlib::string ip, bool& retVal) { result_t hr; retVal = true; const char* src = ip.c_str(); unsigned char dst[sizeof(struct in6_addr)]; hr = inet_pton4(src, dst); if (hr != 0) retVal = false; return 0; }
result_t path_base::dirname(exlib::string path, exlib::string &retVal) { char ch; const char* c_str = path.c_str(); const char *p1 = c_str; const char *p2 = NULL; #ifdef _WIN32 if (c_str[0] != 0 && c_str[1] == ':') { c_str += 2; if (isPathSlash(*c_str)) c_str++; p2 = c_str; } else if (isPathSlash(c_str[0]) && isPathSlash(c_str[1])) { c_str += 2; while (*c_str && !isPathSlash(*c_str)) c_str++; if (*c_str) { c_str++; while (*c_str && !isPathSlash(*c_str)) c_str++; if (*c_str) c_str++; } p2 = c_str; } #endif while (*c_str) { ch = *c_str++; if (isPathSlash(ch) && *c_str) p2 = c_str - 1; } if (p2 == NULL) p2 = p1; if (isPathSlash(*p2) && p2 == p1) p2++; retVal.assign(p1, (int32_t) (p2 - p1)); return 0; }
result_t HttpCollection::set(exlib::string name, Variant value) { int32_t i; bool bFound = false; for (i = 0; i < m_count; i++) if (!qstricmp(m_names[i].c_str(), name.c_str())) { exlib::string s; value.toString(s); m_values[i] = s; bFound = true; break; } if (bFound) { int32_t p = ++i; for (; i < m_count; i++) if (qstricmp(m_names[i].c_str(), name.c_str())) { if (i != p) { m_names[p] = m_names[i]; m_values[p] = m_values[i]; } p++; } m_count = p; } else return add(name, value); return 0; }
result_t HttpCollection::first(exlib::string name, Variant &retVal) { int32_t i; for (i = 0; i < m_count; i++) if (!qstricmp(m_names[i].c_str(), name.c_str())) { retVal = m_values[i]; return 0; } return CALL_RETURN_NULL; }
result_t HttpMessage::set_protocol(exlib::string newVal) { const char* c_str = newVal.c_str(); if (qstrcmp(c_str, "HTTP/", 5) || !qisdigit(c_str[5]) || c_str[6] != '.' || !qisdigit(c_str[7]) || c_str[8]) return CHECK_ERROR(Runtime::setError("HttpRequest: bad protocol version.")); m_keepAlive = ((c_str[5] - '0') * 10 + c_str[7] - '0') > 10; m_protocol = newVal; return 0; }
result_t HttpCollection::has(exlib::string name, bool& retVal) { int32_t i; retVal = false; for (i = 0; i < m_count; i++) if (!qstricmp(m_map[i].first.c_str(), name.c_str())) { retVal = true; break; } return 0; }
result_t HttpCollection::all(exlib::string name, obj_ptr<List_base> &retVal) { obj_ptr<List> list; int32_t i; list = new List(); for (i = 0; i < m_count; i++) if (!qstricmp(m_names[i].c_str(), name.c_str())) list->append(m_values[i]); retVal = list; return 0; }
result_t path_base::basename(exlib::string path, exlib::string ext, exlib::string &retVal) { char ch; const char* c_str = path.c_str(); const char *p1 = c_str; int32_t extlen = (int32_t)ext.length(); while (*c_str) { ch = *c_str++; if (isPathSlash(ch)) p1 = c_str; } if (extlen && ((int32_t) (c_str - p1) >= extlen) && !pathcmp(ext.c_str(), c_str - extlen, extlen)) c_str -= extlen; retVal.assign(p1, (int32_t) (c_str - p1)); return 0; }
result_t XmlElement::setAttributeNS(exlib::string namespaceURI, exlib::string qualifiedName, exlib::string value) { const char* c_str = qualifiedName.c_str(); if (!qstrcmp(c_str, "xmlns:", 6)) { int32_t i; for (i = 0; s_nss[i][0]; i++) if (!qstrcmp(c_str + 6, s_nss[i][0])) return 0; } obj_ptr<XmlAttr> attr = new XmlAttr(this, namespaceURI, qualifiedName, value); return m_attrs->setNamedItem(attr); }
result_t HttpCollection::first(exlib::string name, Variant& retVal) { int32_t i; for (i = 0; i < m_count; i++) { pair &_pair = m_map[i]; if (!qstricmp(_pair.first.c_str(), name.c_str())) { retVal = _pair.second; return 0; } } return CALL_RETURN_NULL; }
result_t Url::parse(exlib::string url, bool parseQueryString, bool slashesDenoteHost) { bool bHost; clear(); m_slashes = false; trimUrl(url, url); const char* c_str = url.c_str(); bool hasHash = qstrchr(c_str, '#') != NULL; if (!slashesDenoteHost && !hasHash && isUrlSlash(*c_str)) { parsePath(c_str); parseQuery(c_str); parseHash(c_str); if (parseQueryString) { m_queryParsed = new HttpCollection(); m_queryParsed->parse(m_query); } return 0; } parseProtocol(c_str); bHost = checkHost(c_str); if (slashesDenoteHost || m_protocol.length() > 0 || bHost) m_slashes = ((isUrlSlash(*c_str) && isUrlSlash(c_str[1])) && (m_protocol.length() <= 0 || m_protocol.compare("javascript:"))); if (m_protocol.compare("javascript:") && m_slashes) { c_str += 2; parseAuth(c_str); parseHost(c_str); } parsePath(c_str); parseQuery(c_str); parseHash(c_str); if (parseQueryString) { m_queryParsed = new HttpCollection(); m_queryParsed->parse(m_query); } return 0; }
result_t SQLite::backup(exlib::string fileName, AsyncEvent *ac) { if (!m_db) return CHECK_ERROR(CALL_E_INVALID_CALL); if (!ac) return CHECK_ERROR(CALL_E_NOSYNC); int32_t rc; struct sqlite3 *db2 = NULL; sqlite3_backup *pBackup; const char* c_str = fileName.c_str(); if (!qstrcmp(c_str, "sqlite:", 7)) c_str += 7; if (sqlite3_open_v2(c_str, &db2, SQLITE_OPEN_FLAGS, 0)) { result_t hr = CHECK_ERROR(Runtime::setError(sqlite3_errmsg(db2))); return hr; } pBackup = sqlite3_backup_init(db2, "main", m_db, "main"); if (pBackup) { do { rc = sqlite3_backup_step(pBackup, 5); if (rc == SQLITE_LOCKED) sqlite3_sleep(1); } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED); sqlite3_backup_finish(pBackup); } else { result_t hr = CHECK_ERROR(Runtime::setError(sqlite3_errmsg(m_db))); sqlite3_close(db2); return hr; } sqlite3_close(db2); return 0; }