File: multipart.cpp

package info (click to toggle)
tntnet 1.5.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,004 kB
  • ctags: 2,381
  • sloc: cpp: 13,553; sh: 8,997; ansic: 1,604; makefile: 573; sql: 10
file content (156 lines) | stat: -rw-r--r-- 3,852 bytes parent folder | download
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
/* multipart.cpp
 * Copyright (C) 2003 Tommi Maekitalo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#include <tnt/multipart.h>
#include <tnt/httpheader.h>
#include <sstream>
#include <stdexcept>
#include <streambuf>

namespace
{
  template <typename iterator>
    class iterator_streambuf : public std::streambuf
  {
    public:
      typedef unsigned long streamsize;

    private:
      iterator& begin;
      iterator end;
      char_type buffer[2];

    protected:
      std::streambuf::int_type overflow(std::streambuf::int_type ch)
        { return traits_type::eof(); }
      int sync()
        {
          if (gptr() == buffer + 1)
            ++begin;
          setg(0, 0, 0);
          return 0;
        }
      std::streambuf::int_type underflow()
        {
          if (begin == end)
            return traits_type::eof();

          if (gptr() == buffer + 1)
            ++begin;

          buffer[0] = *begin;
          setg(buffer, buffer, buffer + 1);
          return buffer[0];
        }

    public:
      iterator_streambuf(iterator& b, iterator e)
        : begin(b),
          end(e)
        { }

  };
}

namespace tnt
{
  Partheader::return_type Partheader::onField(const std::string& name,
    const std::string& value)
  {
    if (name == "Content-Disposition:")
    {
      std::istringstream in(value);
      in >> cd;
      if (!in)
        return FAIL;
    }

    return Messageheader::onField(name, value);
  }

  Part::Part(const_iterator b, const_iterator e)
  {
    iterator_streambuf<const_iterator> buf(b, e);
    std::istream in(&buf);
    in >> header;
    if (!in)
      throw std::runtime_error("error in parsing message-header");
    in.sync();

    bodyBegin = b;
    bodyEnd = e;
  }

  std::string Part::getHeader(const std::string& key) const
  {
    Messageheader::const_iterator it = header.find(key);
    if (it != header.end())
      return it->second;
    return std::string();
  }

  std::string Partheader::getMimetype() const
  {
    const_iterator it = find(httpheader::contentType);
    return it == end() ? std::string() : it->second;
  }

  void Multipart::set(const std::string& boundary, const std::string& b)
  {
    body = b;

    std::string::size_type bpos = body.find(boundary);
    while (bpos != std::string::npos)
    {
      bpos += boundary.size();
      if (body[bpos] == '\r')
        ++bpos;
      if (body[bpos] == '\n')
        ++bpos;

      std::string::size_type bend = body.find(boundary, bpos);
      if (bend == std::string::npos)
        return;

      std::string::size_type nbegin = bend;

      if (body[bend-1] == '-')
        --bend;
      if (body[bend-1] == '-')
        --bend;
      if (body[bend-1] == '\n')
        --bend;
      if (body[bend-1] == '\r')
        --bend;

      parts.push_back(part_type(body.begin() + bpos, body.begin() + bend));
      bpos = nbegin;
    }
  }

  Multipart::const_iterator Multipart::find(const std::string& part_name,
        Multipart::const_iterator start) const
  {
    for (const_iterator it = start; it != end(); ++it)
      if (it->getName() == part_name)
        return it;

    return end();
  }
}