Line data Source code
1 : // Snap Servers -- HTTP string handling (splitting, etc.)
2 : // Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : #include <QMap>
20 : #include <QString>
21 : #include <QVector>
22 :
23 : namespace snap
24 : {
25 : namespace http_strings
26 : {
27 :
28 :
29 :
30 : // for HTTP_ACCEPT_ENCODING, HTTP_ACCEPT_LANGUAGE, HTTP_ACCEPT
31 0 : class WeightedHttpString
32 : {
33 : public:
34 0 : class part_t
35 : {
36 : public:
37 : typedef float level_t;
38 : typedef QVector<part_t> vector_t; // do NOT use a map, we want to keep them in order!
39 :
40 : // an authoritative document at the IANA clearly says that
41 : // the default level (quality value) is 1.0f.
42 : //
43 0 : static level_t constexpr DEFAULT_LEVEL() { return 1.0f; }
44 :
45 0 : static level_t constexpr UNDEFINED_LEVEL() { return -1.0f; }
46 :
47 : part_t();
48 : part_t(QString const & name);
49 :
50 : QString const & get_name() const;
51 : QString const & get_value() const; // in case the first name has a "name=value" part
52 : void set_value(QString const & value);
53 : level_t get_level() const;
54 : void set_level(level_t const level);
55 : QString get_parameter(QString const & name) const;
56 : void add_parameter(QString const & name, QString const & value);
57 : QString to_string() const;
58 :
59 : bool operator < (part_t const & rhs) const;
60 :
61 : private:
62 : typedef QMap<QString, QString> parameters_t;
63 :
64 : QString f_name = QString();
65 : QString f_value = QString();
66 0 : level_t f_level = DEFAULT_LEVEL(); // i.e. q=0.8
67 : // TODO add support for any other parameter
68 : parameters_t f_param = parameters_t();
69 : };
70 :
71 : WeightedHttpString(QString const & str = QString());
72 :
73 : bool parse(QString const & str, bool reset = false);
74 : QString const & get_string() const { return f_str; }
75 : part_t::level_t get_level(QString const & name);
76 : void sort_by_level();
77 0 : part_t::vector_t & get_parts() { return f_parts; }
78 : part_t::vector_t const &get_parts() const { return f_parts; }
79 : QString to_string() const;
80 : QString const & error_messages() const { return f_error_messages; }
81 :
82 : private:
83 : QString f_str = QString();
84 : part_t::vector_t f_parts = part_t::vector_t(); // do NOT use a map, we want to keep them in order
85 : QString f_error_messages = QString();
86 : };
87 :
88 :
89 :
90 : } // namespace http_strings
91 : } // namespace snap
92 : // vim: ts=4 sw=4 et
|