Exemplo n.º 1
0
int verify_request (struct http_request *req, char **data)
{
    const char          *msg = NULL;
    json_object         *json_msg = NULL;
    
    json_msg = json_object_new_object ();
    
    if (req->method != HTTP_METHOD_POST)
    {
        json_object_object_add (json_msg, "result", json_object_new_int (KORE_RESULT_ERROR));
        json_object_object_add (json_msg, "message", json_object_new_string ("method is not post"));
        msg = json_object_to_json_string(json_msg);
        http_response (req, 200, msg,  strlen(msg));
        
        json_object_put (json_msg);
        return (KORE_RESULT_ERROR);
    }
    
    http_populate_post (req);
    
    if (http_argument_get_string (req, "data", data) == KORE_RESULT_ERROR)
    {
        json_object_object_add (json_msg, "result", json_object_new_int (KORE_RESULT_ERROR));
        json_object_object_add (json_msg, "message", json_object_new_string ("argument key is not data"));
        msg = json_object_to_json_string(json_msg);
        http_response (req, 200, msg,  strlen(msg));
        
        json_object_put (json_msg);
        return (KORE_RESULT_ERROR);
    }
    
    json_object_put (json_msg);
    return (KORE_RESULT_OK);
}
Exemplo n.º 2
0
int
page(struct http_request *req)
{
	u_int16_t		id;
	char			*sid;
	struct kore_buf		*buf;

	/*
	 * Before we are able to obtain any parameters given to
	 * us via the query string we must tell Kore to parse and
	 * validate them.
	 *
	 * NOTE: All parameters MUST be declared in a params {} block
	 * inside the configuration for Kore! Kore will filter out
	 * any parameters not explicitly defined.
	 *
	 * See conf/parameters.conf on how that is done, this is an
	 * important step as without the params block you will never
	 * get any parameters returned from Kore.
	 */
	http_populate_get(req);

	/*
	 * Lets grab the "id" parameter if available. Kore can obtain
	 * parameters in different data types native to C.
	 *
	 * In this scenario, lets grab it both as an actual string and
	 * as an u_int16_t (unsigned short).
	 *
	 * When trying to obtain a parameter as something else then
	 * a string, Kore will automatically check if the value fits
	 * in said data type.
	 *
	 * For example if id is 65536 it won't fit in an u_int16_t
	 * and Kore will return an error when trying to read it as such.
	 */

	buf = kore_buf_alloc(128);

	/* Grab it as a string, we shouldn't free the result in sid. */
	if (http_argument_get_string(req, "id", &sid))
		kore_buf_appendf(buf, "id as a string: '%s'\n", sid);

	/* Grab it as an actual u_int16_t. */
	if (http_argument_get_uint16(req, "id", &id))
		kore_buf_appendf(buf, "id as an u_int16_t: %d\n", id);

	/* Now return the result to the client with a 200 status code. */
	http_response(req, 200, buf->data, buf->offset);
	kore_buf_free(buf);

	return (KORE_RESULT_OK);
}
Exemplo n.º 3
0
Arquivo: ktunnel.c Projeto: 2ion/kore
/*
 * Receive a request to open a new connection.
 */
