MCPcopy Create free account
hub / github.com/cocos/cocos-engine / createVhost

Method createVhost

native/cocos/network/WebSocket-libwebsockets.cpp:864–928  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

862}
863
864struct lws_vhost *WebSocketImpl::createVhost(struct lws_protocols *protocols, int *sslConnectionOut) {
865 auto *fileUtils = cc::FileUtils::getInstance();
866 bool isCAFileExist = fileUtils->isFileExist(_caFilePath);
867 if (isCAFileExist) {
868 _caFilePath = fileUtils->fullPathForFilename(_caFilePath);
869 }
870
871 lws_context_creation_info info = convertToContextCreationInfo(protocols, isCAFileExist);
872
873 int sslConnection = *sslConnectionOut;
874 if (sslConnection != 0) {
875 if (isCAFileExist) {
876#if (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS || CC_PLATFORM == CC_PLATFORM_OPENHARMONY)
877 // if ca file is in the apk, try to extract it to writable path
878 ccstd::string writablePath = fileUtils->getWritablePath();
879 ccstd::string caFileName = getFileNameForPath(_caFilePath);
880 ccstd::string newCaFilePath = writablePath + caFileName;
881
882 if (fileUtils->isFileExist(newCaFilePath)) {
883 LOGD("CA file (%s) in writable path exists!", newCaFilePath.c_str());
884 _caFilePath = newCaFilePath;
885 info.ssl_ca_filepath = _caFilePath.c_str();
886 } else {
887 if (fileUtils->isFileExist(_caFilePath)) {
888 ccstd::string fullPath = fileUtils->fullPathForFilename(_caFilePath);
889 LOGD("Found CA file: %s", fullPath.c_str());
890 if (fullPath[0] != '/') {
891 LOGD("CA file is in APK");
892 auto caData = fileUtils->getDataFromFile(fullPath);
893 if (!caData.isNull()) {
894 FILE *fp = fopen(newCaFilePath.c_str(), "wb");
895 if (fp != nullptr) {
896 LOGD("New CA file path: %s", newCaFilePath.c_str());
897 fwrite(caData.getBytes(), caData.getSize(), 1, fp);
898 fclose(fp);
899 _caFilePath = newCaFilePath;
900 info.ssl_ca_filepath = _caFilePath.c_str();
901 } else {
902 CC_ABORT(); // Open new CA file failed
903 }
904 } else {
905 CC_ABORT(); // CA file is empty!
906 }
907 } else {
908 LOGD("CA file isn't in APK!");
909 _caFilePath = fullPath;
910 info.ssl_ca_filepath = _caFilePath.c_str();
911 }
912 } else {
913 CC_ABORT(); // CA file doesn't exist!
914 }
915 }
916#else
917 info.ssl_ca_filepath = _caFilePath.c_str();
918#endif
919 } else {
920 LOGD("WARNING: CA Root file isn't set. SSL connection will not peer server certificate\n");
921 *sslConnectionOut = sslConnection | LCCSCF_ALLOW_SELFSIGNED | LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;

Callers

nothing calls this directly

Calls 9

getFileNameForPathFunction · 0.85
isFileExistMethod · 0.80
fullPathForFilenameMethod · 0.80
getDataFromFileMethod · 0.80
getWritablePathMethod · 0.45
isNullMethod · 0.45
getBytesMethod · 0.45
getSizeMethod · 0.45

Tested by

no test coverage detected