void MailMessage::readPart(std::istream& istr, const MessageHeader& header, PartHandler& handler) { std::string encoding; if (header.has(HEADER_CONTENT_TRANSFER_ENCODING)) { encoding = header.get(HEADER_CONTENT_TRANSFER_ENCODING); // get rid of a parameter if one is set std::string::size_type pos = encoding.find(';'); if (pos != std::string::npos) encoding.resize(pos); } if (icompare(encoding, CTE_QUOTED_PRINTABLE) == 0) { QuotedPrintableDecoder decoder(istr); handlePart(decoder, header, handler); } else if (icompare(encoding, CTE_BASE64) == 0) { Base64Decoder decoder(istr); handlePart(decoder, header, handler); } else { handlePart(istr, header, handler); } }
void handlePart(const MessageHeader& header, std::istream& stream) { _type = header.get("Content-Type", "(unspecified)"); if (header.has("Content-Disposition")) { std::string disp; NameValueCollection params; MessageHeader::splitParameters(header["Content-Disposition"], disp, params); _name = params.get("name", "(unnamed)"); _fileName = params.get("filename", "(unnamed)"); } CountingInputStream istr(stream); NullOutputStream ostr; StreamCopier::copyStream(istr, ostr); _length = istr.chars(); }
void handlePart(const MessageHeader& header, std::istream& stream) { std::string symbolicName; if (_form.has("symbolicName")) symbolicName = _form.get("symbolicName"); if (header.has("Content-Disposition")) { if (symbolicName.empty()) { _pBundle = _pInstaller->installBundle(stream); } else { _pBundle = _pInstaller->replaceBundle(symbolicName, stream); } } }
void HTMLForm::readMultipart(std::istream& istr, PartHandler& handler) { static const int eof = std::char_traits<char>::eof(); int fields = 0; MultipartReader reader(istr, _boundary); while (reader.hasNextPart()) { if (_fieldLimit > 0 && fields == _fieldLimit) throw HTMLFormException("Too many form fields"); MessageHeader header; reader.nextPart(header); std::string disp; NameValueCollection params; if (header.has("Content-Disposition")) { std::string cd = header.get("Content-Disposition"); MessageHeader::splitParameters(cd, disp, params); } if (params.has("filename")) { handler.handlePart(header, reader.stream()); // Ensure that the complete part has been read. while (reader.stream().good()) reader.stream().get(); } else { std::string name = params["name"]; std::string value; std::istream& istr = reader.stream(); int ch = istr.get(); while (ch != eof) { value += (char) ch; ch = istr.get(); } add(name, value); } ++fields; } }
//------------------------------------------------------------------------------ void ofxWebServerUploadRouteHandler::handlePart(const MessageHeader& header, std::istream& stream) { NameValueCollection::ConstIterator iter = header.begin(); // while(iter != header.end()) { // cout << (*iter).first << "=" << (*iter).second << endl; // ++iter; // } // if(header.has("Content-Type")) { string contentType = header["Content-Type"]; if(!isContentTypeValid(contentType)) { ofLogError("ofxWebServerUploadRouteHandler::handlePart") << "Invalid content type: " << contentType; return; // reject } } else { ofLogError("ofxWebServerUploadRouteHandler::handlePart") << "No Content-Type header."; return; } // is this an uploaded file? if(header.has("Content-Disposition")) {// && header.has("form-data")) { string contentDisposition = header["Content-Disposition"]; NameValueCollection parameters; MessageHeader::splitParameters(contentDisposition.begin(),contentDisposition.end(),parameters); if(parameters.has("filename")) { try { ofFile file(settings.uploadFolder + "/" + parameters["filename"], ofFile::WriteOnly); cout << file.getAbsolutePath() << endl; streamsize sz = StreamCopier::copyStream(stream,file); // The section below is from StreamCopier::copyStream, // and can be used for upload progress feedback // int bufferSize = 8192; // Buffer<char> buffer(bufferSize); // streamsize len = 0; // stream.read(buffer.begin(), bufferSize); // streamsize n = stream.gcount(); // while (n > 0) { // len += n; // file.write(buffer.begin(), n); // if (stream && file) { // stream.read(buffer.begin(), bufferSize); // n = stream.gcount(); // cout << n << endl; // } // else n = 0; // } file.close(); } catch(const Exception& exc) { ofLogError("ofxWebServerUploadRouteHandler::handlePart") << exc.displayText(); } catch(const exception& exc) { ofLogError("ofxWebServerUploadRouteHandler::handlePart") << exc.what(); } catch(...) { ofLogError("ofxWebServerUploadRouteHandler::handlePart") << "Uncaught thread exception: Unknown exception."; } } else { // error } } }