[sword-cvs] sword/src/utilfuns url.cpp,1.8,1.9

sword at www.crosswire.org sword at www.crosswire.org
Tue Jul 20 13:42:09 MST 2004


Committed by: scribe

Update of /cvs/core/sword/src/utilfuns
In directory www:/tmp/cvs-serv15261/src/utilfuns

Modified Files:
	url.cpp 
Log Message:
rewrote url parsing logic up to param parsing. should still be reviewed a little


Index: url.cpp
===================================================================
RCS file: /cvs/core/sword/src/utilfuns/url.cpp,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- url.cpp	20 Jul 2004 19:06:41 -0000	1.8
+++ url.cpp	20 Jul 2004 20:42:07 -0000	1.9
@@ -101,74 +101,100 @@
 	 parameterMap.clear();
 	 
 	 // 2. Get the protocol, which is from the begining to the first ://
-	 char* end = strchr( urlPtr, ':' );
-	 if (end) { //protocol was found
+	 const char* end = strchr( urlPtr, ':' );
+	if (end) { //protocol was found
 	 	protocol.append(urlPtr, end-urlPtr);
-	 	pos = end-urlPtr;
-		
-		for (char* c = end; (*c == ':') || (*c == '/') ; ++c, ++pos) {} //find the end of the protocol separator (e.g. "://")
-	 }
+	 	urlPtr = end + 1;
 	
-	 //3.Get the hostname part. This is the part from pos up to the first slash
-	 end = strchr(urlPtr+pos, '/');
-	 if (end) {
-		hostname.append(urlPtr+pos, (end-urlPtr)-pos);
-	 	
-		pos = end - urlPtr;
-	 }
-	 else { //no slash found means there's only the host in the url
-	 	return; //WARNING: return already here!
-	 }
+		//find the end of the protocol separator (e.g. "://")
+		for (; (*urlPtr == ':') || (*urlPtr == '/'); urlPtr++);
+	}
+
+ //3.Get the hostname part. This is the part from pos up to the first slash
+	bool checkPath   = true;
+	bool checkParams = true;
+	bool checkAnchor = true;
+
+	end = strchr(urlPtr, '/');
+	if (!end) {
+		checkPath = false;
+		end = strchr(urlPtr, '?');
+	}
+	if (!end) {
+		checkParams = false;
+		end = strchr(urlPtr, '#');
+	}
+	if (!end) {
+		checkAnchor = false;
+		end = urlPtr+strlen(urlPtr);
+	}
 	 
-	 //4. Get the path, e.g. /dir/script.jsp, this is the part up to the first question mark, if it exists. Otherwise up to the end.
-	 char* start = end;
-	 end = start ? strchr(start, '?') : 0;
-	 if (end) {
-	 	path.append(start, end-start);
+	hostname.append(urlPtr, end-urlPtr);
+	 	
+	urlPtr = end + ((*end)? 1 : 0);
+
+	if (checkPath) { 
+		end = strchr(urlPtr, '?');
+		if (!end) {
+			checkParams = false;
+			end = strchr(urlPtr, '#');
+		}
+		if (!end) {
+			checkAnchor = false;
+			end = urlPtr+strlen(urlPtr);
+		}
+
+	 	path.append(urlPtr, end-urlPtr);
 		
-		pos = end-urlPtr;
-	 }
-	 else {
-	 	path.append(start);
-		return; //WARNING: return already here
+		urlPtr = end + ((*end)? 1 : 0);
 	 }
 
-	//5. Fill the map with the parameters and their values
-	SWBuf paramName;
-	SWBuf paramValue;
-			
-	end = (start && strchr(start, '?')) ? strchr(start, '?')+1 :0;
-	while (end) {
-		paramName = "";
-		paramValue = "";
-		
-		//search for the equal sign to find the value part
-		const char* valueStart = strchr(end, '=');		
-		if (valueStart) {
-			const char* valueEnd = strstr(valueStart, "&") ? strstr(valueStart, "&") : strstr(valueStart, "&"); //try to find a new paramter part
+	if (checkParams) {
+		//5. Fill the map with the parameters and their values
+		SWBuf paramName;
+		SWBuf paramValue;
+				
+/*
+		end = strchr(urlPtr, '#');
+		if (!end) {
+			checkAnchor = false;
+			end = urlPtr+strlen(urlPtr);
+		}
+*/
+		//end = (start && strchr(start, '?')) ? strchr(start, '?')+1 :0;
+		end = urlPtr;
+		while (end) {
+			paramName = "";
+			paramValue = "";
 			
-			if (valueEnd) {
-				paramName.append(end, valueStart-end);
-				paramValue.append(valueStart+1, valueEnd-(valueStart+1));
+			//search for the equal sign to find the value part
+			const char* valueStart = strchr(end, '=');		
+			if (valueStart) {
+				const char* valueEnd = strstr(valueStart, "&") ? strstr(valueStart, "&") : strstr(valueStart, "&"); //try to find a new paramter part
+				
+				if (valueEnd) {
+					paramName.append(end, valueStart-end);
+					paramValue.append(valueStart+1, valueEnd-(valueStart+1));
+				}
+				else { //this is the last paramter of the URL
+					paramName.append(end, valueStart-end);
+					paramValue.append(valueStart+1);
+				}
+				
+				if (paramName.length() && paramValue.length()) {//insert the param into the map if it's valid
+					paramName = decode(paramName.c_str());
+					paramValue = decode(paramValue.c_str());
+					
+					parameterMap[ paramName ] = paramValue;
+				}
 			}
-			else { //this is the last paramter of the URL
-				paramName.append(end, valueStart-end);
-				paramValue.append(valueStart+1);
+			else {
+				break; //no valid parameter in the url
 			}
 			
-			if (paramName.length() && paramValue.length()) {//insert the param into the map if it's valid
-				paramName = decode(paramName.c_str());
-				paramValue = decode(paramValue.c_str());
-				
-				parameterMap[ paramName ] = paramValue;
-			}
-		}
-		else {
-			break; //no valid parameter in the url
+			const char *start = end+1;
+			end = strstr(start, "&") ? strstr(start, "&")+5 : (strstr(start, "&") ? strstr(start, "&")+1 : 0); //try to find a new paramter part
 		}
-		
-		start = end+1;
-		end = strstr(start, "&") ? strstr(start, "&")+5 : (strstr(start, "&") ? strstr(start, "&")+1 : 0); //try to find a new paramter part
 	}
 }
 



More information about the sword-cvs mailing list