bool Init() { XBMC->Log(LOG_DEBUG, "%s", __FUNCTION__); HTTPSocket sock; std::string resp_headers; std::string resp_body; size_t pos; std::string locationUrl; sock.SetURL(g_strServer); if (!sock.Execute(&resp_headers, &resp_body)) { XBMC->Log(LOG_ERROR, "%s: api init failed", __FUNCTION__); return false; } // xpcom.common.js > get_server_params() // check for location header if ((pos = resp_headers.find("Location: ")) != std::string::npos) { locationUrl = resp_headers.substr(pos + 10, resp_headers.find("\r\n", pos) - (pos + 10)); } else { XBMC->Log(LOG_DEBUG, "%s: failed to get api endpoint from location header", __FUNCTION__); // convert to lower case std::transform(resp_body.begin(), resp_body.end(), resp_body.begin(), ::tolower); // check for meta refresh tag if ((pos = resp_body.find("url=")) != std::string::npos) { locationUrl = g_strServer + "/" + resp_body.substr(pos + 4, resp_body.find("\"", pos) - (pos + 4)); } else { XBMC->Log(LOG_DEBUG, "%s: failed to get api endpoint from meta refresh tag", __FUNCTION__); // assume current url is the intended location XBMC->Log(LOG_DEBUG, "%s: assuming current url is the intended location", __FUNCTION__); locationUrl = g_strServer; } } if ((pos = locationUrl.find_last_of("/")) == std::string::npos || locationUrl.substr(pos - 2, 3).compare("/c/") != 0) { XBMC->Log(LOG_ERROR, "%s: failed to get api endpoint", __FUNCTION__); return false; } // strip tail from url path and set api endpoint and referer g_strApiBasePath = locationUrl.substr(0, pos - 1); g_api_endpoint = g_strApiBasePath + "server/load.php"; g_referer = locationUrl.substr(0, pos + 1); XBMC->Log(LOG_DEBUG, "api endpoint: %s", g_api_endpoint.c_str()); XBMC->Log(LOG_DEBUG, "referer: %s", g_referer.c_str()); return true; }
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; }