std::streamsize HTMLForm::calculateContentLength() { if (_boundary.empty()) throw HTMLFormException("Form must be prepared"); HTMLFormCountingOutputStream c; write(c); if (c.getIsValid()) return c.chars(); else return UNKNOWN_CONTENT_LENGTH; }
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 HTMLForm::readUrl(std::istream& istr) { static const int eof = std::char_traits<char>::eof(); int fields = 0; int ch = istr.get(); while (ch != eof) { if (_fieldLimit > 0 && fields == _fieldLimit) throw HTMLFormException("Too many form fields"); std::string name; std::string value; while (ch != eof && ch != '=' && ch != '&') { if (ch == '+') ch = ' '; name += (char) ch; ch = istr.get(); } if (ch == '=') { ch = istr.get(); while (ch != eof && ch != '&') { if (ch == '+') ch = ' '; value += (char) ch; ch = istr.get(); } } std::string decodedName; std::string decodedValue; URI::decode(name, decodedName); URI::decode(value, decodedValue); add(decodedName, decodedValue); ++fields; if (ch == '&') ch = istr.get(); } }