int
open_connection(struct http_request *req)
{
	char			*host, *port;

	/* Don't want to deal with SPDY connections. */
	if (req->owner->proto != CONN_PROTO_HTTP) {
		http_response(req, HTTP_STATUS_BAD_REQUEST, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Parse the query string and grab our arguments. */
	http_populate_arguments(req);
	if (!http_argument_get_string("host", &host, NULL) ||
	    !http_argument_get_string("port", &port, NULL)) {
		http_response(req, HTTP_STATUS_BAD_REQUEST, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Create our tunnel. */
	if (!ktunnel_pipe_create(req->owner, host, port)) {
		http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/*
	 * Hack so http_response() doesn't end up queueing a new
	 * netbuf for receiving more HTTP requests on the same connection.
	 */
	req->owner->flags |= CONN_CLOSE_EMPTY;

	/* Respond to the client now that we're good to go. */
	http_response(req, HTTP_STATUS_OK, NULL, 0);

	/* Unset this so we don't disconnect after returning. */
	req->owner->flags &= ~CONN_CLOSE_EMPTY;

	return (KORE_RESULT_OK);
}
Exemplo n.º 4
0
int
serve_file_upload(struct http_request *req)
{
	u_int8_t		*d;
	struct kore_buf		*b;
	struct http_file	*f;
	size_t			len;
	char			*name, buf[BUFSIZ];

	b = kore_buf_alloc(asset_len_upload_html);
	kore_buf_append(b, asset_upload_html, asset_len_upload_html);

	if (req->method == HTTP_METHOD_POST) {
		if (req->http_body_fd != -1)
			kore_log(LOG_NOTICE, "file is on disk");

		http_populate_multipart_form(req);
		if (http_argument_get_string(req, "firstname", &name)) {
			kore_buf_replace_string(b, "$firstname$",
			    name, strlen(name));
		} else {
			kore_buf_replace_string(b, "$firstname$", NULL, 0);
		}

		if ((f = http_file_lookup(req, "file")) != NULL) {
			(void)snprintf(buf, sizeof(buf),
			    "%s is %ld bytes", f->filename, f->length);
			kore_buf_replace_string(b,
			    "$upload$", buf, strlen(buf));
		} else {
			kore_buf_replace_string(b, "$upload$", NULL, 0);
		}
	} else {
		kore_buf_replace_string(b, "$upload$", NULL, 0);
		kore_buf_replace_string(b, "$firstname$", NULL, 0);
	}

	d = kore_buf_release(b, &len);

	http_response_header(req, "content-type", "text/html");
	http_response(req, 200, d, len);
	kore_free(d);

	return (KORE_RESULT_OK);
}
Exemplo n.º 5
0
int thing_show(struct http_request *req) {
    int rc;
    template_t tmpl;
	attrlist_t attributes;
    char *zErrMsg = 0;
    char *macaddr;

	http_populate_get(req);

    // we shouldn't free the result in macaddr
	if (http_argument_get_string(req, "macaddr", &macaddr))
		kore_log(LOG_DEBUG, "thing_show macaddr %s",macaddr);
    else
        kore_log(LOG_ERR,"thing_show get argument error");

    // prepare query
    snprintf(line,ml,"SELECT * FROM found WHERE macaddr = '%s'",macaddr);
    
    // allocate output buffer
    buf = kore_buf_create(mb);

    // load template
    template_load
        (asset_thing_show_html, asset_len_thing_show_html, &tmpl);
    attributes = attrinit();

    attrcat(attributes, "title", "Dowse information panel");

    // SQL query
    sqlquery(line, thing_show_cb, attributes);

    template_apply(&tmpl,attributes,buf);

	http_response(req, 200, buf->data, buf->offset);

    template_free(&tmpl);
    attrfree(attributes);

    kore_buf_free(buf);

	return (KORE_RESULT_OK);

}
Exemplo n.º 6
0
int
serve_file_upload(struct http_request *req)
{
    int			r;
    u_int8_t		*d;
    struct kore_buf		*b;
    u_int32_t		len;
    char			*name, buf[BUFSIZ];

    b = kore_buf_create(asset_len_upload_html);
    kore_buf_append(b, asset_upload_html, asset_len_upload_html);

    if (req->method == HTTP_METHOD_POST) {
        http_populate_multipart_form(req, &r);
        if (http_argument_get_string("firstname", &name, &len)) {
            kore_buf_replace_string(b, "$firstname$", name, len);
        } else {
            kore_buf_replace_string(b, "$firstname$", NULL, 0);
        }

        if (http_file_lookup(req, "file", &name, &d, &len)) {
            (void)snprintf(buf, sizeof(buf),
                           "%s is %d bytes", name, len);
            kore_buf_replace_string(b,
                                    "$upload$", buf, strlen(buf));
        } else {
            kore_buf_replace_string(b, "$upload$", NULL, 0);
        }
    } else {
        kore_buf_replace_string(b, "$upload$", NULL, 0);
        kore_buf_replace_string(b, "$firstname$", NULL, 0);
    }

    d = kore_buf_release(b, &len);

    http_response_header(req, "content-type", "text/html");
    http_response(req, 200, d, len);
    kore_mem_free(d);

    return (KORE_RESULT_OK);
}
Exemplo n.º 7
0
int
post_back(struct http_request *req)
{
	u_int32_t	len;
	char		*user;

	if (req->method != HTTP_METHOD_POST) {
		http_response(req, 500, NULL, 0);
		return (KORE_RESULT_OK);
	}

	http_populate_arguments(req);
	if (!http_argument_get_string("user", &user, &len)) {
		http_response(req, 500, NULL, 0);
		return (KORE_RESULT_OK);
	}

	/* Simply echo the supplied user argument back. */
	http_response(req, 200, user, len);

	return (KORE_RESULT_OK);
}
Exemplo n.º 8
0
int
serve_params_test(struct http_request *req)
{
    struct kore_buf		*b;
    u_int8_t		*d;
    u_int32_t		len;
    int			r, i;
    char			*test, name[10];

    http_populate_arguments(req);

    b = kore_buf_create(asset_len_params_html);
    kore_buf_append(b, asset_params_html, asset_len_params_html);

    /*
     * The GET parameters will be filtered out on POST.
     */
    if (http_argument_get_string("arg1", &test, &len)) {
        kore_buf_replace_string(b, "$arg1$", test, len);
    } else {
        kore_buf_replace_string(b, "$arg1$", NULL, 0);
    }

    if (http_argument_get_string("arg2", &test, &len)) {
        kore_buf_replace_string(b, "$arg2$", test, len);
    } else {
        kore_buf_replace_string(b, "$arg2$", NULL, 0);
    }

    if (req->method == HTTP_METHOD_GET) {
        kore_buf_replace_string(b, "$test1$", NULL, 0);
        kore_buf_replace_string(b, "$test2$", NULL, 0);
        kore_buf_replace_string(b, "$test3$", NULL, 0);

        if (http_argument_get_uint16("id", &r))
            kore_log(LOG_NOTICE, "id: %d", r);
        else
            kore_log(LOG_NOTICE, "No id set");

        http_response_header(req, "content-type", "text/html");
        d = kore_buf_release(b, &len);
        http_response(req, 200, d, len);
        kore_mem_free(d);

        return (KORE_RESULT_OK);
    }

    for (i = 1; i < 4; i++) {
        (void)snprintf(name, sizeof(name), "test%d", i);
        if (http_argument_get_string(name, &test, &len)) {
            (void)snprintf(name, sizeof(name), "$test%d$", i);
            kore_buf_replace_string(b, name, test, len);
        } else {
            (void)snprintf(name, sizeof(name), "$test%d$", i);
            kore_buf_replace_string(b, name, NULL, 0);
        }
    }

    http_response_header(req, "content-type", "text/html");
    d = kore_buf_release(b, &len);
    http_response(req, 200, d, len);
    kore_mem_free(d);

    return (KORE_RESULT_OK);
}
Exemplo n.º 9
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);
}