blob: c16ba1942fa8eeb8fa9787c456513b79dd13dcdc [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 2001-2005 Open Source Telecom Corporation.
2// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17//
18// As a special exception, you may use this file as part of a free software
19// library without restriction. Specifically, if other files instantiate
20// templates or use macros or inline functions from this file, or you compile
21// this file and link it with other files to produce an executable, this
22// file does not by itself cause the resulting executable to be covered by
23// the GNU General Public License. This exception does not however
24// invalidate any other reasons why the executable file might be covered by
25// the GNU General Public License.
26//
27// This exception applies only to the code released under the name GNU
28// Common C++. If you copy code from other releases into a copy of GNU
29// Common C++, as the General Public License permits, the exception does
30// not apply to the code that you add in this way. To avoid misleading
31// anyone as to the status of such modified files, you must delete
32// this exception notice from them.
33//
34// If you write modifications of your own for GNU Common C++, it is your choice
35// whether to permit this exception to apply to your modifications.
36// If you do not wish that, delete this exception notice.
37//
38
39#include <ucommon-config.h>
40#include <commoncpp/config.h>
41#include <commoncpp/export.h>
42#include <commoncpp/thread.h>
43#include <commoncpp/socket.h>
44#include <commoncpp/exception.h>
45#include <commoncpp/mime.h>
46
47NAMESPACE_COMMONCPP
48using namespace std;
49
50MIMEMultipart::MIMEMultipart(const char *mt)
51{
52 const char *cp = strchr(mt, '/');
53 if(cp)
54 mt = ++cp;
55
56 first = last = NULL;
57 header[1] = NULL;
58 header[0] = mtype;
59 setString(boundry, sizeof(boundry), "xyzzy");
60 snprintf(mtype, sizeof(mtype), "Content-Type: multipart/%s, boundry=%s", mt, boundry);
61}
62
63MIMEMultipart::~MIMEMultipart()
64{}
65
66void MIMEMultipart::head(std::ostream *out)
67{
68 char **list = header;
69
70 while(**list)
71 *out << *(list++) << "\r\n";
72
73 out->flush();
74}
75
76void MIMEMultipart::body(std::ostream *out)
77{
78 MIMEItemPart *item = first;
79
80 while(item) {
81 *out << "--" << boundry << "\r\n";
82 item->head(out);
83 *out << "\r\n";
84 item->body(out);
85 item = item->next;
86 }
87 *out << "--" << boundry << "--\r\n";
88 out->flush();
89}
90
91MIMEItemPart::MIMEItemPart(MIMEMultipart *m, const char *ct)
92{
93 if(m->last) {
94 m->last->next = this;
95 m->last = this;
96 }
97 else
98 m->first = m->last = this;
99 next = NULL;
100 ctype = ct;
101}
102
103MIMEItemPart::~MIMEItemPart()
104{}
105
106MIMEMultipartForm::MIMEMultipartForm() :
107MIMEMultipart("form-data")
108{}
109
110MIMEMultipartForm::~MIMEMultipartForm()
111{}
112
113void MIMEItemPart::head(std::ostream *out)
114{
115 *out << "Content-Type: " << ctype << "\r" << endl;
116}
117
118MIMEFormData::MIMEFormData(MIMEMultipartForm *m, const char *n, const char *v) :
119MIMEItemPart(m, "")
120{
121 name = n;
122 content = v;
123}
124
125MIMEFormData::~MIMEFormData()
126{}
127
128void MIMEFormData::head(std::ostream *out)
129{
130 *out << "Content-Disposition: form-data; name=\"" << name << "\"\r\n";
131}
132
133void MIMEFormData::body(std::ostream *out)
134{
135 *out << content << "\r\n";
136}
137
138END_NAMESPACE
139
140/** EMACS **
141 * Local variables:
142 * mode: c++
143 * c-basic-offset: 4
144 * End:
145 */