Compare commits

...

1 commit

Author SHA1 Message Date
binary1248 a66639ae0e Add support for getting and setting multiple HTTP fields. (#134) 2015-10-02 11:06:54 +02:00
2 changed files with 58 additions and 7 deletions

View file

@ -35,6 +35,7 @@
#include <SFML/System/Time.hpp> #include <SFML/System/Time.hpp>
#include <map> #include <map>
#include <string> #include <string>
#include <vector>
namespace sf namespace sf
@ -96,6 +97,21 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void setField(const std::string& field, const std::string& value); void setField(const std::string& field, const std::string& value);
////////////////////////////////////////////////////////////
/// \brief Set the values of a field
///
/// The field entries are created if they doesn't exist.
/// The name of the field is case-insensitive.
/// By default, a request doesn't contain any fields (but the
/// mandatory fields are added later by the HTTP client when
/// sending the request).
///
/// \param field Name of the field whose values to set
/// \param values Values of the field
///
////////////////////////////////////////////////////////////
void setFieldValues(const std::string& field, const std::vector<std::string>& values);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Set the request method /// \brief Set the request method
/// ///
@ -173,7 +189,7 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Types // Types
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
typedef std::map<std::string, std::string> FieldTable; typedef std::map<std::string, std::vector<std::string> > FieldTable;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
@ -256,6 +272,20 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const std::string& getField(const std::string& field) const; const std::string& getField(const std::string& field) const;
////////////////////////////////////////////////////////////
/// \brief Get the values of a field
///
/// If the field \a field is not found in the response header,
/// an empty container is returned. This function uses
/// case-insensitive comparisons.
///
/// \param field Name of the field whose values to get
///
/// \return Values of the field, or empty container if not found
///
////////////////////////////////////////////////////////////
const std::vector<std::string>& getFieldValues(const std::string& field) const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the response status code /// \brief Get the response status code
/// ///
@ -333,7 +363,7 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Types // Types
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
typedef std::map<std::string, std::string> FieldTable; typedef std::map<std::string, std::vector<std::string> > FieldTable;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data

View file

@ -61,7 +61,16 @@ Http::Request::Request(const std::string& uri, Method method, const std::string&
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Http::Request::setField(const std::string& field, const std::string& value) void Http::Request::setField(const std::string& field, const std::string& value)
{ {
m_fields[toLower(field)] = value; std::vector<std::string> values;
values.push_back(value);
setFieldValues(field, values);
}
////////////////////////////////////////////////////////////
void Http::Request::setFieldValues(const std::string& field, const std::vector<std::string>& values)
{
m_fields[toLower(field)] = values;
} }
@ -121,7 +130,10 @@ std::string Http::Request::prepare() const
// Write fields // Write fields
for (FieldTable::const_iterator i = m_fields.begin(); i != m_fields.end(); ++i) for (FieldTable::const_iterator i = m_fields.begin(); i != m_fields.end(); ++i)
{ {
out << i->first << ": " << i->second << "\r\n"; for (std::vector<std::string>::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
{
out << i->first << ": " << *j << "\r\n";
}
} }
// Use an extra \r\n to separate the header from the body // Use an extra \r\n to separate the header from the body
@ -155,9 +167,9 @@ m_minorVersion(0)
const std::string& Http::Response::getField(const std::string& field) const const std::string& Http::Response::getField(const std::string& field) const
{ {
FieldTable::const_iterator it = m_fields.find(toLower(field)); FieldTable::const_iterator it = m_fields.find(toLower(field));
if (it != m_fields.end()) if ((it != m_fields.end()) && !it->second.empty())
{ {
return it->second; return it->second.front();
} }
else else
{ {
@ -167,6 +179,15 @@ const std::string& Http::Response::getField(const std::string& field) const
} }
////////////////////////////////////////////////////////////
const std::vector<std::string>& Http::Response::getFieldValues(const std::string& field) const
{
static const std::vector<std::string> empty;
FieldTable::const_iterator it = m_fields.find(toLower(field));
return (it != m_fields.end()) ? it->second : empty;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Http::Response::Status Http::Response::getStatus() const Http::Response::Status Http::Response::getStatus() const
{ {
@ -290,7 +311,7 @@ void Http::Response::parseFields(std::istream &in)
value.erase(value.size() - 1); value.erase(value.size() - 1);
// Add the field // Add the field
m_fields[toLower(field)] = value; m_fields[toLower(field)].push_back(value);
} }
} }
} }