Exemplo n.º 1
0
  bool StalkerCall(sc_identity_t *identity, sc_param_request_t *params, std::string *resp_headers, std::string *resp_body, Json::Value *parsed)
  {
    XBMC->Log(LOG_DEBUG, "%s", __FUNCTION__);

    sc_request_t request;
    sc_request_header_t *header;
    HTTPSocket sock;
    size_t pos;
    Json::Reader reader;

    memset(&request, 0, sizeof(request));
    if (!sc_request_build(identity, params, &request)) {
      XBMC->Log(LOG_ERROR, "sc_request_build failed");
    }

    header = request.headers;
    while (header) {
      std::string strValue;

      strValue = header->value;

      //TODO url encode
      while ((pos = strValue.find(":")) != std::string::npos) {
        strValue.replace(pos, 1, "%3A");
      }
      while ((pos = strValue.find("/")) != std::string::npos) {
        strValue.replace(pos, 1, "%2F");
      }

      sock.AddHeader(header->name, strValue);

      header = header->next;
    }

    sock.AddHeader("Referer", g_referer);
    sock.AddHeader("X-User-Agent", "Model: MAG250; Link: WiFi");

    //TODO url encode
    std::string query;
    query = request.query;
    while ((pos = query.find(" ")) != std::string::npos) {
      query.replace(pos, 1, "%20");
    }

    sock.SetURL(g_api_endpoint + "?" + query);

    sc_request_free_headers(request.headers);

    if (!sock.Execute(resp_headers, resp_body)) {
      XBMC->Log(LOG_ERROR, "%s: api call failed", __FUNCTION__);
      return false;
    }

    if (!reader.parse(*resp_body, *parsed)) {
      XBMC->Log(LOG_ERROR, "%s: parsing failed", __FUNCTION__);
      if (resp_body->compare(AUTHORIZATION_FAILED) == 0) {
        XBMC->Log(LOG_ERROR, "%s: authorization failed", __FUNCTION__);
      }
      return false;
    }

    return true;
  }