Пример #1
0
void
kore_task_handle(struct kore_task *t, int finished)
{
	kore_debug("kore_task_handle: %p, %d", t, finished);

	if (t->req != NULL)
		http_request_wakeup(t->req);

	if (finished) {
		kore_task_set_state(t, KORE_TASK_STATE_FINISHED);
		if (t->req != NULL) {
			if (t->req->flags & HTTP_REQUEST_DELETE)
				kore_task_destroy(t);
		}
	}
}
Пример #2
0
int
page_handler(struct http_request *req)
{
	u_int32_t	len;
	char		*user, result[64];

	/*
	 * Lets check if a task has been created yet, this is important
	 * as we only want to fire this off once and we will be called
	 * again once it has been created.
	 */
	if (req->task == NULL) {
		/* Grab the user argument */
		http_populate_arguments(req);
		if (!http_argument_get_string("user", &user, &len)) {
			http_response(req, 500, "ERROR\n", 6);
			return (KORE_RESULT_OK);
		}

		/*
		 * Create a new task that will execute the run_curl()
		 * function and bind it to our request.
		 *
		 * Binding a task to a request means Kore will reschedule
		 * the page handler for that request to refire after the
		 * task has completed or when it writes on the task channel.
		 */
		kore_task_create(&req->task, run_curl);
		kore_task_bind_request(req->task, req);

		/*
		 * Start the task and write the user we received in our
		 * GET request to its channel.
		 */
		kore_task_run(req->task);
		kore_task_channel_write(req->task, user, len);

		/*
		 * Tell Kore to retry us later.
		 */
		return (KORE_RESULT_RETRY);
	}

	/*
	 * Our page handler is scheduled to be called when either the
	 * task finishes or has written data onto the channel.
	 *
	 * In order to distuingish between the two we can inspect the
	 * state of the task.
	 */
	if (kore_task_state(req->task) != KORE_TASK_STATE_FINISHED) {
		http_request_sleep(req);
		return (KORE_RESULT_RETRY);
	}

	/*
	 * Task is finished, check the result.
	 */
	if (kore_task_result(req->task) != KORE_RESULT_OK) {
		kore_task_destroy(req->task);
		http_response(req, 500, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/*
	 * Lets read what our task has written to the channel.
	 *
	 * kore_task_channel_read() will return the amount of bytes
	 * that it received for that read. If the returned bytes is
	 * larger then the buffer you passed this is a sign of truncation
	 * and should be treated carefully.
	 */
	len = kore_task_channel_read(req->task, result, sizeof(result));
	if (len > sizeof(result)) {
		http_response(req, 500, NULL, 0);
	} else {
		http_response(req, 200, result, len);
	}

	/* We good, destroy the task. */
	kore_task_destroy(req->task);

	return (KORE_RESULT_OK);
}