%%{
#include "HTTP.h"

BroString* encode_URI(BroString* URI)
	{
	byte_vec encoded_URI = new unsigned char[URI->Len() * 3 + 1];
	byte_vec end_of_URI = URI->Bytes() + URI->Len();

	byte_vec q = encoded_URI;
	for ( byte_vec p = URI->Bytes(); p < end_of_URI; ++p )
		{
		if ( is_reserved_URI_char(*p) ||
		     is_unreserved_URI_char(*p) || *p == '%' )
			*q++ = *p;
		else
			escape_URI_char(*p, q);
		}
	*q = '\0';
	return new BroString(1, encoded_URI, q - encoded_URI);
	}
%%}

rewriter http_request%(method: string, URI: string, version: string%)
	%{
	const int is_orig = 1;

	@WRITE@(is_orig, method);
	@WRITE@(is_orig, " ");

	@WRITE@(is_orig, URI);

	@WRITE@(is_orig, " HTTP/");
	@WRITE@(is_orig, version);
	@WRITE@(is_orig, "\r\n");
	%}

rewriter http_reply%(version: string, code: count, reason: string%)
	%{
	const int is_orig = 0;

	@WRITE@(is_orig, "HTTP/");
	@WRITE@(is_orig, version);
	@WRITE@(is_orig, " ");
	@WRITE@(is_orig, fmt("%d", code));
	@WRITE@(is_orig, " ");
	@WRITE@(is_orig, reason);
	@WRITE@(is_orig, "\r\n");
	%}

rewriter http_header%(is_orig: bool, name: string, value: string%)
	%{
	@WRITE@(is_orig, name);
	@WRITE@(is_orig, ":");
	@WRITE@(is_orig, value);
	@WRITE@(is_orig, "\r\n");
	%}

rewriter http_data%(is_orig: bool, data: string%)
	%{
	@WRITE@(is_orig, data);
	%}
