static void hev_scgi_handler_cgi_handle(HevSCGIHandler *handler, GObject *scgi_task) { HevSCGIHandlerCGI *self = HEV_SCGI_HANDLER_CGI(handler); HevSCGIHandlerCGIPrivate *priv = HEV_SCGI_HANDLER_CGI_GET_PRIVATE(self); HevSCGIHandlerCGITaskData *task_data = NULL; gchar *str = NULL, **argv = NULL, *workdir = NULL; GPid pid = 0; GError *error = NULL; GObject *connection = NULL; GSocket *socket = NULL; g_debug("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); task_data = g_slice_new0(HevSCGIHandlerCGITaskData); if(!task_data) { g_critical("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); return; } connection = hev_scgi_task_get_socket_connection(HEV_SCGI_TASK(scgi_task)); socket = g_socket_connection_get_socket(G_SOCKET_CONNECTION(connection)); task_data->fd = g_socket_get_fd(socket); task_data->scgi_task = scgi_task; task_data->scgi_request = hev_scgi_task_get_request(HEV_SCGI_TASK(scgi_task)); task_data->scgi_response = hev_scgi_task_get_response(HEV_SCGI_TASK(scgi_task)); task_data->req_hash_table = hev_scgi_request_get_header_hash_table(HEV_SCGI_REQUEST(task_data->scgi_request)); task_data->envp = g_malloc0_n(g_hash_table_size(task_data->req_hash_table)+1, sizeof(gchar *)); g_hash_table_foreach(task_data->req_hash_table, req_hash_table_foreach_handler, task_data); /* Script file and Work dir */ str = g_hash_table_lookup(task_data->req_hash_table, "SCRIPT_FILE"); argv = g_malloc0_n(2, sizeof(gchar *)); if(str) { argv[0] = g_strdup(str); workdir = g_path_get_dirname(str); } else { argv[0] = g_key_file_get_string(priv->config, "Module", "CGIBinPath", NULL); workdir = g_key_file_get_string(priv->config, "Module", "WorkDir", NULL); if(NULL == argv[0]) argv[0] = g_strdup(HEV_SCGI_HANDLER_CGI_BIN_PATH); if(NULL == workdir) workdir = g_strdup(HEV_SCGI_HANDLER_CGI_WORK_DIR); } #ifdef G_OS_UNIX /* User and Group */ str = g_hash_table_lookup(task_data->req_hash_table, "_USER"); if(str) task_data->user = g_strdup(str); else task_data->user = g_key_file_get_string(priv->config, "Module", "User", NULL); str = g_hash_table_lookup(task_data->req_hash_table, "_GROUP"); if(str) task_data->group = g_strdup(str); else task_data->group = g_key_file_get_string(priv->config, "Module", "Group", NULL); #endif /* G_OS_UNIX */ if(g_spawn_async(workdir, argv, task_data->envp, G_SPAWN_DO_NOT_REAP_CHILD, hev_scgi_handler_spawn_child_setup_handler, task_data, &pid, &error)) { g_object_ref(scgi_task); g_child_watch_add(pid, hev_scgi_handler_child_watch_handler, task_data); } else { g_critical("%s:%d[%s]=>(%s)", __FILE__, __LINE__, __FUNCTION__, error->message); g_error_free(error); } g_strfreev(argv); g_free(workdir); }
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); }