/* Copyright 2020, Michele Santullo
 * This file is part of orotool.
 *
 * Orotool is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Orotool 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Orotool.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "private/http_header_parse.hpp"
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;

namespace boost::spirit::x3::traits {

template <typename Char, typename Trait>
struct is_range<std::basic_string_view<Char, Trait>> : boost::mpl::true_ {};

} //namespace boost::spirit::x3::traits

BOOST_FUSION_ADAPT_STRUCT(nap::ParsedHeader,
	version, code, message, fields
);

namespace nap {
namespace {
namespace parser {
//HTTP/1.1 200 OK
//X-RateLimit-Remaining: 5
//Retry-After: 86400
//Server: soapui
//X-RateLimit-Reset: 1592918516
//X-RateLimit-Limit: 6
//Date: Mon, 22 Jun 2020 13:21:55 GMT
//Set-Cookie: __cfduid=dac1342771e458af0ce6c7b462db1e18d1592832115; expires=Wed, 22-Jul-20 13:21:55 GMT; path=/; domain=.originsro.org; HttpOnly; SameSite=Lax
//Content-Type: application/json
//Content-Encoding: gzip
//Content-Length: 116629

using x3::lit;
using x3::digit;
using x3::no_skip;
using x3::string;
using x3::uint_;
using x3::lexeme;
using x3::char_;
using x3::eol;

template <typename Subject>
struct raw_directive : x3::raw_directive<Subject> {
	using x3::raw_directive<Subject>::raw_directive;

	template <typename Iterator, typename Context, typename RContext, typename Attribute>
	bool parse(Iterator& first, Iterator const& last, Context const& context, RContext& rcontext, Attribute& attr) const {
		x3::skip_over(first, last, context);
		Iterator saved = first;
		if (this->subject.parse(first, last, context, rcontext, x3::unused)) {
			attr = { saved, typename Attribute::size_type(first - saved) };
			return true;
		}
		return false;
	}
};

struct raw_gen {
	template <typename Subject>
	raw_directive<typename x3::extension::as_parser<Subject>::value_type> operator[](Subject subject) const {
		return { x3::as_parser(std::move(subject)) };
	}
};

auto const raw = raw_gen{};

x3::rule<class HeaderEntry, std::pair<std::string_view, std::string_view>> header_entry = "header_entry";
auto const header_entry_def =
	raw[lexeme[+(char_ - ':')]] >> ':' >> raw[lexeme[+(char_ - eol)]]
;

x3::rule<class HttpHeaderRule, ParsedHeader> http_header = "http_header";
auto const http_header_def =
	no_skip[lit("HTTP/") >> raw[+digit >> -(string(".") >> +digit)]] > uint_ >
		-raw[lexeme[+(char_ - eol)]] > eol >>
	(header_entry % eol)
;

BOOST_SPIRIT_DEFINE(http_header, header_entry);
} //namespace parser
} //unnamed namespace

ParsedHeader header_parse (std::string_view text) {
	ParsedHeader retval;
	auto beg = text.begin();
	const bool result = x3::phrase_parse(
		beg,
		text.end(),
		parser::http_header,
		x3::space - x3::eol,
		retval
	);
	return retval;
}

} //namespace nap
