File: mime.cpp

package info (click to toggle)
libwibble 1.1-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,040 kB
  • ctags: 2,721
  • sloc: cpp: 14,542; makefile: 196; perl: 87; sh: 26
file content (214 lines) | stat: -rw-r--r-- 6,483 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * net/mime - MIME utilities
 *
 * Copyright (C) 2010--2011  Enrico Zini <enrico@enricozini.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */

#include <wibble/net/mime.h>
#include <wibble/exception.h>
#include <wibble/string.h>
#include <unistd.h>

using namespace std;

namespace wibble {
namespace net {
namespace mime {

Reader::Reader() 
      : header_splitter("^([^:[:blank:]]+)[[:blank:]]*:[[:blank:]]*(.+)", 3)
{
}

bool Reader::read_line(int sock, string& res)
{
    bool has_cr = false;
    res.clear();
    while (true)
    {
        char c;
        ssize_t count = read(sock, &c, 1);
        if (count == 0) return false;
        if (count < 0)
            throw wibble::exception::System("reading from socket");
        switch (c)
        {
            case '\r':
                if (has_cr)
                    res.append(1, '\r');
                has_cr = true;
                break;
            case '\n':
                if (has_cr)
                    return true;
                else
                    res.append(1, '\n');
                break;
            default:
                res.append(1, c);
                break;
        }
    }
}
    
bool Reader::read_headers(int sock, std::map<std::string, std::string>& headers)
{
    string line;
    map<string, string>::iterator last_inserted = headers.end();
    while (read_line(sock, line))
    {
        // Headers are terminated by an empty line
        if (line.empty())
            return true;

        if (isblank(line[0]))
        {
            // Append continuation of previous header body
            if (last_inserted != headers.end())
            {
                last_inserted->second.append(" ");
                last_inserted->second.append(str::trim(line));
            }
        } else {
            if (header_splitter.match(line))
            {
                // rfc2616, 4.2 Message Headers:
                // Multiple message-header fields with
                // the same field-name MAY be present
                // in a message if and only if the
                // entire field-value for that header
                // field is defined as a
                // comma-separated list [i.e.,
                // #(values)].  It MUST be possible to
                // combine the multiple header fields
                // into one "field-name: field-value"
                // pair, without changing the semantics
                // of the message, by appending each
                // subsequent field-value to the first,
                // each separated by a comma.
                string key = str::tolower(header_splitter[1]);
                string val = str::trim(header_splitter[2]);
                map<string, string>::iterator old = headers.find(key);
                if (old == headers.end())
                {
                    // Insert new
                    pair< map<string, string>::iterator, bool > res =
                        headers.insert(make_pair(key, val));
                    last_inserted = res.first;
                } else {
                    // Append comma-separated
                    old->second.append(",");
                    old->second.append(val);
                    last_inserted = old;
                }
            } else
                last_inserted = headers.end();
        }
    }
    return false;
}

bool Reader::readboundarytail(int sock)
{
    // [\-\s]*\r\n
    bool has_cr = false;
    bool has_dash = false;
    while (true)
    {
        char c;
        ssize_t count = read(sock, &c, 1);
        if (count == 0) throw wibble::exception::Consistency("reading from socket", "data ends before MIME boundary");
        if (count < 0) throw wibble::exception::System("reading from socket");
        switch (c)
        {
            case '\r':
                has_cr = true;
                break;
            case '\n':
                if (has_cr) return !has_dash;
                break;
            case '\t':
            case ' ':
                has_cr = false;
                break;
            case '-':
                has_dash = true;
                has_cr = false;
                break;
            default:
                throw wibble::exception::Consistency("reading from socket", "line ends with non-whitespace");
                break;
        }
    }
}

bool Reader::read_until_boundary(int sock, const std::string& boundary, std::ostream& out, size_t max)
{
    size_t read_so_far = 0;
    unsigned got = 0;

    while (got < boundary.size())
    {
        char c;
        ssize_t count = read(sock, &c, 1);
        if (count == 0) throw wibble::exception::Consistency("reading from socket", "data ends before MIME boundary");
        if (count < 0) throw wibble::exception::System("reading from socket");

        if (c == boundary[got])
            ++got;
        else
        {
            if (max == 0 || read_so_far < max)
            {
                if (got > 0)
                {
                    out.write(boundary.data(), got);
                    read_so_far += got;
                }
                out.put(c);
                ++read_so_far;
            }
            got = 0;
        }
    }

    return readboundarytail(sock);
}

bool Reader::discard_until_boundary(int sock, const std::string& boundary)
{
    unsigned got = 0;

    while (got < boundary.size())
    {
        char c;
        ssize_t count = read(sock, &c, 1);
        if (count == 0) throw wibble::exception::Consistency("reading from socket", "data ends before MIME boundary");
        if (count < 0) throw wibble::exception::System("reading from socket");
        if (c == boundary[got])
            ++got;
        else
            got = 0;
    }

    return readboundarytail(sock);
}

}
}
}
// vim:set ts=4 sw=4: