|
| 1 | +// Copyright Allister Levi Sanchez 2008. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | +// |
| 6 | + |
| 7 | +#ifndef __NETWORK_PROTOCOL_MIME__ |
| 8 | +#define __NETWORK_PROTOCOL_MIME__ |
| 9 | + |
| 10 | +#include <string> |
| 11 | +#include <boost/network/message/tags.hpp> |
| 12 | +#include <boost/network/message/traits.hpp> |
| 13 | + |
| 14 | +namespace boost { namespace network { namespace mime { |
| 15 | + |
| 16 | + template <typename Tag> |
| 17 | + class mime { |
| 18 | + |
| 19 | + friend typename ostringstream<Tag> & |
| 20 | + operator << (typename ostringstream<Tag> & out, const typename mime<Tag> & m); |
| 21 | + |
| 22 | + public: |
| 23 | + |
| 24 | + typedef typename headers_container<Tag>::type parameter_map; |
| 25 | + typedef typename string<Tag>::type string_type; |
| 26 | + |
| 27 | + std::string type() { return _type; } |
| 28 | + string_type body() { return _body; } |
| 29 | + std::string encoding() { return _encoding; } |
| 30 | + std::string boundary() { return _boundary; } |
| 31 | + std::string disposition() { return _disposition; } |
| 32 | + const parameter_map & type_parameters() { return _type_params; } |
| 33 | + const parameter_map & disposition_parameters() { return _disposition_params; } |
| 34 | + |
| 35 | + void set_disposition(std::string d) { _disposition = d; } |
| 36 | + void set_disposition_parameter(std::string param, std::string value) { _disposition_params[param] = value; } |
| 37 | + void set_type(std::string t) { _type = t; } |
| 38 | + void set_type_parameter(std::string param, std::string value) { _type_params[param] = value; } |
| 39 | + void set_body(std::string b) { _body = b; } |
| 40 | + void set_encoding(encoding_type e) { _encoding = e; } |
| 41 | + void set_boundary(std::string b) { _boundary = b; } |
| 42 | + |
| 43 | + private: |
| 44 | + |
| 45 | + std::string _disposition; |
| 46 | + parameter_map _disposition_params; |
| 47 | + std::string _type; |
| 48 | + parameter_map _type_params |
| 49 | + string_type _body; |
| 50 | + std::string _boundary; |
| 51 | + std::string _encoding; |
| 52 | + }; |
| 53 | + |
| 54 | + |
| 55 | + template <class Tag> |
| 56 | + ostringstream<Tag> & operator << (ostringstream<Tag> & out, const mime & m) |
| 57 | + { |
| 58 | + std::string CRLF = "\r\n"; |
| 59 | + out << "--" << m._boundary << CRLF << CRLF; |
| 60 | + if (m._type.size() > 0) |
| 61 | + out << "Content-Type: " << m._type; |
| 62 | + else |
| 63 | + out << "Content-Type: text/plain"; |
| 64 | + |
| 65 | + typedef typename headers_container<Tag>::type map_t; |
| 66 | + typedef typename map_t::const_iterator iter_t; |
| 67 | + |
| 68 | + if (m._type_params.empty()) |
| 69 | + out << "; charset=us-ascii" << CRLF; |
| 70 | + else { |
| 71 | + for (iter_t i = m._type_params.begin(); i != m._type_params.end(); ++i) |
| 72 | + { |
| 73 | + out << "; " << i->first << "=" << i->second; |
| 74 | + } |
| 75 | + out << CRLF; |
| 76 | + } |
| 77 | + |
| 78 | + if (m._encoding.size() > 0) |
| 79 | + out << "Content-Type-Encoding: " << m._encoding << CRLF; |
| 80 | + else |
| 81 | + out << "Content-Type-Encoding: 7bit"; |
| 82 | + |
| 83 | + if (m._disposition.size() > 0) { |
| 84 | + out << "Content-Disposition: " << m._disposition; |
| 85 | + |
| 86 | + for (iter_t i = m._disposition_params.begin(); i != m._disposition_params.end(); ++i) |
| 87 | + { |
| 88 | + out << "; " << i->first << "=" << i->second; |
| 89 | + } |
| 90 | + out << CRLF; |
| 91 | + } |
| 92 | + out << CRLF; |
| 93 | + out << m._body; |
| 94 | + out << CRLF; |
| 95 | + out << "--" << m._boundary << CRLF << CRLF; |
| 96 | + } |
| 97 | +} // namespace mime |
| 98 | + |
| 99 | +} // namespace network |
| 100 | + |
| 101 | +} // namespace boost |
| 102 | + |
| 103 | +#endif // __NETWORK_PROTOCOL_MIME__ |
0 commit comments