#include "mods.h"
#include "config.h"
#include "log.h"
-
+#include "main.h" // for g_settings
+#include "settings.h"
+#include "version.h"
+#include "httpfetch.h"
+#include "porting.h"
+
+Json::Value fetchJsonValue(const std::string &url,
+ struct curl_slist *chunk) {
#if USE_CURL
-#include <curl/curl.h>
-
-static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
-{
- ((std::string*)userp)->append((char*)contents, size * nmemb);
- return size * nmemb;
-}
-
-#endif
-
-Json::Value fetchJsonValue(const std::string url,
- struct curl_slist *chunk) {
-#if USE_CURL
- std::string liststring;
- CURL *curl;
-
- curl = curl_easy_init();
- if (curl)
- {
- CURLcode res;
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
+ HTTPFetchRequest fetchrequest;
+ HTTPFetchResult fetchresult;
+ fetchrequest.url = url;
+ fetchrequest.caller = HTTPFETCH_SYNC;
- if (chunk != 0)
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
-
-
- res = curl_easy_perform(curl);
- if (res != CURLE_OK)
- errorstream<<"Jsonreader: "<< url <<" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
- curl_easy_cleanup(curl);
+ struct curl_slist* runptr = chunk;
+ while(runptr) {
+ fetchrequest.extra_headers.push_back(runptr->data);
+ runptr = runptr->next;
}
+ httpfetch_sync(fetchrequest,fetchresult);
- Json::Value root;
- Json::Reader reader;
- std::istringstream stream(liststring);
- if (!liststring.size()) {
+ if (!fetchresult.succeeded) {
return Json::Value();
}
+ Json::Value root;
+ Json::Reader reader;
+ std::istringstream stream(fetchresult.data);
if (!reader.parse( stream, root ) )
{
errorstream << "URL: " << url << std::endl;
errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
- errorstream << "data: \"" << liststring << "\"" << std::endl;
+ errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
return Json::Value();
}
//id
if (modlist[i]["id"].asString().size()) {
- const char* id_raw = modlist[i]["id"].asString().c_str();
+ std::string id_raw = modlist[i]["id"].asString();
char* endptr = 0;
- int numbervalue = strtol(id_raw,&endptr,10);
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
toadd.id = numbervalue;
}
+ else {
+ errorstream << "readModStoreList: missing id" << std::endl;
+ toadd.valid = false;
+ }
}
else {
+ errorstream << "readModStoreList: missing id" << std::endl;
toadd.valid = false;
}
toadd.title = modlist[i]["title"].asString();
}
else {
+ errorstream << "readModStoreList: missing title" << std::endl;
toadd.valid = false;
}
toadd.basename = modlist[i]["basename"].asString();
}
else {
+ errorstream << "readModStoreList: missing basename" << std::endl;
toadd.valid = false;
}
ModStoreVersionEntry toadd;
if (details["version_set"][i]["id"].asString().size()) {
- const char* id_raw = details["version_set"][i]["id"].asString().c_str();
+ std::string id_raw = details["version_set"][i]["id"].asString();
char* endptr = 0;
- int numbervalue = strtol(id_raw,&endptr,10);
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
toadd.id = numbervalue;
}
}
else {
+ errorstream << "readModStoreModDetails: missing version_set id" << std::endl;
retval.valid = false;
}
toadd.file = details["version_set"][i]["file"].asString();
}
else {
+ errorstream << "readModStoreModDetails: missing version_set file" << std::endl;
retval.valid = false;
}
}
if (retval.versions.size() < 1) {
+ infostream << "readModStoreModDetails: not a single version specified!" << std::endl;
retval.valid = false;
}
if (details["categories"][i]["id"].asString().size()) {
- const char* id_raw = details["categories"][i]["id"].asString().c_str();
+ std::string id_raw = details["categories"][i]["id"].asString();
char* endptr = 0;
- int numbervalue = strtol(id_raw,&endptr,10);
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
toadd.id = numbervalue;
}
}
else {
+ errorstream << "readModStoreModDetails: missing categories id" << std::endl;
retval.valid = false;
}
if (details["categories"][i]["title"].asString().size()) {
toadd.name = details["categories"][i]["title"].asString();
}
else {
+ errorstream << "readModStoreModDetails: missing categories title" << std::endl;
retval.valid = false;
}
if (details["author"].isObject()) {
if (details["author"]["id"].asString().size()) {
- const char* id_raw = details["author"]["id"].asString().c_str();
+ std::string id_raw = details["author"]["id"].asString();
char* endptr = 0;
- int numbervalue = strtol(id_raw,&endptr,10);
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
retval.author.id = numbervalue;
}
else {
+ errorstream << "readModStoreModDetails: missing author id (convert)" << std::endl;
retval.valid = false;
}
}
else {
+ errorstream << "readModStoreModDetails: missing author id" << std::endl;
retval.valid = false;
}
retval.author.username = details["author"]["username"].asString();
}
else {
+ errorstream << "readModStoreModDetails: missing author username" << std::endl;
retval.valid = false;
}
}
else {
+ errorstream << "readModStoreModDetails: missing author" << std::endl;
retval.valid = false;
}
if (details["license"].isObject()) {
if (details["license"]["id"].asString().size()) {
- const char* id_raw = details["license"]["id"].asString().c_str();
+ std::string id_raw = details["license"]["id"].asString();
char* endptr = 0;
- int numbervalue = strtol(id_raw,&endptr,10);
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
retval.license.id = numbervalue;
}
}
else {
+ errorstream << "readModStoreModDetails: missing license id" << std::endl;
retval.valid = false;
}
retval.license.shortinfo = details["license"]["short"].asString();
}
else {
+ errorstream << "readModStoreModDetails: missing license short" << std::endl;
retval.valid = false;
}
}
+ //titlepic
+ if (details["titlepic"].isObject()) {
+ if (details["titlepic"]["id"].asString().size()) {
+
+ std::string id_raw = details["titlepic"]["id"].asString();
+ char* endptr = 0;
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
+
+ if ((id_raw != "") && (*endptr == 0)) {
+ retval.titlepic.id = numbervalue;
+ }
+ }
+
+ if (details["titlepic"]["file"].asString().size()) {
+ retval.titlepic.file = details["titlepic"]["file"].asString();
+ }
+
+ if (details["titlepic"]["desc"].asString().size()) {
+ retval.titlepic.description = details["titlepic"]["desc"].asString();
+ }
+
+ if (details["titlepic"]["mod"].asString().size()) {
+
+ std::string mod_raw = details["titlepic"]["mod"].asString();
+ char* endptr = 0;
+ int numbervalue = strtol(mod_raw.c_str(),&endptr,10);
+
+ if ((mod_raw != "") && (*endptr == 0)) {
+ retval.titlepic.mod = numbervalue;
+ }
+ }
+ }
+
//id
if (details["id"].asString().size()) {
- const char* id_raw = details["id"].asString().c_str();
+ std::string id_raw = details["id"].asString();
char* endptr = 0;
- int numbervalue = strtol(id_raw,&endptr,10);
+ int numbervalue = strtol(id_raw.c_str(),&endptr,10);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
retval.id = numbervalue;
}
}
else {
+ errorstream << "readModStoreModDetails: missing id" << std::endl;
retval.valid = false;
}
retval.title = details["title"].asString();
}
else {
+ errorstream << "readModStoreModDetails: missing title" << std::endl;
retval.valid = false;
}
retval.basename = details["basename"].asString();
}
else {
+ errorstream << "readModStoreModDetails: missing basename" << std::endl;
retval.valid = false;
}
//value
if (details["rating"].asString().size()) {
- const char* id_raw = details["rating"].asString().c_str();
+ std::string id_raw = details["rating"].asString();
char* endptr = 0;
- float numbervalue = strtof(id_raw,&endptr);
+ float numbervalue = strtof(id_raw.c_str(),&endptr);
- if ((*id_raw != 0) && (*endptr == 0)) {
+ if ((id_raw != "") && (*endptr == 0)) {
retval.rating = numbervalue;
}
}