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 void CGIFormFile::read_multipart (MotorEnv* env) {
27 std::cerr << "boundary:" << boundary << "\n";
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";
41 bool CGIFormFile::saveData (MotorEnv* env) {
42 static const int bsize = 65536 * 4;
43 size_t size = postSize ();
46 pid_t mypid = getpid ();
49 if (size == 0 || size > env->appenv->postfilelimit)
51 tmpfile = env->path_to_posttemp ();
53 if (fp.openReadWrite (tmpfile.c_str ())) {
54 unlink (tmpfile.c_str ());
56 s = (size < bsize) ? size : bsize;
57 s = std::cin.read (&b[0], s).gcount ();
60 s = ::write (fp.fd, &b[0], s);
64 std::cerr << "post-file[" << mypid << ":" << env->scriptName << "]: reading " << mapsize << "Bytes...\n";
69 std::cerr << "post-file[" << mypid << ":" << env->scriptName << "]: done " << mapsize << "Bytes\n";
70 mapdata = (char*)mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fp.fd, 0);
72 std::cerr << (void*) mapdata << ": " << mapsize << "\n";
75 throw (ustring (CharConst ("configuration error: can't open temporary file.")));
80 void CGIFormFile::unlinkTmpFile () {
82 munmap (mapdata, mapsize);
85 if (tmpfile.size () > 0) {
86 // unlink (tmpfile.c_str ());
91 bool CGIFormFile::isMultipart () {
92 ustring e = getenvString (kCONTENT_TYPE);
94 static uregex re ("^" kMIME_FORMDATA "\\s*;\\s*boundary=\"?([ '()+,./0-9:=?A-Z_a-z-]*['()+,./0-9:=?A-Z_a-z-])");
96 if (usearch (e, m, re)) {
97 boundary = ustring (m[1]);
104 void CGIFormFile::searchPart (MotorEnv* env) {
106 char* e = mapdata + mapsize;
108 boost::match_results<char*> m;
117 std::cerr << "b:" << (void*)b << " e:" << (void*)e << "\n";
118 std::cerr << "mapdata:" << ustring (b, b + 40) << "\n";
120 if (b != e && regex_search (b, e, m, re1, boost::regex_constants::match_single_line)) {
122 std::cerr << "match:" << ustring (m[0].first, m[0].second) << "\n";
125 while (b != e && regex_search (b, e, m, reN, boost::regex_constants::match_single_line)) {
128 std::cerr << "match:" << ustring (m[0].first, m[0].second) << "\n";
130 readMimeHead (b, x, disp, name, filename, type);
132 std::cerr << "disp:" << disp << " name:" << name << " filename:" << filename << " type:" << type << "\n";
134 if (filename.size () > 0) {
136 // v = to_ustring (parts.size ());
139 // insert (filearg, name, v);
140 parts.push_back (part (b, x));
142 k1 = insert (iarg, name, filePart_osSafe (filename));
143 datamap.insert (sary_t::value_type (k1, k2));
145 std::cerr << "insert(" << k1 << "," << k2 << ")\n";
150 if (size < env->appenv->postlimit) {
152 name = omitNul (name);
156 insert (iarg, name, v);
158 *env->log << "form variable '" << name << "': size limit.\n";
165 } else if (b[0] == '-' && b[1] == '-') {
167 std::cerr << "break\n";
170 } else if (b[0] == '\r' && b[1] == '\n') {
173 break; // format error;
179 void CGIFormFile::compileReg () {
181 ustring t = escape_re (boundary);
183 a.append (CharConst ("^--"));
187 std::cerr << "re1:" << a << "\n";
191 a.append (CharConst ("--"));
194 std::cerr << "reN:" << a << "\n";
199 void CGIFormFile::readMimeHead (char*& b, char* e, ustring& disp, ustring& name, ustring& filename, ustring& type) {
200 boost::match_results<char*> m;
201 boost::match_results<char*> m2;
203 static uregex re_disp1 ("^Content-Disposition:\\s*(.*);\\s*name=\"(.*)\";\\s*filename=\"(.*)\"$");
204 static uregex re_disp2 ("^Content-Disposition:\\s*(.*);\\s*name=\"(.*)\"$");
205 static uregex re_type ("^Content-Type:\\s*([a-zA-Z_0-9/.+-]*)(;\\s*(.*))?$");
211 while (b != e && regex_search (b, e, m, re_nl, boost::regex_constants::match_single_line)) {
214 std::cerr << "line:" << ustring (b, x) << "\n";
216 if (b == x) { // empty line
220 if (regex_search (b, x, m2, re_disp1, boost::regex_constants::match_single_line)) {
221 disp.assign (m2[1].first, m2[1].second - m2[1].first);
222 name.assign (m2[2].first, m2[2].second - m2[2].first);
223 filename.assign (m2[3].first, m2[3].second - m2[3].first);
224 } else if (regex_search (b, x, m2, re_disp2, boost::regex_constants::match_single_line)) {
225 disp.assign (m2[1].first, m2[1].second - m2[1].first);
226 name.assign (m2[2].first, m2[2].second - m2[2].first);
227 } else if (regex_search (b, x, m2, re_type, boost::regex_constants::match_single_line)) {
228 type.assign (m2[1].first, m2[1].second - m2[1].first);
231 std::cerr << "not match:" << ustring (b, x) << "\n";
239 bool CGIFormFile::readFilename (int i, ustring& filename) {
240 if (0 <= i && i < parts.size ()) {
241 filename = filenames[i];
248 bool CGIFormFile::saveFile (int i, const ustring& path, size_t max) {
249 static size_t bsize = 65536;
255 if (0 <= i && i < parts.size ()) {
257 assert (mapdata <= p.first && p.first <= p.second && p.second < mapdata + mapsize);
259 size = p.second - p.first;
260 if (max > 0 && size > max)
264 std::cerr << "saveFile(" << i << "," << path << "): " << (void*)p.first << "--" << (p.second - p.first) << "\n";
266 f.openWrite (path.c_str ());
268 std::cerr << "write:" << path << "\n";
271 s = (size < bsize) ? size : bsize;
272 s = ::write (f.fd, b, s);