2008-08-08 12:35:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Andrew Garrett.
|
|
|
|
* Copyright (c) 2008 River Tarnell <river@wikimedia.org>
|
|
|
|
* Derived from public domain code contributed by Victor Vasiliev.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely. This software is provided 'as-is', without any express or
|
|
|
|
* implied warranty.
|
|
|
|
*/
|
|
|
|
|
2008-08-10 03:52:17 +00:00
|
|
|
#include <string>
|
|
|
|
#include <istream>
|
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
2008-08-08 00:01:22 +00:00
|
|
|
#include "request.h"
|
2008-08-10 03:52:17 +00:00
|
|
|
#include "fray.h"
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
namespace afp {
|
|
|
|
|
2008-08-08 06:31:46 +00:00
|
|
|
/* Perhaps, these should be configurable */
|
|
|
|
static const int MAX_FILTER_LEN = 1024 * 10; /* 10 KB */
|
|
|
|
static const int MAX_VARNAME_LEN = 255;
|
|
|
|
static const int MAX_VALUE_LEN = 1024 * 256; /* 256 KB */
|
|
|
|
|
2008-08-08 00:01:22 +00:00
|
|
|
// Protocol:
|
|
|
|
// code NULL <key> NULL <value> NULL ... <value> NULL NULL
|
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
template<typename charT, typename Traits>
|
|
|
|
struct basic_nul_terminated_string_reader {
|
|
|
|
typedef std::istream_iterator<charT> iterator_t;
|
|
|
|
typedef std::basic_istream<charT, Traits> stream_t;
|
2008-08-10 03:52:17 +00:00
|
|
|
typedef basic_fray<charT, Traits> string_t;
|
2008-08-08 07:25:10 +00:00
|
|
|
|
|
|
|
basic_nul_terminated_string_reader(stream_t &stream)
|
|
|
|
: stream_(stream)
|
|
|
|
, it_(stream)
|
|
|
|
, first_(true)
|
|
|
|
{
|
|
|
|
}
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
boost::optional<string_t> read(std::size_t max_len = 0) {
|
2008-08-12 15:19:38 +00:00
|
|
|
std::vector<char, std::allocator<char> > ret;
|
2008-08-10 03:52:17 +00:00
|
|
|
ret.reserve(1024); /* probably enough for most inputs */
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
if (first_)
|
|
|
|
first_ = false;
|
|
|
|
else
|
|
|
|
++it_;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
for (; it_ != end_; ++it_) {
|
|
|
|
if (*it_ == stream_.widen('\0')) {
|
2008-08-10 03:52:17 +00:00
|
|
|
return basic_fray<charT>(&ret[0], ret.size());
|
2008-08-08 07:25:10 +00:00
|
|
|
}
|
2008-08-08 06:31:46 +00:00
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
if (max_len && (ret.size() > max_len))
|
|
|
|
return boost::optional<string_t>();
|
|
|
|
|
2008-08-10 03:52:17 +00:00
|
|
|
ret.push_back(*it_);
|
2008-08-08 07:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return boost::optional<string_t>();
|
2008-08-08 00:01:22 +00:00
|
|
|
}
|
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
private:
|
|
|
|
stream_t &stream_;
|
|
|
|
iterator_t it_, end_;
|
|
|
|
bool first_;
|
|
|
|
};
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
typedef basic_nul_terminated_string_reader<char, std::char_traits<char> >
|
|
|
|
nul_terminated_string_reader;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
bool
|
|
|
|
request::load(std::istream &inp) {
|
2008-08-10 03:52:17 +00:00
|
|
|
f.clear_variables();
|
|
|
|
|
2008-08-08 07:25:10 +00:00
|
|
|
inp.unsetf(std::ios_base::skipws);
|
|
|
|
|
|
|
|
nul_terminated_string_reader reader(inp);
|
2008-08-10 03:52:17 +00:00
|
|
|
boost::optional<fray> str;
|
2008-08-08 07:25:10 +00:00
|
|
|
|
|
|
|
if (!(str = reader.read(MAX_FILTER_LEN)))
|
|
|
|
return false;
|
2008-08-10 03:52:17 +00:00
|
|
|
filter = make_u32fray(*str);
|
2008-08-08 07:25:10 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2008-08-10 03:52:17 +00:00
|
|
|
fray key, value;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
|
|
|
/* read the key */
|
2008-08-08 07:25:10 +00:00
|
|
|
if (!(str = reader.read(MAX_VARNAME_LEN)))
|
2008-08-08 00:01:22 +00:00
|
|
|
return false;
|
2008-08-08 07:25:10 +00:00
|
|
|
key = *str;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
|
|
|
if (key.empty())
|
|
|
|
/* empty string means end of input */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* read the value */
|
2008-08-08 07:25:10 +00:00
|
|
|
if (!(str = reader.read(MAX_VALUE_LEN)))
|
2008-08-08 00:01:22 +00:00
|
|
|
return false;
|
2008-08-08 07:25:10 +00:00
|
|
|
value = *str;
|
2008-08-08 00:01:22 +00:00
|
|
|
|
2008-08-10 03:52:17 +00:00
|
|
|
f.add_variable( make_u32fray(key),
|
2008-08-09 16:34:34 +00:00
|
|
|
u32datum::from_string_convert(
|
2008-08-10 03:52:17 +00:00
|
|
|
make_u32fray(value)));
|
2008-08-08 00:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
request::evaluate()
|
|
|
|
{
|
|
|
|
return f.evaluate(filter);
|
|
|
|
}
|
|
|
|
|
2008-08-08 03:23:34 +00:00
|
|
|
} // namespace afp
|