<commit_before>/*
 * WebRadio web-based Software Defined Radio
 *
 * Copyright (C) 2013 <NAME>
 *
 * This file is part of WebRadio (http://www.mike-stirling.com/webradio)
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <sstream>

#include "httpserver.h"
#include "debug.h"

#define DEFAULT_URL_SCHEME		"http://"

using namespace std;

int HttpRequestHandler::populate_args(void *self, enum MHD_ValueKind kind,
	const char *key, const char *value)
{
	switch (kind) {
	case MHD_HEADER_KIND:
		((HttpRequestHandler*)self)->_requestHeaders[key] = value;
		break;
	case MHD_COOKIE_KIND:
		((HttpRequestHandler*)self)->_requestCookies[key] = value;
		break;
	case MHD_GET_ARGUMENT_KIND:
		((HttpRequestHandler*)self)->_requestArgs[key] = value;
		break;
	default:
		return MHD_NO;
	}
	return MHD_YES;
}

void HttpRequestHandler::doError(unsigned short status)
{
	stringstream ss;
	string errorstr;

	switch (status) {
	case 400: errorstr = "Bad request"; break;
	case 401: errorstr = "Unauthorized"; break;
	case 403: errorstr = "Forbidden"; break;
	case 404: errorstr = "Not found"; break;
	case 405: errorstr = "Method not allowed"; break;
	case 406: errorstr = "Not acceptable"; break;
	default:
		errorstr = "An error occurred";
	}

	ss << "<!DOCTYPE html>\n" <<
		"<html>\n" <<
		"<head><title>Error " << status << "</title></head>\n" <<
		"<body>\n" <<
		"<h1>Error " << status << "</h1>\n" <<
		"<p>" << errorstr << "</p>\n" <<
		"</body>\n" <<
		"</html>\n";

	string response = ss.str();
	_data.insert(_data.end(), response.begin(), response.end());
	_contentType = "text/html";
}

/*******************************/

int HttpServer::handlerCallback(void *arg,
		struct MHD_Connection *conn,
		const char *path,
		const char *method,
		const char *version,
		const char *upload_data,
		size_t *upload_data_size,
		void **ptr)
{
	HttpServer *self = (HttpServer*)arg;
	HttpRequestHandler *handler = NULL;
	struct MHD_Response *response;
	int rc;
	unsigned short status = MHD_HTTP_OK;
	const char *host;
	vector<char> *upload_buffer;
	vector<string> url_wildcards;

	/* Create vector for upload buffer if required */
	if (*ptr == NULL) {
		*ptr = new vector<char>;
		return MHD_YES;
	}
	upload_buffer = (vector<char>*)*ptr;

	/* Get POST data if present */
	if (*upload_data_size > 0) {
		LOG_DEBUG("adding %lu bytes\n", *upload_data_size);
		/* Append to upload_buffer */
		upload_buffer->insert(upload_buffer->end(),
			upload_data, upload_data + *upload_data_size);
		*upload_data_size = 0;
		return MHD_YES;<commit_msg>Don't assume size_t will be 64-bits wide<commit_after>/*
 * WebRadio web-based Software Defined Radio
 *
 * Copyright (C) 2013 <NAME>
 *
 * This file is part of WebRadio (http://www.mike-stirling.com/webradio)
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <sstream>

#include "httpserver.h"
#include "debug.h"

#define DEFAULT_URL_SCHEME		"http://"

using namespace std;

int HttpRequestHandler::populate_args(void *self, enum MHD_ValueKind kind,
	const char *key, const char *value)
{
	switch (kind) {
	case MHD_HEADER_KIND:
		((HttpRequestHandler*)self)->_requestHeaders[key] = value;
		break;
	case MHD_COOKIE_KIND:
		((HttpRequestHandler*)self)->_requestCookies[key] = value;
		break;
	case MHD_GET_ARGUMENT_KIND:
		((HttpRequestHandler*)self)->_requestArgs[key] = value;
		break;
	default:
		return MHD_NO;
	}
	return MHD_YES;
}

void HttpRequestHandler::doError(unsigned short status)
{
	stringstream ss;
	string errorstr;

	switch (status) {
	case 400: errorstr = "Bad request"; break;
	case 401: errorstr = "Unauthorized"; break;
	case 403: errorstr = "Forbidden"; break;
	case 404: errorstr = "Not found"; break;
	case 405: errorstr = "Method not allowed"; break;
	case 406: errorstr = "Not acceptable"; break;
	default:
		errorstr = "An error occurred";
	}

	ss << "<!DOCTYPE html>\n" <<
		"<html>\n" <<
		"<head><title>Error " << status << "</title></head>\n" <<
		"<body>\n" <<
		"<h1>Error " << status << "</h1>\n" <<
		"<p>" << errorstr << "</p>\n" <<
		"</body>\n" <<
		"</html>\n";

	string response = ss.str();
	_data.insert(_data.end(), response.begin(), response.end());
	_contentType = "text/html";
}

/*******************************/

int HttpServer::handlerCallback(void *arg,
		struct MHD_Connection *conn,
		const char *path,
		const char *method,
		const char *version,
		const char *upload_data,
		size_t *upload_data_size,
		void **ptr)
{
	HttpServer *self = (HttpServer*)arg;
	HttpRequestHandler *handler = NULL;
	struct MHD_Response *response;
	int rc;
	unsigned short status = MHD_HTTP_OK;
	const char *host;
	vector<char> *upload_buffer;
	vector<string> url_wildcards;

	/* Create vector for upload buffer if required */
	if (*ptr == NULL) {
		*ptr = new vector<char>;
		return MHD_YES;
	}
	upload_buffer = (vector<char>*)*ptr;

	/* Get POST data if present */
	if (*upload_data_size > 0) {
		LOG_DEBUG("adding %u bytes\n", (unsigned int)*upload_data_size);
		/* Append to upload_buffer */
		upload_buffer->insert(upload_buffer->end(),
			upload_data, upload_data + *upload_data_size);
		*upload_data_size = 0;
		return MHD_YES;
{'fraction_non_alphanumeric': 0.14259708737864077, 'fraction_numerical': 0.00849514563106796, 'mean_word_length': 4.382040816326531, 'pattern_counts': {'":': 0, '<': 109, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '200737', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2175, 'n_words': 678}