Пример #1
0
Status parseApplicationAliasData(const std::string& data, std::string& result) {
  std::string decoded_data = base64Decode(data);
  if (decoded_data.empty()) {
    return Status(1, "Failed to base64 decode data");
  }

  CFDataRef resourceData = CFDataCreate(
      nullptr,
      static_cast<const UInt8*>(static_cast<const void*>(decoded_data.c_str())),
      decoded_data.length());
  if (resourceData == nullptr) {
    return Status(1, "Failed to allocate resource data");
  }

  auto alias = (CFDataRef)CFPropertyListCreateWithData(kCFAllocatorDefault,
                                                       resourceData,
                                                       kCFPropertyListImmutable,
                                                       nullptr,
                                                       nullptr);
  CFRelease(resourceData);
  if (alias == nullptr) {
    return Status(1, "Failed to allocate alias data");
  }

  auto bookmark =
      CFURLCreateBookmarkDataFromAliasRecord(kCFAllocatorDefault, alias);
  CFRelease(alias);
  if (bookmark == nullptr) {
    return Status(1, "Alias data is not a bookmark");
  }

  auto url = CFURLCreateByResolvingBookmarkData(
      kCFAllocatorDefault, bookmark, 0, nullptr, nullptr, nullptr, nullptr);
  CFRelease(bookmark);
  if (url == nullptr) {
    return Status(1, "Alias data is not a URL bookmark");
  }

  auto replaced = CFURLCreateStringByReplacingPercentEscapes(
      kCFAllocatorDefault, CFURLGetString(url), CFSTR(""));
  CFRelease(url);
  if (replaced == nullptr) {
    return Status(1, "Failed to replace percent escapes.");
  }

  // Get the URL-formatted path.
  result = stringFromCFString(replaced);
  CFRelease(replaced);
  if (result.empty()) {
    return Status(1, "Return result is zero size");
  }
  if (result.length() > 6 && result.substr(0, 7) == "file://") {
    result = result.substr(7);
  }

  return Status(0, "OK");
}
Пример #2
0
static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) {
    switch (type) {
        case kCFSocketDataCallBack:
            CFDataRef data(reinterpret_cast<CFDataRef>(value));
            Client *client(reinterpret_cast<Client *>(info));

            if (client->message_ == NULL)
                client->message_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE);

            if (!CFHTTPMessageAppendBytes(client->message_, CFDataGetBytePtr(data), CFDataGetLength(data)))
                CFLog(kCFLogLevelError, CFSTR("CFHTTPMessageAppendBytes()"));
            else if (CFHTTPMessageIsHeaderComplete(client->message_)) {
                CFURLRef url(CFHTTPMessageCopyRequestURL(client->message_));
                Boolean absolute;
                CFStringRef path(CFURLCopyStrictPath(url, &absolute));
                CFRelease(client->message_);

                CFStringRef code(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, path, CFSTR("")));
                CFRelease(path);

                JSStringRef script(JSStringCreateWithCFString(code));
                CFRelease(code);

                JSValueRef result(JSEvaluateScript(CYGetJSContext(), script, NULL, NULL, 0, NULL));
                JSStringRelease(script);

                CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1));
                CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8"));

                CFStringRef json(CYCopyJSONString(CYGetJSContext(), result, NULL));
                CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL));
                CFRelease(json);

                CFStringRef length(CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"), CFDataGetLength(body)));
                CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), length);
                CFRelease(length);

                CFHTTPMessageSetBody(response, body);
                CFRelease(body);

                CFDataRef serialized(CFHTTPMessageCopySerializedMessage(response));
                CFRelease(response);

                CFSocketSendData(socket, NULL, serialized, 0);
                CFRelease(serialized);

                CFRelease(url);
            }
        break;
    }
}