<commit_before>#include "stdafx.h"
#include "luanode.h"
#include "luanode_http_parser.h"
#include "blogger.h"

using namespace LuaNode::Http;


/*static*/ struct http_parser_settings LuaNode::Http::Parser::s_settings;

void LuaNode::Http::RegisterFunctions(lua_State* L) {
	luaL_Reg methods[] = {
		{ 0, 0 }
	};
	luaL_register(L, "Http", methods);
	lua_pop(L, 1);

	Parser::s_settings.on_message_begin    = Parser::on_message_begin;
	Parser::s_settings.on_path             = Parser::on_path;
	Parser::s_settings.on_query_string     = Parser::on_query_string;
	Parser::s_settings.on_url              = Parser::on_url;
	Parser::s_settings.on_fragment         = Parser::on_fragment;
	Parser::s_settings.on_header_field     = Parser::on_header_field;
	Parser::s_settings.on_header_value     = Parser::on_header_value;
	Parser::s_settings.on_headers_complete = Parser::on_headers_complete;
	Parser::s_settings.on_body             = Parser::on_body;
	Parser::s_settings.on_message_complete = Parser::on_message_complete;
}

//////////////////////////////////////////////////////////////////////////
/// 
static inline const char* method_to_str(unsigned short m) {
	switch (m) {
		case HTTP_DELETE:     return "DELETE";
		case HTTP_GET:        return "GET";
		case HTTP_HEAD:       return "HEAD";
		case HTTP_POST:       return "POST";
		case HTTP_PUT:        return "PUT";
		case HTTP_CONNECT:    return "CONNECT";
		case HTTP_OPTIONS:    return "OPTIONS";
		case HTTP_TRACE:      return "TRACE";
		case HTTP_COPY:       return "COPY";
		case HTTP_LOCK:       return "LOCK";
		case HTTP_MKCOL:      return "MKCOL";
		case HTTP_MOVE:       return "MOVE";
		case HTTP_PROPFIND:   return "PROPFIND";
		case HTTP_PROPPATCH:  return "PROPPATCH";
		case HTTP_UNLOCK:     return "UNLOCK";
		case HTTP_REPORT:     return "REPORT";
		case HTTP_MKACTIVITY: return "MKACTIVITY";
		case HTTP_CHECKOUT:   return "CHECKOUT";
		case HTTP_MERGE:      return "MERGE";
		/*case HTTP_MSEARCH:    return "M-SEARCH";
		case HTTP_NOTIFY:     return "NOTIFY";
		case HTTP_SUBSCRIBE:  return "SUBSCRIBE";
		case HTTP_UNSUBSCRIBE:return "UNSUSCRIBE";*/

		default:              return "UNKNOWN";
	}
}


const char* Parser::className = "HTTPParser";
const Parser::RegType Parser::methods[] = {
	{"execute", &Parser::Execute},
	{"finish", &Parser::Finish},
	{"reinitialize", &Parser::Reinitialize},
	{0}
};

const Parser::RegType Parser::setters[] = {
	{0}
};

const Parser::RegType Parser::getters[] = {
	{0}
};

Parser::Parser(lua_State* L) : 
	m_L( LuaNode::GetLuaVM() )
{
	LogDebug("Constructing HTTP Parser (%p)", this);

	const char* options[] = {
		"request",
		"response",
		NULL
	};

	int chosen_option = luaL_checkoption(L, 1, NULL, options);
	switch(chosen_option) {
		case 0:	// request
			Init(HTTP_REQUEST);
		break;
		case 1:	// response
			Init(HTTP_RESPONSE);
		break;
		
		default:
			luaL_error(L, "Constructor argument must be 'request' or 'response'");
		break;
	}
}

Parser::~Parser(void)
{
	LogDebug("Destructing HTTP Parser (%p)", this);
}

void Parser::Init(enum http_parser_type type) {
	http_parser_init(&m_parser, type);
	m_parser.data = this;
}

//////////////////////////////////////////////////////////////////////////
/// // var bytesParsed = parser->execute(buffer, off, len);
int Parser::Execute(lua_State* L) {
	const char* buffer = luaL_checkstring(L, 2);
	size_t offset = luaL_checkinteger(L, 3);
	size_t len = luaL_checkinteger(L, 4);

	m_got_exception = false;

	size_t nparsed = http_parser_execute(&m_parser, &Parser::s_settings, buffer + offset, len);

	if(m_got_exception) {
		// TODO: signal error
		return 0;
	}

	if(!m_parser.upgrade && nparsed != len) {
		lua_pushnil(L);
		lua_pushfstring(L, "Parse error. %lu bytes parsed", (unsigned long)nparsed);
		return 2;
	}

	lua_pushinteger(L, nparsed);
	return 1;
}
<commit_msg>Changes %u to %d, because lua_pushfstring does not recognize them.<commit_after>#include "stdafx.h"
#include "luanode.h"
#include "luanode_http_parser.h"
#include "blogger.h"

