Пример #1
0
	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();
	}
//------------------------------------------------------------------------------
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);
}
Пример #4
0
void MyPartHandler::handlePart(const MessageHeader& messageHeader, std::istream& stream)
{
    stringstream headerSS;
    messageHeader.write(headerSS);
    _headers.push_back(headerSS.str());

    string contentType = messageHeader.get("Content-Type", "nil");

    if((MyString::ToLower(contentType)).find("multipart") == 0) {
        MultipartReader multipartReader(stream);

        while(multipartReader.hasNextPart()) {
            MessageHeader subMessageHeader;
            multipartReader.nextPart(subMessageHeader);

            string subContentType = subMessageHeader.get("Content-Type", "nil");
            // Convert to lower case for comparison only
            string lc_subctype = MyString::ToLower(subContentType);

            //Priority is text/plain format, else save text/html format
            if(lc_subctype == "nil") {
                continue;
            } else if(lc_subctype.find("application") != string::npos && lc_subctype.find("name") != string::npos) {
                // Save attachment(s) in sub-content part
                string disp;
                string filename;
                string attachment;
                NameValueCollection params;

                MessageHeader::splitParameters(lc_subctype, disp, params);
                filename = params.get("name", "nil");
                if(filename != "nil") {
                    // Filename and Attachments might be encoded in Base64 or QuotedPrintable
                    _filenames.push_back(DecodeString(filename));
                    string encoder = MyString::ToLower(subMessageHeader.get("Content-Transfer-Encoding", "nil"));
                    if(encoder == "base64") {
                        Poco::Base64Decoder base64Decoder(multipartReader.stream());
                        StreamCopier::copyToString(base64Decoder, attachment);
                    } else if(encoder == "quoted-printable") {
                        Poco::Net::QuotedPrintableDecoder qpDecoder(multipartReader.stream());
                        StreamCopier::copyToString(qpDecoder, attachment);
                    } else {
                        StreamCopier::copyToString(multipartReader.stream(), attachment);
                    }

                    if (!attachment.empty()) {
                        _attachments.push_back(attachment);
                    }
                }
            } else if(lc_subctype.find("boundary") != string::npos) {
                int bStart = 0;
                if(_myboundary.empty()) {
                    bStart = subContentType.find('_');
                    _myboundary = MyString::FixField(subContentType, bStart, (subContentType.length() - (bStart + 1)));
                }
            } else if(lc_subctype.find("text/plain") == 0) {
                string charset;

                if(subContentType.find("charset") != string::npos) {
                    //Outlook: Content-Type text/plain charset="us-ascii"
                    //Yahoo: Content-Type text/plain charset=iso-8859-1
                    string subct_clean = MyString::RemoveChar(subContentType, '"');
                    int charpos = subct_clean.find("charset=") + 8; //+8 to bypass the word "charset="
                    charset = MyString::FixField(subct_clean, charpos, (subContentType.length() - charpos) );
                }

                //If body variable is not empty, it has the text/plain format of the email body.
                string cte = subMessageHeader.get("Content-Transfer-Encoding", "nil");
                //For some reasons, emails from outlook (content transfer encoding is specified as quoted-printable in header), it generates nil result in QuotedPrintableDecoder
                if(charset.compare("us-ascii") != 0) {
                    if(cte == "base64")	{
                        Poco::Base64Decoder base64Decoder(multipartReader.stream());
                        StreamCopier::copyToString(base64Decoder, _body);
                    } else if(cte == "quoted-printable") {
                        Poco::Net::QuotedPrintableDecoder qpDecoder(multipartReader.stream());
                        StreamCopier::copyToString(qpDecoder, _body);
                    } else {
                        StreamCopier::copyToString(multipartReader.stream(), _body);
                    }
                } else {
                    StreamCopier::copyToString(multipartReader.stream(), _body);
                }

                if(!_myboundary.empty() && _myboundary.compare(multipartReader.boundary()) != 0) {
                    _body = MyString::Trim(MyString::FixField(_body, 0, (_body.find(_myboundary) - 2))); //-2 for the boundary heading, e.g. --_000_OD67Eexchau_
                }
            } else {
                if(_body.empty() || _body.length() > 0) break;
                // Will hit error "Malformed message: Field value too long/no CRLF found" under MesssageHeader.read() in MessageHeader.cpp
                // if "continue" is used.  "text/plain" part will always come before "text/html" part
                //Keep this code for reference of retrieving text/html content, ignore text/html part at this moment
                /*
                  else if(subContentType.find("text/html") == 0) {
                    string cte = subMessageHeader.get("Content-Transfer-Encoding", "nil");
                    if(cte == "base64") {
                      Poco::Base64Decoder base64Decoder(multipartReader.stream());
                      StreamCopier::copyToString(base64Decoder, _body);
                    } else if(cte == "quoted-printable") {
                  Poco::Net::QuotedPrintableDecoder qpDecoder(multipartReader.stream());
                  StreamCopier::copyToString(qpDecoder, _body);
                    } else
                  StreamCopier::copyToString(stream, _body);
                */
            }
        }
    } else {
        //Email body content
        //Change request 20101007: Ignore text/html part
        if(contentType.find("text/html") == string::npos && (_body.empty() || _body.length() > 0))
            StreamCopier::copyToString(stream, _body);
    }

}
Пример #5
0
bool FileServerRequestHandler::isAdminLoggedIn(const HTTPRequest& request,
                                               HTTPResponse &response)
{
    assert(LOOLWSD::AdminEnabled);

    const auto& config = Application::instance().config();

    NameValueCollection cookies;
    request.getCookies(cookies);
    try
    {
        const std::string jwtToken = cookies.get("jwt");
        LOG_INF("Verifying JWT token: " << jwtToken);
        JWTAuth authAgent("admin", "admin", "admin");
        if (authAgent.verify(jwtToken))
        {
            LOG_TRC("JWT token is valid");
            return true;
        }

        LOG_INF("Invalid JWT token, let the administrator re-login");
    }
    catch (const Poco::Exception& exc)
    {
        LOG_INF("No existing JWT cookie found");
    }

    // If no cookie found, or is invalid, let the admin re-login
    HTTPBasicCredentials credentials(request);
    const std::string& userProvidedUsr = credentials.getUsername();
    const std::string& userProvidedPwd = credentials.getPassword();

    // Deny attempts to login without providing a username / pwd and fail right away
    // We don't even want to allow a password-less PAM module to be used here,
    // or anything.
    if (userProvidedUsr.empty() || userProvidedPwd.empty())
    {
        LOG_WRN("An attempt to log into Admin Console without username or password.");
        return false;
    }

    // Check if the user is allowed to use the admin console
    if (config.getBool("admin_console.enable_pam", "false"))
    {
        // use PAM - it needs the username too
        if (!isPamAuthOk(userProvidedUsr, userProvidedPwd))
            return false;
    }
    else
    {
        // use the hash or password in the config file
        if (!isConfigAuthOk(userProvidedUsr, userProvidedPwd))
            return false;
    }

    // authentication passed, generate and set the cookie
    JWTAuth authAgent("admin", "admin", "admin");
    const std::string jwtToken = authAgent.getAccessToken();

    Poco::Net::HTTPCookie cookie("jwt", jwtToken);
    // bundlify appears to add an extra /dist -> dist/dist/admin
    cookie.setPath(LOOLWSD::ServiceRoot + "/loleaflet/dist/");
    cookie.setSecure(LOOLWSD::isSSLEnabled() ||
                     LOOLWSD::isSSLTermination());
    response.addCookie(cookie);

    return true;
}