<commit_before>/* <src/capi.cpp>
 *
 * This file is part of the x0 web server project and is released under GPL-3.
 * http://www.xzero.io/
 *
 * (c) 2009-2013 <NAME> <<EMAIL>>
 */

#include <x0/capi.h>
#include <x0/http/HttpServer.h>
#include <x0/http/HttpWorker.h>
#include <x0/http/HttpRequest.h>
#include <x0/io/BufferSource.h>
#include <cstdarg>

using namespace x0;

struct x0_server_s
{
	HttpServer server;
	bool autoflush;

	x0_server_s(struct ev_loop* loop) :
		server(loop),
		autoflush(true)
	{}
};

struct x0_request_s
{
	x0_server_t* server;
	HttpRequest* request;

	x0_request_body_fn body_cb;
	void* body_userdata;

	x0_request_abort_fn abort_cb;
	void* abort_userdata;

	x0_request_s(HttpRequest* r, x0_server_t* s) :
		server(s),
		request(r),
		body_cb(nullptr),
		body_userdata(nullptr),
		abort_cb(nullptr),
		abort_userdata(nullptr)
	{
		request->connection.setAutoFlush(server->autoflush);
	}

	void bodyCallback(const BufferRef& ref) {
		if (body_cb) {
			body_cb(this, ref.data(), ref.size(), body_userdata);
		}
	}

	static void abortCallback(void* p) {
		x0_request_s* self = (x0_request_s*) p;
		if (self->abort_cb) {
			self->abort_cb(self->abort_userdata);
		}
	}
};

x0_server_t* x0_server_create(struct ev_loop* loop)
{
	auto s = new x0_server_t(loop);

	s->server.requestHandler = [](HttpRequest* r) -> bool {
		BufferSource body("Hello, I am lazy to serve anything; I wasn't configured properly\n");

		r->status = HttpStatus::InternalServerError;

		char buf[40];
		snprintf(buf, sizeof(buf), "%zu", body.size());
		r->responseHeaders.push_back("Content-Length", buf);

		r->responseHeaders.push_back("Content-Type", "plain/text");
		r->write<BufferSource>(body);

		r->finish();

		return true;
	};

	return s;
}

int x0_listener_add(x0_server_t* server, const char* bind, int port, int backlog)
{
	auto listener = server->server.setupListener(bind, port, 128);
	if (listener == nullptr)
		return -1;
<commit_msg>capi: expose support for XZERO_DEBUG envvar debugging<commit_after>/* <src/capi.cpp>
 *
 * This file is part of the x0 web server project and is released under GPL-3.
 * http://www.xzero.io/
 *
 * (c) 2009-2013 <NAME> <<EMAIL>>
 */

#include <x0/capi.h>
#include <x0/http/HttpServer.h>
#include <x0/http/HttpWorker.h>
#include <x0/http/HttpRequest.h>
#include <x0/io/BufferSource.h>
#include <x0/DebugLogger.h>
#include <cstdarg>

using namespace x0;

struct x0_server_s
{
	HttpServer server;
	bool autoflush;

	x0_server_s(struct ev_loop* loop) :
		server(loop),
		autoflush(true)
	{}
};

struct x0_request_s
{
	x0_server_t* server;
	HttpRequest* request;

	x0_request_body_fn body_cb;
	void* body_userdata;

	x0_request_abort_fn abort_cb;
	void* abort_userdata;

	x0_request_s(HttpRequest* r, x0_server_t* s) :
		server(s),
		request(r),
		body_cb(nullptr),
		body_userdata(nullptr),
		abort_cb(nullptr),
		abort_userdata(nullptr)
	{
		request->connection.setAutoFlush(server->autoflush);
	}

	void bodyCallback(const BufferRef& ref) {
		if (body_cb) {
			body_cb(this, ref.data(), ref.size(), body_userdata);
		}
	}

	static void abortCallback(void* p) {
		x0_request_s* self = (x0_request_s*) p;
		if (self->abort_cb) {
			self->abort_cb(self->abort_userdata);
		}
	}
};

x0_server_t* x0_server_create(struct ev_loop* loop)
{
	DebugLogger::get().configure("XZERO_DEBUG");

	auto s = new x0_server_t(loop);

	s->server.requestHandler = [](HttpRequest* r) -> bool {
		BufferSource body("Hello, I am lazy to serve anything; I wasn't configured properly\n");

		r->status = HttpStatus::InternalServerError;

		char buf[40];
		snprintf(buf, sizeof(buf), "%zu", body.size());
		r->responseHeaders.push_back("Content-Length", buf);

		r->responseHeaders.push_back("Content-Type", "plain/text");
		r->write<BufferSource>(body);

		r->finish();

		return true;
	};

	return s;
}

int x0_listener_add(x0_server_t* server, const char* bind, int port, int backlog)
{
	auto listener = server->server.setupListener(bind, port, 128);
	if (listener == nullptr)
		return -1;

{'fraction_non_alphanumeric': 0.14816662509353953, 'fraction_numerical': 0.018707907208780246, 'mean_word_length': 4.6879432624113475, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7503895', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1566, 'n_words': 321}