void MultiPartParser::handlePart(const Poco::Net::MessageHeader &header, std::istream &stream) { QueryVar qv; std::string data; if(header.has("Content-Disposition")) { std::string disp; Poco::Net::NameValueCollection nvc; Poco::Net::MessageHeader::splitParameters(header["Content-Disposition"],disp,nvc); qv.SetName(nvc.get("name","")); qv.SetFileName(nvc.get("filename","")); if(header.has("Content-Type")) { qv.SetContentType(header["Content-Type"]); } else { qv.SetContentType(""); } Poco::StreamCopier::copyToString(stream,data); qv.SetData(data); m_vars[qv.GetName()]=qv; } }
void handlePart(const Poco::Net::MessageHeader& header, std::istream& stream) { _type = header.get("Content-Type", "(unspecified)"); if (header.has("Content-Disposition")) { std::string disp; Poco::Net::NameValueCollection params; Poco::Net::MessageHeader::splitParameters(header["Content-Disposition"], disp, params); _name = params.get("name", "(unnamed)"); _fileName = params.get("filename", "(unnamed)"); } Poco::CountingInputStream istr(stream); Poco::NullOutputStream ostr; Poco::StreamCopier::copyStream(istr, ostr); _length = istr.chars(); }
void PostRouteFileHandler::handlePart(const Poco::Net::MessageHeader& header, std::istream& stream) { if(header.has("Content-Type")) { std::string contentType = header["Content-Type"]; if(!_parent.getSettings().getValidContentTypes().empty() && !isContentTypeValid(contentType)) { ofLogError("PostRouteFileHandler::handlePart") << "Invalid content type: " << contentType; return; // reject } } else { ofLogError("PostRouteFileHandler::handlePart") << "No Content-Type header."; return; } // is this an uploaded file? if(header.has("Content-Disposition"))// && header.has("form-data")) { std::string contentDisposition = header["Content-Disposition"]; Poco::Net::NameValueCollection parameters; Poco::Net::MessageHeader::splitParameters(contentDisposition.begin(), contentDisposition.end(), parameters); std::string formFileName = parameters.get("filename", ""); std::string formFieldName = parameters.get("name", ""); if(!formFileName.empty()) { try { std::stringstream ss; ss << _parent.getSettings().getUploadFolder(); ss << "/"; ss << Poco::UUIDGenerator::defaultGenerator().createOne().toString(); ss << "."; ss << Poco::Path(formFileName).getExtension(); std::string newFilename = ofToDataPath(ss.str(), true); ofFile file(newFilename, ofFile::WriteOnly); Poco::Net::MediaType contentType(header["Content-Type"]); PostUploadEventArgs args(_sessionUUID, _request, _formUUID, formFieldName, formFileName, newFilename, contentType, 0, PostUploadEventArgs::UPLOAD_STARTING); ofNotifyEvent(_parent.events.onHTTPUploadEvent, args, &_parent); ofLogVerbose("PostRouteFileHandler::handlePart") << "Writing file to absolute path : " << file.getAbsolutePath(); // The section below is from StreamCopier::copyStream, // and might be used for upload progress feedback Poco::Buffer<char> buffer(_parent.getSettings().getWriteBufferSize()); std::streamsize sz = 0; stream.read(buffer.begin(), _parent.getSettings().getWriteBufferSize()); std::streamsize n = stream.gcount(); while (n > 0) { if (sz > _parent.getSettings().getMaximumFileUploadSize()) { ofLogError("PostRouteFileHandler::handlePart") << "File upload size exceeded. Removing file."; file.close(); ofFile::removeFile(newFilename, false); return; } sz += n; file.write(buffer.begin(), n); if (stream && file) { stream.read(buffer.begin(), _parent.getSettings().getWriteBufferSize()); n = stream.gcount(); } else { n = 0; } PostUploadEventArgs args(_sessionUUID, _request, _formUUID, formFieldName, formFileName, newFilename, contentType, sz, PostUploadEventArgs::UPLOAD_PROGRESS); ofNotifyEvent(_parent.events.onHTTPUploadEvent, args, &_parent); } file.close(); PostUploadEventArgs finishedArgs(_sessionUUID, _request, _formUUID, formFieldName, formFileName, newFilename, contentType, sz, PostUploadEventArgs::UPLOAD_FINISHED); ofNotifyEvent(_parent.events.onHTTPUploadEvent, finishedArgs, &_parent); } catch (const Poco::Exception& exc) { ofLogError("PostRouteFileHandler::handlePart") << exc.displayText(); } catch (const std::exception& exc) { ofLogError("PostRouteFileHandler::handlePart") << exc.what(); } catch ( ... ) { ofLogError("PostRouteFileHandler::handlePart") << "Uncaught thread exception: Unknown exception."; } } else { ofLogError("PostRouteFileHandler::handlePart") << "No filename in header."; } } }