Beispiel #1
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);
}
Beispiel #2
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);
}
Beispiel #3
0
int
serve_b64test(struct http_request *req)
{
	int			i;
	size_t			len;
	struct kore_buf		*res;
	u_int8_t		*data;

	res = kore_buf_alloc(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_free(data);

	return (KORE_RESULT_OK);
}
Beispiel #4
0
int
serve_params_test(struct http_request *req)
{
	struct kore_buf		*b;
	u_int8_t		*d;
	size_t			len;
	int			r, i;
	char			*test, name[10];

	if (req->method == HTTP_METHOD_GET)
		http_populate_get(req);
	else if (req->method == HTTP_METHOD_POST)
		http_populate_post(req);

	b = kore_buf_alloc(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(req, "arg1", &test)) {
		kore_buf_replace_string(b, "$arg1$", test, strlen(test));
	} else {
		kore_buf_replace_string(b, "$arg1$", NULL, 0);
	}

	if (http_argument_get_string(req, "arg2", &test)) {
		kore_buf_replace_string(b, "$arg2$", test, strlen(test));
	} 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(req, "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_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(req, name, &test)) {
			(void)snprintf(name, sizeof(name), "$test%d$", i);
			kore_buf_replace_string(b, name, test, strlen(test));
		} 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_free(d);

	return (KORE_RESULT_OK);
}
Beispiel #5
0
void
kore_accesslog_gather(void *arg, u_int64_t now, int force)
{
	int				id;
	struct kore_worker		*kw;
	struct kore_alog_header		*hdr;
	struct kore_domain		*dom;
	size_t				off, remain;

	if (logbuf == NULL)
		logbuf = kore_buf_alloc(LOGBUF_SIZE);

	for (id = 0; id < worker_count; id++) {
		kw = kore_worker_data(id);

		accesslog_lock(kw);

		if (force || kw->lb.offset >= KORE_ACCESSLOG_SYNC) {
			kore_buf_append(logbuf, kw->lb.buf, kw->lb.offset);
			kw->lb.offset = 0;
		}

		accesslog_unlock(kw);
	}

	if (force || logbuf->offset >= LOGBUF_SIZE) {
		off = 0;
		remain = logbuf->offset;

		while (remain > 0) {
			if (remain < sizeof(*hdr)) {
				kore_log(LOG_ERR,
				    "invalid log buffer: (%zu remain)", remain);
				break;
			}

			hdr = (struct kore_alog_header *)(logbuf->data + off);
			off += sizeof(*hdr);
			remain -= sizeof(*hdr);

			if (hdr->loglen > remain) {
				kore_log(LOG_ERR,
				    "invalid log header: %u (%zu remain)",
				    hdr->loglen, remain);
				break;
			}

			if ((dom = kore_domain_byid(hdr->domain)) == NULL)
				fatal("unknown domain id %u", hdr->domain);

			if (dom->logbuf == NULL)
				dom->logbuf = kore_buf_alloc(DOMAIN_LOGBUF_LEN);

			kore_buf_append(dom->logbuf, &logbuf->data[off],
			    hdr->loglen);

			off += hdr->loglen;
			remain -= hdr->loglen;

			accesslog_flush(dom, now, force);
		}

		kore_buf_reset(logbuf);
	}

	if (force)
		kore_domain_callback(accesslog_flush_cb);
}