OSDN Git Service

fix add-on function bug when posted by form-data.
[hmh/hhml.git] / lib / formfile.cc
1 #include "formfile.h"
2 #include "motorenv.h"
3 #include "util_const.h"
4 #include "util_string.h"
5 #include "ustring.h"
6 #include "filemacro.h"
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/mman.h>
10 #include <assert.h>
11
12 int  CGIFormFile::partAt (int i) {
13     if (i >= 0) {
14         sary_t::iterator  it = datamap.find (i);
15         if (it == datamap.end ()) {
16             return -1;
17         } else {
18             return it->second;
19         }
20     } else {
21         return -1;
22     }
23 }
24
25 void  CGIFormFile::read_multipart (MotorEnv* env) {
26 #ifdef DEBUG2
27     std::cerr << "boundary:" << boundary << "\n";
28 #endif /* DEBUG */
29     compileReg ();
30     if (saveData (env)) {
31         searchPart (env);
32 #ifdef DEBUG2
33         dump (std::cerr);
34         for (int i = 0; i < parts.size (); i ++) {
35             std::cerr << "file:" << i << " (" << (void*)parts[i].first << ", " << (void*)parts[i].second << ") filename:" << filenames[i] << "\n";
36         }
37 #endif /* DEBUG */
38     }
39 }
40
41 bool  CGIFormFile::saveData (MotorEnv* env) {
42     static const int  bsize = 65536 * 4;
43     size_t  size = postSize ();
44     ustring  b;
45     size_t  s;
46     pid_t  mypid = getpid ();
47
48     b.resize (bsize);
49     if (size == 0 || size > env->appenv->postfilelimit)
50         return false;           // NG
51     tmpfile = env->path_to_posttemp ();
52     mapsize = 0;
53     if (fp.openReadWrite (tmpfile.c_str ())) {
54         unlink (tmpfile.c_str ());
55         while (size > 0) {
56             s = (size < bsize) ? size : bsize;
57             s = std::cin.read (&b[0], s).gcount ();
58             if (s <= 0)
59                 break;
60             s = ::write (fp.fd, &b[0], s);
61             size -= s;
62             mapsize += s;
63             if (size > 0) {
64                 std::cerr << "post-file[" << mypid << ":" << env->scriptName << "]: reading " << mapsize << "Bytes...\n"; // cerrはバッファリングされない
65             }
66         }
67 //      fp.close ();
68         std::cerr << "post-file[" << mypid << ":" << env->scriptName << "]: done    " << mapsize << "Bytes\n";
69         mapdata = (char*)mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fp.fd, 0);
70 #ifdef DEBUG2
71         std::cerr << (void*) mapdata << ": " << mapsize << "\n";
72 #endif /* DEBUG */
73     } else {
74         throw (ustring (CharConst ("configuration error: can't open temporary file.")));
75     }
76     return true;
77 }
78
79 void  CGIFormFile::unlinkTmpFile () {
80     if (mapdata > 0) {
81         munmap (mapdata, mapsize);
82         mapdata = NULL;
83     }
84     if (tmpfile.size () > 0) {
85 //      unlink (tmpfile.c_str ());
86         tmpfile.resize (0);
87     }
88 }
89
90 bool  CGIFormFile::isMultipart () {
91     ustring  e = getenvString (kCONTENT_TYPE);
92     umatch  m;
93     static uregex  re ("^" kMIME_FORMDATA "\\s*;\\s*boundary=\"?([ '()+,./0-9:=?A-Z_a-z-]*['()+,./0-9:=?A-Z_a-z-])");
94
95     if (usearch (e, m, re)) {
96         boundary = ustring (m[1]);
97         return true;
98     } else {
99         return false;
100     }
101 }
102
103 void  CGIFormFile::searchPart (MotorEnv* env) {
104     char*  b = mapdata;
105     char*  e = mapdata + mapsize;
106     char*  x;
107     boost::match_results<char*>  m;
108     ustring  disp;
109     ustring  name;
110     ustring  filename;
111     ustring  type;
112     ustring  v;
113     size_t  size;
114
115 #ifdef DEBUG2
116     std::cerr << "b:" << (void*)b << " e:" << (void*)e << "\n";
117     std::cerr << "mapdata:" << ustring (b, b + 40) << "\n";
118 #endif /* DEBUG */
119     if (b != e && regex_search (b, e, m, re1, boost::regex_constants::match_single_line)) {
120 #ifdef DEBUG2
121         std::cerr << "match:" << ustring (m[0].first, m[0].second) << "\n";
122 #endif /* DEBUG */
123         b = m[0].second;
124         while (b != e && regex_search (b, e, m, reN, boost::regex_constants::match_single_line)) {
125             x = m[0].first;
126 #ifdef DEBUG2
127             std::cerr << "match:" << ustring (m[0].first, m[0].second) << "\n";
128 #endif /* DEBUG */
129             readMimeHead (b, x, disp, name, filename, type);
130 #ifdef DEBUG2
131             std::cerr << "disp:" << disp << " name:" << name << " filename:" << filename << " type:" << type << "\n";
132 #endif /* DEBUG */
133             if (filename.size () > 0) {
134                 int  k1, k2;
135 //              v = to_ustring (parts.size ());
136                 k2 = parts.size ();
137                 fix (name);
138 //              insert (filearg, name, v);
139                 parts.push_back (part (b, x));
140                 fix (filename);
141                 k1 = insert (iarg, name, filePart_osSafe (filename));
142                 datamap.insert (sary_t::value_type (k1, k2));
143 #ifdef DEBUG2
144                 std::cerr << "insert(" << k1 << "," << k2 << ")\n";
145 #endif /* DEBUG */
146             } else {
147                 // no filename
148                 size = x - b;
149                 if (size < env->appenv->postlimit) {
150                     v = ustring (b, x);
151                     name = omitNul (name);
152                     fix (name);
153                     v = omitNul (v);
154                     fix (v);
155                     insert (iarg, name, v);
156                 } else {
157                     *env->log << "form variable '" << name << "': size limit.\n";
158                 }
159             }
160
161             b = m[0].second;
162             if (e - b < 2) {
163                 break;
164             } else if (b[0] == '-' && b[1] == '-') {
165 #ifdef DEBUG2
166                 std::cerr << "break\n";
167 #endif /* DEBUG */
168                 break;
169             } else if (b[0] == '\r' && b[1] == '\n') {
170                 b += 2;
171             } else {
172                 break;          // format error;
173             }
174         }
175     }
176 }
177
178 void  CGIFormFile::compileReg () {
179     ustring  a;
180     ustring  t = escape_re (boundary);
181
182     a.append (CharConst ("^--"));
183     a.append (t);
184     a.append (uCRLF);
185 #ifdef DEBUG2
186     std::cerr << "re1:" << a << "\n";
187 #endif /* DEBUG */
188     re1.assign (a);
189     a = uCRLF;
190     a.append (CharConst ("--"));
191     a.append (t);
192 #ifdef DEBUG2
193     std::cerr << "reN:" << a << "\n";
194 #endif /* DEBUG */
195     reN.assign (a);
196 }
197
198 void  CGIFormFile::readMimeHead (char*& b, char* e, ustring& disp, ustring& name, ustring& filename, ustring& type) {
199     boost::match_results<char*>  m;
200     boost::match_results<char*>  m2;
201     char*  x;
202     static uregex  re_disp1 ("^Content-Disposition:\\s*(.*);\\s*name=\"(.*)\";\\s*filename=\"(.*)\"$");
203     static uregex  re_disp2 ("^Content-Disposition:\\s*(.*);\\s*name=\"(.*)\"$");
204     static uregex  re_type ("^Content-Type:\\s*([a-zA-Z_0-9/.+-]*)(;\\s*(.*))?$");
205
206     disp.resize (0);
207     name.resize (0);
208     filename.resize (0);
209     type.resize (0);
210     while (b != e && regex_search (b, e, m, re_nl, boost::regex_constants::match_single_line)) {
211         x = m[0].first;
212 #ifdef DEBUG2
213         std::cerr << "line:" << ustring (b, x) << "\n";
214 #endif /* DEBUG */
215         if (b == x) {           // empty line
216             b = m[0].second;
217             break;
218         }
219         if (regex_search (b, x, m2, re_disp1, boost::regex_constants::match_single_line)) {
220             disp.assign (m2[1].first, m2[1].second - m2[1].first);
221             name.assign (m2[2].first, m2[2].second - m2[2].first);
222             filename.assign (m2[3].first, m2[3].second - m2[3].first);
223         } else if (regex_search (b, x, m2, re_disp2, boost::regex_constants::match_single_line)) {
224             disp.assign (m2[1].first, m2[1].second - m2[1].first);
225             name.assign (m2[2].first, m2[2].second - m2[2].first);
226         } else if (regex_search (b, x, m2, re_type, boost::regex_constants::match_single_line)) {
227             type.assign (m2[1].first, m2[1].second - m2[1].first);
228         } else {
229 #ifdef DEBUG2
230             std::cerr << "not match:" << ustring (b, x) << "\n";
231 #endif /* DEBUG */
232         }
233         b = m[0].second;
234     }
235 }
236
237 #if 0
238 bool  CGIFormFile::readFilename (int i, ustring& filename) {
239     if (0 <= i && i < parts.size ()) {
240         filename = filenames[i];
241         return true;
242     }
243     return false;
244 }
245 #endif
246
247 bool  CGIFormFile::saveFile (int i, const ustring& path, size_t max) {
248     static size_t  bsize = 65536;
249     part  p;
250     char*  b;
251     size_t  s, size;
252     FileMacro  f;
253
254     if (0 <= i && i < parts.size ()) {
255         p = parts[i];
256         assert (mapdata <= p.first && p.first <= p.second && p.second < mapdata + mapsize);
257         b = p.first;
258         size = p.second - p.first;
259         if (max > 0 && size > max)
260             return false;
261
262 #ifdef DEBUG2
263         std::cerr << "saveFile(" << i << "," << path << "): " << (void*)p.first << "--" << (p.second - p.first) << "\n";
264 #endif /* DEBUG */
265         f.openWrite (path.c_str ());
266 #ifdef DEBUG2
267         std::cerr << "write:" << path << "\n";
268 #endif /* DEBUG */
269         while (size > 0) {
270             s = (size < bsize) ? size : bsize;
271             s = ::write (f.fd, b, s);
272             size -= s;
273             b += s;
274         }
275         f.close ();
276         return true;
277     }
278     return false;
279 }
280