Initial commit: GET requests

This commit is contained in:
Dimitri Lozeve 2023-10-01 17:01:26 +02:00
commit 9bb42177d8
5 changed files with 193 additions and 0 deletions

1
config.bqn Normal file
View file

@ -0,0 +1 @@
libcurlPath"/usr/lib/x86_64-linux-gnu/libcurl.so"

74
curl.bqn Normal file
View file

@ -0,0 +1,74 @@
Get
easyInit,
easyCleanup,
easySetoptStr,
easySetoptPtr,
easySetoptLong,
easyPerform,
easyGetinfoStr,
easyGetinfoLong,
easyGetinfoDouble,
slistAppend,
slistFreeAll,
fopen,
fclose,
options,
info
•Import"ffi.bqn"
RandID•Import"utils.bqn"
Get{
𝕊 url: 𝕊 url ;
headers 𝕊 url:
idRandID 32
filename"/tmp/bqncurl."id
filePtrFopenfilename@,"w+"
headerFilename"/tmp/bqncurl.header."id
headerFilePtrFopenheaderFilename@,"w+"
sessionEasyInit
ruseragentEasySetoptStrsession,options.useragent,"curl/bqn"@
("Error setting user agent: "•Fmt ruseragent)!ruseragent=0
rurlEasySetoptStrsession,options.url,url@
("Error setting URL: "•Fmt rurl)!rurl=0
rfileEasySetoptPtrsession,options.writedata,filePtr
("Error setting file target: "•Fmt rfile)!rfile=0
rheaderfileEasySetoptPtrsession,options.headerdata,headerFilePtr
("Error setting header file target: "•Fmt rheaderfile)!rheaderfile=0
rredirectEasySetoptLongsession,options.followlocation,1
("Error setting redirect option: "•Fmt rredirect)!rredirect=0
# EasySetoptLong⟨session,options.header,1⟩
# EasySetoptLong⟨session,options.verbose,1⟩
slist(80){SlistAppend𝕩,𝕨@}´headers
rheadersEasySetoptPtrsession,options.httpHeader,slist
("Error setting headers: "•Fmt rheaders)!rheaders=0
rperformEasyPerformsession
("Error performing request: "•Fmt rperform)!rperform=0
SlistFreeAll slist
rcodecodeEasyGetinfoLongsession,info.responseCode,0
("Error retrieving response code: "•Fmt rcode)!rcode=0
# ("Request failed with status code "∾•Fmt code)!2=⌊code÷100
rtimetimeEasyGetinfoDoublesession,info.totalTime,0.0
("Error retrieving request time: "•Fmt rtime)!rtime=0
Fclose filePtr
Fclose headerFilePtr
content•file.Bytes filename
responseHeaders•file.Chars headerFilename
•file.Remove filename
EasyCleanup session
{
codecode,
timetime,
headersresponseHeaders,
contentcontent,
}
}

104
ffi.bqn Normal file
View file

@ -0,0 +1,104 @@
easyInit,
easyCleanup,
easySetoptStr,
easySetoptPtr,
easySetoptLong,
easyPerform,
easyGetinfoStr,
easyGetinfoLong,
easyGetinfoDouble,
slistAppend,
slistFreeAll,
fopen,
fclose,
options,
info
Hex2Num•Import"utils.bqn"
libcurlPath•Import"config.bqn"
CurlFFIlibcurlPath•FFI
curlPtr"*:i8"
curlOption"i32"
curlCode"i32"
easyInitCurlFFI curlPtr"curl_easy_init"
easyCleanupCurlFFI """curl_easy_cleanup"(">"curlPtr)
easySetoptStrCurlFFI curlCode"curl_easy_setopt"curlPtrcurlOption"*u8:c8"
easySetoptPtrCurlFFI curlCode"curl_easy_setopt"curlPtrcurlOption"*:i8"
easySetoptLongCurlFFI curlCode"curl_easy_setopt"curlPtrcurlOption"i32"
easyPerformCurlFFI curlCode"curl_easy_perform"curlPtr
easyGetinfoStrCurlFFI curlCode"curl_easy_getinfo"curlPtrcurlOption"&u8:c8"
easyGetinfoLongCurlFFI curlCode"curl_easy_getinfo"curlPtrcurlOption"&i32"
easyGetinfoDoubleCurlFFI curlCode"curl_easy_getinfo"curlPtrcurlOption"&f64"
slistAppendCurlFFI "*:i8""curl_slist_append""*:i8""*u8:c8"
slistFreeAllCurlFFI """curl_slist_free_all"">*:i8"
fopen@•FFI "*:i8""fopen""*u8:c8""*u8:c8"
fclose@•FFI """fclose"">*:i8"
options{
strOffset10000
slistOffset10000
ptrOffset10000
# boolean options
verbose41
header42
noprogress43
post47
followlocation52
httpget80
# long integer options
timeout78 # timeout in seconds
timeoutms155 # timeout in milliseconds
# string options, null-terminated
urlstrOffset+2
userpwdstrOffset+5 # user:password
useragentstrOffset+18
# pointer options
writedataptrOffset+1
headerdataptrOffset+29
# linked list options
httpHeaderslistOffset+23
}
info{
strOffsetHex2Num"100000"
longOffsetHex2Num"200000"
doubleOffsetHex2Num"300000"
slistOffsetHex2Num"400000"
ptrOffsetHex2Num"400000"
# long integer info
responseCodelongOffset+2
headerSizelongOffset+11 # in bytes
requestSizelongOffset+12 # in bytes
redirectCountlongOffset+20
httpVersionlongOffset+46
# double info
totalTimedoubleOffset+3 # in seconds
nameLookupTimedoubleOffset+4 # in seconds
connectTimedoubleOffset+5 # in seconds
# string info
effectiveUrlstrOffset+1
contentTypestrOffset+18
primaryIPstrOffset+32
httpSchemestrOffset+49
effectiveMethodstrOffset+58
}

7
test.bqn Normal file
View file

@ -0,0 +1,7 @@
Get•Import"curl.bqn"
r"Hello: World","Blabla: Blublu"Get"http://httpbin.org/get?key=value"
r.code
r.headers
r.content
r.time

7
utils.bqn Normal file
View file

@ -0,0 +1,7 @@
Hex2Num,RandID
# Parse hexadecimal number
Hex2Num{16×+˜´("0A"+¨106)𝕩}
# random alphanumeric ID of 𝕩 characters
RandId{(𝕩•rand.Range 62)"0aA"+¨¨102626}