<commit_before>#include "http_main.h"

bool http_client_info_cb(EV_P, void *cb_data, DB_result *res);

/*
All this function does is check the database connection, then call a callback
*/
void http_main_cnxn_cb(EV_P, void *cb_data, DB_conn *conn) {
	SDEBUG("http_main_cnxn_cb");
	struct sock_ev_client *client = cb_data;
	char *str_response = NULL;
	char *_str_response = NULL;
	char *str_uri = NULL;
	char *str_uri_temp = NULL;
	char *str_sql = NULL;
	char *ptr_end_uri = NULL;
	char str_length[50];
	size_t int_uri_len = 0;
	size_t int_sql_len = 0;
	size_t int_response_len = 0;

	SFINISH_CHECK(conn->int_status == 1, "%s", conn->str_response);

	str_uri = str_uri_path(client->str_request, client->int_request_len, &int_uri_len);
	SFINISH_CHECK(str_uri, "str_uri_path failed");

#ifdef ENVELOPE
#else
	if (isdigit(str_uri[9])) {
		str_uri_temp = str_uri;
		char *str_temp = strchr(str_uri_temp + 9, '/');
		SFINISH_CHECK(str_temp != NULL, "strchr failed");
		SFINISH_SNCAT(str_uri, &int_uri_len,
			"/postage/app", (size_t)12,
			str_temp, strlen(str_temp));
		SFREE(str_uri_temp);
	}
#endif
	ptr_end_uri = strchr(str_uri, '?');
	if (ptr_end_uri != NULL) {
		*ptr_end_uri = '\0';
		int_uri_len = (size_t)(ptr_end_uri - str_uri);
	}
	ptr_end_uri = strchr(str_uri, '#');
	if (ptr_end_uri != NULL) {
		*ptr_end_uri = '\0';
		int_uri_len = (size_t)(ptr_end_uri - str_uri);
	}

	SDEBUG("str_uri: %s", str_uri);

#ifdef ENVELOPE
	if (strncmp(str_uri, "/env/upload", 12) == 0) {
#else
	if (strncmp(str_uri, "/postage/app/upload", 22) == 0) {
#endif
		http_upload_step1(client);
#ifdef ENVELOPE
#else
#ifdef POSTAGE_INTERFACE_LIBPQ
	} else if (strncmp(str_uri, "/postage/app/export", 22) == 0) {
		http_export_step1(client);
#endif
#endif
#ifdef ENVELOPE
	} else if (strncmp(str_uri, "/env/action_ev", 15) == 0) {
		http_ev_step1(client);
	} else if (strncmp(str_uri, "/env/action_select", 19) == 0) {
		http_select_step1(client);
	} else if (strncmp(str_uri, "/env/action_insert", 19) == 0) {
		http_insert_step1(client);
	} else if (strncmp(str_uri, "/env/action_update", 19) == 0) {
		http_update_step1(client);
	} else if (strncmp(str_uri, "/env/action_delete", 19) == 0) {
		http_delete_step1(client);
	} else if (strncmp(str_uri, "/env/action_info", 17) == 0) {
#else
	} else if (strncmp(str_uri, "/postage/app/action_ev", 23) == 0) {
		http_ev_step1(client);
	} else if (strncmp(str_uri, "/postage/app/action_info", 25) == 0) {
#endif
		if (DB_connection_driver(client->conn) == DB_DRIVER_POSTGRES) {
			SFINISH_SNCAT(str_sql, &int_sql_len,
				"SELECT version() "
				"UNION ALL "
				"SELECT SESSION_USER::text "
				"UNION ALL "
				"SELECT g.rolname "
				"	FROM pg_catalog.pg_roles g "
				"	LEFT JOIN pg_catalog.pg_auth_members m ON m.roleid = g.oid "
				"	LEFT JOIN pg_catalog.pg_roles u ON m.member = u.oid "
				"	WHERE g.rolcanlogin = FALSE AND u.rolname = SESSION_USER::text AND g.rolname IS NOT NULL",
				strlen("SELECT version() "
					"UNION ALL "
					"SELECT SESSION_USER::text "
					"UNION ALL "
					"SELECT g.rolname "
					"	FROM pg_catalog.pg_roles g "
					"	LEFT JOIN pg_catalog.pg_auth_members m ON m.roleid = g.oid "
					"	LEFT JOIN pg_catalog.pg_roles u ON m.member = u.oid "
					"	WHERE g.rolcanlogin = FALSE AND u.rolname = SESSION_USER::text AND g.rolname IS NOT NULL"));
		} else {
			SFINISH_SNCAT(str_sql, &int_sql_len,
				"SELECT CAST(@@VERSION AS nvarchar(MAX)), '0' AS srt "
				"UNION ALL "
				"SELECT CAST(SYSTEM_USER AS nvarchar(MAX)) AS user_group_name, '1' AS srt "
				"UNION ALL "
				"SELECT CAST([name] AS nvarchar(MAX)) AS user_group_name, '2' AS srt "
				"	FROM ( "
				"		SELECT *, is_member(name) AS [is_member] "
				"			FROM [sys].[database_principals] "
				"	) em "
				"	WHERE [is_member] = 1 "
				"	ORDER BY 2, 1",
				strlen("SELECT CAST(@@VERSION AS nvarchar(MAX)), '0' AS srt "
					"UNION ALL "
					"SELECT CAST(SYSTEM_USER AS nvarchar(MAX)) AS user_group_name, '1' AS srt "
					"UNION ALL "
					"SELECT CAST([name] AS nvarchar(MAX)) AS user_group_name, '2' AS srt "
					"	FROM ( "
					"		SELECT *, is_member(name) AS [is_member] "
					"			FROM [sys].[database_principals] "
					"	) em "
					"	WHERE [is_member] = 1 "
					"	ORDER BY 2, 1"));
		}

		SFINISH_CHECK(DB_exec(EV_A, client->conn, client, str_sql, http_client_info_cb), "DB_exec failed");

#ifdef ENVELOPE
	} else if (strstr(str_uri, "accept_") != NULL || strstr(str_uri, "acceptnc_") != NULL) {
		char *ptr_dot = strstr(str_uri, ".");
		if (
			(
				ptr_dot != NULL &&
				(
					strncmp(ptr_dot + 1, "accept_", 7) == 0 ||
					strncmp(ptr_dot + 1, "acceptnc_", 9) == 0
				)
			) ||
			strncmp(str_uri, "/env/accept_", 12) == 0 ||
			strncmp(str_uri, "/env/acceptnc_", 14) == 0
		) {
			http_accept_step1(client);
		} else {
			http_file_step1(client);
		}
	} else if (strstr(str_uri, "action_") != NULL || strstr(str_uri, "actionnc_") != NULL) {
		char *ptr_dot = strstr(str_uri, ".");
		if (
			(
				ptr_dot != NULL &&
				(
					strncmp(ptr_dot + 1, "action_", 7) == 0 ||
					strncmp(ptr_dot + 1, "actionnc_", 9) == 0
				)
			) ||
			strncmp(str_uri, "/env/action_", 12) == 0 ||
			strncmp(str_uri, "/env/actionnc_", 14) == 0
		) {
			http_action_step1(client);
		} else {
			http_file_step1(client);
		}
#endif
	} else {
		SDEBUG("http_file_step1");
		http_file_step1(client);
	}
finish:
	bol_error_state = false;
	if (str_response != NULL) {
		_str_response = str_response;
		snprintf(str_length, 50, "%zu", strlen(_str_response));
		SFINISH_SNCAT(str_response, &int_response_len,
			"HTTP/1.1 500 Internal Server Error\015\012Content-Length: ", (size_t)52,
			str_length, strlen(str_length),
			"\015\012Connection: close\015\012\015\012", (size_t)21,
			_str_response, strlen(_str_response));
		SFREE(_str_response);
		if (CLIENT_WRITE(client, str_response, int_response_len) < int_response_len) {
			if (bol_tls) {
				SERROR_NORESPONSE_LIBTLS_CONTEXT(client->tls_postage_io_context, "tls_write() failed");
			} else {
				SERROR_NORESPONSE("write() failed");
			}
		}
		if (str_response == client->str_response) {
			client->str_response = NULL;
		}
		SFREE(str_response);

		SFINISH_CLIENT_CLOSE(client);<commit_msg>fix NC errors (close workflowproducts/envelope#53)<commit_after>#include "http_main.h"

bool http_client_info_cb(EV_P, void *cb_data, DB_result *res);

/*
All this function does is check the database connection, then call a callback
*/
void http_main_cnxn_cb(EV_P, void *cb_data, DB_conn *conn) {
	SDEBUG("http_main_cnxn_cb");
	struct sock_ev_client *client = cb_data;
	char *str_response = NULL;
	char *_str_response = NULL;
	char *str_uri = NULL;
	char *str_uri_temp = NULL;
	char *str_sql = NULL;
	char *ptr_end_uri = NULL;
	char str_length[50];
	size_t int_uri_len = 0;
	size_t int_sql_len = 0;
	size_t int_response_len = 0;

	SFINISH_CHECK(conn->int_status == 1, "%s", conn->str_response);

	str_uri = str_uri_path(client->str_request, client->int_request_len, &int_uri_len);
	SFINISH_CHECK(str_uri, "str_uri_path failed");

#ifdef ENVELOPE
#else
	if (isdigit(str_uri[9])) {
		str_uri_temp = str_uri;
		char *str_temp = strchr(str_uri_temp + 9, '/');
		SFINISH_CHECK(str_temp != NULL, "strchr failed");
		SFINISH_SNCAT(str_uri, &int_uri_len,
			"/postage/app", (size_t)12,
			str_temp, strlen(str_temp));
		SFREE(str_uri_temp);
	}
#endif
	ptr_end_uri = strchr(str_uri, '?');
	if (ptr_end_uri != NULL) {
		*ptr_end_uri = '\0';
		int_uri_len = (size_t)(ptr_end_uri - str_uri);
	}
	ptr_end_uri = strchr(str_uri, '#');
	if (ptr_end_uri != NULL) {
		*ptr_end_uri = '\0';
		int_uri_len = (size_t)(ptr_end_uri - str_uri);
	}

	SDEBUG("str_uri: %s", str_uri);

#ifdef ENVELOPE
	if (strncmp(str_uri, "/env/upload", 12) == 0) {
#else
	if (strncmp(str_uri, "/postage/app/upload", 22) == 0) {
#endif
		http_upload_step1(client);
#ifdef ENVELOPE
#else
#ifdef POSTAGE_INTERFACE_LIBPQ
	} else if (strncmp(str_uri, "/postage/app/export", 22) == 0) {
		http_export_step1(client);
#endif
#endif
#ifdef ENVELOPE
	} else if (strncmp(str_uri, "/env/action_ev", 15) == 0) {
		http_ev_step1(client);
	} else if (strncmp(str_uri, "/env/action_select", 19) == 0) {
		http_select_step1(client);
	} else if (strncmp(str_uri, "/env/action_insert", 19) == 0) {
		http_insert_step1(client);
	} else if (strncmp(str_uri, "/env/action_update", 19) == 0) {
		http_update_step1(client);
	} else if (strncmp(str_uri, "/env/action_delete", 19) == 0) {
		http_delete_step1(client);
	} else if (strncmp(str_uri, "/env/action_info", 17) == 0) {
#else
	} else if (strncmp(str_uri, "/postage/app/action_ev", 23) == 0) {
		http_ev_step1(client);
	} else if (strncmp(str_uri, "/postage/app/action_info", 25) == 0) {
#endif
		if (DB_connection_driver(client->conn) == DB_DRIVER_POSTGRES) {
			SFINISH_SNCAT(str_sql, &int_sql_len,
				"SELECT version() "
				"UNION ALL "
				"SELECT SESSION_USER::text "
				"UNION ALL "
				"SELECT g.rolname "
				"	FROM pg_catalog.pg_roles g "
				"	LEFT JOIN pg_catalog.pg_auth_members m ON m.roleid = g.oid "
				"	LEFT JOIN pg_catalog.pg_roles u ON m.member = u.oid "
				"	WHERE g.rolcanlogin = FALSE AND u.rolname = SESSION_USER::text AND g.rolname IS NOT NULL",
				strlen("SELECT version() "
					"UNION ALL "
					"SELECT SESSION_USER::text "
					"UNION ALL "
					"SELECT g.rolname "
					"	FROM pg_catalog.pg_roles g "
					"	LEFT JOIN pg_catalog.pg_auth_members m ON m.roleid = g.oid "
					"	LEFT JOIN pg_catalog.pg_roles u ON m.member = u.oid "
					"	WHERE g.rolcanlogin = FALSE AND u.rolname = SESSION_USER::text AND g.rolname IS NOT NULL"));
		} else {
			SFINISH_SNCAT(str_sql, &int_sql_len,
				"SELECT CAST(@@VERSION AS nvarchar(MAX)), '0' AS srt "
				"UNION ALL "
				"SELECT CAST(SYSTEM_USER AS nvarchar(MAX)) AS user_group_name, '1' AS srt "
				"UNION ALL "
				"SELECT CAST([name] AS nvarchar(MAX)) AS user_group_name, '2' AS srt "
				"	FROM ( "
				"		SELECT *, is_member(name) AS [is_member] "
				"			FROM [sys].[database_principals] "
				"	) em "
				"	WHERE [is_member] = 1 "
				"	ORDER BY 2, 1",
				strlen("SELECT CAST(@@VERSION AS nvarchar(MAX)), '0' AS srt "
					"UNION ALL "
					"SELECT CAST(SYSTEM_USER AS nvarchar(MAX)) AS user_group_name, '1' AS srt "
					"UNION ALL "
					"SELECT CAST([name] AS nvarchar(MAX)) AS user_group_name, '2' AS srt "
					"	FROM ( "
					"		SELECT *, is_member(name) AS [is_member] "
					"			FROM [sys].[database_principals] "
					"	) em "
					"	WHERE [is_member] = 1 "
					"	ORDER BY 2, 1"));
		}

		SFINISH_CHECK(DB_exec(EV_A, client->conn, client, str_sql, http_client_info_cb), "DB_exec failed");

#ifdef ENVELOPE
	} else if (strstr(str_uri, "accept_") != NULL || strstr(str_uri, "acceptnc_") != NULL) {
		char *ptr_dot = strstr(str_uri, ".");
		if (
			(
				ptr_dot != NULL &&
				(
					strncmp(ptr_dot + 1, "accept_", 7) == 0 ||
					strncmp(ptr_dot + 1, "acceptnc_", 9) == 0
				)
			) ||
			strncmp(str_uri, "/env/accept_", 12) == 0 ||
			strncmp(str_uri, "/env/acceptnc_", 14) == 0
		) {
			http_accept_step1(client);
		} else {
			http_file_step1(client);
		}
	} else if (strstr(str_uri, "action_") != NULL || strstr(str_uri, "actionnc_") != NULL) {
		char *ptr_dot = strstr(str_uri, ".");
		if (
			(
				ptr_dot != NULL &&
				(
					strncmp(ptr_dot + 1, "action_", 7) == 0 ||
					strncmp(ptr_dot + 1, "actionnc_", 9) == 0
				)
			) ||
			strncmp(str_uri, "/env/action_", 12) == 0 ||
			strncmp(str_uri, "/env/actionnc_", 14) == 0
		) {
			http_action_step1(client);
		} else {
			http_file_step1(client);
		}
#endif
	} else {
		SDEBUG("http_file_step1");
		http_file_step1(client);
	}
finish:
	bol_error_state = false;
	if (str_response != NULL) {
		_str_response = str_response;
		snprintf(str_length, 50, "%zu", strlen(_str_response));
		SFINISH_SNCAT(str_response, &int_response_len,
			"HTTP/1.1 500 Internal Server Error\015\012Content-Length: ", (size_t)52,
			str_length, strlen(str_length),
			"\015\012Connection: close\015\012\015\012", (size_t)23,
			_str_response, strlen(_str_response));
		SFREE(_str_response);
		if (CLIENT_WRITE(client, str_response, int_response_len) < int_response_len) {
			if (bol_tls) {
				SERROR_NORESPONSE_LIBTLS_CONTEXT(client->tls_postage_io_context, "tls_write() failed");
			} else {
				SERROR_NORESPONSE("write() failed");
			}
		}
		if (str_response == client->str_response) {
			client->str_response = NULL;
		}
		SFREE(str_response);

		SFINISH_CLIENT_CLOSE(client);
{'fraction_non_alphanumeric': 0.15498989898989898, 'fraction_numerical': 0.02117171717171717, 'mean_word_length': 3.88783570300158, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '305308', 'n_tokens_mistral': 5845, 'n_tokens_neox': 5079, 'n_words': 1019}