Ejemplo n.º 1
0
Archivo: buf.c Proyecto: acml/kore
void
kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src)
{
	u_int8_t	*d;
	u_int32_t	len;

	d = kore_buf_release(src, &len);
	kore_buf_append(buf, d, len);
}
Ejemplo n.º 2
0
void
kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src)
{
	u_int8_t	*d;
	size_t		len;

	d = kore_buf_release(src, &len);
	kore_buf_append(buf, d, len);
	kore_mem_free(d);
}
Ejemplo n.º 3
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);
}
Ejemplo 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);
}
Ejemplo n.º 5
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);
}
Ejemplo 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);
}
Ejemplo n.º 7
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);
}