#include "Request.h" #include "Response.h" #include #include #include using namespace std; int main() { HTTPWrapper::Request req; HTTPWrapper::Response res; ofstream ofs("log.txt"); ofs << req.contentType << endl; ofs << "formdata as following" << endl; int cnt_success = 0; int cnt_trans = 0; int cnt_failed = 0; int cnt_noname = 0; int cnt_write_failed = 0; res.writer << "Upload Result"; res.writer << "

Upload Result

"; if (!req.formdata.empty()) { ofs << "FormData size: " << req.formdata.size() << endl; res.writer << "

FormData size: " << req.formdata.size() << "

"; for (int i = 0; i < req.formdata.size(); i++) { ofs << "Name: " << req.formdata[i].name << endl; ofs << "Filename: " << req.formdata[i].filename << endl; ofs << "ContentType: " << req.formdata[i].contentType << endl; string outputname = req.formdata[i].filename; bool ok = true; if (!outputname.empty()) { if (outputname.substr(outputname.size() - 4) == ".mp4") { // Ends with .mp4 ofs << outputname << " ends with mp4. No need to change." << endl; cnt_success++; res.writer << "

MP4 File: " << outputname << "

"; } else if (outputname.substr(outputname.size() - 5) == ".vdat") { // Ends with .vdat ofs << outputname << " ends with vdat. Change it." << endl; outputname = outputname.substr(0, outputname.size() - 5).append(".mp4"); cnt_trans++; res.writer << "

Vdat File: " << outputname << "

"; } else { ofs << outputname << " does not fit." << endl; cnt_failed++; ok = false; res.writer << "

Invalid: " << outputname << "

"; } } else { ofs << "No filename. Use default name." << endl; outputname = "Upload_" + to_string(time(NULL)) + ".mp4"; cnt_noname++; res.writer << "

No name: " << outputname << "

"; } if (ok) { string diskname = "F:\\faaq\\OutSideVideo\\" + outputname; FILE* fp = fopen(diskname.c_str(), "rb"); if (fp) { fclose(fp); ofs << "File already exists: " << diskname << endl; cnt_write_failed++; res.writer << "

File already exists: " << diskname << "

"; } else { fp = fopen(diskname.c_str(), "wb"); if (fp) { if (fwrite(req.formdata[i].content.data(), req.formdata[i].content.size(), 1, fp) == 0) { ofs << "Error while writting data to opened file: " << diskname << endl; cnt_write_failed++; res.writer << "

Write Failed: " << diskname << "

"; } else { res.writer << "

Write Done: " << diskname << "

"; } fclose(fp); } else { ofs << "Failed to open: " << diskname << endl; cnt_write_failed++; res.writer << "

Open Failed: " << diskname << "

"; } } } } res.writer << "
"; } ofs << "formdata end." << endl; res.writer << "

Upload finished.

" << "

success: " << cnt_success << "

" << "

Trans: " << cnt_trans << "

" << "

Failed: " << cnt_failed << "

" << "

Noname: " << cnt_noname << "

" << "

Written Failed: " << cnt_write_failed << "

" << "
"; res.writer << ""; return 0; }