<commit_before>#ifndef sw_x0_fastcgi_protocol_h
#define sw_x0_fastcgi_protocol_h

// this file holds the protocol bits

#include <stdint.h>    // uint16_t, ...
#include <string.h>    // memset
#include <arpa/inet.h> // htons/ntohs/ntohl/htonl

#include <string>
#include <vector>

#include <x0/Buffer.h>
#include <x0/BufferRef.h>

namespace FastCgi {

enum class Type
{
	BeginRequest    = 1,
	AbortRequest    = 2,
	EndRequest      = 3,
	Params          = 4,
	StdIn           = 5,
	StdOut          = 6,
	StdErr          = 7,
	Data            = 8,
	GetValues       = 9,
	GetValuesResult = 10,
	UnknownType     = 11
};

enum class Role
{
	Responder  = 1,
	Authorizer = 2,
	Filter     = 3
};

enum class ProtocolStatus
{
	RequestComplete     = 0,
	CannotMpxConnection = 1,
	Overloaded          = 2,
	UnknownRole         = 3
};

struct Record
{
private:
	uint8_t version_;
	uint8_t type_;
	uint16_t requestId_;
	uint16_t contentLength_;
	uint8_t paddingLength_;
	uint8_t reserved_;

public:
	Record(Type type, uint16_t requestId, uint16_t contentLength, uint8_t paddingLength) :
		version_(1),
		type_(static_cast<uint8_t>(type)),
		requestId_(htons(requestId)),
		contentLength_(htons(contentLength)),
		paddingLength_(paddingLength),
		reserved_(0)
	{}

	//static Record *create(Type type, uint16_t requestId, uint16_t contentLength);
	//void destroy();

	int version() const { return version_; }
	Type type() const { return static_cast<Type>(type_); }
	int requestId() const { return ntohs(requestId_); }
	int contentLength() const { return ntohs(contentLength_); }
	int paddingLength() const { return paddingLength_; }

	const char *content() const { return reinterpret_cast<const char *>(this) + sizeof(*this); }

	const char *data() const { return reinterpret_cast<const char *>(this); }
	uint16_t size() const { return sizeof(Record) + contentLength() + paddingLength(); }

	bool isManagement() const { return requestId() == 0; }
	bool isApplication() const { return requestId() != 0; }

	const char *type_str() const
	{
		static const char *map[] = {
			0, "BeginRequest", "AbortRequest", "EndRequest", "Params", "StdIn", "StdOut",
			"StdErr", "Data", "GetValues", "GetValuesResult", "UnknownType"
		};

		return type_ > 0 && type_ < 12 ? map[type_] : "invalid";
	}
};

struct BeginRequestRecord : public Record
{<commit_msg>[plugins] fastcgi: Record::size() shall return integer of 32 bit instead 16 to avoid overflow<commit_after>#ifndef sw_x0_fastcgi_protocol_h
#define sw_x0_fastcgi_protocol_h

// this file holds the protocol bits

#include <stdint.h>    // uint16_t, ...
#include <string.h>    // memset
#include <arpa/inet.h> // htons/ntohs/ntohl/htonl

#include <string>
#include <vector>

#include <x0/Buffer.h>
#include <x0/BufferRef.h>

namespace FastCgi {

enum class Type
{
	BeginRequest    = 1,
	AbortRequest    = 2,
	EndRequest      = 3,
	Params          = 4,
	StdIn           = 5,
	StdOut          = 6,
	StdErr          = 7,
	Data            = 8,
	GetValues       = 9,
	GetValuesResult = 10,
	UnknownType     = 11
};

enum class Role
{
	Responder  = 1,
	Authorizer = 2,
	Filter     = 3
};

enum class ProtocolStatus
{
	RequestComplete     = 0,
	CannotMpxConnection = 1,
	Overloaded          = 2,
	UnknownRole         = 3
};

struct Record
{
private:
	uint8_t version_;
	uint8_t type_;
	uint16_t requestId_;
	uint16_t contentLength_;
	uint8_t paddingLength_;
	uint8_t reserved_;

public:
	Record(Type type, uint16_t requestId, uint16_t contentLength, uint8_t paddingLength) :
		version_(1),
		type_(static_cast<uint8_t>(type)),
		requestId_(htons(requestId)),
		contentLength_(htons(contentLength)),
		paddingLength_(paddingLength),
		reserved_(0)
	{}

	//static Record *create(Type type, uint16_t requestId, uint16_t contentLength);
	//void destroy();

	int version() const { return version_; }
	Type type() const { return static_cast<Type>(type_); }
	int requestId() const { return ntohs(requestId_); }
	int contentLength() const { return ntohs(contentLength_); }
	int paddingLength() const { return paddingLength_; }

	const char *content() const { return reinterpret_cast<const char *>(this) + sizeof(*this); }

	const char *data() const { return reinterpret_cast<const char *>(this); }
	uint32_t size() const { return sizeof(Record) + contentLength() + paddingLength(); }

	bool isManagement() const { return requestId() == 0; }
	bool isApplication() const { return requestId() != 0; }

	const char *type_str() const
	{
		static const char *map[] = {
			0, "BeginRequest", "AbortRequest", "EndRequest", "Params", "StdIn", "StdOut",
			"StdErr", "Data", "GetValues", "GetValuesResult", "UnknownType"
		};

		return type_ > 0 && type_ < 12 ? map[type_] : "invalid";
	}
};

struct BeginRequestRecord : public Record
{
{'fraction_non_alphanumeric': 0.1268467707893626, 'fraction_numerical': 0.023638666103841282, 'mean_word_length': 3.806288032454361, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '1330462', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1595, 'n_words': 425}