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 
21 using Poco::Random;
22 using Poco::NumberFormatter;
23 
24 
25 namespace Poco {
26 namespace Net {
27 
28 
MultipartWriter(std::ostream & ostr)29 MultipartWriter::MultipartWriter(std::ostream& ostr):
30 	_ostr(ostr),
31 	_boundary(createBoundary()),
32 	_firstPart(true)
33 {
34 }
35 
36 
MultipartWriter(std::ostream & ostr,const std::string & boundary)37 MultipartWriter::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 
~MultipartWriter()47 MultipartWriter::~MultipartWriter()
48 {
49 }
50 
51 
nextPart(const MessageHeader & header)52 void 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 
close()64 void MultipartWriter::close()
65 {
66 	_ostr << "\r\n--" << _boundary << "--\r\n";
67 }
68 
69 
boundary() const70 const std::string& MultipartWriter::boundary() const
71 {
72 	return _boundary;
73 }
74 
75 
createBoundary()76 std::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