void Request::addParameter(const std::string &name, const std::string &value) { std::string d_name = UriDecode(name); std::string d_value = UriDecode(value); post[d_name] = d_value; }
UriCodecTest::UriCodecTest() { assert(UriEncode("ABC") == "ABC"); const std::string ORG("\0\1\2", 3); const std::string ENC("%00%01%02"); assert(UriEncode(ORG) == ENC); assert(UriDecode(ENC) == ORG); assert(UriEncode("\xFF") == "%FF"); assert(UriDecode("%FF") == "\xFF"); assert(UriDecode("%ff") == "\xFF"); // unsafe chars test, RFC1738 const std::string UNSAFE(" <>#{}|\\^~[]`"); std::string sUnsafeEnc = UriEncode(UNSAFE); assert(std::string::npos == sUnsafeEnc.find_first_of(UNSAFE)); assert(UriDecode(sUnsafeEnc) == UNSAFE); // random test const int MAX_LEN = 128; char a[MAX_LEN]; srand((unsigned)time(NULL)); for (int i = 0; i < 100; i++) { for (int j = 0; j < MAX_LEN; j++) a[j] = rand() % (1 << 8); int nLen = rand() % MAX_LEN; std::string sOrg(a, nLen); std::string sEnc = UriEncode(sOrg); assert(sOrg == UriDecode(sEnc)); } }
TEST(Utils, UriCoding) { string src1 = "This is a simple & short test."; string src2 = "$ & < > ? ; # : = , \" ' ~ + %-_"; string src3 = "! * ' ( ) ; : @ & = + $ , / ? % # [ ]"; string src4 = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i " "j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 - _ . ~"; string dst1 = "This%20is%20a%20simple%20%26%20short%20test."; string dst2 = "%24%20%26%20%3C%20%3E%20%3F%20%3B%20%23%20%3A%20%3D%20%2C%20%22%20%27%" "20~%20%2B%20%25-_"; string dst3 = "%21%20%2A%20%27%20%28%20%29%20%3B%20%3A%20%40%20%26%20%3D%20%2B%20%24%" "20%2C%20%2F%20%3F%20%25%20%23%20%5B%20%5D"; string dst4 = "A%20B%20C%20D%20E%20F%20G%20H%20I%20J%20K%20L%20M%20N%20O%20P%20Q%20R%" "20S%20T%20U%20V%20W%20X%20Y%20Z%20a%20b%20c%20d%20e%20f%20g%20h%20i%" "20j%20k%20l%20m%20n%20o%20p%20q%20r%20s%20t%20u%20v%20w%20x%20y%20z%" "200%201%202%203%204%205%206%207%208%209%20-%20_%20.%20~"; EXPECT_EQ(dst1, UriEncode(src1)); EXPECT_EQ(dst2, UriEncode(src2)); EXPECT_EQ(dst3, UriEncode(src3)); EXPECT_EQ(dst4, UriEncode(src4)); EXPECT_EQ(src1, UriDecode(dst1)); EXPECT_EQ(src2, UriDecode(dst2)); EXPECT_EQ(src3, UriDecode(dst3)); EXPECT_EQ(src4, UriDecode(dst4)); }
void ofxRemoteUI::setValuesFromString( string values ){ stringstream in(values); string name, value; vector<string>changedParam; while( !in.eof() ){ getline( in, name, '=' ); getline( in, value, '\n' ); if( params.find( name ) != params.end() ){ RemoteUIParam param = params[name]; RemoteUIParam original = params[name]; changedParam.push_back(name); stringstream valstr( UriDecode(value) ); switch (param.type) { case REMOTEUI_PARAM_FLOAT: valstr >> param.floatVal; break; case REMOTEUI_PARAM_INT: valstr >> param.intVal; break; case REMOTEUI_PARAM_COLOR: { std::istringstream ss(UriDecode(value)); std::string token; std::getline(ss, token, ' '); param.redVal = atoi(token.c_str()); std::getline(ss, token, ' '); param.greenVal = atoi(token.c_str()); std::getline(ss, token, ' '); param.blueVal = atoi(token.c_str()); std::getline(ss, token, ' '); param.alphaVal = atoi(token.c_str()); } case REMOTEUI_PARAM_ENUM: valstr >> param.intVal; break; case REMOTEUI_PARAM_BOOL: valstr >> param.boolVal; break; case REMOTEUI_PARAM_STRING: param.stringVal = valstr.str(); break; case REMOTEUI_PARAM_SPACER: break; default: break; } if ( !param.isEqualTo(original) ){ // if the udpdate changed the param, keep track of it params[name] = param; paramsChangedSinceLastCheck.insert(name); } } }
string getPOSTPayload(string data, string tag){ int tagStart = 0, tagEnd = -1, dataEnd = -1;; //Iterate across data looking for tags //Format {tag}={data}&{tag2}={data2}... string currentTag; while(currentTag != tag){ tagStart = dataEnd + 1; tagEnd = data.find("=", tagStart); if(tagEnd == string::npos){ return ""; } currentTag = data.substr(tagStart, tagEnd - tagStart); dataEnd = data.find("&", tagEnd); if(dataEnd == string::npos){ dataEnd = data.size(); } } //Found Tag return UriDecode(data.substr(tagEnd + 1, dataEnd - tagEnd - 1), true); }
int main( int argc, const char *argv[] ) { self = argv[0]; srand( getpid() * time( 0 ) ); enum { ZMS_UNKNOWN, ZMS_MONITOR, ZMS_EVENT } source = ZMS_UNKNOWN; enum { ZMS_JPEG, ZMS_MPEG, ZMS_RAW, ZMS_ZIP, ZMS_SINGLE } mode = ZMS_JPEG; char format[32] = ""; int monitor_id = 0; time_t event_time = 0; uint64_t event_id = 0; unsigned int frame_id = 1; unsigned int scale = 100; unsigned int rate = 100; double maxfps = 10.0; unsigned int bitrate = 100000; unsigned int ttl = 0; EventStream::StreamMode replay = EventStream::MODE_NONE; std::string username; std::string password; char auth[64] = ""; unsigned int connkey = 0; unsigned int playback_buffer = 0; bool nph = false; const char *basename = strrchr(argv[0], '/'); if ( basename ) //if we found a / lets skip past it basename++; else //argv[0] will not always contain the full path, but rather just the script name basename = argv[0]; const char *nph_prefix = "nph-"; if ( basename && !strncmp(basename, nph_prefix, strlen(nph_prefix)) ) { nph = true; } zmLoadConfig(); const char *query = getenv("QUERY_STRING"); if ( query ) { Debug(1, "Query: %s", query); char temp_query[1024]; strncpy(temp_query, query, sizeof(temp_query)); char *q_ptr = temp_query; char *parms[16]; // Shouldn't be more than this int parm_no = 0; while( (parm_no < 16) && (parms[parm_no] = strtok(q_ptr, "&")) ) { parm_no++; q_ptr = NULL; } for ( int p = 0; p < parm_no; p++ ) { char *name = strtok(parms[p], "="); char *value = strtok(NULL, "="); if ( !value ) value = (char *)""; if ( !strcmp(name, "source") ) { source = !strcmp(value, "event")?ZMS_EVENT:ZMS_MONITOR; } else if ( !strcmp(name, "mode") ) { mode = !strcmp(value, "jpeg")?ZMS_JPEG:ZMS_MPEG; mode = !strcmp(value, "raw")?ZMS_RAW:mode; mode = !strcmp(value, "zip")?ZMS_ZIP:mode; mode = !strcmp(value, "single")?ZMS_SINGLE:mode; } else if ( !strcmp( name, "format" ) ) { strncpy( format, value, sizeof(format) ); } else if ( !strcmp( name, "monitor" ) ) { monitor_id = atoi( value ); if ( source == ZMS_UNKNOWN ) source = ZMS_MONITOR; } else if ( !strcmp( name, "time" ) ) { event_time = atoi( value ); } else if ( !strcmp( name, "event" ) ) { event_id = strtoull( value, (char **)NULL, 10 ); source = ZMS_EVENT; } else if ( !strcmp( name, "frame" ) ) { frame_id = strtoull( value, (char **)NULL, 10 ); source = ZMS_EVENT; } else if ( !strcmp( name, "scale" ) ) { scale = atoi( value ); } else if ( !strcmp( name, "rate" ) ) { rate = atoi( value ); } else if ( !strcmp( name, "maxfps" ) ) { maxfps = atof( value ); } else if ( !strcmp( name, "bitrate" ) ) { bitrate = atoi( value ); } else if ( !strcmp( name, "ttl" ) ) { ttl = atoi(value); } else if ( !strcmp( name, "replay" ) ) { if ( !strcmp(value, "gapless") ) { replay = EventStream::MODE_ALL_GAPLESS; } else if ( !strcmp(value, "all") ) { replay = EventStream::MODE_ALL; } else if ( !strcmp(value, "none") ) { replay = EventStream::MODE_NONE; } else if ( !strcmp(value, "single") ) { replay = EventStream::MODE_SINGLE; } else { Error("Unsupported value %s for replay, defaulting to none", value); } } else if ( !strcmp( name, "connkey" ) ) { connkey = atoi(value); } else if ( !strcmp( name, "buffer" ) ) { playback_buffer = atoi(value); } else if ( config.opt_use_auth ) { if ( strcmp( config.auth_relay, "none" ) == 0 ) { if ( !strcmp( name, "user" ) ) { username = value; } } else { //if ( strcmp( config.auth_relay, "hashed" ) == 0 ) { if ( !strcmp( name, "auth" ) ) { strncpy( auth, value, sizeof(auth)-1 ); } } //else if ( strcmp( config.auth_relay, "plain" ) == 0 ) { if ( !strcmp( name, "user" ) ) { username = UriDecode( value ); } if ( !strcmp( name, "pass" ) ) { password = UriDecode( value ); Debug( 1, "Have %s for password", password.c_str() ); } } } } } // end foreach parm } // end if query char log_id_string[32] = "zms"; if ( monitor_id ) { snprintf(log_id_string, sizeof(log_id_string), "zms_m%d", monitor_id); } else { snprintf(log_id_string, sizeof(log_id_string), "zms_e%" PRIu64, event_id); } logInit( log_id_string ); if ( config.opt_use_auth ) { User *user = 0; if ( strcmp(config.auth_relay, "none") == 0 ) { if ( username.length() ) { user = zmLoadUser(username.c_str()); } } else { //if ( strcmp( config.auth_relay, "hashed" ) == 0 ) { if ( *auth ) { user = zmLoadAuthUser(auth, config.auth_hash_ips); } } //else if ( strcmp( config.auth_relay, "plain" ) == 0 ) { if ( username.length() && password.length() ) { user = zmLoadUser(username.c_str(), password.c_str()); } } } if ( !user ) { Error("Unable to authenticate user"); logTerm(); zmDbClose(); return -1; } ValidateAccess(user, monitor_id); } // end if config.opt_use_auth hwcaps_detect(); zmSetDefaultTermHandler(); zmSetDefaultDieHandler(); setbuf( stdout, 0 ); if ( nph ) { fprintf( stdout, "HTTP/1.0 200 OK\r\n" ); } fprintf( stdout, "Server: ZoneMinder Video Server/%s\r\n", ZM_VERSION ); time_t now = time( 0 ); char date_string[64]; strftime( date_string, sizeof(date_string)-1, "%a, %d %b %Y %H:%M:%S GMT", gmtime( &now ) ); fprintf( stdout, "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n" ); fprintf( stdout, "Last-Modified: %s\r\n", date_string ); fprintf( stdout, "Cache-Control: no-store, no-cache, must-revalidate\r\n" ); fprintf( stdout, "Cache-Control: post-check=0, pre-check=0\r\n" ); fprintf( stdout, "Pragma: no-cache\r\n"); // Removed as causing more problems than it fixed. //if ( !nph ) //{ //fprintf( stdout, "Content-Length: 0\r\n"); //} if ( source == ZMS_MONITOR ) { MonitorStream stream; stream.setStreamScale( scale ); stream.setStreamReplayRate( rate ); stream.setStreamMaxFPS( maxfps ); stream.setStreamTTL( ttl ); stream.setStreamQueue( connkey ); stream.setStreamBuffer( playback_buffer ); if ( ! stream.setStreamStart( monitor_id ) ) { Error( "Unable to connect to zmc process for monitor %d", monitor_id ); fprintf( stderr, "Unable to connect to zmc process. Please ensure that it is running." ); logTerm(); zmDbClose(); return( -1 ); } if ( mode == ZMS_JPEG ) { stream.setStreamType( MonitorStream::STREAM_JPEG ); } else if ( mode == ZMS_RAW ) { stream.setStreamType( MonitorStream::STREAM_RAW ); } else if ( mode == ZMS_ZIP ) { stream.setStreamType( MonitorStream::STREAM_ZIP ); } else if ( mode == ZMS_SINGLE ) { stream.setStreamType( MonitorStream::STREAM_SINGLE ); } else { #if HAVE_LIBAVCODEC stream.setStreamFormat( format ); stream.setStreamBitrate( bitrate ); stream.setStreamType( MonitorStream::STREAM_MPEG ); #else // HAVE_LIBAVCODEC Error( "MPEG streaming of '%s' attempted while disabled", query ); fprintf( stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n" ); logTerm(); zmDbClose(); return( -1 ); #endif // HAVE_LIBAVCODEC } stream.runStream(); } else if ( source == ZMS_EVENT ) { if ( ! event_id ) { Fatal( "Can't view an event without specifying an event_id." ); } Debug(3,"Doing event stream scale(%d)", scale ); EventStream stream; stream.setStreamScale( scale ); stream.setStreamReplayRate( rate ); stream.setStreamMaxFPS( maxfps ); stream.setStreamMode( replay ); stream.setStreamQueue( connkey ); if ( monitor_id && event_time ) { stream.setStreamStart(monitor_id, event_time); } else { Debug(3, "Setting stream start to frame (%d)", frame_id); stream.setStreamStart(event_id, frame_id); } if ( mode == ZMS_JPEG ) { stream.setStreamType( EventStream::STREAM_JPEG ); } else { #if HAVE_LIBAVCODEC stream.setStreamFormat( format ); stream.setStreamBitrate( bitrate ); stream.setStreamType( EventStream::STREAM_MPEG ); #else // HAVE_LIBAVCODEC Error( "MPEG streaming of '%s' attempted while disabled", query ); fprintf( stderr, "MPEG streaming is disabled.\nYou should ensure the ffmpeg libraries are installed and detected and rebuild to use this functionality.\n" ); logTerm(); zmDbClose(); return( -1 ); #endif // HAVE_LIBAVCODEC } // end if jpeg or mpeg stream.runStream(); } else { Error("Neither a monitor or event was specified."); } // end if monitor or event logTerm(); zmDbClose(); return( 0 ); }
// A C++ near equivalent for PHP's urldecode function. string filter_url_urldecode (string url) { url = UriDecode (url); replace (url.begin (), url.end (), '+', ' '); return url; }