#include "pihttptypes.h"


const char * PIHTTP::methodName(Method m) {
	switch (m) {
	case Method::Get: return "GET";
	case Method::Head: return "HEAD";
	case Method::Post: return "POST";
	case Method::Put: return "PUT";
	case Method::Delete: return "DELETE";
	case Method::Connect: return "CONNECT";
	case Method::Options: return "OPTIONS";
	case Method::Trace: return "TRACE";
	case Method::Patch: return "PATCH";
	default: break;
	};
	return "UNKNOWN";
}


// PIHTTP::MessageConst

bool PIHTTP::MessageConst::isCodeInformational() const {
	return (int)code() >= 100 && (int)code() < 200;
}


bool PIHTTP::MessageConst::isCodeSuccess() const {
	return (int)code() >= 200 && (int)code() < 300;
}


bool PIHTTP::MessageConst::isCodeRedirection() const {
	return (int)code() >= 300 && (int)code() < 400;
}


bool PIHTTP::MessageConst::isCodeClientError() const {
	return (int)code() >= 400 && (int)code() < 500;
}


bool PIHTTP::MessageConst::isCodeServerError() const {
	return (int)code() >= 500 && (int)code() < 600;
}


// PIHTTP::MessageMutable

PIHTTP::MessageMutable & PIHTTP::MessageMutable::addHeader(const PIString & header, const PIString & value) {
	m_headers[header] = value;
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::removeHeader(const PIString & header) {
	m_headers.remove(header);
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::addQueryArgument(const PIString & arg, const PIString & value) {
	m_query_arguments[arg] = value;
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::removeQueryArgument(const PIString & arg) {
	m_query_arguments.remove(arg);
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::addPathArgument(const PIString & arg, const PIString & value) {
	m_path_arguments[arg] = value;
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::removePathArgument(const PIString & arg) {
	m_path_arguments.remove(arg);
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::setMethod(Method m) {
	m_method = m;
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::setCode(Code c) {
	m_code = c;
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::setPath(PIString p) {
	m_path = std::move(p);
	return *this;
}


PIHTTP::MessageMutable & PIHTTP::MessageMutable::setBody(PIByteArray b) {
	m_body = std::move(b);
	return *this;
}


PIHTTP::MessageMutable PIHTTP::MessageMutable::fromCode(Code c) {
	PIHTTP::MessageMutable ret;
	ret.setCode(c);
	return ret;
}


PIHTTP::MessageMutable PIHTTP::MessageMutable::fromMethod(Method m) {
	PIHTTP::MessageMutable ret;
	ret.setMethod(m);
	return ret;
}
