<commit_before>#include "magma.h"

typedef struct {
	chr_t *string;
	void (*callback)(connection_t *con);
} api_lookup_t;

// NOTE - These must be sorted alphabetically!
static
api_lookup_t
api_methods[] = {
	{
		.string = "auth",
		.callback = &api_endpoint_auth
	},
	{
		.string = "change_password",
		.callback = &api_endpoint_change_password
	},
	{
		.string = "delete_user",
		.callback = &api_endpoint_delete_user
	},
	{
		.string = "register",
		.callback = &api_endpoint_register
	}
};

static
int_t
api_method_compare(
	void const *compare,
	void const *command)
{
	api_lookup_t *cmd = (api_lookup_t *)command;
	api_lookup_t *cmp = (api_lookup_t *)compare;

	return strcmp(cmp->string, cmd->string);
}

static
bool_t
is_localhost(connection_t *con) {
	return con_addr_word(con, 0) == 0x0100007f;
}

static
bool_t
is_ssl(connection_t *con) {
	return con_secure(con) == 1;
}

static
void
internal_error(connection_t *con) {
	api_error(
		con,
		HTTP_ERROR_500,
		JSON_RPC_2_ERROR_SERVER_INTERNAL,
		"Internal server error.");
}

static
void
bad_request_error(connection_t *con) {
	api_error(
		con,
		HTTP_ERROR_400,
		JSON_RPC_2_ERROR_PARSE_MALFORMED,
		"Internal server error.");
}

static
void
not_found_error(connection_t *con) {
	api_error(
		con,
		HTTP_ERROR_400,
		JSON_RPC_2_ERROR_SERVER_METHOD_UNAVAIL,
		"Method not found.");
}

void json_api_dispatch(connection_t *con) {
	json_error_t jansson_err;
	json_t *method;
	json_t *id;
	api_lookup_t *found_method;
	api_lookup_t method_lookup = {
		.callback = NULL
	};

	if (
		//magma.web.portal.safeguard &&
		!is_ssl(con) &&
		!is_localhost(con))
	{
		log_pedantic("Insecure request denied");
		con->http.mode = HTTP_ERROR_400;<commit_msg>Better error for insecure connections<commit_after>#include "magma.h"

typedef struct {
	chr_t *string;
	void (*callback)(connection_t *con);
} api_lookup_t;

// NOTE - These must be sorted alphabetically!
static
api_lookup_t
api_methods[] = {
	{
		.string = "auth",
		.callback = &api_endpoint_auth
	},
	{
		.string = "change_password",
		.callback = &api_endpoint_change_password
	},
	{
		.string = "delete_user",
		.callback = &api_endpoint_delete_user
	},
	{
		.string = "register",
		.callback = &api_endpoint_register
	}
};

static
int_t
api_method_compare(
	void const *compare,
	void const *command)
{
	api_lookup_t *cmd = (api_lookup_t *)command;
	api_lookup_t *cmp = (api_lookup_t *)compare;

	return strcmp(cmp->string, cmd->string);
}

static
bool_t
is_localhost(connection_t *con) {
	return con_addr_word(con, 0) == 0x0100007f;
}

static
bool_t
is_ssl(connection_t *con) {
	return con_secure(con) == 1;
}

static
void
internal_error(connection_t *con) {
	api_error(
		con,
		HTTP_ERROR_500,
		JSON_RPC_2_ERROR_SERVER_INTERNAL,
		"Internal server error.");
}

static
void
bad_request_error(connection_t *con) {
	api_error(
		con,
		HTTP_ERROR_400,
		JSON_RPC_2_ERROR_PARSE_MALFORMED,
		"Internal server error.");
}

static
void
not_found_error(connection_t *con) {
	api_error(
		con,
		HTTP_ERROR_400,
		JSON_RPC_2_ERROR_SERVER_METHOD_UNAVAIL,
		"Method not found.");
}

void json_api_dispatch(connection_t *con) {
	json_error_t jansson_err;
	json_t *method;
	json_t *id;
	api_lookup_t *found_method;
	api_lookup_t method_lookup = {
		.callback = NULL
	};

	if (
		//magma.web.portal.safeguard &&
		!is_ssl(con) &&
		!is_localhost(con))
	{
		log_pedantic("Insecure request denied");
		bad_request_error(con);
{'fraction_non_alphanumeric': 0.12029197080291971, 'fraction_numerical': 0.013722627737226278, 'mean_word_length': 4.876500857632933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3487963', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1355, 'n_words': 256}