void submitLoginPage(HTTPSClientSession& clientSession, NameValueCollection& cookies) { HTTPRequest request(HTTPRequest::HTTP_POST, "/royalgreenwich/sessions", HTTPMessage::HTTP_1_1); request.setCookies(cookies); HTTPResponse response; HTMLForm loginForm; loginForm.add("barcode", "28028005913354"); loginForm.add("pin", "3347"); loginForm.prepareSubmit(request); std::ostream& ostr = clientSession.sendRequest(request); loginForm.write(ostr); std::istream& rs = clientSession.receiveResponse(response); int statusCode = response.getStatus(); poco_information_f1(logger(), "Status %d", statusCode); std::vector<HTTPCookie> newCookies; response.getCookies(newCookies); for (HTTPCookie cookie : newCookies) { poco_information_f1(logger(), "Cookie %s", cookie.toString()); if (cookies.has(cookie.getName())) { cookies.set(cookie.getName(), cookie.getValue()); } else { cookies.add(cookie.getName(), cookie.getValue()); } } }
void fetchAccountPage(HTTPSClientSession& clientSession, NameValueCollection& cookies) { HTTPRequest request(HTTPRequest::HTTP_GET, "/royalgreenwich/account", HTTPMessage::HTTP_1_1); request.setCookies(cookies); HTTPResponse response; std::ostream& ostr = clientSession.sendRequest(request); std::istream& rs = clientSession.receiveResponse(response); int statusCode = response.getStatus(); poco_information_f1(logger(), "Status %d", statusCode); std::vector<HTTPCookie> newCookies; response.getCookies(newCookies); for (HTTPCookie cookie : newCookies) { poco_information_f1(logger(), "Cookie %s", cookie.toString()); if (cookies.has(cookie.getName())) { cookies.set(cookie.getName(), cookie.getValue()); } else { cookies.add(cookie.getName(), cookie.getValue()); } } StreamCopier::copyStream(rs, std::cout); }
void fetchLoginPage(HTTPSClientSession& clientSession, NameValueCollection& cookies) { HTTPRequest request(HTTPRequest::HTTP_GET, "/royalgreenwich/login?message=borrowerservices_notloggedin&referer=https%3A%2F%2Fcapitadiscovery.co.uk%2Froyalgreenwich%2Faccount", HTTPMessage::HTTP_1_1); request.setCookies(cookies); HTTPResponse response; std::ostream& ostr = clientSession.sendRequest(request); std::istream& rs = clientSession.receiveResponse(response); int statusCode = response.getStatus(); poco_information_f1(logger(), "Status %d", statusCode); std::vector<HTTPCookie> newCookies; response.getCookies(newCookies); for (HTTPCookie cookie : newCookies) { poco_information_f1(logger(), "Cookie %s", cookie.toString()); if (cookies.has(cookie.getName())) { cookies.set(cookie.getName(), cookie.getValue()); } else { cookies.add(cookie.getName(), cookie.getValue()); } } }
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 } } }
//------------------------------------------------------------------------------ HTTPRequestHandler* ofxIpVideoServerRoute::createRequestHandler(const HTTPServerRequest& request) { URI uri(request.getURI()); if(ofxIpVideoServerRouteHandler::matchRoute(uri,settings.route)) { ofxIpVideoServerFrame::Settings targetSettings; string query = uri.getQuery(); NameValueCollection queryMap = ofGetQueryMap(uri); if(queryMap.has("vflip")) { string vflip = queryMap.get("vflip"); targetSettings.bFlipVertical = icompare(vflip,"1") == 0 || icompare(vflip,"true") == 0 || icompare(vflip,"t") == 0 || icompare(vflip,"y") == 0 || icompare(vflip,"yes") == 0; } if(queryMap.has("hflip")) { string hflip = queryMap.get("hflip"); targetSettings.bFlipHorizontal = icompare(hflip,"1") == 0 || icompare(hflip,"true") == 0 || icompare(hflip,"t") == 0 || icompare(hflip,"y") == 0 || icompare(hflip,"yes") == 0; } if(queryMap.has("size")) { string size = queryMap.get("size"); toLowerInPlace(size); vector<string> tokens = ofSplitString(size,"x"); if(tokens.size() == 2) { int width = ofToInt(tokens[0]); int height = ofToInt(tokens[1]); if(width > 0 && height > 0) { width = MIN(width,MAX_VIDEO_DIM); height = MIN(height,MAX_VIDEO_DIM); targetSettings.width = width; targetSettings.height = height; } } } if(queryMap.has("quality")) { string quality = queryMap.get("quality"); if(icompare(quality,"best")) { targetSettings.quality = OF_IMAGE_QUALITY_BEST; } else if(icompare(quality,"high")) { targetSettings.quality = OF_IMAGE_QUALITY_HIGH; } else if(icompare(quality,"medium")) { targetSettings.quality = OF_IMAGE_QUALITY_MEDIUM; } else if(icompare(quality,"low")) { targetSettings.quality = OF_IMAGE_QUALITY_LOW; } else if(icompare(quality,"worst")) { targetSettings.quality = OF_IMAGE_QUALITY_WORST; } else { // no change } } ofxIpVideoServerFrameQueue* queue = new ofxIpVideoServerFrameQueue(targetSettings); queues.push_back(queue); return new ofxIpVideoServerRouteHandler(settings,*queue); } else { return NULL; } }
void NameValueCollectionTest::testNameValueCollection() { NameValueCollection nvc; assert (nvc.empty()); assert (nvc.size() == 0); nvc.set("name", "value"); assert (!nvc.empty()); assert (nvc["name"] == "value"); assert (nvc["Name"] == "value"); nvc.set("name2", "value2"); assert (nvc.get("name2") == "value2"); assert (nvc.get("NAME2") == "value2"); assert (nvc.size() == 2); try { std::string value = nvc.get("name3"); fail("not found - must throw"); } catch (NotFoundException&) { } try { std::string value = nvc["name3"]; fail("not found - must throw"); } catch (NotFoundException&) { } assert (nvc.get("name", "default") == "value"); assert (nvc.get("name3", "default") == "default"); assert (nvc.has("name")); assert (nvc.has("name2")); assert (!nvc.has("name3")); nvc.add("name3", "value3"); assert (nvc.get("name3") == "value3"); nvc.add("name3", "value31"); NameValueCollection::ConstIterator it = nvc.find("Name3"); assert (it != nvc.end()); std::string v1 = it->second; assert (it->first == "name3"); ++it; assert (it != nvc.end()); std::string v2 = it->second; assert (it->first == "name3"); assert ((v1 == "value3" && v2 == "value31") || (v1 == "value31" && v2 == "value3")); nvc.erase("name3"); assert (!nvc.has("name3")); assert (nvc.find("name3") == nvc.end()); it = nvc.begin(); assert (it != nvc.end()); ++it; assert (it != nvc.end()); ++it; assert (it == nvc.end()); nvc.clear(); assert (nvc.empty()); assert (nvc.size() == 0); }