LibreOffice Module tools (master) 1
inetmsg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/types.h>
21#include <tools/datetime.hxx>
22#include <tools/inetmsg.hxx>
23#include <comphelper/string.hxx>
24#include <rtl/character.hxx>
25#include <o3tl/safeint.hxx>
26#include <o3tl/sprintf.hxx>
27#include <o3tl/string_view.hxx>
28
29#include <map>
30
31void INetMIMEMessage::SetHeaderField_Impl (
32 const OString &rName,
33 const OUString &rValue,
34 sal_uInt32 &rnIndex)
35{
36 SetHeaderField_Impl (
37 INetMessageHeader (rName, rValue.toUtf8()), rnIndex);
38}
39
40/* ParseDateField and local helper functions.
41 *
42 * Parses a String in (implied) GMT format into class Date and tools::Time objects.
43 * Four formats are accepted:
44 *
45 * [Wkd,] 1*2DIGIT Mon 2*4DIGIT 00:00:00 [GMT] (rfc1123)
46 * [Wkd,] 00 Mon 0000 00:00:00 [GMT]) (rfc822, rfc1123)
47 * Weekday, 00-Mon-00 00:00:00 [GMT] (rfc850, rfc1036)
48 * Wkd Mon 00 00:00:00 0000 [GMT] (ctime)
49 * 1*DIGIT (delta seconds)
50 */
51
52static const char *months[12] =
53{
54 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
55 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
56};
57
58static sal_uInt16 ParseNumber(std::string_view rStr, size_t& nIndex)
59{
60 size_t n = nIndex;
61 while ((n < rStr.size())
62 && rtl::isAsciiDigit(static_cast<unsigned char>(rStr[n])))
63 n++;
64
65 std::string_view aNum(rStr.substr(nIndex, (n - nIndex)));
66 nIndex = n;
67
68 return static_cast<sal_uInt16>(o3tl::toInt32(aNum));
69}
70
71static sal_uInt16 ParseMonth(std::string_view rStr, size_t& nIndex)
72{
73 size_t n = nIndex;
74 while ((n < rStr.size())
75 && rtl::isAsciiAlpha(static_cast<unsigned char>(rStr[n])))
76 n++;
77
78 std::string_view aMonth(rStr.substr(nIndex, 3));
79 nIndex = n;
80
81 sal_uInt16 i;
82 for (i = 0; i < 12; i++)
83 if (o3tl::equalsIgnoreAsciiCase(aMonth, months[i])) break;
84 return (i + 1);
85}
86
87bool INetMIMEMessage::ParseDateField (
88 std::u16string_view rDateFieldW, DateTime& rDateTime)
89{
90 OString aDateField(OUStringToOString(rDateFieldW,
91 RTL_TEXTENCODING_ASCII_US));
92
93 if (aDateField.isEmpty()) return false;
94
95 if (aDateField.indexOf(':') != -1)
96 {
97 // Some DateTime format.
98 size_t nIndex = 0;
99
100 // Skip over <Wkd> or <Weekday>, leading and trailing space.
101 while ((nIndex < o3tl::make_unsigned(aDateField.getLength())) &&
102 (aDateField[nIndex] == ' '))
103 nIndex++;
104
105 while (
106 (nIndex < o3tl::make_unsigned(aDateField.getLength())) &&
107 (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField[nIndex])) ||
108 (aDateField[nIndex] == ',') ))
109 nIndex++;
110
111 while ((nIndex < o3tl::make_unsigned(aDateField.getLength())) &&
112 (aDateField[nIndex] == ' '))
113 nIndex++;
114
115 if (rtl::isAsciiAlpha (static_cast<unsigned char>(aDateField[nIndex])))
116 {
117 // Format: ctime().
118 if ((aDateField.getLength() - nIndex) < 20) return false;
119
120 rDateTime.SetMonth (ParseMonth (aDateField, nIndex)); nIndex++;
121 rDateTime.SetDay (ParseNumber (aDateField, nIndex)); nIndex++;
122
123 rDateTime.SetHour (ParseNumber (aDateField, nIndex)); nIndex++;
124 rDateTime.SetMin (ParseNumber (aDateField, nIndex)); nIndex++;
125 rDateTime.SetSec (ParseNumber (aDateField, nIndex)); nIndex++;
126 rDateTime.SetNanoSec (0);
127
128 sal_uInt16 nYear = ParseNumber (aDateField, nIndex);
129 if (nYear < 100) nYear += 1900;
130 rDateTime.SetYear (nYear);
131 }
132 else
133 {
134 // Format: RFC1036 or RFC1123.
135 if ((aDateField.getLength() - nIndex) < 17) return false;
136
137 rDateTime.SetDay (ParseNumber (aDateField, nIndex)); nIndex++;
138 rDateTime.SetMonth (ParseMonth (aDateField, nIndex)); nIndex++;
139
140 sal_uInt16 nYear = ParseNumber (aDateField, nIndex); nIndex++;
141 if (nYear < 100) nYear += 1900;
142 rDateTime.SetYear (nYear);
143
144 rDateTime.SetHour (ParseNumber (aDateField, nIndex)); nIndex++;
145 rDateTime.SetMin (ParseNumber (aDateField, nIndex)); nIndex++;
146 rDateTime.SetSec (ParseNumber (aDateField, nIndex)); nIndex++;
147 rDateTime.SetNanoSec (0);
148
149 const char cPossiblePlusMinus = nIndex < o3tl::make_unsigned(aDateField.getLength()) ? aDateField[nIndex] : 0;
150 if (cPossiblePlusMinus == '+' || cPossiblePlusMinus == '-')
151 {
152 // Offset from GMT: "(+|-)HHMM".
153 bool bEast = (aDateField[nIndex++] == '+');
154 sal_uInt16 nOffset = ParseNumber (aDateField, nIndex);
155 if (nOffset > 0)
156 {
158 aDiff.SetHour (nOffset / 100);
159 aDiff.SetMin (nOffset % 100);
160 aDiff.SetSec (0);
161 aDiff.SetNanoSec (0);
162
163 if (bEast)
164 rDateTime -= aDiff;
165 else
166 rDateTime += aDiff;
167 }
168 }
169 }
170 }
171 else if (comphelper::string::isdigitAsciiString(aDateField))
172 {
173 // Format: delta seconds.
174 tools::Time aDelta (0);
175 aDelta.SetTime (aDateField.toInt32() * 100);
176
178 aNow += aDelta;
179 aNow.ConvertToUTC();
180
181 rDateTime.SetDate (aNow.GetDate());
182 rDateTime.SetTime (aNow.GetTime());
183 }
184 else
185 {
186 // Junk.
187 return false;
188 }
189
190 return (rDateTime.IsValidAndGregorian() &&
191 !((rDateTime.GetSec() > 59) ||
192 (rDateTime.GetMin() > 59) ||
193 (rDateTime.GetHour() > 23) ));
194}
195
196const std::map<InetMessageMime, const char*> ImplINetMIMEMessageHeaderData =
197{
198 { InetMessageMime::VERSION, "MIME-Version"},
199 { InetMessageMime::CONTENT_DISPOSITION, "Content-Disposition"},
200 { InetMessageMime::CONTENT_TYPE, "Content-Type"},
201 { InetMessageMime::CONTENT_TRANSFER_ENCODING, "Content-Transfer-Encoding"}
202};
203
204INetMIMEMessage::INetMIMEMessage()
205 : pParent(nullptr)
206{
207 for (sal_uInt16 i = 0; i < static_cast<int>(InetMessageMime::NUMHDR); i++)
208 m_nMIMEIndex[static_cast<InetMessageMime>(i)] = SAL_MAX_UINT32;
209}
210
211INetMIMEMessage::~INetMIMEMessage()
212{
213}
214
215void INetMIMEMessage::SetMIMEVersion (const OUString& rVersion)
216{
217 SetHeaderField_Impl (
219 m_nMIMEIndex[InetMessageMime::VERSION]);
220}
221
222void INetMIMEMessage::SetContentDisposition (const OUString& rDisposition)
223{
224 SetHeaderField_Impl (
227}
228
229void INetMIMEMessage::SetContentType (const OUString& rType)
230{
231 SetHeaderField_Impl (
233 m_nMIMEIndex[InetMessageMime::CONTENT_TYPE]);
234}
235
236void INetMIMEMessage::SetContentTransferEncoding (
237 const OUString& rEncoding)
238{
239 SetHeaderField_Impl (
242}
243
244OUString INetMIMEMessage::GetDefaultContentType()
245{
246 if (pParent != nullptr)
247 {
248 OUString aParentCT (pParent->GetContentType());
249 if (aParentCT.isEmpty())
250 aParentCT = pParent->GetDefaultContentType();
251
252 if (aParentCT.equalsIgnoreAsciiCase("multipart/digest"))
253 return "message/rfc822";
254 }
255 return "text/plain; charset=us-ascii";
256}
257
258void INetMIMEMessage::EnableAttachMultipartFormDataChild()
259{
260 // Check context.
261 if (IsContainer())
262 return;
263
264 // Generate a unique boundary from current time.
265 char sTail[16 + 1];
267 sal_uInt64 nThis = reinterpret_cast< sal_uIntPtr >( this ); // we can be on a 64bit architecture
268 nThis = ( ( nThis >> 32 ) ^ nThis ) & SAL_MAX_UINT32;
269 o3tl::sprintf (sTail, "%08X%08X",
270 static_cast< unsigned int >(aCurTime.GetTime()),
271 static_cast< unsigned int >(nThis));
272 m_aBoundary = "------------_4D48";
273 m_aBoundary += sTail;
274
275 // Set header fields.
276 SetMIMEVersion("1.0");
277 SetContentType(
278 "multipart/form-data; boundary=" + OUString::fromUtf8(m_aBoundary));
279 SetContentTransferEncoding("7bit");
280}
281
282void INetMIMEMessage::AttachChild(std::unique_ptr<INetMIMEMessage> pChildMsg)
283{
284 assert(IsContainer());
285 if (IsContainer())
286 {
287 pChildMsg->pParent = this;
288 aChildren.push_back( std::move(pChildMsg) );
289 }
290}
291
292/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetDate(sal_Int32 nNewDate)
Definition: tdate.cxx:53
bool IsValidAndGregorian() const
If the represented date is valid (1<=month<=12, 1<=day<=(28,29,30,31) depending on month/year) AND is...
Definition: tdate.cxx:311
void SetMonth(sal_uInt16 nNewMonth)
nNewMonth must be <100
Definition: tdate.cxx:120
void SetYear(sal_Int16 nNewYear)
nNewYear must be != 0
Definition: tdate.cxx:125
void SetDay(sal_uInt16 nNewDay)
nNewDay must be <100
Definition: tdate.cxx:115
void SetMin(sal_uInt16 nNewMin)
Definition: ttime.cxx:161
sal_uInt16 GetSec() const
Definition: time.hxx:91
sal_uInt16 GetMin() const
Definition: time.hxx:88
void SetNanoSec(sal_uInt32 nNewNanoSec)
Definition: ttime.cxx:195
sal_uInt16 GetHour() const
Definition: time.hxx:85
@ SYSTEM
Definition: time.hxx:45
void SetTime(sal_Int64 nNewTime)
Definition: time.hxx:77
void SetSec(sal_uInt16 nNewSec)
Definition: ttime.cxx:178
void SetHour(sal_uInt16 nNewHour)
Definition: ttime.cxx:147
static sal_uInt16 ParseNumber(std::string_view rStr, size_t &nIndex)
Definition: inetmsg.cxx:58
const std::map< InetMessageMime, const char * > ImplINetMIMEMessageHeaderData
Definition: inetmsg.cxx:196
static sal_uInt16 ParseMonth(std::string_view rStr, size_t &nIndex)
Definition: inetmsg.cxx:71
static const char * months[12]
Definition: inetmsg.cxx:52
InetMessageMime
Definition: inetmsg.hxx:67
sal_Int32 nIndex
sal_Int64 n
bool isdigitAsciiString(std::string_view rString)
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
int sprintf(char(&s)[N], char const *format, T &&... arguments)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
#define SAL_MAX_UINT32