/[debian]/mimetic/branches/upstream/current/mimetic/contenttype.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/contenttype.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2647 - (show annotations)
Sat Sep 10 15:09:38 2011 UTC (10 years ago) by gregoa
File size: 4193 byte(s)
[svn-upgrade] new version mimetic (0.9.7)
1 /***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: contenttype.cxx,v 1.3 2008-10-07 11:06:25 tat Exp $
6 ***************************************************************************/
7 #include <cstdlib>
8 #include <ctime>
9 #include <sstream>
10 #include <iomanip>
11 #include <cassert>
12 #include <mimetic/contenttype.h>
13 #include <mimetic/tokenizer.h>
14 #include <mimetic/utils.h>
15
16 namespace mimetic
17 {
18 using namespace std;
19
20 const char ContentType::label[] = "Content-Type";
21
22 int ContentType::Boundary::ms_i = 0;
23 string ContentType::Boundary::ms_common_boundary = string();
24
25
26 ContentType::Boundary::Boundary()
27 {
28 if(ms_i++ == 0)
29 { // initialize static boundary string
30 const char tb[] = "0123456789"
31 "abcdefghijklmnopqrstuvwxyz"
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33 "-_."; // "=+,()/"
34 stringstream ss;
35 srand(time(0));
36 short tbSize = sizeof(tb)-1;
37 for(uint i=0; i < 48; ++i)
38 {
39 unsigned int r = rand();
40 ss << tb[r % tbSize];
41 }
42 ms_common_boundary = "----" + ss.str();
43 }
44 m_boundary = ms_common_boundary + "=_" + utils::int2hex(ms_i) + "_";
45 }
46
47 ContentType::Boundary::operator const string&() const
48 {
49 return m_boundary;
50 }
51
52 ContentType::ContentType()
53 {
54 // m_paramList.push_back(Param("charset", "us-ascii"));
55 }
56
57 ContentType::ContentType(const char* cstr)
58 {
59 set(cstr);
60 }
61
62 ContentType::ContentType(const string& value)
63 {
64 set(value);
65 }
66
67 ContentType::ContentType(const string& stype, const string& ssubtype)
68 {
69 set(stype, ssubtype);
70 }
71
72 void ContentType::set(const string& stype, const string& ssubtype)
73 {
74 type(stype);
75 subtype(ssubtype);
76 }
77
78 bool ContentType::isMultipart() const
79 {
80 return m_type == "multipart";
81 }
82
83 void ContentType::param(const string& name, const string& value)
84 {
85 ParamList::iterator bit = m_paramList.begin(), eit = m_paramList.end();
86 for(; bit != eit; ++bit)
87 {
88 if(bit->name() == name)
89 {
90 bit->value(value);
91 return;
92 }
93 }
94 m_paramList.push_back(Param(name, value));
95 }
96
97 const string& ContentType::param(const string& field) const
98 {
99 ParamList::const_iterator bit = m_paramList.begin(), eit = m_paramList.end();
100 for(; bit != eit; ++bit)
101 {
102 if(bit->name() == field)
103 return bit->value();
104 }
105 return nullstring;
106 }
107
108 void ContentType::type(const string& v)
109 {
110 m_type = v;
111 // if(isMultipart())
112 // m_paramList.push_back(ContentType::Param("boundary", Boundary()));
113 }
114
115 void ContentType::subtype(const string& v)
116 {
117 m_subtype = v;
118 }
119
120 const istring& ContentType::type() const
121 {
122 return m_type;
123 }
124
125 const istring& ContentType::subtype() const
126 {
127 return m_subtype;
128 }
129
130 const ContentType::ParamList& ContentType::paramList() const
131 {
132 return m_paramList;
133 }
134
135 ContentType::ParamList& ContentType::paramList()
136 {
137 return m_paramList;
138 }
139
140
141 void ContentType::set(const string& val)
142 {
143 StringTokenizer stok(&val, ";");
144 string ct;
145 if(!stok.next(ct))
146 return;
147
148 // parse type/subtype
149 string stype, ssubtype;
150 stok.setDelimList("/");
151 stok.setSource(&ct);
152 stok.next(stype);
153 stok.next(ssubtype);
154 set(stype, ssubtype);
155
156 // parse field params
157 string params(val, min(val.length(), ct.length() + 1));
158 if(!params.length())
159 return;
160 string paramValue;
161 stok.setDelimList(";");
162 stok.setSource(&params);
163 while(stok.next(paramValue))
164 {
165 ContentType::Param p(paramValue);
166 m_paramList.push_back(p);
167 }
168 }
169
170 string ContentType::str() const
171 {
172 string ostr = m_type + "/" + m_subtype;
173 ParamList::const_iterator bit = m_paramList.begin(),
174 eit = m_paramList.end();
175 for(; bit != eit; ++bit)
176 ostr += "; " + bit->name() + "=\"" + bit->value() + "\"";
177 return ostr;
178 }
179
180
181 FieldValue* ContentType::clone() const
182 {
183 return new ContentType(*this);
184 }
185
186 }
187

  ViewVC Help
Powered by ViewVC 1.1.26