1//
2// MultipartWriter.cpp
3//
4// Library: Net
5// Package: Messages
6// Module: MultipartWriter
7//
8// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Net/MultipartWriter.h"
16#include "Poco/Net/MessageHeader.h"
17#include "Poco/Random.h"
18#include "Poco/NumberFormatter.h"
19
20
21using Poco::Random;
22using Poco::NumberFormatter;
23
24
25namespace Poco {
26namespace Net {
27
28
29MultipartWriter::MultipartWriter(std::ostream& ostr):
30 _ostr(ostr),
31 _boundary(createBoundary()),
32 _firstPart(true)
33{
34}
35
36
37MultipartWriter::MultipartWriter(std::ostream& ostr, const std::string& boundary):
38 _ostr(ostr),
39 _boundary(boundary),
40 _firstPart(true)
41{
42 if (_boundary.empty())
43 _boundary = createBoundary();
44}
45
46
47MultipartWriter::~MultipartWriter()
48{
49}
50
51
52void MultipartWriter::nextPart(const MessageHeader& header)
53{
54 if (_firstPart)
55 _firstPart = false;
56 else
57 _ostr << "\r\n";
58 _ostr << "--" << _boundary << "\r\n";
59 header.write(_ostr);
60 _ostr << "\r\n";
61}
62
63
64void MultipartWriter::close()
65{
66 _ostr << "\r\n--" << _boundary << "--\r\n";
67}
68
69
70const std::string& MultipartWriter::boundary() const
71{
72 return _boundary;
73}
74
75
76std::string MultipartWriter::createBoundary()
77{
78 std::string boundary("MIME_boundary_");
79 Random rnd;
80 rnd.seed();
81 NumberFormatter::appendHex(boundary, rnd.next(), 8);
82 NumberFormatter::appendHex(boundary, rnd.next(), 8);
83 return boundary;
84}
85
86
87} } // namespace Poco::Net
88