class EL_HTTP_COOKIE_TABLE

(source code)

Client examples: HTTP_CONNECTION_TEST_SET

description

Parses HTTP cookies from cookie file creating a table of name-value pairs

notes

Format Example (Contains tabs)

# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

httpbin.org   FALSE   /   FALSE   0   city   "K\303\266ln"
httpbin.org   FALSE   /   FALSE   0   district   "K\303\266ln-Altstadt-S\303\274d"
note
	description: "Parses HTTP cookies from cookie file creating a table of name-value pairs"
	notes: "[
		Format Example (Contains tabs)
				
			# Netscape HTTP Cookie File
			# http://curl.haxx.se/docs/http-cookies.html
			# This file was generated by libcurl! Edit at your own risk.

			httpbin.org	FALSE	/	FALSE	0	city	"K\303\266ln"
			httpbin.org	FALSE	/	FALSE	0	district	"K\303\266ln-Altstadt-S\303\274d"
	]"

	author: "Finnian Reilly"
	copyright: "Copyright (c) 2001-2022 Finnian Reilly"
	contact: "finnian at eiffel hyphen loop dot com"

	license: "MIT license (See: en.wikipedia.org/wiki/MIT_License)"
	date: "2024-09-22 14:15:12 GMT (Sunday 22nd September 2024)"
	revision: "18"

class
	EL_HTTP_COOKIE_TABLE

inherit
	EL_ZSTRING_TABLE
		redefine
			default_create
		end

	EL_STRING_STATE_MACHINE [STRING_8]
		rename
			make as make_machine
		undefine
			default_create, is_equal, copy
		end

	EL_MODULE_FILE

create
	make_from_file, default_create

feature {NONE} -- Initialization

	default_create
		do
			make_machine
			make_sized (1)
		end

	make_from_file (a_file_path: FILE_PATH)
		do
			make_machine
			if attached File.plain_text_lines (a_file_path) as lines then
				make_sized (lines.count)
				do_with_split (agent find_first_cookie, lines, False)
			end
		end

feature {NONE} -- State handlers

	find_first_cookie (line: STRING)
		do
			if not (line.is_empty or line.starts_with (Comment_start)) then
				state := agent parse_cookie
				parse_cookie (line)
			end
		end

	parse_cookie (line: STRING)
		local
			tab_splitter: EL_SPLIT_ON_CHARACTER [STRING]; value: ZSTRING;
			cookie_value: EL_COOKIE_STRING_8; name: IMMUTABLE_STRING_8
		do
			create tab_splitter.make (line, '%T')
			across tab_splitter as split loop
				inspect split.cursor_index
					when 6 then
						name := split.item_copy
					when 7 then
						if split.item.has ('\') then
--							assume to contain octal rather than hexadecimal escape sequences
							create {EL_OCTAL_COOKIE_STRING_8} cookie_value.make_encoded (split.item)
						else
							cookie_value := split.item
						end
						value := cookie_value.decoded
						if value.has_quotes (2) then
							value.remove_quotes
						end
						put (value, name)
				else
				end
			end
		end

feature {NONE} -- Constants

	Comment_start: STRING = "# "

end