<commit_before>/**
 * This file is part of Slideshow.
 * Copyright (C) 2008-2011 <NAME> <<EMAIL>>
 *
 * Slideshow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Slideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Slideshow.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "browser.h"
#include "core/asprintf.h"
#include "core/curl_local.h"
#include "core/log.h"
#include <json/json.h>
#include <string.h>

#define FRONTEND_API_VERSION "1"

typedef struct {
	struct browser_module_t module;
	void* handle;
	struct curl_httppost* formpost;
	int id;
} frontend_context_t;

MODULE_INFO("Frontend Browser", BROWSER_MODULE, "<NAME>");

static int next_slide_v1(frontend_context_t* this, slide_context_t* slide, struct json_object* data){
	struct json_object* assembler = json_object_object_get(data, "assembler");
	struct json_object* slide_id  = json_object_object_get(data, "slide-id");
	struct json_object* filename  = json_object_object_get(data, "filename");
	struct json_object* context   = json_object_object_get(data, "context");

	/* is assembler isn't set, no field can be assumed to be. It means no slide could be fetched (e.g. empty queue). */
	if ( assembler ){
		slide->assembler = strdup(json_object_get_string(assembler));

		if ( strcmp(slide->assembler, "video") != 0 ){
			slide->filename = asprintf2("%s/slides/show/%d", this->module.context.host, json_object_get_int(slide_id));
		} else {
			slide->filename = strdup(json_object_get_string(filename));
		}

		this->id = json_object_get_int(context);
	}

	json_object_put(context);
	json_object_put(filename);
	json_object_put(slide_id);
	json_object_put(assembler);

	return 0;
}

static slide_context_t next_slide(frontend_context_t* this){
	slide_context_t slide;
	slide.filename = NULL;
	slide.assembler = NULL;

	struct MemoryStruct chunk;
	long response;

	chunk.memory = (char*)malloc(1);  /* will be grown as needed by the realloc above */
	chunk.size = 0;    /* no data at this point */

	char* url = asprintf2("%s/instance/next/%d", this->module.context.host, this->id);
	curl_easy_setopt(this->handle, CURLOPT_URL, url);
	free(url);

	curl_easy_setopt(this->handle, CURLOPT_WRITEDATA, (void *)&chunk);
	curl_easy_perform(this->handle);
	curl_easy_getinfo(this->handle, CURLINFO_RESPONSE_CODE, &response);

	if ( response != 200 ){ /* HTTP OK */
		log_message(Log_Warning, "Server replied with code %ld\n", response);
		return slide;
	}

	/* parse */
	json_object* data = json_tokener_parse(chunk.memory);
	if ( !data ){
		log_message(Log_Warning, "Failed to parse server reply: %s\n", json_object_to_json_string(data));
		return slide;
	}

	int version;
	{
		struct json_object* tmp = json_object_object_get(data, "version");
		version = json_object_get_int(tmp);
		json_object_put(tmp);
	}

	int ret;
	switch ( version ){
	case 1:
		ret = next_slide_v1(this, &slide, data);
		break;
	default:
		log_message(Log_Warning, "frontend replied with unsuppored version %d\n", version);<commit_msg>fix (some) json deprecation warnings<commit_after>/**
 * This file is part of Slideshow.
 * Copyright (C) 2008-2011 <NAME> <<EMAIL>>
 *
 * Slideshow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Slideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Slideshow.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "browser.h"
#include "core/asprintf.h"
#include "core/curl_local.h"
#include "core/log.h"
#include <json/json.h>
#include <string.h>

#define FRONTEND_API_VERSION "1"

typedef struct {
	struct browser_module_t module;
	void* handle;
	struct curl_httppost* formpost;
	int id;
} frontend_context_t;

MODULE_INFO("Frontend Browser", BROWSER_MODULE, "<NAME>");

static int next_slide_v1(frontend_context_t* this, slide_context_t* slide, struct json_object* data){
	struct json_object* assembler = NULL;
	struct json_object* slide_id  = NULL;
	struct json_object* filename  = NULL;
	struct json_object* context   = NULL;

	/* is assembler isn't set, no field can be assumed to be. It means no slide could be fetched (e.g. empty queue). */
	if ( json_object_object_get_ex(data, "assembler", &assembler) ){
		slide->assembler = strdup(json_object_get_string(assembler));

		/** @todo add error checking */
		json_object_object_get_ex(data, "slide-id", &slide_id);
		json_object_object_get_ex(data, "filename", &filename);
		json_object_object_get_ex(data, "context", &context);

		if ( strcmp(slide->assembler, "video") != 0 ){
			slide->filename = asprintf2("%s/slides/show/%d", this->module.context.host, json_object_get_int(slide_id));
		} else {
			slide->filename = strdup(json_object_get_string(filename));
		}

		this->id = json_object_get_int(context);
	}

	return 0;
}

static slide_context_t next_slide(frontend_context_t* this){
	slide_context_t slide;
	slide.filename = NULL;
	slide.assembler = NULL;

	struct MemoryStruct chunk;
	long response;

	chunk.memory = (char*)malloc(1);  /* will be grown as needed by the realloc above */
	chunk.size = 0;    /* no data at this point */

	char* url = asprintf2("%s/instance/next/%d", this->module.context.host, this->id);
	curl_easy_setopt(this->handle, CURLOPT_URL, url);
	free(url);

	curl_easy_setopt(this->handle, CURLOPT_WRITEDATA, (void *)&chunk);
	curl_easy_perform(this->handle);
	curl_easy_getinfo(this->handle, CURLINFO_RESPONSE_CODE, &response);

	if ( response != 200 ){ /* HTTP OK */
		log_message(Log_Warning, "Server replied with code %ld\n", response);
		return slide;
	}

	/* parse */
	json_object* data = json_tokener_parse(chunk.memory);
	if ( !data ){
		log_message(Log_Warning, "Failed to parse server reply: %s\n", json_object_to_json_string(data));
		return slide;
	}

	int version = -1;
	struct json_object* tmp;
	if ( json_object_object_get_ex(data, "version", &tmp) ){
		version = json_object_get_int(tmp);
	}

	int ret;
	switch ( version ){
	case 1:
		ret = next_slide_v1(this, &slide, data);
		break;
	case -1:
		log_message(Log_Warning, "frontend did not reply with a version %d\n", version);
		return slide;
	default:
		log_message(Log_Warning, "frontend replied with unsuppored version %d\n", version);
{'fraction_non_alphanumeric': 0.11943893454236328, 'fraction_numerical': 0.006517427033153868, 'mean_word_length': 4.824257425742574, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '321098', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2354, 'n_words': 678}