--[[----------------------------------------------------------------------------

KrUri.lua


********************************************************************************

ADOBE CONFIDENTIAL
------------------
Copyright 2003 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--


local parse = function( base )
	local _, _, a, b =  string.find( base, "^(([^:/?#]+):)" )
	if a then base = string.sub( base, string.len( a )+1 ) end
	local _, _, c, d =  string.find( base, "^(//([^/?#]*))" )
	if c then base = string.sub( base, string.len( c )+1 ) end
	local _, _, e=  string.find( base, "^([^?#]*)" )
	if e then base = string.sub( base, string.len( e )+1 ) end
	local _, _, f, g =  string.find( base, "^(\?([^#]*))" )
	if f then base = string.sub( base, string.len( f )+1 ) end
	local _, _, h, i =  string.find( base, "^(#(.*))" )
	if h then base = string.sub( base, string.len( h )+1 ) end
	return a, b, c, d, e, f, g, h, i
end



local AgURI_parse = function( uristr )
	local _, bscheme, _, bauthority, bpath, _, bquery, _, _ = parse( uristr )

	-- fix + type of URI encoding
	local decodedpath = string.replace( bpath, '+', ' ' )
	-- fix %20 type of URI encoding
	decodedpath = string.gsub( decodedpath, '%%(%x%x)', function( hex ) return string.char( tonumber( hex, 16 ) ) end )

	return {
		scheme = bscheme,
		authority = bauthority,
		path = decodedpath,
		rawPath = bpath,
		query = bquery,
		uri = uristr,
	}
end


---------------------------------------------------------------------------

return AgURI_parse
