 $token $error
    } elseif {[catch {eof $sock} iseof] || $iseof} {
	Eof $token
    } else {
	CopyStart $sock $token
    }
}

# http::Eof
#
#	Handle eof on the socket
#
# Arguments
#	token	The token returned from http::geturl
#
# Side Effects
#	Clean up the socket

proc http::Eof {token {force 0}} {
    variable $token
    upvar 0 $token state
    if {$state(state) eq "header"} {
	# Premature eof
	set state(status) eof
    } else {
	set state(status) ok
    }

    if {($state(coding) eq "gzip") && [string length $state(body)] > 0} {
        if {[catch {
	    if {[package vsatisfies [package present Tcl] 8.6]} {
		# The zlib integration into 8.6 includes proper gzip support
		set state(body) [zlib gunzip $state(body)]
	    } else {
		set state(body) [Gunzip $state(body)]
	    }
        } err]} then {
	    return [Finish $token $err]
        }
    }

    if {!$state(binary)} {
        # If we are getting text, set the incoming channel's encoding
        # correctly.  iso8859-1 is the RFC default, but this could be any IANA
        # charset.  However, we only know how to convert what we have
        # encodings for.

        set enc [CharsetToEncoding $state(charset)]
        if {$enc ne "binary"} {
	    set state(body) [encoding convertfrom $enc $state(body)]
        }

        # Translate text line endings.
        set state(body) [string map {\r\n \n \r \n} $state(body)]
    }

    Finish $token
}

# http::wait --
#
#	See documentation for details.
#
# Arguments:
#	token	Connection token.
#
# Results:
#        The status after the wait.

proc http::wait {token} {
    variable $token
    upvar 0 $token state

    if {![info exists state(status)] || $state(status) eq ""} {
	# We must wait on the original variable name, not the upvar alias
	vwait ${token}(status)
    }

    return [status $token]
}

# http::formatQuery --
#
#	See documentation for details.  Call http::formatQuery with an even
#	number of arguments, where the first is a name, the second is a value,
#	the third is another name, and so on.
#
# Arguments:
#	args	A list of name-value pairs.
#
# Results:
#	TODO

proc http::formatQuery {args} {
    set result ""
    set sep ""
    foreach i $args {
	append result $sep [mapReply $i]
	if {$sep eq "="} {
	    set sep &
	} else {
	    set sep =
	}
    }
    return $result
}

# http::mapReply --
#
#	Do x-www-urlencoded character mapping
#
# Arguments:
#	string	The string the needs to be encoded
#
# Results:
#       The encoded string

proc http::mapReply {string} {
    variable http
    variable formMap

    # The spec says: "non-alphanumeric characters are replaced by '%HH'". Use
    # a pre-computed map and [string map] to do the conversion (much faster
    # than [regsub]/[subst]). [Bug 1020491]

    if {$http(-urlencoding) ne ""} {
	set string [encoding convertto $http(-urlencoding) $string]
	return [string map $formMap $string]
    }
    set converted [string map $formMap $string]
    if {[string match "*\[\u0100-\uffff\]*" $converted]} {
	regexp {[\u0100-\uffff]} $converted badChar
	# Return this error messa{"catalogItem":{"entryId":"{D8A8EDA0-F227-43F2-B96E-409AFAA815CF}","userSID":"S-1-5-18","userIdentity":"","productId":"9MZ95KL8MR0L","packageFamilyName":"Microsoft.ScreenSketch_8wekyb3d8bbwe","mainPackageFamilyName":"","contentId":"","categoryId":"de4f0463-7db2-48ee-959c-70e02541f4e6","fulfillmentId":"","publisherCertificateName":"CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US","packageIdentityName":"Microsoft.ScreenSketch","platformDependencyXmlBlob":"{\"blob.version\":1688867040526336,\"content.bundledPackages\":[\"Microsoft.ScreenSketch_10.2008.2277.0_arm__8wekyb3d8bbwe\",\"Microsoft.ScreenSketch_10.2008.2277.0_x86__8wekyb3d8bbwe\",\"Microsoft.ScreenSketch_10.2008.2277.0_arm64__8wekyb3d8bbwe\",\"Microsoft.ScreenSketch_10.2008.2277.0_x64__8wekyb3d8bbwe\"],\"content.isMain\":false,\"content.packageId\":\"Microsoft.ScreenSketch_2020.814.2355.0_neutral_~_8wekyb3d8bbwe\",\"content.productId\":\"72ba66d3-9233-4202-a261-0d99de2942b4\",\"content.targetPlatforms\":[{\"platform.maxVersionTested\":2814750970478592,\"platform.minVersion\":2814750931222528,\"platform.target\":0}],\"content.type\":7,\"policy\":{\"category.first\":\"app\",\"category.second\":\"Utilities & tools\",\"optOut.backupRestore\":false,\"optOut.removeableMedia\":false},\"policy2\":{\"ageRating\":1,\"optOut.DVR\":false,\"thirdPartyAppRatings\":[{\"level\":7,\"systemId\":3},{\"level\":12,\"systemId\":5},{\"level\":48,\"systemId\":12},{\"level\":27,\"systemId\":9},{\"level\":76,\"systemId\":16},{\"level\":68,\"systemId\":15},{\"level\":54,\"systemId\":13}]}}","packageFullName":"Microsoft.ScreenSketch_2020.814.2355.0_neutral_~_8wekyb3d8bbwe","images":[{"ImagePurpose":"Logo","Uri":"//store-images.s-microsoft.com/image/apps.31022.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.d9314390-f07c-4fd2-aac1-a7fb3b3e9969","BackgroundColor":"#0078D4","ForegroundColor":"","Width":50,"Height":50},{"ImagePurpose":"Logo","Uri":"//store-images.s-microsoft.com/image/apps.60457.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.44ce7eda-0968-45ee-97ba-aa6c482e65ca","BackgroundColor":"#0078D4","ForegroundColor":"","Width":75,"Height":75},{"ImagePurpose":"Logo","Uri":"//store-images.s-microsoft.com/image/apps.53817.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.ce772153-f330-4f6e-98ff-ef611d3be08b","BackgroundColor":"#0078D4","ForegroundColor":"","Width":100,"Height":100},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.16138.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.781ce5ee-5207-4c2a-a1a2-05f997d5281a","BackgroundColor":"#0078D4","ForegroundColor":"","Width":150,"Height":150},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.61574.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.2a3cc14a-4e59-489c-81ac-02fa908817d6","BackgroundColor":"#0078D4","ForegroundColor":"","Width":225,"Height":225},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.36191.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.9ef2384a-0753-4779-a12b-3584f5b5bdb0","BackgroundColor":"#0078D4","ForegroundColor":"","Width":300,"Height":300},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.23722.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.b9808d32-05e7-4008-acdb-e794bf934599","BackgroundColor":"#0078D4","ForegroundColor":"","Width":44,"Height":44},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.16916.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.62adb421-6763-41b3-a109-d4250c06fe97","BackgroundColor":"#0078D4","ForegroundColor":"","Width":66,"Height":66},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.24447.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.c1a4f4ce-acbb-44a9-9713-43faadcd99e6","BackgroundColor":"#0078D4","ForegroundColor":"","Width":88,"Height":88},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.46691.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.9d487d51-19bb-4abb-9a2e-e29979cfd3c0","BackgroundColor":"#0078D4","ForegroundColor":"","Width":310,"Height":310},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.59213.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.9d5f1ff6-fd8f-4067-ac9b-0ab535ddb1b4","BackgroundColor":"#0078D4","ForegroundColor":"","Width":465,"Height":465},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.9028.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.2a9eb253-fa1f-49af-ade2-a69e352463d7","BackgroundColor":"#0078D4","ForegroundColor":"","Width":620,"Height":620},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.7623.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.117ef273-630a-47ac-bcbb-784831ef5e3c","BackgroundColor":"#0078D4","ForegroundColor":"","Width":71,"Height":71},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.53609.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.3e9f815a-875b-439f-b343-20833db2635f","BackgroundColor":"#0078D4","ForegroundColor":"","Width":107,"Height":107},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.63450.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.fabb8459-08cc-4913-9819-45a84d8eae25","BackgroundColor":"#0078D4","ForegroundColor":"","Width":142,"Height":142},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.57447.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.14ff8d93-fd0f-478f-9028-e1a8633ac1f7","BackgroundColor":"#0078D4","ForegroundColor":"","Width":310,"Height":150},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.5493.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.97761288-920f-4866-9f69-77b3a094142d","BackgroundColor":"#0078D4","ForegroundColor":"","Width":465,"Height":225},{"ImagePurpose":"Tile","Uri":"//store-images.s-microsoft.com/image/apps.29597.13878650002538161.36d39bfb-e0bd-480f-bb6f-e45ba4810ca5.9f007e50-55f2-482f-b78c-ec5605f2c9af","BackgroundColor":"#0078D4","ForegroundColor":"","Width":620,"Height":300},{"ImagePurpose":"Screenshot","Uri":"//store-images.s-microsoft.com/image/apps.36328.13878650002538161.13e58cb6-af1f-48e4-80fb-ec63b30288ea.d8d9c30b-acd2-43eb-8567-5096518e875f","BackgroundColor":"#0078D4","ForegroundColor":"","Width":1920,"Height":1080},{"ImagePurpose":"Screenshot","Uri":"//store-images.s-microsoft.com/image/apps.2836.13878650002538161.0c548e9c-88e3-4bd5-83e3-e73675de6418.b044e2a9-249c-4ab9-9c2e-65242698cf77","BackgroundColor":"#0078D4","ForegroundColor":"","Width":1920,"Height":1080}],"bundledProducts":[],"packageFormat":"AppxBundle","productTitle":"Snip & Sketch","fulfillmentPluginId":"WU","fulfillmentData":"{\"ProductId\":\"9MZ95KL8MR0L\",\"WuBundleId\":\"961b2dba-97b8-413e-bbc7-ff516034350b\",\"WuCategoryId\":\"de4f0463-7db2-48ee-959c-70e02541f4e6\",\"PackageFamilyName\":\"Microsoft.ScreenSketch_8wekyb3d8bbwe\",\"SkuId\":\"0010\",\"Content\":null,\"PackageFeatures\":null}","packageRelativeAppIds":["App"],"canFulfill":true,"hrInstallAllowed":0,"created":"2020-11-28T12:01:17-08:00","requiresElevation":false,"market":"","isVisibleInAppList":true,"isExclusivityFailed":false,"isCompanionApp":false,"isPlugin":false,"skuId":"0010","availabilityId":"B4WXHK3BF0M5","catalogId":"","workId":""}}