3 #include "util_const.h"
4 #include "util_string.h"
12 int CGIFormFile::partAt (int i) {
14 sary_t::iterator it = datamap.find (i);
15 if (it == datamap.end ()) {
25 ustring CGIFormFile::typeAt (int i) {
27 tary_t::iterator it = typemap.find (i);
28 if (it == typemap.end ()) {
38 void CGIFormFile::read_multipart (MotorEnv* env) {
40 std::cerr << "boundary:" << boundary << "\n";
47 for (int i = 0; i < parts.size (); i ++) {
48 std::cerr << "file:" << i << " (" << (void*)parts[i].first << ", " << (void*)parts[i].second << ") filename:" << filenames[i] << "\n";
54 bool CGIFormFile::saveData (MotorEnv* env) {
55 static const size_t bsize = 65536 * 4;
56 size_t size = postSize ();
59 pid_t mypid = getpid ();
62 if (size == 0 || size > env->appenv->postfilelimit)
64 tmpfile = env->path_to_posttemp ();
66 if (fp.openReadWrite (tmpfile.c_str ())) {
67 unlink (tmpfile.c_str ());
69 s = (size < bsize) ? size : bsize;
70 s = std::cin.read (&b[0], s).gcount ();
73 ::write (fp.fd, &b[0], s);
77 // fp.close (); mmapした後にクローズする。
78 // std::cerr << "post-file[" << mypid << ":" << env->scriptName << "]: done " << mapsize << "Bytes\n"; // 出力サイズが大きいと、デッドロックする。
79 mapdata = (char*)mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fp.fd, 0);
81 std::cerr << (void*) mapdata << ": " << mapsize << "\n";
84 throw (ustring (CharConst ("configuration error: can't open temporary file.")));
89 void CGIFormFile::unlinkTmpFile () {
90 if (mapdata != NULL) {
91 munmap (mapdata, mapsize);
94 if (tmpfile.size () > 0) {
95 // unlink (tmpfile.c_str ());
100 void CGIFormFile::searchPart (MotorEnv* env) {
102 char* e = mapdata + mapsize;
104 boost::match_results<char*> m;
113 std::cerr << "b:" << (void*)b << " e:" << (void*)e << "\n";
114 std::cerr << "mapdata:" << ustring (b, b + 40) << "\n";
116 if (b != e && regex_search (b, e, m, re1, boost::regex_constants::match_single_line)) {
118 std::cerr << "match:" << ustring (m[0].first, m[0].second) << "\n";
121 while (b != e && regex_search (b, e, m, reN, boost::regex_constants::match_single_line)) {
124 std::cerr << "match:" << ustring (m[0].first, m[0].second) << "\n";
126 readMimeHead (b, x, disp, name, filename, type);
128 std::cerr << "disp:" << disp << " name:" << name << " filename:" << filename << " type:" << type << "\n";
130 if (filename.size () > 0) {
134 parts.push_back (part (b, x));
137 k1 = insert (iarg, name, filePart_osSafe (filename));
138 datamap.insert (sary_t::value_type (k1, k2));
139 typemap.insert (tary_t::value_type (k2, type));
141 std::cerr << "insert(" << k1 << "," << k2 << ")\n";
146 if (size < env->appenv->postlimit) {
148 name = omitNul (name);
152 insert (iarg, name, v);
154 *env->log << "form variable '" << name << "': size limit.\n";
161 } else if (b[0] == '-' && b[1] == '-') {
163 std::cerr << "break\n";
166 } else if (b[0] == '\r' && b[1] == '\n') {
169 break; // format error;
175 void CGIFormFile::compileReg () {
177 ustring t = escape_re (boundary);
179 a.append (CharConst ("^--"));
183 std::cerr << "re1:" << a << "\n";
187 a.append (CharConst ("--"));
190 std::cerr << "reN:" << a << "\n";
202 ChSplitterNL (char* _begin, char* _end) {
212 return ustring (b, t);
241 for (; t < e; ++ t) {
245 } else if (*t == '\r') {
247 if (u < e && *u == '\n')
256 void CGIFormFile::readMimeHead (char*& b, char* e, ustring& disp, ustring& name, ustring& filename, ustring& type) {
257 ChSplitterNL sp (b, e);
258 boost::match_results<char*> m2;
259 static uregex re_disp1 ("^Content-Disposition:\\s*(.*);\\s*name=\"(.*)\";\\s*filename=\"(.*)\"$");
260 static uregex re_disp2 ("^Content-Disposition:\\s*(.*);\\s*name=\"(.*)\"$");
261 static uregex re_type ("^Content-Type:\\s*([a-zA-Z_0-9/.+-]*)(;\\s*(.*))?$");
269 std::cerr << "line:" << sp.pre () << "\n";
275 if (regex_search (sp.b, sp.t, m2, re_disp1, boost::regex_constants::match_single_line)) {
276 disp.assign (m2[1].first, m2[1].second - m2[1].first);
277 name.assign (m2[2].first, m2[2].second - m2[2].first);
278 filename.assign (m2[3].first, m2[3].second - m2[3].first);
279 } else if (regex_search (sp.b, sp.t, m2, re_disp2, boost::regex_constants::match_single_line)) {
280 disp.assign (m2[1].first, m2[1].second - m2[1].first);
281 name.assign (m2[2].first, m2[2].second - m2[2].first);
282 } else if (regex_search (sp.b, sp.t, m2, re_type, boost::regex_constants::match_single_line)) {
283 type.assign (m2[1].first, m2[1].second - m2[1].first);
286 std::cerr << "not match:" << sp.pre () << "\n";
292 bool CGIFormFile::saveFile (int i, const ustring& path, size_t max) {
293 static size_t bsize = 65536;
299 if (0 <= i && i < parts.size ()) {
301 assert (mapdata <= p.first && p.first <= p.second && p.second < mapdata + mapsize);
303 size = p.second - p.first;
304 if (max > 0 && size > max)
308 std::cerr << "saveFile(" << i << "," << path << "): " << (void*)p.first << "--" << (p.second - p.first) << "\n";
310 f.openWrite (path.c_str ());
312 std::cerr << "write:" << path << "\n";
315 s = (size < bsize) ? size : bsize;
316 s = ::write (f.fd, b, s);