Example #1
0
static void hev_scgi_handler_response_error_message(HevSCGIHandlerCGITaskData *task_data,
			const gchar *message)
{
	GHashTable *res_hash_table = NULL;
	GOutputStream *output_stream = NULL;

	g_debug("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__);

	res_hash_table =
		hev_scgi_response_get_header_hash_table(HEV_SCGI_RESPONSE(task_data->scgi_response));
	g_hash_table_insert(res_hash_table, g_strdup("Status"), g_strdup("500 Internal Server Error"));
	g_hash_table_insert(res_hash_table, g_strdup("ContentType"), g_strdup("text/plain"));
	hev_scgi_response_write_header(HEV_SCGI_RESPONSE(task_data->scgi_response), NULL);

	output_stream = hev_scgi_response_get_output_stream(HEV_SCGI_RESPONSE(task_data->scgi_response));
	g_output_stream_write_all(output_stream, message, strlen(message), NULL, NULL, NULL);

	exit(EXIT_FAILURE);
}
Example #2
0
static void
filebox_uploader_handle_task_handler (GTask *task, gpointer source_object,
			gpointer task_data, GCancellable *cancellable)
{
	HevFileboxUploader *self = HEV_FILEBOX_UPLOADER (source_object);
	HevFileboxUploaderPrivate *priv = HEV_FILEBOX_UPLOADER_GET_PRIVATE (self);
	GObject *scgi_task = task_data;
	gboolean status = TRUE;
	GObject *request = NULL;
	GInputStream *req_stream = NULL;
	GHashTable *req_htb = NULL;
	GObject *response = NULL;
	GOutputStream *res_stream = NULL;
	GHashTable *res_htb = NULL;
	const gchar *content_type = NULL, *content_length = NULL;
	GRegex *regex = NULL;
	GMatchInfo *match_info = NULL;
	gchar rand_pass[16], *boundary = NULL, *fp_path = NULL, *fm_path = NULL, *ft_path = NULL;
	gint rand_pass_len;
	guint64 length = 0;
	GFile *file_tmp = NULL;

	g_debug ("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__);

	request = hev_scgi_task_get_request (HEV_SCGI_TASK (scgi_task));
	req_stream = hev_scgi_request_get_input_stream (HEV_SCGI_REQUEST (request));
	req_htb = hev_scgi_request_get_header_hash_table (HEV_SCGI_REQUEST (request));
	response = hev_scgi_task_get_response (HEV_SCGI_TASK (scgi_task));
	res_stream = hev_scgi_response_get_output_stream (HEV_SCGI_RESPONSE (response));
	res_htb = hev_scgi_response_get_header_hash_table (HEV_SCGI_RESPONSE (response));

	content_type = g_hash_table_lookup (req_htb, "CONTENT_TYPE");
	content_length = g_hash_table_lookup (req_htb, "CONTENT_LENGTH");

	/* get boundary string from content type */
	regex = g_regex_new ("^multipart/form-data;\\s*boundary=(.+)$", 0, 0, NULL);
	if (!g_regex_match (regex, content_type, 0, &match_info)) {
		g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("400 Bad Request"));
		hev_scgi_response_write_header (HEV_SCGI_RESPONSE (response), NULL);
		g_regex_unref (regex);
		g_task_return_boolean (task, FALSE);
		return;
	}
	boundary = g_match_info_fetch (match_info, 1);
	g_match_info_unref (match_info);
	g_regex_unref (regex);

	fp_path = g_key_file_get_string (priv->config, "Module", "FilePoolPath", NULL);
	fm_path = g_key_file_get_string (priv->config, "Module", "FileMetaPath", NULL);
	ft_path = g_key_file_get_string (priv->config, "Module", "FileTempPath", NULL);

	length = g_ascii_strtoull (content_length, NULL, 10);
	rand_pass_len = g_snprintf (rand_pass, 16, "%u", g_random_int_range (99999, 999999));
	g_object_set_data (scgi_task, "rand-pass", rand_pass);

	/* create tmp file */
	file_tmp = filebox_uploader_handle_task_create_tmp (self, scgi_task,
				req_stream, req_htb, res_htb, ft_path, length);
	if (file_tmp) {
		gchar *path = NULL;
		GMappedFile *mapped_file = NULL;

		path = g_file_get_path (file_tmp);
		mapped_file = g_mapped_file_new (path, FALSE, NULL);
		g_free (path);
		if (mapped_file) {
			GPtrArray *files = NULL;
			gchar *duration = NULL, *one_off = NULL;

			/* split files from tmp file */
			files = filebox_uploader_handle_task_split_tmp (self, scgi_task,
						mapped_file, res_htb, fp_path, fm_path, boundary,
						&duration, &one_off);
			if (files) {
				/* write meta files */
				g_object_set_data (scgi_task, "duration", duration);
				g_object_set_data (scgi_task, "one-off", one_off);
				g_ptr_array_foreach (files,
							file_ptr_array_foreach_write_meta_handler,
							scgi_task);
				g_ptr_array_unref (files);
			}
			
			g_free (duration);
			g_free (one_off);
			g_mapped_file_unref (mapped_file);
		}
		
		g_file_delete (file_tmp, NULL, NULL);
		g_object_unref (file_tmp);
	}

	g_free (boundary);
	g_free (fp_path);
	g_free (fm_path);
	g_free (ft_path);

	if (!g_hash_table_contains (res_htb, "Status"))
	  g_hash_table_insert (res_htb, g_strdup ("Status"), g_strdup ("200 OK"));
	hev_scgi_response_write_header (HEV_SCGI_RESPONSE (response), NULL);
	g_output_stream_write_all (res_stream, rand_pass, rand_pass_len, NULL, NULL, NULL);

	g_task_return_boolean (task, status);
}