Beispiel #1
0
/*
 * This is the function that is executed by our task which is created
 * in the page_handler() callback.
 *
 * It sets up a CURL POST request to /post_back passing along the
 * user argument which it receives from its channel from page_handler().
 */
int
run_curl(struct kore_task *t)
{
	int			l;
	struct kore_buf		*b;
	u_int32_t		len;
	CURLcode		res;
	u_int8_t		*data;
	CURL			*curl;
	char			user[64], fields[128];

	/*
	 * Read the channel in order to obtain the user argument
	 * that was written to it by page_handler().
	 */
	len = kore_task_channel_read(t, user, sizeof(user));
	if (len > sizeof(user))
		return (KORE_RESULT_ERROR);

	l = snprintf(fields, sizeof(fields), "user=%.*s", len, user);
	if (l == -1 || (size_t)l >= sizeof(fields))
		return (KORE_RESULT_ERROR);

	if ((curl = curl_easy_init()) == NULL)
		return (KORE_RESULT_ERROR);

	b = kore_buf_create(128);

	/* Do CURL magic. */
	curl_easy_setopt(curl, CURLOPT_POST, 1);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, b);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb);
	curl_easy_setopt(curl, CURLOPT_URL, "https://127.0.0.1:4443/post_back");

	res = curl_easy_perform(curl);
	if (res != CURLE_OK) {
		kore_buf_free(b);
		curl_easy_cleanup(curl);
		return (KORE_RESULT_ERROR);
	}

	/*
	 * Grab the response from the CURL request and write the
	 * result back to the task channel.
	 */
	data = kore_buf_release(b, &len);
	kore_task_channel_write(t, data, len);
	kore_mem_free(data);

	return (KORE_RESULT_OK);
}
Beispiel #2
0
int things_list(struct http_request *req) {
    int rc;
    char *zErrMsg = 0;
    char *query = "SELECT * FROM found ORDER BY last DESC";
    template_t tmpl;
	attrlist_t attributes;

    struct timespec when;

    buf = kore_buf_create(mb);

    if(!thing) thing = hashmap_new();

    // load template from assets
    template_load
        (asset_things_list_html, asset_len_things_list_html, &tmpl);

    // initialise attribute list
    attributes = attrinit();

    if( ! parse_datetime(&when, "now", NULL) )
        kore_log(LOG_ERR,"parse-datetime error");
    else {
        struct tm *tt;
        tt = localtime (&when.tv_sec);
        mktime(tt);
        strftime(line, ml, "Dowse :: %d %m %Y - %H:%M:%S", tt);
        attrcat(attributes, "title", line);
    }
    
    sqlquery(query, things_list_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);
}
Beispiel #3
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);

}
Beispiel #4
0
int
serve_b64test(struct http_request *req)
{
    int			i;
    u_int32_t		len;
    struct kore_buf		*res;
    u_int8_t		*data;

    res = kore_buf_create(1024);
    for (i = 0; b64tests[i] != NULL; i++)
        test_base64((u_int8_t *)b64tests[i], strlen(b64tests[i]), res);

    data = kore_buf_release(res, &len);

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

    return (KORE_RESULT_OK);
}
Beispiel #5
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);
}
Beispiel #6
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);
}