| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GUARD_MSG_ENTITY_HEADER_HH |
|---|
| 21 | #define GUARD_MSG_ENTITY_HEADER_HH |
|---|
| 22 | |
|---|
| 23 | #include "content_type.hh" |
|---|
| 24 | |
|---|
| 25 | #include <boost/algorithm/string/case_conv.hpp> |
|---|
| 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
|---|
| 27 | #include <boost/shared_ptr.hpp> |
|---|
| 28 | #include <list> |
|---|
| 29 | #include <map> |
|---|
| 30 | #include <string> |
|---|
| 31 | |
|---|
| 32 | namespace msg |
|---|
| 33 | { |
|---|
| 34 | class entity_header |
|---|
| 35 | { |
|---|
| 36 | public: |
|---|
| 37 | typedef boost::shared_ptr<entity_header> ptr; |
|---|
| 38 | typedef boost::shared_ptr<const entity_header> const_ptr; |
|---|
| 39 | typedef std::list<std::string>::const_iterator field_iterator; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | static ptr mk(std::string &str, bool validate, |
|---|
| 43 | std::string default_ct) { |
|---|
| 44 | ptr rv(new entity_header(str, validate, default_ct)); |
|---|
| 45 | return rv; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | explicit entity_header(std::string default_ct) |
|---|
| 49 | : the_date(boost::posix_time::not_a_date_time) |
|---|
| 50 | , default_ct(default_ct) |
|---|
| 51 | {} |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | explicit entity_header(std::string &msg, bool validate, |
|---|
| 55 | std::string default_ct); |
|---|
| 56 | |
|---|
| 57 | entity_header(const entity_header &o) |
|---|
| 58 | : field_content(o.field_content) |
|---|
| 59 | , the_date(o.the_date) |
|---|
| 60 | , default_ct(o.default_ct) |
|---|
| 61 | { reindex(); } |
|---|
| 62 | |
|---|
| 63 | entity_header &operator=(const entity_header &o) { |
|---|
| 64 | if (this != &o) |
|---|
| 65 | { |
|---|
| 66 | field_content = o.field_content; |
|---|
| 67 | the_date = o.the_date; |
|---|
| 68 | default_ct = o.default_ct; |
|---|
| 69 | reindex(); |
|---|
| 70 | } |
|---|
| 71 | return *this; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | boost::posix_time::ptime parsed_date() const; |
|---|
| 75 | |
|---|
| 76 | field_iterator fields_begin() const { |
|---|
| 77 | return field_content.begin(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | field_iterator fields_end() const { |
|---|
| 81 | return field_content.end(); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | std::string get_header() const; |
|---|
| 85 | |
|---|
| 86 | bool has_field(std::string s) const{ |
|---|
| 87 | boost::to_lower(s); |
|---|
| 88 | return fields.find(s) != fields.end(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | std::string get_field(std::string s, bool full) const; |
|---|
| 93 | |
|---|
| 94 | void add_field(std::string name, std::string body) { |
|---|
| 95 | field_content.push_front(name + ": " + body + "\r\n"); |
|---|
| 96 | boost::to_lower(name); |
|---|
| 97 | fields[name] = field_content.begin(); |
|---|
| 98 | if (name == "date") |
|---|
| 99 | the_date = boost::posix_time::not_a_date_time; |
|---|
| 100 | } |
|---|
| 101 | void replace_field(std::string name, std::string body); |
|---|
| 102 | void delete_field(std::string name); |
|---|
| 103 | |
|---|
| 104 | content_type get_content_type() const; |
|---|
| 105 | |
|---|
| 106 | private: |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | std::list<std::string> field_content; |
|---|
| 110 | std::map<std::string, std::list<std::string>::iterator> fields; |
|---|
| 111 | mutable boost::posix_time::ptime the_date; |
|---|
| 112 | std::string default_ct; |
|---|
| 113 | |
|---|
| 114 | void reindex(); |
|---|
| 115 | }; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | #endif |
|---|