using namespace LuaNode::Http;


/*static*/ struct http_parser_settings LuaNode::Http::Parser::s_settings;

void LuaNode::Http::RegisterFunctions(lua_State* L) {
	luaL_Reg methods[] = {
		{ 0, 0 }
	};
	luaL_register(L, "Http", methods);
	lua_pop(L, 1);

	Parser::s_settings.on_message_begin    = Parser::on_message_begin;
	Parser::s_settings.on_path             = Parser::on_path;
	Parser::s_settings.on_query_string     = Parser::on_query_string;
	Parser::s_settings.on_url              = Parser::on_url;
	Parser::s_settings.on_fragment         = Parser::on_fragment;
	Parser::s_settings.on_header_field     = Parser::on_header_field;
	Parser::s_settings.on_header_value     = Parser::on_header_value;
	Parser::s_settings.on_headers_complete = Parser::on_headers_complete;
	Parser::s_settings.on_body             = Parser::on_body;
	Parser::s_settings.on_message_complete = Parser::on_message_complete;
}

//////////////////////////////////////////////////////////////////////////
/// 
static inline const char* method_to_str(unsigned short m) {
	switch (m) {
		case HTTP_DELETE:     return "DELETE";
		case HTTP_GET:        return "GET";
		case HTTP_HEAD:       return "HEAD";
		case HTTP_POST:       return "POST";
		case HTTP_PUT:        return "PUT";
		case HTTP_CONNECT:    return "CONNECT";
		case HTTP_OPTIONS:    return "OPTIONS";
		case HTTP_TRACE:      return "TRACE";
		case HTTP_COPY:       return "COPY";
		case HTTP_LOCK:       return "LOCK";
		case HTTP_MKCOL:      return "MKCOL";
		case HTTP_MOVE:       return "MOVE";
		case HTTP_PROPFIND:   return "PROPFIND";
		case HTTP_PROPPATCH:  return "PROPPATCH";
		case HTTP_UNLOCK:     return "UNLOCK";
		case HTTP_REPORT:     return "REPORT";
		case HTTP_MKACTIVITY: return "MKACTIVITY";
		case HTTP_CHECKOUT:   return "CHECKOUT";
		case HTTP_MERGE:      return "MERGE";
		/*case HTTP_MSEARCH:    return "M-SEARCH";
		case HTTP_NOTIFY:     return "NOTIFY";
		case HTTP_SUBSCRIBE:  return "SUBSCRIBE";
		case HTTP_UNSUBSCRIBE:return "UNSUSCRIBE";*/

		default:              return "UNKNOWN";
	}
}


const char* Parser::className = "HTTPParser";
const Parser::RegType Parser::methods[] = {
	{"execute", &Parser::Execute},
	{"finish", &Parser::Finish},
	{"reinitialize", &Parser::Reinitialize},
	{0}
};

const Parser::RegType Parser::setters[] = {
	{0}
};

const Parser::RegType Parser::getters[] = {
	{0}
};

Parser::Parser(lua_State* L) : 
	m_L( LuaNode::GetLuaVM() )
{
	LogDebug("Constructing HTTP Parser (%p)", this);

	const char* options[] = {
		"request",
		"response",
		NULL
	};

	int chosen_option = luaL_checkoption(L, 1, NULL, options);
	switch(chosen_option) {
		case 0:	// request
			Init(HTTP_REQUEST);
		break;
		case 1:	// response
			Init(HTTP_RESPONSE);
		break;
		
		default:
			luaL_error(L, "Constructor argument must be 'request' or 'response'");
		break;
	}
}

Parser::~Parser(void)
{
	LogDebug("Destructing HTTP Parser (%p)", this);
}

void Parser::Init(enum http_parser_type type) {
	http_parser_init(&m_parser, type);
	m_parser.data = this;
}

//////////////////////////////////////////////////////////////////////////
/// // var bytesParsed = parser->execute(buffer, off, len);
int Parser::Execute(lua_State* L) {
	const char* buffer = luaL_checkstring(L, 2);
	size_t offset = luaL_checkinteger(L, 3);
	size_t len = luaL_checkinteger(L, 4);

	m_got_exception = false;

	size_t nparsed = http_parser_execute(&m_parser, &Parser::s_settings, buffer + offset, len);

	if(m_got_exception) {
		// TODO: signal error
		return 0;
	}

	if(!m_parser.upgrade && nparsed != len) {
		lua_pushnil(L);
		lua_pushfstring(L, "Parse error. %d bytes parsed", (int)nparsed);
		return 2;
	}

	lua_pushinteger(L, nparsed);
	return 1;
}

{'fraction_non_alphanumeric': 0.170080373347161, 'fraction_numerical': 0.00388903292714545, 'mean_word_length': 3.4802555168408826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3192324', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2711, 'n_words': 572}