blob: e8ca90ac44cdb3aaa4ebc8e5f1fb1f000625f336 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// 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 <cc++/config.h>
40#ifdef CCXX_WITHOUT_EXTRAS
41#include <cc++/export.h>
42#endif
43#include <cc++/file.h>
44#include <cc++/thread.h>
45#include <cc++/socket.h>
46#include <cc++/exception.h>
47#ifndef CCXX_WITHOUT_EXTRAS
48#include <cc++/export.h>
49#endif
50#include <cc++/mime.h>
51
52#ifdef CCXX_NAMESPACES
53namespace ost {
54using namespace std;
55#endif
56
57MIMEMultipart::MIMEMultipart(const char *mt)
58{
59 const char *cp = strchr(mt, '/');
60 if(cp)
61 mt = ++cp;
62
63 first = last = NULL;
64 header[1] = NULL;
65 header[0] = mtype;
66 setString(boundry, sizeof(boundry), "xyzzy");
67 snprintf(mtype, sizeof(mtype), "Content-Type: multipart/%s, boundry=%s", mt, boundry);
68}
69
70MIMEMultipart::~MIMEMultipart()
71{}
72
73void MIMEMultipart::head(std::ostream *out)
74{
75 char **list = header;
76
77 while(**list)
78 *out << *(list++) << "\r\n";
79
80 out->flush();
81}
82
83void MIMEMultipart::body(std::ostream *out)
84{
85 MIMEItemPart *item = first;
86
87 while(item) {
88 *out << "--" << boundry << "\r\n";
89 item->head(out);
90 *out << "\r\n";
91 item->body(out);
92 item = item->next;
93 }
94 *out << "--" << boundry << "--\r\n";
95 out->flush();
96}
97
98MIMEItemPart::MIMEItemPart(MIMEMultipart *m, const char *ct)
99{
100 if(m->last) {
101 m->last->next = this;
102 m->last = this;
103 }
104 else
105 m->first = m->last = this;
106 next = NULL;
107 ctype = ct;
108}
109
110MIMEItemPart::~MIMEItemPart()
111{}
112
113MIMEMultipartForm::MIMEMultipartForm() :
114MIMEMultipart("form-data")
115{}
116
117MIMEMultipartForm::~MIMEMultipartForm()
118{}
119
120void MIMEItemPart::head(std::ostream *out)
121{
122 *out << "Content-Type: " << ctype << "\r" << endl;
123}
124
125MIMEFormData::MIMEFormData(MIMEMultipartForm *m, const char *n, const char *v) :
126MIMEItemPart(m, "")
127{
128 name = n;
129 content = v;
130}
131
132MIMEFormData::~MIMEFormData()
133{}
134
135void MIMEFormData::head(std::ostream *out)
136{
137 *out << "Content-Disposition: form-data; name=\"" << name << "\"\r\n";
138}
139
140void MIMEFormData::body(std::ostream *out)
141{
142 *out << content << "\r\n";
143}
144
145#ifdef CCXX_NAMESPACES
146}
147#endif
148
149/** EMACS **
150 * Local variables:
151 * mode: c++
152 * c-basic-offset: 4
153 * End:
154 */