Skip to content

Instantly share code, notes, and snippets.

@hochan222
Created March 31, 2021 11:05
Show Gist options
  • Save hochan222/4b7ca05b355c9ffe68ff4dbf511da607 to your computer and use it in GitHub Desktop.
Save hochan222/4b7ca05b355c9ffe68ff4dbf511da607 to your computer and use it in GitHub Desktop.
Accept-Language
std::string
Request::getAcceptLanguage()
{
if (this->m_content_type != "")
return (this->m_content_type);
std::map<std::string, std::string>::const_iterator it;
it = this->m_headers.find("Accept-Language");
// `Accept-Language Header` not exist
if (it == this->m_headers.end())
return (this->m_content_type = "ko");
// Quality not exist
if ((*it).second.find(";") == std::string::npos)
if ((*it).second.find("en") == std::string::npos)
return (this->m_content_type = "ko");
// Quality exist
std::vector<std::string> line;
std::string content_type = "";
float quality = 0;
line = ft::split((*it).second, ',');
for (std::vector<std::string>::const_iterator line_iter = line.begin() ; line_iter != line.end() ; ++line_iter)
{
if ((*line_iter).find(";") != std::string::npos)
{
std::vector<std::string> content_type_line;
content_type_line = ft::split(*line_iter, ';');
// Compare Quality
if (std::stof(content_type_line[1].substr(2)) > quality)
{
quality = std::stof(content_type_line[1].substr(2));
content_type = content_type_line[0];
}
}
}
return (this->m_content_type = content_type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment