text
stringlengths
0
15.7k
source
stringlengths
6
112
considering diacriticals, hyphens, punctuation and white space but ignoring case and numeric strings
lns3.scpt
repeat with aRef in my _list
lns3.scpt
if {textEncoding} is in aRef's item 1 then return aRef's item 2
lns3.scpt
end getEncoding
lns3.scpt
to _parseCharset(asocContentType)
lns3.scpt
set asocPattern to current application's NSRegularExpression's regularExpressionWithPattern:"(?i);\\s*charset\\s*=\\s*(\"?)(.+?)\\1\\s*(?:;|$)" options:0 |error|:(missing value) -- sloppy non-RFC1341 pattern not appropriate for general parameter matching, but adequate here as the value will be rejected anyway unless it matches one of the encoding names defined above
lns3.scpt
set asocMatch to asocPattern's firstMatchInString:asocContentType options:0 range:{0, asocContentType's |length|()}
lns3.scpt
if asocMatch is missing value then return missing value
lns3.scpt
return (asocContentType's substringWithRange:(asocMatch's rangeAtIndex:2)) as text
lns3.scpt
end _parseCharset
lns3.scpt
to _makeHTTPRequest(theURL, httpMethod, requestHeaders, requestBody, timeoutInSeconds)
lns3.scpt
set httpRequest to current application's NSMutableURLRequest's requestWithURL:(_support's asNSURLParameter(theURL, "to"))
lns3.scpt
set httpMethod to _support's asTextParameter(httpMethod, "method") -- TO DO: what if any checks are needed here (e.g. non-empty/invalid chars)
lns3.scpt
httpRequest's setHTTPMethod:httpMethod
lns3.scpt
if timeoutInSeconds is not missing value then httpRequest's setTimeoutInterval:timeoutInSeconds -- If during a connection attempt the request remains idle for longer than the timeout interval, the request is considered to have timed out.
lns3.scpt
if requestHeaders is not missing value then
lns3.scpt
repeat with aRef in _support's asListParameter(requestHeaders, "headers")
lns3.scpt
set headerRecord to aRef's contents as record
lns3.scpt
if (count headerRecord each text) ≠ 2 then error number -1703 -- note: requiring text values here avoids any risk of numbers being accidentally coerced to inappropriate localized representations (e.g. "1,0" instead of "1.0"); user should use Number library's `format number` command to convert numbers to non-localized format first
lns3.scpt
set {headerName, headerValue} to {headerRecord's headerName, headerRecord's headerValue}
lns3.scpt
on error eText number eNum from eFrom to eTo
lns3.scpt
if {eNum} is not in {-1700, -1703, -1728} then error eText number eNum from eFrom to eTo
lns3.scpt
_support's throwInvalidParameter(aRef, "headers", list, "Not a list of valid HTTP header records.")
lns3.scpt
if headerName is in _excludeHeaders then
lns3.scpt
_support's throwInvalidParameter(aRef, "headers", list, "The following header is not allowed in the headers list as it is already set automatically: " & headerName)
lns3.scpt
(httpRequest's setValue:headerValue forHTTPHeaderField:headerName) -- TO DO: does NSHTTPRequest sanitize these inputs; if not, how should they be treated here?
lns3.scpt
if requestBody is not missing value then -- requestBody may be supplied as text or NSData -- TO DO: what about file object?
lns3.scpt
if (count {requestBody} each text) = 1 then -- given a text value to use as the request body, encode it using rgw xhEAWR specified by the user-supplied Content-Type, if any,
lns3.scpt
set contentType to httpRequest's valueForHTTPHeaderField:"Content-Type"
lns3.scpt
if contentType is missing value then -- encode text-based body as UTF8 by default
lns3.scpt
httpRequest's setValue:"text/plain;charset=utf-8" forHTTPHeaderField:"Content-Type"
lns3.scpt
set requestBodyEncoding to NSUTF8StringEncoding
lns3.scpt
else -- user has specified Content-Type for body (e.g. "application/json;charset=utf8")
lns3.scpt
set charsetName to _parseCharset(_support's asNSString(contentType))
lns3.scpt
if charsetName is missing value then -- if Content-Type doesn't include charset parameter then use UTF8 by default
lns3.scpt
httpRequest's setValue:(contentType & ";charset=utf-8") forHTTPHeaderField:"Content-Type"
lns3.scpt
else -- automatically encode the body text using the specified charset (assuming it's one directly recognized by NSString's dataUsingEncoding: method)
lns3.scpt
set requestBodyEncoding to _NSStringEncodings's getEncoding(charsetName)
lns3.scpt
if requestBodyEncoding is missing value then
lns3.scpt
_support's throwInvalidParameter(requestHeaders, "headers", list, "The Content-Type header specifies a charset that cannot be automatically encoded by this handler (e.g. use a different charset or supply the request body as an NSData object instead).")
lns3.scpt
set bodyData to _support's asNSString(requestBody)'s dataUsingEncoding:requestBodyEncoding
lns3.scpt
if bodyData is missing value then
lns3.scpt
_support's throwInvalidParameter(requestHeaders, "body", {text, «class ocid»}, "Can’t encode the given text using the charset specified by the Content-Type header: " & contentType)
lns3.scpt
httpRequest's setHTTPBody:bodyData
lns3.scpt
else if _support's checkTypeForValue(requestBody, «class ocid») and (requestBody's isInstanceOfType:(current application's NSData's |class|())) then
lns3.scpt
httpRequest's setHTTPBody:requestBody
lns3.scpt
if httpRequest's valueForHTTPHeaderField:("Content-Type" is missing value) then
lns3.scpt
httpRequest's setValue:"application/octet-stream" forHTTPHeaderField:"Content-Type" -- arbitrary binary data
lns3.scpt
_support's throwInvalidParameter(requestBody, "body", {text, «class ocid»}, "Not a text or NSData object.")
lns3.scpt
return httpRequest
lns3.scpt
end _makeHTTPRequest
lns3.scpt
to _unpackHTTPResponse(httpResponse, responseBodyType, responseBodyData)
lns3.scpt
set headerFields to {}
lns3.scpt
set asocHeaderFields to httpResponse's allHeaderFields()
lns3.scpt
set headerKeys to asocHeaderFields's allKeys()
lns3.scpt
repeat with i from 0 to (asocHeaderFields's |count|()) - 1
lns3.scpt
set asocKey to (headerKeys's objectAtIndex:i)
lns3.scpt
set end of headerFields to {headerName:asocKey as text, headerValue:(asocHeaderFields's objectForKey:asocKey) as any}
lns3.scpt
if responseBodyType is text then
lns3.scpt
set responseBodyEncoding to missing value
lns3.scpt
set asocContentType to asocHeaderFields's objectForKey:"Content-Type"
lns3.scpt
if asocContentType is not missing value then set responseBodyEncoding to _NSStringEncodings's getEncoding(_parseCharset(asocContentType))
lns3.scpt
if responseBodyEncoding is missing value then
lns3.scpt
error "Can't automatically decode HTTP response as text as it didn't specify a Content-Type with a supported charset." number -1700
lns3.scpt
set responseBody to (current application's NSString's alloc()'s initWithData:responseBodyData encoding:responseBodyEncoding) as text
lns3.scpt
else if responseBodyType is data then -- return NSData
lns3.scpt
set responseBody to responseBodyData's |copy|()
lns3.scpt
else if responseBodyType is missing value then
lns3.scpt
set responseBody to missing value
lns3.scpt
_support's throwInvalidConstantParameter(responseBodyType, "returning")
lns3.scpt
return {statusCode:httpResponse's statusCode(), responseHeaders:headerFields, responseBody:responseBody}
lns3.scpt
end _unpackHTTPResponse
lns3.scpt
to send HTTP request to theURL method httpMethod : ("GET") headers requestHeaders : {} body requestBody : (missing value) timeout timeoutInSeconds : (missing value) returning responseBodyType : (text) -- TO DO: make URL direct param (can't think of any situation where it'd be a script object)
lns3.scpt
if timeoutInSeconds is not missing value then
lns3.scpt
set timeoutInSeconds to _support's asIntegerParameter(timeoutInSeconds, "timeout")
lns3.scpt
set httpRequest to _makeHTTPRequest(theURL, httpMethod, requestHeaders, requestBody, timeoutInSeconds)
lns3.scpt
set sessionConfig to current application's NSURLSessionConfiguration's ephemeralSessionConfiguration()
lns3.scpt
if timeoutInSeconds is not missing value then sessionConfig's setTimeoutIntervalForRequest:timeoutInSeconds -- controls how long (in seconds) a task should wait for additional data to arrive before giving up
lns3.scpt
if responseBodyType is missing value then
lns3.scpt
set asocSession to current application's NSURLSession's sessionWithConfiguration:sessionConfig
lns3.scpt
set responseBodyData to missing value
lns3.scpt
set responseBodyData to current application's NSMutableData's |data|()
lns3.scpt
script sessionTaskDelegate
lns3.scpt
on URLSession:asocSession dataTask:asocTask didReceiveData:asocData
lns3.scpt
responseBodyData's appendData:asocData
lns3.scpt
end URLSession:dataTask:didReceiveData:
lns3.scpt
set asocSession to current application's NSURLSession's sessionWithConfiguration:sessionConfig delegate:sessionTaskDelegate delegateQueue:(missing value)
lns3.scpt
set asocTask to asocSession's dataTaskWithRequest:httpRequest
lns3.scpt
asocTask's resume()
lns3.scpt
repeat while asocTask's state() is current application's NSURLSessionTaskStateRunning
lns3.scpt
current application's NSThread's sleepForTimeInterval:0.1 -- sleep for 0.1sec
lns3.scpt
if asocTask's state() is current application's NSURLSessionTaskStateSuspended then -- sanity check (shouldn't be possible for task to be suspended)
lns3.scpt
asocTask's cancel()
lns3.scpt
if asocTask's state() is current application's NSURLSessionTaskStateCanceling then -- TO DO: does Cmd-period cancel task?
lns3.scpt
set taskError to asocTask's |error|()
lns3.scpt
if taskError is not missing value then
lns3.scpt
error (taskError's localizedDescription() as text) number taskError's code() from theURL
lns3.scpt
return _unpackHTTPResponse(asocTask's response(), responseBodyType, responseBodyData)
lns3.scpt
_error("send HTTP request", eText, eNumber, eFrom, eTo)
lns3.scpt
end send HTTP request
lns3.scpt