The library's homepage: https://kristaps.bsd.lv/kcgi/
kcgi.bi
Code: Select all
#pragma once
extern "C"
const KCGI_VMAJOR = 0
const KCGI_VMINOR = 13
const KCGI_VBUILD = 0
#define KCGI_VERSION 0.13.0
const KCGI_VSTAMP = ((KCGI_VBUILD + 1) + ((KCGI_VMINOR + 1) * 100)) + ((KCGI_VMAJOR + 1) * 10000)
type khttp as long
enum
KHTTP_100
KHTTP_101
KHTTP_103
KHTTP_200
KHTTP_201
KHTTP_202
KHTTP_203
KHTTP_204
KHTTP_205
KHTTP_206
KHTTP_207
KHTTP_300
KHTTP_301
KHTTP_302
KHTTP_303
KHTTP_304
KHTTP_306
KHTTP_307
KHTTP_308
KHTTP_400
KHTTP_401
KHTTP_402
KHTTP_403
KHTTP_404
KHTTP_405
KHTTP_406
KHTTP_407
KHTTP_408
KHTTP_409
KHTTP_410
KHTTP_411
KHTTP_412
KHTTP_413
KHTTP_414
KHTTP_415
KHTTP_416
KHTTP_417
KHTTP_424
KHTTP_428
KHTTP_429
KHTTP_431
KHTTP_500
KHTTP_501
KHTTP_502
KHTTP_503
KHTTP_504
KHTTP_505
KHTTP_507
KHTTP_511
KHTTP__MAX
end enum
type krequ as long
enum
KREQU_ACCEPT
KREQU_ACCEPT_CHARSET
KREQU_ACCEPT_ENCODING
KREQU_ACCEPT_LANGUAGE
KREQU_AUTHORIZATION
KREQU_DEPTH
KREQU_FROM
KREQU_HOST
KREQU_IF
KREQU_IF_MODIFIED_SINCE
KREQU_IF_MATCH
KREQU_IF_NONE_MATCH
KREQU_IF_RANGE
KREQU_IF_UNMODIFIED_SINCE
KREQU_MAX_FORWARDS
KREQU_PROXY_AUTHORIZATION
KREQU_RANGE
KREQU_REFERER
KREQU_USER_AGENT
KREQU__MAX
end enum
type kresp as long
enum
KRESP_ACCESS_CONTROL_ALLOW_ORIGIN
KRESP_ACCEPT_RANGES
KRESP_AGE
KRESP_ALLOW
KRESP_CACHE_CONTROL
KRESP_CONNECTION
KRESP_CONTENT_ENCODING
KRESP_CONTENT_LANGUAGE
KRESP_CONTENT_LENGTH
KRESP_CONTENT_LOCATION
KRESP_CONTENT_MD5
KRESP_CONTENT_DISPOSITION
KRESP_CONTENT_RANGE
KRESP_CONTENT_TYPE
KRESP_DATE
KRESP_ETAG
KRESP_EXPIRES
KRESP_LAST_MODIFIED
KRESP_LINK
KRESP_LOCATION
KRESP_P3P
KRESP_PRAGMA
KRESP_PROXY_AUTHENTICATE
KRESP_REFRESH
KRESP_RETRY_AFTER
KRESP_SERVER
KRESP_SET_COOKIE
KRESP_STATUS
KRESP_STRICT_TRANSPORT_SECURITY
KRESP_TRAILER
KRESP_TRANSFER_ENCODING
KRESP_UPGRADE
KRESP_VARY
KRESP_VIA
KRESP_WARNING
KRESP_WWW_AUTHENTICATE
KRESP_X_FRAME_OPTIONS
KRESP__MAX
end enum
type kattrx as long
enum
KATTRX_STRING
KATTRX_INT
KATTRX_DOUBLE
end enum
type kmethod as long
enum
KMETHOD_ACL
KMETHOD_CONNECT
KMETHOD_COPY
KMETHOD_DELETE
KMETHOD_GET
KMETHOD_HEAD
KMETHOD_LOCK
KMETHOD_MKCALENDAR
KMETHOD_MKCOL
KMETHOD_MOVE
KMETHOD_OPTIONS
KMETHOD_POST
KMETHOD_PROPFIND
KMETHOD_PROPPATCH
KMETHOD_PUT
KMETHOD_REPORT
KMETHOD_TRACE
KMETHOD_UNLOCK
KMETHOD__MAX
end enum
type kpairtype as long
enum
KPAIR_INTEGER
KPAIR_STRING
KPAIR_DOUBLE
KPAIR__MAX
end enum
type kscheme as long
enum
KSCHEME_AAA
KSCHEME_AAAS
KSCHEME_ABOUT
KSCHEME_ACAP
KSCHEME_ACCT
KSCHEME_CAP
KSCHEME_CID
KSCHEME_COAP
KSCHEME_COAPS
KSCHEME_CRID
KSCHEME_DATA
KSCHEME_DAV
KSCHEME_DICT
KSCHEME_DNS
KSCHEME_FILE
KSCHEME_FTP
KSCHEME_GEO
KSCHEME_GO
KSCHEME_GOPHER
KSCHEME_H323
KSCHEME_HTTP
KSCHEME_HTTPS
KSCHEME_IAX
KSCHEME_ICAP
KSCHEME_IM
KSCHEME_IMAP
KSCHEME_INFO
KSCHEME_IPP
KSCHEME_IRIS
KSCHEME_IRIS_BEEP
KSCHEME_IRIS_XPC
KSCHEME_IRIS_XPCS
KSCHEME_IRIS_LWZ
KSCHEME_JABBER
KSCHEME_LDAP
KSCHEME_MAILTO
KSCHEME_MID
KSCHEME_MSRP
KSCHEME_MSRPS
KSCHEME_MTQP
KSCHEME_MUPDATE
KSCHEME_NEWS
KSCHEME_NFS
KSCHEME_NI
KSCHEME_NIH
KSCHEME_NNTP
KSCHEME_OPAQUELOCKTOKEN
KSCHEME_POP
KSCHEME_PRES
KSCHEME_RELOAD
KSCHEME_RTSP
KSCHEME_RTSPS
KSCHEME_RTSPU
KSCHEME_SERVICE
KSCHEME_SESSION
KSCHEME_SHTTP
KSCHEME_SIEVE
KSCHEME_SIP
KSCHEME_SIPS
KSCHEME_SMS
KSCHEME_SNMP
KSCHEME_SOAP_BEEP
KSCHEME_SOAP_BEEPS
KSCHEME_STUN
KSCHEME_STUNS
KSCHEME_TAG
KSCHEME_TEL
KSCHEME_TELNET
KSCHEME_TFTP
KSCHEME_THISMESSAGE
KSCHEME_TN3270
KSCHEME_TIP
KSCHEME_TURN
KSCHEME_TURNS
KSCHEME_TV
KSCHEME_URN
KSCHEME_VEMMI
KSCHEME_WS
KSCHEME_WSS
KSCHEME_XCON
KSCHEME_XCON_USERID
KSCHEME_XMLRPC_BEEP
KSCHEME_XMLRPC_BEEPS
KSCHEME_XMPP
KSCHEME_Z39_50R
KSCHEME_Z39_50S
KSCHEME__MAX
end enum
type kmime as long
enum
KMIME_APP_JAVASCRIPT
KMIME_APP_JSON
KMIME_APP_OCTET_STREAM
KMIME_APP_PDF
KMIME_APP_XML
KMIME_APP_ZIP
KMIME_IMAGE_GIF
KMIME_IMAGE_JPEG
KMIME_IMAGE_PNG
KMIME_IMAGE_SVG_XML
KMIME_TEXT_CALENDAR
KMIME_TEXT_CSS
KMIME_TEXT_CSV
KMIME_TEXT_HTML
KMIME_TEXT_PLAIN
KMIME_TEXT_XML
KMIME__MAX
end enum
type kmimemap
name_ as const zstring ptr
mime as uinteger
end type
type kpairstate as long
enum
KPAIR_UNCHECKED = 0
KPAIR_VALID
KPAIR_INVALID
end enum
const KREQ_DEBUG_WRITE = &h01
const KREQ_DEBUG_READ_BODY = &h02
union parsed
i as longint
s as const zstring ptr
d as double
end union
type kpair
key as zstring ptr
keypos as uinteger
val_ as zstring ptr
valsz as uinteger
file as zstring ptr
ctype as zstring ptr
ctypepos as uinteger
xcode as zstring ptr
next_ as kpair ptr
state as kpairstate
type_ as kpairtype
parsed as parsed
end type
type kvalid
valid as function(byval kp as kpair ptr) as long
name_ as const zstring ptr
end type
type kauth as long
enum
KAUTH_NONE = 0
KAUTH_BASIC
KAUTH_DIGEST
KAUTH_BEARER
KAUTH_UNKNOWN
end enum
type khttpalg as long
enum
KHTTPALG_MD5 = 0
KHTTPALG_MD5_SESS
KHTTPALG__MAX
end enum
type khttpqop as long
enum
KHTTPQOP_NONE = 0
KHTTPQOP_AUTH
KHTTPQOP_AUTH_INT
KHTTPQOP__MAX
end enum
type khttpdigest
alg as khttpalg
qop as khttpqop
user as zstring ptr
uri as zstring ptr
realm as zstring ptr
nonce as zstring ptr
cnonce as zstring ptr
response as zstring ptr
count as ulong
opaque as zstring ptr
end type
type khttpbasic
response as zstring ptr
end type
union khttpauth_d
digest as khttpdigest
basic as khttpbasic
end union
type khttpauth
type_ as kauth
authorised as long
digest as zstring ptr
d as khttpauth_d
end type
type khead
key as zstring ptr
val_ as zstring ptr
end type
type kreq
reqmap(0 to KREQU__MAX - 1) as khead ptr
reqs as khead ptr
reqsz as uinteger
method as kmethod
auth as kauth
rawauth as khttpauth
cookies as kpair ptr
cookiesz as uinteger
cookiemap as kpair ptr ptr
cookienmap as kpair ptr ptr
fields as kpair ptr
fieldmap as kpair ptr ptr
fieldnmap as kpair ptr ptr
fieldsz as uinteger
mime as uinteger
page as uinteger
scheme as kscheme
path as zstring ptr
suffix as zstring ptr
fullpath as zstring ptr
pagename as zstring ptr
remote as zstring ptr
host as zstring ptr
port as ushort
kdata as kdata ptr
keys as const kvalid ptr
keysz as uinteger
pname as zstring ptr
arg as any ptr
end type
type kopts
sndbufsz as integer
end type
type kcgi_buf
buf as zstring ptr
maxsz as uinteger
sz as uinteger
growsz as uinteger
end type
type ktemplate
key as const zstring const ptr ptr
keysz as uinteger
arg as any ptr
cb as function(byval as uinteger, byval as any ptr) as long
end type
type kcgi_err as long
enum
KCGI_OK = 0
KCGI_ENOMEM
KCGI_EXIT
KCGI_HUP
KCGI_ENFILE
KCGI_EAGAIN
KCGI_FORM
KCGI_SYSTEM
KCGI_WRITER
end enum
type ktemplate_writef as function(byval as const zstring ptr, byval as uinteger, byval as any ptr) as kcgi_err
type ktemplatex
writer as ktemplate_writef
fbk as function(byval as const zstring ptr, byval as uinteger, byval as any ptr) as long
end type
declare function kcgi_strerror(byval as kcgi_err) as const zstring ptr
declare function khttp_body(byval as kreq ptr) as kcgi_err
declare function khttp_body_compress(byval as kreq ptr, byval as long) as kcgi_err
declare sub khttp_free(byval as kreq ptr)
declare sub khttp_child_free(byval as kreq ptr)
declare function khttp_head(byval as kreq ptr, byval as const zstring ptr, byval as const zstring ptr, ...) as kcgi_err
declare function khttp_parse(byval as kreq ptr, byval as const kvalid ptr, byval as uinteger, byval as const zstring const ptr ptr, byval as uinteger, byval as uinteger) as kcgi_err
declare function khttp_parsex(byval as kreq ptr, byval as const kmimemap ptr, byval as const zstring const ptr ptr, byval as uinteger, byval as const kvalid ptr, byval as uinteger, byval as const zstring const ptr ptr, byval as uinteger, byval as uinteger, byval as uinteger, byval as any ptr, byval as sub(byval as any ptr), byval as ulong, byval as const kopts ptr) as kcgi_err
declare function khttp_printf(byval as kreq ptr, byval as const zstring ptr, ...) as kcgi_err
declare function khttp_putc(byval as kreq ptr, byval as long) as kcgi_err
declare function khttp_puts(byval as kreq ptr, byval as const zstring ptr) as kcgi_err
declare function khttp_template(byval as kreq ptr, byval as const ktemplate ptr, byval as const zstring ptr) as kcgi_err
declare function khttp_template_fd(byval as kreq ptr, byval as const ktemplate ptr, byval as long, byval as const zstring ptr) as kcgi_err
declare function khttp_template_buf(byval as kreq ptr, byval as const ktemplate ptr, byval as const zstring ptr, byval as uinteger) as kcgi_err
declare function khttp_templatex(byval as const ktemplate ptr, byval as const zstring ptr, byval as const ktemplatex ptr, byval as any ptr) as kcgi_err
declare function khttp_templatex_buf(byval as const ktemplate ptr, byval as const zstring ptr, byval as uinteger, byval as const ktemplatex ptr, byval as any ptr) as kcgi_err
declare function khttp_templatex_fd(byval as const ktemplate ptr, byval as long, byval as const zstring ptr, byval as const ktemplatex ptr, byval as any ptr) as kcgi_err
declare function khttp_write(byval as kreq ptr, byval as const zstring ptr, byval as uinteger) as kcgi_err
declare function kcgi_buf_printf(byval as kcgi_buf ptr, byval as const zstring ptr, ...) as kcgi_err
declare function kcgi_buf_putc(byval as kcgi_buf ptr, byval as byte) as kcgi_err
declare function kcgi_buf_puts(byval as kcgi_buf ptr, byval as const zstring ptr) as kcgi_err
declare function kcgi_buf_write(byval as const zstring ptr, byval as uinteger, byval as any ptr) as kcgi_err
declare function khttpdigest_validate(byval as const kreq ptr, byval as const zstring ptr) as long
declare function khttpdigest_validatehash(byval as const kreq ptr, byval as const zstring ptr) as long
declare function khttpbasic_validate(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr) as long
declare function kvalid_bit(byval as kpair ptr) as long
declare function kvalid_date(byval as kpair ptr) as long
declare function kvalid_double(byval as kpair ptr) as long
declare function kvalid_email(byval as kpair ptr) as long
declare function kvalid_int(byval as kpair ptr) as long
declare function kvalid_string(byval as kpair ptr) as long
declare function kvalid_stringne(byval as kpair ptr) as long
declare function kvalid_udouble(byval as kpair ptr) as long
declare function kvalid_uint(byval as kpair ptr) as long
declare sub kcgi_writer_disable(byval as kreq ptr)
declare function khttp_fcgi_parse(byval as kfcgi ptr, byval as kreq ptr) as kcgi_err
declare function khttp_fcgi_init(byval as kfcgi ptr ptr, byval as const kvalid ptr, byval as uinteger, byval as const zstring const ptr ptr, byval as uinteger, byval as uinteger) as kcgi_err
declare function khttp_fcgi_initx(byval as kfcgi ptr ptr, byval as const zstring const ptr ptr, byval as uinteger, byval as const kvalid ptr, byval as uinteger, byval as const kmimemap ptr, byval as uinteger, byval as const zstring const ptr ptr, byval as uinteger, byval as uinteger, byval as any ptr, byval as sub(byval as any ptr), byval as ulong, byval as const kopts ptr) as kcgi_err
declare function khttp_fcgi_free(byval as kfcgi ptr) as kcgi_err
declare sub khttp_fcgi_child_free(byval as kfcgi ptr)
declare function khttp_fcgi_test() as long
declare sub khttp_epoch2datetime(byval as longint, byval as longint ptr, byval as longint ptr, byval as longint ptr, byval as longint ptr, byval as longint ptr, byval as longint ptr, byval as longint ptr, byval as longint ptr)
declare function khttp_epoch2str(byval as longint, byval as zstring ptr, byval as uinteger) as zstring ptr
declare function khttp_epoch2ustr(byval as longint, byval as zstring ptr, byval as uinteger) as zstring ptr
declare function khttp_epoch2tms(byval as longint, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr) as long
#define KHTTP_EPOCH2TM(_tt, _tm) khttp_epoch2tms((_tt), @(_tm)->tm_sec, @(_tm)->tm_min, @(_tm)->tm_hour, @(_tm)->tm_mday, @(_tm)->tm_mon, @(_tm)->tm_year, @(_tm)->tm_wday, @(_tm)->tm_yday)
declare function khttp_datetime2epoch(byval as longint ptr, byval as longint, byval as longint, byval as longint, byval as longint, byval as longint, byval as longint) as long
declare function khttp_date2epoch(byval as longint ptr, byval as longint, byval as longint, byval as longint) as long
declare function khttp_urlabs(byval as kscheme, byval as const zstring ptr, byval as ushort, byval as const zstring ptr, ...) as zstring ptr
declare function khttp_urldecode(byval as const zstring ptr, byval as zstring ptr ptr) as kcgi_err
declare function khttp_urldecode_inplace(byval as zstring ptr) as kcgi_err
declare function khttp_urlencode(byval as const zstring ptr) as zstring ptr
declare function khttp_urlpart(byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, ...) as zstring ptr
declare function khttp_urlpartx(byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, ...) as zstring ptr
declare function khttp_vurlabs(byval as kscheme, byval as const zstring ptr, byval as ushort, byval as const zstring ptr, byval as va_list) as zstring ptr
declare function khttp_vurlpart(byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list) as zstring ptr
declare function khttp_vurlpartx(byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list) as zstring ptr
declare sub kutil_invalidate(byval as kreq ptr, byval as kpair ptr)
declare function kutil_openlog(byval as const zstring ptr) as long
declare sub kutil_verr(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_verrx(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_vinfo(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_vlog(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_vlogx(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_vwarn(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_vwarnx(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as va_list)
declare sub kutil_log(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare sub kutil_logx(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare sub kutil_info(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare sub kutil_warn(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare sub kutil_warnx(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare sub kutil_err(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare sub kutil_errx(byval as const kreq ptr, byval as const zstring ptr, byval as const zstring ptr, ...)
declare function kasprintf(byval as zstring ptr ptr, byval as const zstring ptr, ...) as long
declare function kvasprintf(byval as zstring ptr ptr, byval as const zstring ptr, byval as va_list) as long
declare function kcalloc(byval as uinteger, byval as uinteger) as any ptr
declare function kmalloc(byval as uinteger) as any ptr
declare function krealloc(byval as any ptr, byval as uinteger) as any ptr
declare function kreallocarray(byval as any ptr, byval as uinteger, byval as uinteger) as any ptr
declare function kstrdup(byval as const zstring ptr) as zstring ptr
extern kmimetypes(0 to KMIME__MAX - 1) as const zstring const ptr
extern khttps(0 to KHTTP__MAX - 1) as const zstring const ptr
extern kschemes(0 to KSCHEME__MAX - 1) as const zstring const ptr
extern kresps(0 to KRESP__MAX - 1) as const zstring const ptr
extern kmethods(0 to KMETHOD__MAX - 1) as const zstring const ptr
extern ksuffixmap(0 to ...) as const kmimemap
extern ksuffixes(0 to KMIME__MAX - 1) as const zstring const ptr
#define KUTIL_EPOCH2TM(_tt, _tm) kutil_epoch2tmvals((_tt), @(_tm)->tm_sec, @(_tm)->tm_min, @(_tm)->tm_hour, @(_tm)->tm_mday, @(_tm)->tm_mon, @(_tm)->tm_year, @(_tm)->tm_wday, @(_tm)->tm_yday)
declare sub kutil_epoch2tmvals(byval as longint, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr, byval as long ptr)
declare function kutil_epoch2str(byval as longint, byval as zstring ptr, byval as uinteger) as zstring ptr
declare function kutil_epoch2utcstr(byval as longint, byval as zstring ptr, byval as uinteger) as zstring ptr
declare function kutil_date2epoch(byval as longint, byval as longint, byval as longint) as longint
declare function kutil_date_check(byval as longint, byval as longint, byval as longint) as long
declare function kutil_datetime2epoch(byval as longint, byval as longint, byval as longint, byval as longint, byval as longint, byval as longint) as longint
declare function kutil_datetime_check(byval as longint, byval as longint, byval as longint, byval as longint, byval as longint, byval as longint) as long
declare function kutil_urlabs(byval as kscheme, byval as const zstring ptr, byval as ushort, byval as const zstring ptr) as zstring ptr
declare function kutil_urldecode(byval as const zstring ptr, byval as zstring ptr ptr) as kcgi_err
declare function kutil_urldecode_inplace(byval as zstring ptr) as kcgi_err
declare function kutil_urlencode(byval as const zstring ptr) as zstring ptr
declare function kutil_urlpart(byval as kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, ...) as zstring ptr
declare function kutil_urlpartx(byval as kreq ptr, byval as const zstring ptr, byval as const zstring ptr, byval as const zstring ptr, ...) as zstring ptr
end extern
I removed all of the unneeded #defines and hardcoded KCGI_VERSION to 0.13.0
It needs a web server and I don't know how to setup one so I can't test it.
This is the example on their homepage translated to FreeBASIC (don't know if it's correct or not):
Code: Select all
#include once "kcgi.bi"
#define NULL 0
private function main() as long
dim r as kreq
dim page as const zstring ptr = "index"
if khttp_parse(@r, NULL, 0, @page, 1, 0) <> KCGI_OK then
return 1
end if
khttp_head(@r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200])
khttp_head(@r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_PLAIN])
khttp_body(@r)
khttp_puts(@r, "Hello, world!")
khttp_free(@r)
return 0
end function
main()