goahead.h 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207
  1. /*
  2. goahead.h -- GoAhead Web Server Header
  3. Copyright (c) All Rights Reserved. See details at the end of the file.
  4. */
  5. #ifndef _h_GOAHEAD
  6. #define _h_GOAHEAD 1
  7. /************************************ Overrides *******************************/
  8. /*
  9. Override osdep defaults
  10. */
  11. #define ME_MAX_IP 64 /**< Maximum IP address size */
  12. /************************************ Includes ********************************/
  13. #include "me.h"
  14. #include "osdep.h"
  15. #include "libipmi_struct.h"
  16. /************************************ Defaults ********************************/
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**************** jimbo modify ****************/
  21. #define IPv4_ADDR0 192
  22. #define IPv4_ADDR1 168
  23. #define IPv4_ADDR2 0
  24. #define IPv4_ADDR3 203
  25. #define ME_COMPILER_HAS_SINGLE_STACK 1
  26. /*************** jimbo end *******************/
  27. #if (ME_COM_MBEDTLS + ME_COM_MATRIXSSL + ME_COM_NANOSSL + ME_COM_OPENSSL) > 1
  28. #error "Cannot have more than one SSL provider configured"
  29. #endif
  30. #ifndef ME_GOAHEAD_LOGGING
  31. #define ME_GOAHEAD_LOGGING 1 /**< Default for logging is "on" */
  32. #endif
  33. #ifndef ME_GOAHEAD_TRACING
  34. #define ME_GOAHEAD_TRACING 1 /**< Default for tracing "on" */
  35. #endif
  36. #ifndef ME_GOAHEAD_DEBUG
  37. #if ME_DEBUG
  38. #define ME_GOAHEAD_DEBUG 1 /**< Debug logging on in debug builds by default */
  39. #else
  40. #define ME_GOAHEAD_DEBUG 0
  41. #endif
  42. #endif
  43. #if ECOS
  44. #if ME_GOAHEAD_CGI
  45. #error "Ecos does not support CGI. Disable ME_GOAHEAD_CGI"
  46. #endif
  47. #endif /* ECOS */
  48. #if QNX
  49. typedef long fd_mask;
  50. #define NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */
  51. #endif
  52. #if MACOSX
  53. typedef int32_t fd_mask;
  54. #endif
  55. #if WINDOWS
  56. typedef fd_set fd_mask;
  57. #endif
  58. #if !LINUX
  59. PUBLIC char *basename(char *name);
  60. #endif
  61. #if VXWORKS
  62. PUBLIC int vxchdir(char *dirname);
  63. #endif
  64. #if DOXYGEN
  65. typedef int Socket;
  66. typedef int Socklen;
  67. typedef int64 Offset;
  68. #endif
  69. /**
  70. File status structure
  71. */
  72. typedef struct stat WebsStat;
  73. /*
  74. Copyright. The software license requires that this not be modified or removed.
  75. */
  76. #define EMBEDTHIS_GOAHEAD_COPYRIGHT \
  77. "Copyright (c) Embedthis Software Inc., 1993-2014. All Rights Reserved." \
  78. "Copyright (c) GoAhead Software Inc., 2003. All Rights Reserved."
  79. /************************************* Main ***********************************/
  80. #define ME_MAX_ARGC 32
  81. #if VXWORKS
  82. #define MAIN(name, _argc, _argv, _envp) \
  83. static int innerMain(int argc, char **argv, char **envp); \
  84. int name(char *arg0, ...) { \
  85. va_list args; \
  86. char *argp, *largv[ME_MAX_ARGC]; \
  87. int largc = 0; \
  88. va_start(args, arg0); \
  89. largv[largc++] = #name; \
  90. if (arg0) { \
  91. largv[largc++] = arg0; \
  92. } \
  93. for (argp = va_arg(args, char*); argp && largc < ME_MAX_ARGC; argp = va_arg(args, char*)) { \
  94. largv[largc++] = argp; \
  95. } \
  96. return innerMain(largc, largv, NULL); \
  97. } \
  98. static int innerMain(_argc, _argv, _envp)
  99. #elif ME_WIN_LIKE
  100. #define MAIN(name, _argc, _argv, _envp) \
  101. APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *command, int junk2) { \
  102. extern int main(); \
  103. char *largv[ME_MAX_ARGC]; \
  104. int largc; \
  105. largc = websParseArgs(command, &largv[1], ME_MAX_ARGC - 1); \
  106. largv[0] = #name; \
  107. main(largc, largv, NULL); \
  108. } \
  109. int main(_argc, _argv, _envp)
  110. #else
  111. #define MAIN(name, _argc, _argv, _envp) int main(_argc, _argv, _envp)
  112. #endif
  113. PUBLIC int websParseArgs(char *args, char **argv, int maxArgc);
  114. #if WINDOWS
  115. PUBLIC void websSetInst(HINSTANCE inst);
  116. PUBLIC HINSTANCE websGetInst();
  117. #endif
  118. /************************************ Tunables ********************************/
  119. #define WEBS_MAX_LISTEN 8 /**< Maximum number of listen endpoints */
  120. #define WEBS_SMALL_HASH 31 /**< General small hash size */
  121. #define WEBS_MAX_PASSWORD 32 /**< Default maximum password */
  122. /************************************* Error **********************************/
  123. #if ME_GOAHEAD_LOGGING
  124. #define WEBS_L __FILE__, __LINE__
  125. #define WEBS_ARGS_DEC char *file, int line
  126. #define WEBS_ARGS file, line
  127. PUBLIC_DATA int logLevel;
  128. /**
  129. Standard logging trace levels are 0 to 9 with 0 being the most verbose. These are ored with the error source
  130. and type flags. The WEBS_LOG_MASK is used to extract the trace level from a flags word. We expect most apps
  131. to run with level 2 trace enabled.
  132. */
  133. #define WEBS_ERROR 1 /**< Hard error trace level */
  134. #define WEBS_WARN 2 /**< Soft warning trace level */
  135. #define WEBS_CONFIG 2 /**< Configuration settings trace level. */
  136. #define WEBS_VERBOSE 9 /**< Highest level of trace */
  137. #define WEBS_LEVEL_MASK 0xF /**< Level mask */
  138. /*
  139. Log message flags
  140. */
  141. #define WEBS_ASSERT_MSG 0x10 /**< Originated from assert */
  142. #define WEBS_ERROR_MSG 0x20 /**< Originated from error */
  143. #define WEBS_LOG_MSG 0x100 /**< Originated from logmsg */
  144. #define WEBS_RAW_MSG 0x200 /**< Raw message output */
  145. #define WEBS_TRACE_MSG 0x400 /**< Originated from trace */
  146. #if ME_GOAHEAD_TRACING && ME_GOAHEAD_LOGGING
  147. #if ME_COMPILER_HAS_MACRO_VARARGS
  148. #define trace(l, ...) if (((l) & WEBS_LEVEL_MASK) <= websGetLogLevel()) { traceProc(l, __VA_ARGS__); } else {}
  149. #else
  150. inline void trace(int level, cchar *fmt, ...) {
  151. WebsLogHandler logHandler = logGetHandler();
  152. if ((level & WEBS_LEVEL_MASK) <= logLevel && logHandler) {
  153. va_list args; va_start(args, fmt);
  154. char *message = sfmtv((char*) fmt, args);
  155. logHandler(level | WEBS_TRACE_MSG, message);
  156. wfree(message);
  157. va_end(args);
  158. }
  159. }
  160. #endif
  161. #else
  162. #define trace(l, ...) if (1) ; else {}
  163. #endif
  164. #if ME_GOAHEAD_LOGGING
  165. #if ME_COMPILER_HAS_MACRO_VARARGS
  166. #define logmsg(l, ...) if ((l) <= logLevel) { logmsgProc(l, __VA_ARGS__); } else {}
  167. #else
  168. inline void logmsg(int level, cchar *fmt, ...) {
  169. WebsLogHandler logHandler = logGetHandler();
  170. if ((level & WEBS_LEVEL_MASK) <= logLevel && logHandler) {
  171. va_list args; va_start(args, fmt);
  172. char *message = sfmtv((char*) fmt, args);
  173. logHandler(level | WEBS_TRACE_MSG, message);
  174. wfree(message);
  175. va_end(args);
  176. }
  177. }
  178. #endif
  179. #else
  180. #define logmsg(l, ...) if (1) ; else {}
  181. #endif
  182. #if DOXYGEN
  183. #undef assert
  184. /**
  185. Assure that an assert condition is true
  186. @param cond Boolean result of a conditional test
  187. @stability Stable
  188. */
  189. extern void assert(bool cond);
  190. #elif ME_GOAHEAD_DEBUG
  191. #define assert(C) if (C) ; else assertError(WEBS_L, "%s", #C)
  192. PUBLIC void assertError(WEBS_ARGS_DEC, char *fmt, ...);
  193. #else
  194. #define assert(C) if (1) ; else {}
  195. #endif
  196. /**
  197. Callback for emitting trace log output
  198. @param level Integer between 0 and 9. Zero is the lowest trace level used for the most important messages.
  199. @param msg Message to log
  200. @return Zero if successful
  201. @internal
  202. */
  203. typedef void (*WebsLogHandler)(int level, cchar *msg);
  204. /**
  205. Emit an error message
  206. @return Zero if successful
  207. @stability Stable
  208. */
  209. PUBLIC void error(cchar *fmt, ...);
  210. /**
  211. Open the log logging module
  212. @return Zero if successful
  213. @internal
  214. */
  215. PUBLIC int logOpen(void);
  216. /**
  217. Close the log logging module
  218. @internal
  219. */
  220. PUBLIC void logClose(void);
  221. /**
  222. Get the log callback
  223. @return handler Callback handler function of type WebsLogHandler
  224. @stability Stable
  225. */
  226. PUBLIC WebsLogHandler logGetHandler(void);
  227. /**
  228. Set a log callback
  229. @param handler Callback handler function of type WebsLogHandler
  230. @return The previous callback function
  231. @stability Stable
  232. */
  233. PUBLIC WebsLogHandler logSetHandler(WebsLogHandler handler);
  234. /**
  235. Get the current trace log level
  236. @return Number between 0 and 9
  237. @ingroup Webs
  238. @stability Stable
  239. */
  240. PUBLIC int websGetLogLevel(void);
  241. /**
  242. Set the current trace log level
  243. @return Number between 0 and 9
  244. @ingroup Webs
  245. @stability Prototype
  246. */
  247. void websSetLogLevel(int level);
  248. /**
  249. Set the filename to save logging output
  250. @param path Filename path to use
  251. @stability Stable
  252. */
  253. PUBLIC void logSetPath(cchar *path);
  254. /**
  255. Emit a message to the log
  256. @description This emits a message at the specified level. GoAhead filters logging messages by defining a verbosity
  257. level at startup. Level 0 is the least verbose where only the most important messages will be output. Level 9 is the
  258. Logging support is enabled by the MakeMe setting: "logging: true" which creates the ME_GOAHEAD_LOGGING define in me.h
  259. most verbose. Level 2-4 are the most useful for debugging.
  260. @param level Integer verbosity level (0-9).
  261. @param fmt Printf style format string
  262. @param ... Arguments for the format string
  263. @stability Stable
  264. */
  265. PUBLIC void logmsgProc(int level, cchar *fmt, ...);
  266. /**
  267. Emit a debug trace message to the log
  268. @description This emits a message at the specified level. GoAhead filters logging messages by defining a verbosity
  269. level at startup. Level 0 is the least verbose where only the most important messages will be output. Level 9 is the
  270. most verbose. Level 2-4 are the most useful for debugging.
  271. Debug trace support is enabled by the MakeMe setting: "tracing: true" which creates the ME_GOAHEAD_TRACING define in
  272. me.h.
  273. @param level Integer verbosity level (0-9).
  274. @param fmt Printf style format string
  275. @param ... Arguments for the format string
  276. @stability Stable
  277. */
  278. PUBLIC void traceProc(int level, cchar *fmt, ...);
  279. #else /*! ME_GOAHEAD_LOGGING */
  280. #define assert(C) if (1) ; else {}
  281. #define error(l, ...) if (1) ; else {}
  282. #define trace(l, ...) if (1) ; else {}
  283. #define logOpen() if (1) ; else {}
  284. #define logClose() if (1) ; else {}
  285. #define websGetLogLevel() 0
  286. #define logmsg(l, ...) if (1) ; else {}
  287. #define logSetPath(p) if (1) ; else {}
  288. #endif
  289. /*********************************** HTTP Codes *******************************/
  290. /*
  291. Standard HTTP/1.1 status codes
  292. */
  293. #define HTTP_CODE_CONTINUE 100 /**< Continue with request, only partial content transmitted */
  294. #define HTTP_CODE_OK 200 /**< The request completed successfully */
  295. #define HTTP_CODE_CREATED 201 /**< The request has completed and a new resource was created */
  296. #define HTTP_CODE_ACCEPTED 202 /**< The request has been accepted and processing is continuing */
  297. #define HTTP_CODE_NOT_AUTHORITATIVE 203 /**< The request has completed but content may be from another source */
  298. #define HTTP_CODE_NO_CONTENT 204 /**< The request has completed and there is no response to send */
  299. #define HTTP_CODE_RESET 205 /**< The request has completed with no content. Client must reset view */
  300. #define HTTP_CODE_PARTIAL 206 /**< The request has completed and is returning partial content */
  301. #define HTTP_CODE_MOVED_PERMANENTLY 301 /**< The requested URI has moved permanently to a new location */
  302. #define HTTP_CODE_MOVED_TEMPORARILY 302 /**< The URI has moved temporarily to a new location */
  303. #define HTTP_CODE_SEE_OTHER 303 /**< The requested URI can be found at another URI location */
  304. #define HTTP_CODE_NOT_MODIFIED 304 /**< The requested resource has changed since the last request */
  305. #define HTTP_CODE_USE_PROXY 305 /**< The requested resource must be accessed via the location proxy */
  306. #define HTTP_CODE_TEMPORARY_REDIRECT 307 /**< The request should be repeated at another URI location */
  307. #define HTTP_CODE_BAD_REQUEST 400 /**< The request is malformed */
  308. #define HTTP_CODE_UNAUTHORIZED 401 /**< Authentication for the request has failed */
  309. #define HTTP_CODE_PAYMENT_REQUIRED 402 /**< Reserved for future use */
  310. #define HTTP_CODE_FORBIDDEN 403 /**< The request was legal, but the server refuses to process */
  311. #define HTTP_CODE_NOT_FOUND 404 /**< The requested resource was not found */
  312. #define HTTP_CODE_BAD_METHOD 405 /**< The request HTTP method was not supported by the resource */
  313. #define HTTP_CODE_NOT_ACCEPTABLE 406 /**< The requested resource cannot generate the required content */
  314. #define HTTP_CODE_REQUEST_TIMEOUT 408 /**< The server timed out waiting for the request to complete */
  315. #define HTTP_CODE_CONFLICT 409 /**< The request had a conflict in the request headers and URI */
  316. #define HTTP_CODE_GONE 410 /**< The requested resource is no longer available*/
  317. #define HTTP_CODE_LENGTH_REQUIRED 411 /**< The request did not specify a required content length*/
  318. #define HTTP_CODE_PRECOND_FAILED 412 /**< The server cannot satisfy one of the request preconditions */
  319. #define HTTP_CODE_REQUEST_TOO_LARGE 413 /**< The request is too large for the server to process */
  320. #define HTTP_CODE_REQUEST_URL_TOO_LARGE 414 /**< The request URI is too long for the server to process */
  321. #define HTTP_CODE_UNSUPPORTED_MEDIA_TYPE 415 /**< The request media type is not supported by the server or resource */
  322. #define HTTP_CODE_RANGE_NOT_SATISFIABLE 416 /**< The request content range does not exist for the resource */
  323. #define HTTP_CODE_EXPECTATION_FAILED 417 /**< The server cannot satisfy the Expect header requirements */
  324. #define HTTP_CODE_NO_RESPONSE 444 /**< The connection was closed with no response to the client */
  325. #define HTTP_CODE_INTERNAL_SERVER_ERROR 500 /**< Server processing or configuration error. No response generated */
  326. #define HTTP_CODE_NOT_IMPLEMENTED 501 /**< The server does not recognize the request or method */
  327. #define HTTP_CODE_BAD_GATEWAY 502 /**< The server cannot act as a gateway for the given request */
  328. #define HTTP_CODE_SERVICE_UNAVAILABLE 503 /**< The server is currently unavailable or overloaded */
  329. #define HTTP_CODE_GATEWAY_TIMEOUT 504 /**< The server gateway timed out waiting for the upstream server */
  330. #define HTTP_CODE_BAD_VERSION 505 /**< The server does not support the HTTP protocol version */
  331. #define HTTP_CODE_INSUFFICIENT_STORAGE 507 /**< The server has insufficient storage to complete the request */
  332. /*
  333. Proprietary HTTP status codes
  334. */
  335. #define HTTP_CODE_START_LOCAL_ERRORS 550
  336. #define HTTP_CODE_COMMS_ERROR 550 /**< The server had a communications error responding to the client */
  337. /************************************* WebsValue ******************************/
  338. #ifdef WITH_QT
  339. /*
  340. QT creates a global "hex" -- Ugh!
  341. */
  342. #define hex hexvalue
  343. #endif
  344. /**
  345. Value types.
  346. */
  347. typedef enum WebsType {
  348. undefined = 0,
  349. byteint = 1,
  350. shortint = 2,
  351. integer = 3,
  352. hex = 4,
  353. percent = 5,
  354. octal = 6,
  355. big = 7,
  356. flag = 8,
  357. floating = 9,
  358. string = 10,
  359. bytes = 11,
  360. symbol = 12,
  361. errmsg = 13
  362. } WebsType;
  363. /**
  364. System native time type. This is the time in seconds.
  365. This may be 32 or 64 bits and may be signed or unsigned on some systems.
  366. */
  367. typedef time_t WebsTime;
  368. /**
  369. Value union to store primitive value types
  370. */
  371. typedef struct WebsValue {
  372. union {
  373. char flag;
  374. char byteint;
  375. short shortint;
  376. char percent;
  377. long integer;
  378. long hex;
  379. long octal;
  380. long big[2];
  381. #if ME_FLOAT
  382. double floating;
  383. #endif
  384. char *string;
  385. char *bytes;
  386. char *errmsg;
  387. void *symbol;
  388. } value;
  389. WebsType type;
  390. uint valid : 8;
  391. uint allocated : 8; /* String was allocated */
  392. } WebsValue;
  393. /**
  394. The value is a numeric type
  395. */
  396. #define value_numeric(t) (t >= byteint && t <= big)
  397. /**
  398. The value is a string type
  399. */
  400. #define value_str(t) (t >= string && t <= bytes)
  401. /**
  402. The value is valid supported type
  403. */
  404. #define value_ok(t) (t > undefined && t <= symbol)
  405. /**
  406. Allocate strings using malloc
  407. */
  408. #define VALUE_ALLOCATE 0x1
  409. /**
  410. Create an integer value
  411. @param value Integer long value
  412. @return Value object containing the integer
  413. @stability Stable
  414. */
  415. PUBLIC WebsValue valueInteger(long value);
  416. /**
  417. Create an string value
  418. @param value String long value
  419. @param flags Set to VALUE_ALLOCATE to store a copy of the string reference
  420. @return Value object containing the string
  421. @stability Stable
  422. */
  423. PUBLIC WebsValue valueString(cchar *value, int flags);
  424. /**
  425. Create an symbol value containing an object reference
  426. @param value Value reference
  427. @return Value object containing the symbol reference
  428. @stability Stable
  429. */
  430. PUBLIC WebsValue valueSymbol(void *value);
  431. /**
  432. Free any allocated string in a value
  433. @param value Value object
  434. @stability Stable
  435. */
  436. PUBLIC void valueFree(WebsValue *value);
  437. /************************************* Ringq **********************************/
  438. /**
  439. A WebsBuf (ring queue) allows maximum utilization of memory for data storage and is
  440. ideal for input/output buffering. This module provides a highly efficient
  441. implementation and a vehicle for dynamic strings.
  442. \n\n
  443. WARNING: This is a public implementation and callers have full access to
  444. the queue structure and pointers. Change this module very carefully.
  445. \n\n
  446. This module follows the open/close model.
  447. \n\n
  448. Operation of a WebsBuf where bp is a pointer to a WebsBuf :
  449. bp->buflen contains the size of the buffer.
  450. bp->buf will point to the start of the buffer.
  451. bp->servp will point to the first (un-consumed) data byte.
  452. bp->endp will point to the next free location to which new data is added
  453. bp->endbuf will point to one past the end of the buffer.
  454. \n\n
  455. Eg. If the WebsBuf contains the data "abcdef", it might look like :
  456. \n\n
  457. +-------------------------------------------------------------------+
  458. | | | | | | | | a | b | c | d | e | f | | | | |
  459. +-------------------------------------------------------------------+
  460. ^ ^ ^ ^
  461. | | | |
  462. bp->buf bp->servp bp->endp bp->enduf
  463. \n\n
  464. The queue is empty when servp == endp. This means that the queue will hold
  465. at most bp->buflen -1 bytes. It is the fillers responsibility to ensure
  466. the WebsBuf is never filled such that servp == endp.
  467. \n\n
  468. It is the fillers responsibility to "wrap" the endp back to point to
  469. bp->buf when the pointer steps past the end. Correspondingly it is the
  470. consumers responsibility to "wrap" the servp when it steps to bp->endbuf.
  471. The bufPutc and bufGetc routines will do this automatically.
  472. @defgroup WebsBuf WebsBuf
  473. @stability Stable
  474. */
  475. typedef struct WebsBuf {
  476. char *buf; /**< Holding buffer for data */
  477. char *servp; /**< Pointer to start of data */
  478. char *endp; /**< Pointer to end of data */
  479. char *endbuf; /**< Pointer to end of buffer */
  480. ssize buflen; /**< Length of ring queue */
  481. ssize maxsize; /**< Maximum size */
  482. int increment; /**< Growth increment */
  483. } WebsBuf;
  484. /**
  485. Add a trailing null to the buffer. The end pointer is not changed.
  486. @param bp Buffer reference
  487. @ingroup WebsBuf
  488. @stability Stable
  489. */
  490. PUBLIC void bufAddNull(WebsBuf *bp);
  491. /**
  492. Adjust the endp pointer by the specified size.
  493. @description This is useful after manually copying data into the buffer and needing to adjust the end pointer.
  494. @param bp Buffer reference
  495. @param size Size of adjustment. May be positive or negative value.
  496. @ingroup WebsBuf
  497. @stability Stable
  498. */
  499. PUBLIC void bufAdjustEnd(WebsBuf *bp, ssize size);
  500. /**
  501. Adjust the start (servp) reference
  502. @param bp Buffer reference
  503. @param count Number of bytes to adjust
  504. @ingroup WebsBuf
  505. @stability Stable
  506. */
  507. PUBLIC void bufAdjustStart(WebsBuf *bp, ssize count);
  508. /**
  509. Compact the data in the buffer and move to the start of the buffer
  510. @param bp Buffer reference
  511. @ingroup WebsBuf
  512. @stability Stable
  513. */
  514. PUBLIC void bufCompact(WebsBuf *bp);
  515. /**
  516. Create a buffer
  517. @param bp Buffer reference
  518. @param increment Incremental size to grow the buffer. This will be increased by a power of two each time
  519. the buffer grows.
  520. @param maxsize Maximum size of the buffer
  521. @return Zero if successful
  522. @ingroup WebsBuf
  523. @stability Stable
  524. */
  525. PUBLIC int bufCreate(WebsBuf *bp, int increment, int maxsize);
  526. /**
  527. Flush all data in the buffer and reset the pointers.
  528. @param bp Buffer reference
  529. @ingroup WebsBuf
  530. @stability Stable
  531. */
  532. PUBLIC void bufFlush(WebsBuf *bp);
  533. /**
  534. Free allocated storage for the buffer
  535. @param bp Buffer reference
  536. @return Zero if successful
  537. @ingroup WebsBuf
  538. @stability Stable
  539. */
  540. PUBLIC void bufFree(WebsBuf *bp);
  541. /**
  542. Copy a block of from the buffer and adjust the servp.
  543. @param bp Buffer reference
  544. @param blk Block into which to place the data
  545. @param len Length of the block
  546. @return Number of bytes copied.
  547. @ingroup WebsBuf
  548. @stability Stable
  549. */
  550. PUBLIC ssize bufGetBlk(WebsBuf *bp, char *blk, ssize len);
  551. /**
  552. Return the maximum number of bytes the buffer can provide via a single block copy.
  553. @description Useful if the user is doing their own data retrieval.
  554. @param bp Buffer reference
  555. @return Number of bytes available for copying.
  556. @ingroup WebsBuf
  557. @stability Stable
  558. */
  559. PUBLIC ssize bufGetBlkMax(WebsBuf *bp);
  560. /**
  561. Get a character from the buffer and increment the servp
  562. @param bp Buffer reference
  563. @return The next character or -1 if the buffer is empty
  564. @ingroup WebsBuf
  565. @stability Stable
  566. */
  567. PUBLIC int bufGetc(WebsBuf *bp);
  568. /**
  569. Grow the buffer by at least the required amount of room
  570. @param bp Buffer reference
  571. @param room Available size required after growing the buffer
  572. @return True if the buffer can be grown to have the required amount of room.
  573. @ingroup WebsBuf
  574. @stability Stable
  575. */
  576. PUBLIC bool bufGrow(WebsBuf *bp, ssize room);
  577. /**
  578. Get the length of available data in the buffer
  579. @param bp Buffer reference
  580. @return Size of available data in bytes
  581. @ingroup WebsBuf
  582. @stability Stable
  583. */
  584. PUBLIC ssize bufLen(WebsBuf *bp);
  585. /**
  586. Insert a character to the buffer before the servp position and decrement the servp
  587. @param bp Buffer reference
  588. @param c Character to insert
  589. @return Zero if successful
  590. @ingroup WebsBuf
  591. @stability Stable
  592. */
  593. PUBLIC int bufInsertc(WebsBuf *bp, char c);
  594. /**
  595. Append a character to the buffer at the endp position and increment the endp
  596. @param bp Buffer reference
  597. @param c Character to append
  598. @return Zero if successful
  599. @ingroup WebsBuf
  600. @stability Stable
  601. */
  602. PUBLIC int bufPutc(WebsBuf *bp, char c);
  603. /**
  604. Put a block to the buffer.
  605. @param bp Buffer reference
  606. @param blk Block to append to the buffer
  607. @param len Size of the block
  608. @return Length of data appended. Should equal len.
  609. @ingroup WebsBuf
  610. @stability Stable
  611. */
  612. PUBLIC ssize bufPutBlk(WebsBuf *bp, cchar *blk, ssize len);
  613. /**
  614. Append a formatted string to the buffer at the endp position and increment the endp
  615. @param bp Buffer reference
  616. @param fmt Printf style format string
  617. @param ... Variable arguments for the format string
  618. @return Count of characters appended. Returns negative if there is an allocation error.
  619. @ingroup WebsBuf
  620. @stability Stable
  621. */
  622. PUBLIC ssize bufPut(WebsBuf *bp, cchar *fmt, ...) PRINTF_ATTRIBUTE(2,3);
  623. /**
  624. Append a string to the buffer at the endp position and increment the endp
  625. @param bp Buffer reference
  626. @param str String to append
  627. @return Count of characters appended. Returns negative if there is an allocation error.
  628. @ingroup WebsBuf
  629. @stability Stable
  630. */
  631. PUBLIC ssize bufPutStr(WebsBuf *bp, cchar *str);
  632. /**
  633. Reset the buffer pointers to the start of the buffer if empty
  634. @param bp Buffer reference
  635. @ingroup WebsBuf
  636. @stability Stable
  637. */
  638. PUBLIC void bufReset(WebsBuf *bp);
  639. /**
  640. Determine the room available in the buffer.
  641. @description This returns the maximum number of bytes the buffer can absorb in a single block copy.
  642. @param bp Buffer reference
  643. @return Number of bytes of available space.
  644. @ingroup WebsBuf
  645. @stability Stable
  646. */
  647. PUBLIC ssize bufRoom(WebsBuf *bp);
  648. /**
  649. Get a reference to the start of buffer data
  650. @param bp Buffer reference
  651. @return A string pointer.
  652. @ingroup WebsBuf
  653. @stability Stable
  654. */
  655. PUBLIC char *bufStart(WebsBuf *bp);
  656. /******************************* Malloc Replacement ***************************/
  657. #if ME_GOAHEAD_REPLACE_MALLOC
  658. /**
  659. GoAhead allocator memory block
  660. Memory block classes are: 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536.
  661. @defgroup WebsAlloc WebsAlloc
  662. @stability Stable
  663. */
  664. typedef struct WebsAlloc {
  665. union {
  666. void *next; /**< Pointer to next in q */
  667. int size; /**< Actual requested size */
  668. } u;
  669. int flags; /**< Per block allocation flags */
  670. } WebsAlloc;
  671. #define WEBS_DEFAULT_MEM (64 * 1024) /**< Default memory allocation */
  672. #define WEBS_MAX_CLASS 13 /**< Maximum class number + 1 */
  673. #define WEBS_SHIFT 4 /**< Convert size to class */
  674. #define WEBS_ROUND ((1 << (B_SHIFT)) - 1)
  675. #define WEBS_MALLOCED 0x80000000 /* Block was malloced */
  676. #define WEBS_FILL_CHAR (0x77) /* Fill byte for buffers */
  677. #define WEBS_FILL_WORD (0x77777777) /* Fill word for buffers */
  678. /*
  679. Flags. The integrity value is used as an arbitrary value to fill the flags.
  680. */
  681. #define WEBS_USE_MALLOC 0x1 /**< Okay to use malloc if required */
  682. #define WEBS_USER_BUF 0x2 /* User supplied buffer for mem */
  683. #define WEBS_INTEGRITY 0x8124000 /* Integrity value */
  684. #define WEBS_INTEGRITY_MASK 0xFFFF000 /* Integrity mask */
  685. #endif /* ME_GOAHEAD_REPLACE_MALLOC */
  686. /**
  687. Close the GoAhead memory allocator
  688. @ingroup WebsAlloc
  689. @stability Stable
  690. */
  691. PUBLIC void wcloseAlloc(void);
  692. /**
  693. Initialize the walloc module.
  694. @description The wopenAlloc function should be called the very first thing after the application starts and wclose
  695. should be called the last thing before exiting. If wopenAlloc is not called, it will be called on the first allocation
  696. with default values. "buf" points to memory to use of size "bufsize". If buf is NULL, memory is allocated using malloc.
  697. flags may be set to WEBS_USE_MALLOC if using malloc is okay. This routine will allocate * an initial buffer of size
  698. bufsize for use by the application.
  699. @param buf Optional user supplied block of memory to use for allocations
  700. @param bufsize Size of buf
  701. @param flags Allocation flags. Set to WEBS_USE_MALLOC to permit the use of malloc() to grow memory.
  702. @return Zero if successful, otherwise -1.
  703. @ingroup WebsAlloc
  704. @stability Stable
  705. */
  706. PUBLIC int wopenAlloc(void *buf, int bufsize, int flags);
  707. /**
  708. Allocate a block of the requested size
  709. @param size Memory size required
  710. @return A reference to the allocated block
  711. @ingroup WebsAlloc
  712. @stability Stable
  713. */
  714. PUBLIC void *walloc(ssize size);
  715. /**
  716. Free an allocated block of memory
  717. @param blk Reference to the memory block to free.
  718. @ingroup WebsAlloc
  719. @stability Stable
  720. */
  721. PUBLIC void wfree(void *blk);
  722. /**
  723. Reallocate a block of memory and grow its size
  724. @description If the new size is larger than the existing block, a new block will be allocated and the old data
  725. will be copied to the new block.
  726. @param blk Original block reference
  727. @param newsize Size of the new block.
  728. @return Reference to the new memory block
  729. @ingroup WebsAlloc
  730. @stability Stable
  731. */
  732. PUBLIC void *wrealloc(void *blk, ssize newsize);
  733. /**
  734. Duplicate memory
  735. @param ptr Original block reference
  736. @param usize Size to allocate
  737. @return Reference to the new memory block
  738. @ingroup WebsAlloc
  739. */
  740. PUBLIC void *wdup(cvoid *ptr, size_t usize);
  741. typedef void (*WebsMemNotifier)(ssize size);
  742. /**
  743. Define a global memory allocation notifier.
  744. @description The notifier is called if any memory allocation fails. It is called with the requested allocation size
  745. as its only parameter.
  746. @param cback Callback function to invoke for allocation failures.
  747. @ingroup WebsAlloc
  748. @stability Evolving
  749. */
  750. PUBLIC void websSetMemNotifier(WebsMemNotifier cback);
  751. #ifndef WEBS_SHIFT
  752. #define WEBS_SHIFT 4
  753. #endif
  754. #if DEPRECATE
  755. /*
  756. Deprecated in 4.0.0
  757. */
  758. PUBLIC ssize mtow(wchar *dest, ssize count, char *src, ssize len);
  759. PUBLIC ssize wtom(char *dest, ssize count, wchar *src, ssize len);
  760. PUBLIC wchar *amtow(char *src, ssize *len);
  761. PUBLIC char *awtom(wchar *src, ssize *len);
  762. #endif
  763. /******************************* Hash Table *********************************/
  764. /**
  765. Hash table entry structure.
  766. @description The hash structure supports growable hash tables with high performance, collision resistant hashes.
  767. Each hash entry has a descriptor entry. This is used to manage the hash table link chains.
  768. @see hashCreate hashFree hashLookup hashEnter hashDelete hashWalk hashFirst hashNext
  769. @defgroup WebsHash WebsHash
  770. @stability Stable
  771. */
  772. typedef struct WebsKey {
  773. struct WebsKey *forw; /* Pointer to next hash list */
  774. WebsValue name; /* Name of symbol */
  775. WebsValue content; /* Value of symbol */
  776. int arg; /* Parameter value */
  777. int bucket; /* Bucket index */
  778. } WebsKey;
  779. /**
  780. Hash table ID returned by hashCreate
  781. */
  782. typedef int WebsHash; /* Returned by symCreate */
  783. /**
  784. Create a hash table
  785. @param size Minimum size of the hash index
  786. @return Hash table ID. Negative if the hash cannot be created.
  787. @ingroup WebsHash
  788. @stability Stable
  789. */
  790. PUBLIC WebsHash hashCreate(int size);
  791. /**
  792. Free a hash table
  793. @param id Hash table id returned by hashCreate
  794. @ingroup WebsHash
  795. @stability Stable
  796. */
  797. PUBLIC void hashFree(WebsHash id);
  798. /**
  799. Lookup a name in the hash table
  800. @param id Hash table id returned by hashCreate
  801. @param name Key name to search for
  802. @return Reference to the WebKey object storing the key and value
  803. @ingroup WebsHash
  804. @stability Stable
  805. */
  806. PUBLIC WebsKey *hashLookup(WebsHash id, cchar *name);
  807. /**
  808. Lookup a name in the hash table and return a symbol reference
  809. @param sd Hash table id returned by hashCreate
  810. @param name Key name to search for
  811. @return Reference to the symbole
  812. @ingroup WebsHash
  813. @stability Evolving
  814. */
  815. PUBLIC void *hashLookupSymbol(WebsHash sd, cchar *name);
  816. /**
  817. Enter a new key and value into the hash table
  818. @param id Hash table id returned by hashCreate
  819. @param name Key name to create
  820. @param value Key value to enter
  821. @param arg Optional extra argument to store with the value
  822. @return Reference to the WebKey object storing the key and value
  823. @ingroup WebsHash
  824. @stability Stable
  825. */
  826. PUBLIC WebsKey *hashEnter(WebsHash id, cchar *name, WebsValue value, int arg);
  827. /**
  828. Delete a key by name
  829. @param id Hash table id returned by hashCreate
  830. @param name Key name to delete
  831. @return Zero if the delete was successful. Otherwise -1 if the key was not found.
  832. @ingroup WebsHash
  833. @stability Stable
  834. */
  835. PUBLIC int hashDelete(WebsHash id, cchar *name);
  836. /**
  837. Start walking the hash keys by returning the first key entry in the hash
  838. @param id Hash table id returned by hashCreate
  839. @return Reference to the first WebKey object. Return null if there are no keys in the hash.
  840. @ingroup WebsHash
  841. @stability Stable
  842. */
  843. PUBLIC WebsKey *hashFirst(WebsHash id);
  844. /**
  845. Continue walking the hash keys by returning the next key entry in the hash
  846. @param id Hash table id returned by hashCreate
  847. @param last Reference to a WebsKey to hold the current traversal key state.
  848. @return Reference to the next WebKey object. Returns null if no more keys exist to be traversed.
  849. @ingroup WebsHash
  850. @stability Stable
  851. */
  852. PUBLIC WebsKey *hashNext(WebsHash id, WebsKey *last);
  853. /************************************ Socket **********************************/
  854. /*
  855. Socket flags
  856. */
  857. #define SOCKET_EOF 0x1 /**< Seen end of file */
  858. #define SOCKET_CONNECTING 0x2 /**< Connect in progress */
  859. #define SOCKET_RESERVICE 0x4 /**< Socket needs re-servicing */
  860. #define SOCKET_ASYNC 0x8 /**< Use async connect */
  861. #define SOCKET_BLOCK 0x10 /**< Use blocking I/O */
  862. #define SOCKET_LISTENING 0x20 /**< Socket is server listener */
  863. #define SOCKET_CLOSING 0x40 /**< Socket is closing */
  864. #define SOCKET_CONNRESET 0x80 /**< Socket connection was reset */
  865. #define SOCKET_HANDSHAKING 0x100 /**< Doing SSL handshake */
  866. #define SOCKET_BUFFERED_READ 0x200 /**< Message pending on this socket */
  867. #define SOCKET_BUFFERED_WRITE 0x400 /**< Message pending on this socket */
  868. #define SOCKET_NODELAY 0x800 /**< Disable Nagle algorithm */
  869. #define SOCKET_PORT_MAX 0xffff /**< Max Port size */
  870. #ifndef ME_MAX_IP
  871. #define ME_MAX_IP 64 /**< Maximum IP address size */
  872. #endif
  873. /*
  874. Socket error values
  875. */
  876. #define SOCKET_WOULDBLOCK 1 /**< Socket would block on I/O */
  877. #define SOCKET_RESET 2 /**< Socket has been reset */
  878. #define SOCKET_NETDOWN 3 /**< Network is down */
  879. #define SOCKET_AGAIN 4 /**< Issue the request again */
  880. #define SOCKET_INTR 5 /**< Call was interrupted */
  881. #define SOCKET_INVAL 6 /**< Invalid */
  882. /*
  883. Handler event masks
  884. */
  885. #define SOCKET_READABLE 0x2 /**< Make socket readable */
  886. #define SOCKET_WRITABLE 0x4 /**< Make socket writable */
  887. #define SOCKET_EXCEPTION 0x8 /**< Interested in exceptions */
  888. /**
  889. Socket I/O callback
  890. @param sid Socket ID handle returned from socketConnect or when a new socket is passed to a SocketAccept
  891. callback..
  892. @param mask Mask of events of interest. Set to SOCKET_READABLE | SOCKET_WRITABLE | SOCKET_EXCEPTION.
  893. @param data Data argument to pass to the callback function.
  894. @ingroup WebsSocket
  895. @stability Stable
  896. */
  897. typedef void (*SocketHandler)(int sid, int mask, void *data);
  898. /**
  899. Socket accept callback
  900. @param sid Socket ID handle for the newly accepted socket
  901. @param ipaddr IP address of the connecting client.
  902. @param port Port of the connecting client.
  903. @param listenSid Socket ID for the listening socket
  904. @ingroup WebsSocket
  905. @stability Stable
  906. */
  907. typedef int (*SocketAccept)(int sid, cchar *ipaddr, int port, int listenSid);
  908. /**
  909. Socket control structure
  910. @see socketAddress socketAddressIsV6 socketClose socketCloseConnection socketCreateHandler
  911. socketDeletehandler socketReservice socketEof socketGetPort socketInfo socketIsV6
  912. socketOpen socketListen socketParseAddress socketProcess socketRead socketWrite socketWriteString
  913. socketSelect socketGetHandle socketSetBlock socketGetBlock socketAlloc socketFree socketGetError
  914. socketSetError socketPtr socketWaitForEvent socketRegisterInterest
  915. @defgroup WebsSocket WebsSocket
  916. @stability Stable
  917. */
  918. typedef struct WebsSocket {
  919. WebsBuf lineBuf; /**< Line ring queue */
  920. SocketAccept accept; /**< Accept handler */
  921. SocketHandler handler; /**< User I/O handler */
  922. char *ip; /**< Server listen address or remote client address */
  923. void *handler_data; /**< User handler data */
  924. int handlerMask; /**< Handler events of interest */
  925. int sid; /**< Index into socket[] */
  926. int port; /**< Port to listen on */
  927. int flags; /**< Current state flags */
  928. Socket sock; /**< Actual socket handle */
  929. int fileHandle; /**< ID of the file handler */
  930. int interestEvents; /**< Mask of events to watch for */
  931. int currentEvents; /**< Mask of ready events (FD_xx) */
  932. int selectEvents; /**< Events being selected */
  933. int saveMask; /**< saved Mask for socketFlush */
  934. int error; /**< Last error */
  935. int secure; /**< Socket is using SSL */
  936. int handshakes; /**< Number of renegotiations */
  937. } WebsSocket;
  938. /**
  939. Extract the numerical IP address and port for the given socket info
  940. @param addr Reference to the socket address.
  941. @param addrlen Length of the socket address
  942. @param ipbuf Buffer to contain the parsed IP address
  943. @param ipLen Size of ipbuf
  944. @param port Reference to an integer to hold the parsed port.
  945. @return Zero if successful. Otherwise -1 for parse errors.
  946. @ingroup WebsSocket
  947. @stability Stable
  948. */
  949. PUBLIC int socketAddress(struct sockaddr *addr, int addrlen, char *ipbuf, int ipLen, int *port);
  950. /**
  951. Determine if an IP address is an IPv6 address.
  952. @param ip String IP address.
  953. @return True if the address is an IPv6 address.
  954. @ingroup WebsSocket
  955. @stability Stable
  956. */
  957. PUBLIC bool socketAddressIsV6(cchar *ip);
  958. /**
  959. Allocate a socket object
  960. @param host String host IP address.
  961. @param port Socket port
  962. @param accept Optional SocketAccept accept callback function
  963. @param flags Control flags
  964. @return Socket ID handle to use with other APIs.
  965. @ingroup WebsSocket
  966. @stability Stable
  967. */
  968. PUBLIC int socketAlloc(cchar *host, int port, SocketAccept accept, int flags);
  969. /**
  970. Close the socket module
  971. @ingroup WebsSocket
  972. @stability Stable
  973. */
  974. PUBLIC void socketClose(void);
  975. /**
  976. Close a socket connection
  977. @param sid Socket ID handle returned from socketConnect or socketAccept.
  978. @ingroup WebsSocket
  979. @stability Stable
  980. */
  981. PUBLIC void socketCloseConnection(int sid);
  982. /**
  983. Connect to a server and create a new socket
  984. @param host Host IP address.
  985. @param port Port number to connect to
  986. @param flags Set to SOCKET_BLOCK for blocking I/O. Otherwise non-blocking I/O is used.
  987. @return True if the address is an IPv6 address.
  988. @ingroup WebsSocket
  989. @internal
  990. @stability Stable
  991. */
  992. PUBLIC int socketConnect(cchar *host, int port, int flags);
  993. /**
  994. Create a socket handler that will be invoked when I/O events occur.
  995. @param sid Socket ID handle returned from socketConnect or socketAccept.
  996. @param mask Mask of events of interest. Set to SOCKET_READABLE | SOCKET_WRITABLE | SOCKET_EXCEPTION.
  997. @param handler Socket handler function.
  998. @param arg Arbitrary object reference to pass to the SocketHandler callback function.
  999. @return True if the address is an IPv6 address.
  1000. @ingroup WebsSocket
  1001. @stability Stable
  1002. */
  1003. PUBLIC void socketCreateHandler(int sid, int mask, SocketHandler handler, void *arg);
  1004. /**
  1005. Delete a socket handler created via socketCreateHandler
  1006. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1007. @ingroup WebsSocket
  1008. @stability Stable
  1009. */
  1010. PUBLIC void socketDeleteHandler(int sid);
  1011. /**
  1012. Determine if the socket is at end-of-file for input.
  1013. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1014. @return True if the address is at EOF
  1015. @ingroup WebsSocket
  1016. @stability Stable
  1017. */
  1018. PUBLIC bool socketEof(int sid);
  1019. /**
  1020. Free (and close) the socket
  1021. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1022. @ingroup WebsSocket
  1023. @stability Stable
  1024. */
  1025. PUBLIC void socketFree(int sid);
  1026. /**
  1027. Get the current blocking mode
  1028. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1029. @return True if the socket is in blocking mode.
  1030. @ingroup WebsSocket
  1031. @stability Stable
  1032. */
  1033. PUBLIC int socketGetBlock(int sid);
  1034. /**
  1035. Get the error code for the last socket operation on this thread.
  1036. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1037. @return Integer error code. See errno or GetLastError() on windows.
  1038. @ingroup WebsSocket
  1039. @stability Stable
  1040. */
  1041. PUBLIC int socketGetError(int sid);
  1042. /**
  1043. Get the underlying socket operating system socket/file handle
  1044. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1045. @return The socket handle
  1046. @ingroup WebsSocket
  1047. @stability Stable
  1048. */
  1049. PUBLIC Socket socketGetHandle(int sid);
  1050. /**
  1051. Get the list of sockets
  1052. @return The socket list pointer
  1053. @ingroup WebsSocket
  1054. @stability Stable
  1055. */
  1056. PUBLIC WebsSocket **socketGetList(void);
  1057. /**
  1058. Get the IP port associated with this socket.
  1059. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1060. @return The TCP/IP port for this socket
  1061. @ingroup WebsSocket
  1062. @stability Stable
  1063. */
  1064. PUBLIC int socketGetPort(int sid);
  1065. /**
  1066. Indicate if the system has a dual IPv4 and IPv6 stack
  1067. @return True if IPv4 and IPv6 are supported on a single stack
  1068. @ingroup WebsSocket
  1069. @stability Stable
  1070. */
  1071. PUBLIC bool socketHasDualNetworkStack(void);
  1072. /**
  1073. Indicate if the system has IPv6 support
  1074. @return True if IPv6 is supported on this system
  1075. @ingroup WebsSocket
  1076. @stability Stable
  1077. */
  1078. PUBLIC bool socketHasIPv6(void);
  1079. /**
  1080. Indicate that the application layer has buffered data for the socket.
  1081. @description This is used by SSL and other network stacks that buffer pending data
  1082. @param sp Socket object returned from #socketPtr
  1083. @param len Length of buffered data in bytes
  1084. @param dir Buffer direction. Set to MPR_READABLE for buffered read data and MPR_WRITABLE for buffered write data.
  1085. @ingroup WebsSocket
  1086. @stability Stable
  1087. */
  1088. PUBLIC void socketHiddenData(WebsSocket *sp, ssize len, int dir);
  1089. /**
  1090. Get a socket address structure for the specified IP:Port
  1091. @description This returns address details in *family, *protocol, *addr, and *addrlen.
  1092. @param ip IP address to parse
  1093. @param port TCP/IP port number
  1094. @param family Reference to an integer to hold the address family
  1095. @param protocol Reference to an integer to hold the address protocol
  1096. @param addr Reference to an integer to hold the address structure
  1097. @param addrlen Reference to an integer to hold the address structure length
  1098. @return Zero if successful, otherwise -1.
  1099. @ingroup WebsSocket
  1100. @stability Stable
  1101. */
  1102. PUBLIC int socketInfo(cchar *ip, int port, int *family, int *protocol, struct sockaddr_storage *addr, Socklen *addrlen);
  1103. /**
  1104. Determine if a socket is bound to an IPv6 address.
  1105. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1106. @return True if the socket is using IPv6.
  1107. @ingroup WebsSocket
  1108. @stability Stable
  1109. */
  1110. PUBLIC bool socketIsV6(int sid);
  1111. /**
  1112. Open a listening socket
  1113. @param host Host IP address on which to listen. Set to NULL to listen on all interfaces.
  1114. @param port TCP/IP port on which to listen
  1115. @param accept SocketAccept callback function to invoke to receive incoming connections.
  1116. @param flags Reserved
  1117. @return Zero if successful, otherwise -1.
  1118. @ingroup WebsSocket
  1119. @stability Stable
  1120. */
  1121. PUBLIC int socketListen(cchar *host, int port, SocketAccept accept, int flags);
  1122. /**
  1123. Open the socket module
  1124. @return Zero if successful, otherwise -1.
  1125. @ingroup WebsSocket
  1126. @stability Stable
  1127. */
  1128. PUBLIC int socketOpen(void);
  1129. /**
  1130. Parse an IP address into its constituent parts.
  1131. @description Parse the IP address and return the IP address and port components. Handles ipv4 and ipv6 addresses.
  1132. If the IP portion is absent, pip is set to null. If the port portion is absent, port is set to the defaultPort.
  1133. If a ":*" port specifier is used, pport is set to -1;
  1134. When an address contains an ipv6 port it should be written as
  1135. aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh:iiii
  1136. or
  1137. [aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh:iiii]:port
  1138. If supplied an IPv6 address, the backets are stripped in the returned IP address.
  1139. @param ipAddrPort IP address which may contain an optional ":port" component.
  1140. @param pip Returns a reference to an allocated string containing the IP address portion. Caller must free.
  1141. @param pport Reference to an integer to hold the port component.
  1142. @param secure Reference to an integer to be set to true if the address is using SSL/TLS.
  1143. @param defaultPort Default port number to use if no port specifier is included in ipAddrPort.
  1144. @return Zero if successful, otherwise -1.
  1145. @ingroup WebsSocket
  1146. @stability Stable
  1147. */
  1148. PUBLIC int socketParseAddress(cchar *ipAddrPort, char **pip, int *pport, int *secure, int defaultPort);
  1149. /**
  1150. Process pending socket I/O events.
  1151. @ingroup WebsSocket
  1152. @stability Stable
  1153. @internal
  1154. */
  1155. PUBLIC void socketProcess(void);
  1156. /**
  1157. Read data from a socket
  1158. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1159. @param buf Buffer to hold read data
  1160. @param len Size of the buffer
  1161. @return Count of bytes actually read. Returns -1 for errors and EOF. Distinguish between errors and EOF
  1162. via socketEof().
  1163. @ingroup WebsSocket
  1164. @stability Stable
  1165. */
  1166. PUBLIC ssize socketRead(int sid, void *buf, ssize len);
  1167. /**
  1168. Register interest in socket I/OEvents
  1169. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1170. @param mask Mask of events of interest. Set to SOCKET_READABLE | SOCKET_WRITABLE | SOCKET_EXCEPTION.
  1171. @ingroup WebsSocket
  1172. @stability Stable
  1173. */
  1174. PUBLIC void socketRegisterInterest(int sid, int mask);
  1175. /**
  1176. Request that the socket be reserviced.
  1177. @description This routine is useful when upper layers have unprocessed, buffered data for the socket.
  1178. This routine will cause the socket I/O callback handler to be invoked soon in the future.
  1179. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1180. @ingroup WebsSocket
  1181. @stability Stable
  1182. */
  1183. PUBLIC void socketReservice(int sid);
  1184. /**
  1185. Wait for I/O on a socket
  1186. @description This call uses the mask of events of interest defined by socketRegisterInterest. It blocks the caller
  1187. until a suitable I/O event or timeout occurs.
  1188. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1189. @param timeout Timeout in milliseconds.
  1190. @return Number of I/O events.
  1191. @ingroup WebsSocket
  1192. @stability Stable
  1193. */
  1194. PUBLIC int socketSelect(int sid, int timeout);
  1195. /**
  1196. Set the socket blocking mode
  1197. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1198. @param on Set to 1 to enable blocking
  1199. @return The previous blocking mode
  1200. @ingroup WebsSocket
  1201. @stability Stable
  1202. */
  1203. PUBLIC int socketSetBlock(int sid, int on);
  1204. /**
  1205. Set the error code for the last socket operation on this thread.
  1206. @param error Integer error code. See errno or GetLastError() on windows.
  1207. @ingroup WebsSocket
  1208. @stability Stable
  1209. */
  1210. PUBLIC void socketSetError(int error);
  1211. /**
  1212. Set the socket delay mode
  1213. @description This is used to enable or disable the TCP Nagle algorithm
  1214. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1215. @param on Set to 1 to disable the Nagle algorithm
  1216. @return The previous blocking mode
  1217. @ingroup WebsSocket
  1218. @stability Stable
  1219. */
  1220. PUBLIC int socketSetNoDelay(int sid, bool on);
  1221. /**
  1222. Wait for a socket I/O event
  1223. @param sp Socket object
  1224. @param mask Mask of events of interest. Set to SOCKET_READABLE | SOCKET_WRITABLE | SOCKET_EXCEPTION.
  1225. @return Zero if successful in waiting for the desired event, otherwise return -1.
  1226. @ingroup WebsSocket
  1227. @stability Stable
  1228. */
  1229. PUBLIC int socketWaitForEvent(WebsSocket *sp, int mask);
  1230. /**
  1231. Write data to the socket
  1232. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1233. @param buf Buffer containing data to write
  1234. @param len Size of buf
  1235. @return Count of bytes written. May be less than len if the socket is in non-blocking mode.
  1236. Returns -1 for errors and if the socket cannot absorb any more data. If the transport is saturated,
  1237. will return a negative error and errno will be set to EAGAIN or EWOULDBLOCK.
  1238. @ingroup WebsSocket
  1239. @stability Stable
  1240. */
  1241. PUBLIC ssize socketWrite(int sid, void *buf, ssize len);
  1242. /**
  1243. Return the socket object for the socket ID.
  1244. @param sid Socket ID handle returned from socketConnect or socketAccept.
  1245. @return Corresponding socket object.
  1246. @ingroup WebsSocket
  1247. @stability Stable
  1248. */
  1249. PUBLIC WebsSocket *socketPtr(int sid);
  1250. /*********************************** Runtime **********************************/
  1251. /**
  1252. GoAhead Web Server Runtime
  1253. @description GoAhead provides a secure runtime environment for safe string manipulation and to
  1254. help prevent buffer overflows and other potential security traps.
  1255. @defgroup WebsRuntime WebsRuntime
  1256. @see fmt wallocHandle wallocObject wfreeHandle hextoi itosbuf scaselesscmp scaselessmatch
  1257. sclone scmp scopy sfmt sfmtv slen slower smatch sstarts sncaselesscmp sncmp sncopy stok strim supper
  1258. @stability Stable
  1259. */
  1260. /**
  1261. Format a string into a static buffer.
  1262. @description This call format a string using printf style formatting arguments. A trailing null will
  1263. always be appended. The call returns the size of the allocated string excluding the null.
  1264. @param buf Pointer to the buffer.
  1265. @param maxSize Size of the buffer.
  1266. @param format Printf style format string
  1267. @param ... Variable arguments to format
  1268. @return Returns the buffer.
  1269. @ingroup WebsRuntime
  1270. @stability Stable
  1271. */
  1272. PUBLIC char *fmt(char *buf, ssize maxSize, cchar *format, ...);
  1273. /**
  1274. Allocate a handle from a map
  1275. @param map Reference to a location holding the map reference. On the first call, the map is allocated.
  1276. @return Integer handle index. Otherwise return -1 on allocation errors.
  1277. @ingroup WebsRuntime
  1278. @stability Stable
  1279. */
  1280. PUBLIC int wallocHandle(void *map);
  1281. /**
  1282. Allocate an object in a halloc map
  1283. @param map Reference to a location holding the map reference. On the first call, the map is allocated.
  1284. @param max Reference to an integer that holds the maximum handle in the map.
  1285. @param size Size of the object to allocate.
  1286. @return Integer handle index. Otherwise return -1 on allocation errors.
  1287. @ingroup WebsRuntime
  1288. @stability Stable
  1289. */
  1290. PUBLIC int wallocObject(void *map, int *max, int size);
  1291. /**
  1292. Free a handle in the map
  1293. @param map Reference to a location to hold the map reference.
  1294. @param handle Handle to free in the map.
  1295. @return Integer handle index. Otherwise return -1 on allocation errors.
  1296. @ingroup WebsRuntime
  1297. @stability Stable
  1298. */
  1299. PUBLIC int wfreeHandle(void *map, int handle);
  1300. /**
  1301. Convert a hex string to an integer
  1302. @description This call converts the supplied string to an integer using base 16.
  1303. @param str Pointer to the string to parse.
  1304. @return Returns the integer equivalent value of the string.
  1305. @ingroup WebsRuntime
  1306. @stability Stable
  1307. */
  1308. PUBLIC uint hextoi(cchar *str);
  1309. /**
  1310. Convert an integer to a string buffer.
  1311. @description This call converts the supplied 64 bit integer into a string formatted into the supplied buffer according
  1312. to the specified radix.
  1313. @param buf Pointer to the buffer that will hold the string.
  1314. @param size Size of the buffer.
  1315. @param value Integer value to convert
  1316. @param radix The base radix to use when encoding the number
  1317. @return Returns a reference to the string.
  1318. @ingroup WebsRuntime
  1319. @stability Stable
  1320. */
  1321. PUBLIC char *itosbuf(char *buf, ssize size, int64 value, int radix);
  1322. /**
  1323. Compare strings ignoring case. This is a safe replacement for strcasecmp. It can handle NULL args.
  1324. @description Compare two strings ignoring case differences. This call operates similarly to strcmp.
  1325. @param s1 First string to compare.
  1326. @param s2 Second string to compare.
  1327. @return Returns zero if the strings are equivalent, < 0 if s1 sorts lower than s2 in the collating sequence
  1328. or > 0 if it sorts higher.
  1329. @ingroup WebsRuntime
  1330. @stability Stable
  1331. */
  1332. PUBLIC int scaselesscmp(cchar *s1, cchar *s2);
  1333. /**
  1334. Compare strings ignoring case. This is similar to scaselesscmp but it returns a boolean.
  1335. @description Compare two strings ignoring case differences.
  1336. @param s1 First string to compare.
  1337. @param s2 Second string to compare.
  1338. @return Returns true if the strings are equivalent, otherwise false.
  1339. @ingroup WebsRuntime
  1340. @stability Stable
  1341. */
  1342. PUBLIC bool scaselessmatch(cchar *s1, cchar *s2);
  1343. /**
  1344. Clone a string
  1345. @description Copy a string into a newly allocated block.
  1346. @param str Pointer to the block to duplicate.
  1347. @return Returns a newly allocated string.
  1348. @ingroup WebsRuntime
  1349. @stability Stable
  1350. */
  1351. PUBLIC char *sclone(cchar *str);
  1352. /**
  1353. Clone a substring.
  1354. @description Copy a substring into a newly allocated block.
  1355. @param str Pointer to the block to duplicate.
  1356. @param len Number of bytes to copy. The actual length copied is the minimum of the given length and the length of
  1357. the supplied string. The result is null terminated.
  1358. @return Returns a newly allocated string.
  1359. @ingroup WebsRuntime
  1360. @stability Stable
  1361. */
  1362. PUBLIC char *snclone(cchar *str, ssize len);
  1363. /**
  1364. Compare strings.
  1365. @description Compare two strings. This is a safe replacement for strcmp. It can handle null args.
  1366. @param s1 First string to compare.
  1367. @param s2 Second string to compare.
  1368. @return Returns zero if the strings are identical. Return -1 if the first string is less than the second. Return 1
  1369. if the first string is greater than the second.
  1370. @ingroup WebsRuntime
  1371. @stability Stable
  1372. */
  1373. PUBLIC int scmp(cchar *s1, cchar *s2);
  1374. /**
  1375. Copy a string.
  1376. @description Safe replacement for strcpy. Copy a string and ensure the destination buffer is not overflowed.
  1377. The call returns the length of the resultant string or an error code if it will not fit into the target
  1378. string. This is similar to strcpy, but it will enforce a maximum size for the copied string and will
  1379. ensure it is always terminated with a null.
  1380. @param dest Pointer to a pointer that will hold the address of the allocated block.
  1381. @param destMax Maximum size of the target string in characters.
  1382. @param src String to copy
  1383. @return Returns the number of characters in the target string.
  1384. @ingroup WebsRuntime
  1385. @stability Stable
  1386. */
  1387. PUBLIC ssize scopy(char *dest, ssize destMax, cchar *src);
  1388. /*
  1389. String trim flags
  1390. */
  1391. #define WEBS_TRIM_START 0x1 /**< Flag for strim to trim from the start of the string */
  1392. #define WEBS_TRIM_END 0x2 /**< Flag for strim to trim from the end of the string */
  1393. #define WEBS_TRIM_BOTH 0x3 /**< Flag for strim to trim from both the start and the end of the string */
  1394. /**
  1395. Format a string. This is a secure version of printf that can handle null args.
  1396. @description Format the given arguments according to the printf style format. See fmt() for a full list of the
  1397. format specifies. This is a secure replacement for sprintf, it can handle null arguments without crashes.
  1398. @param format Printf style format string
  1399. @param ... Variable arguments for the format string
  1400. @return Returns a newly allocated string
  1401. @ingroup WebsRuntime
  1402. @stability Stable
  1403. */
  1404. PUBLIC char *sfmt(cchar *format, ...);
  1405. /**
  1406. Format a string with varargs. This is a secure version of printf that can handle null args.
  1407. @description Format the given arguments according to the printf style format. See fmt() for a full list of the
  1408. format specifies. This is a secure replacement for sprintf, it can handle null arguments without crashes.
  1409. @param format Printf style format string
  1410. @param args Varargs argument obtained from va_start.
  1411. @return Returns a newly allocated string
  1412. @ingroup WebsRuntime
  1413. @stability Stable
  1414. */
  1415. PUBLIC char *sfmtv(cchar *format, va_list args);
  1416. /**
  1417. Return the length of a string.
  1418. @description Safe replacement for strlen. This call returns the length of a string and tests if the length is
  1419. less than a given maximum. It will return zero for NULL args.
  1420. @param str String to measure.
  1421. @return Returns the length of the string
  1422. @ingroup WebsRuntime
  1423. @stability Stable
  1424. */
  1425. PUBLIC ssize slen(cchar *str);
  1426. /**
  1427. Convert a string to lower case.
  1428. @description Convert a string to its lower case equivalent.
  1429. @param str String to convert. This string is modified.
  1430. @return Reference to the supplied str. Caller must not free.
  1431. @ingroup WebsRuntime
  1432. @stability Stable
  1433. */
  1434. PUBLIC char *slower(char *str);
  1435. /**
  1436. Compare strings
  1437. @description Compare two strings. This is similar to #scmp but it returns a boolean.
  1438. @param s1 First string to compare.
  1439. @param s2 Second string to compare.
  1440. @return Returns true if the strings are equivalent, otherwise false.
  1441. @ingroup WebsRuntime
  1442. @stability Stable
  1443. */
  1444. PUBLIC bool smatch(cchar *s1, cchar *s2);
  1445. /**
  1446. Compare strings ignoring case.
  1447. @description Compare two strings ignoring case differences for a given string length. This call operates
  1448. similarly to strncasecmp.
  1449. @param s1 First string to compare.
  1450. @param s2 Second string to compare.
  1451. @param len Length of characters to compare.
  1452. @return Returns zero if the strings are equivalent, < 0 if s1 sorts lower than s2 in the collating sequence
  1453. or > 0 if it sorts higher.
  1454. @ingroup WebsRuntime
  1455. @stability Stable
  1456. */
  1457. PUBLIC int sncaselesscmp(cchar *s1, cchar *s2, ssize len);
  1458. /**
  1459. Compare strings.
  1460. @description Compare two strings for a given string length. This call operates similarly to strncmp.
  1461. @param s1 First string to compare.
  1462. @param s2 Second string to compare.
  1463. @param len Length of characters to compare.
  1464. @return Returns zero if the strings are equivalent, < 0 if s1 sorts lower than s2 in the collating sequence
  1465. or > 0 if it sorts higher.
  1466. @ingroup WebsRuntime
  1467. @stability Stable
  1468. */
  1469. PUBLIC int sncmp(cchar *s1, cchar *s2, ssize len);
  1470. /**
  1471. Copy characters from a string.
  1472. @description Safe replacement for strncpy. Copy bytes from a string and ensure the target string is not overflowed.
  1473. The call returns the length of the resultant string or an error code if it will not fit into the target
  1474. string. This is similar to strcpy, but it will enforce a maximum size for the copied string and will
  1475. ensure it is terminated with a null.
  1476. @param dest Pointer to a pointer that will hold the address of the allocated block.
  1477. @param destMax Maximum size of the target string in characters.
  1478. @param src String to copy
  1479. @param count Maximum count of characters to copy
  1480. @return Returns a reference to the destination if successful or NULL if the string won't fit.
  1481. @ingroup WebsRuntime
  1482. @stability Stable
  1483. */
  1484. PUBLIC ssize sncopy(char *dest, ssize destMax, cchar *src, ssize count);
  1485. /*
  1486. Test if a string is a radix 10 number.
  1487. @description The supported format is: [(+|-)][DIGITS]
  1488. @return true if all characters are digits or '+' or '-'
  1489. @ingroup WebsRuntime
  1490. @stability Stable
  1491. */
  1492. PUBLIC bool snumber(cchar *s);
  1493. /**
  1494. Split a string at a delimiter
  1495. @description Split a string and return parts. The string is modified.
  1496. This routiner never returns null. If there are leading delimiters, the empty string will be returned
  1497. and *last will be set to the portion after the delimiters.
  1498. If str is null, an empty string will be returned.
  1499. If there are no characters after the delimiter, then *last will be set to the empty string.
  1500. @param str String to tokenize.
  1501. @param delim Set of characters that are used as token separators.
  1502. @param last Reference to the portion after the delimiters. Will return an empty string if is not trailing portion.
  1503. @return Returns a pointer to the first part before the delimiters. If the string begins with delimiters, the empty
  1504. string will be returned.
  1505. @ingroup WebsRuntime
  1506. @stability Evolving
  1507. */
  1508. PUBLIC char *ssplit(char *str, cchar *delim, char **last);
  1509. /**
  1510. Test if the string starts with a given pattern.
  1511. @param str String to examine
  1512. @param prefix Pattern to search for
  1513. @return Returns TRUE if the pattern was found. Otherwise returns zero.
  1514. @ingroup MprString
  1515. @stability Stable
  1516. */
  1517. PUBLIC bool sstarts(cchar *str, cchar *prefix);
  1518. /**
  1519. Tokenize a string
  1520. @description Split a string into tokens.
  1521. @param str String to tokenize.
  1522. @param delim String of characters to use as token separators.
  1523. @param last Last token pointer.
  1524. @return Returns a pointer to the next token.
  1525. @ingroup WebsRuntime
  1526. @stability Stable
  1527. */
  1528. PUBLIC char *stok(char *str, cchar *delim, char **last);
  1529. /**
  1530. Trim a string.
  1531. @description Trim leading and trailing characters off a string.
  1532. @param str String to trim.
  1533. @param set String of characters to remove.
  1534. @param where Flags to indicate trim from the start, end or both. Use WEBS_TRIM_START, WEBS_TRIM_END, WEBS_TRIM_BOTH.
  1535. @return Returns a pointer to the trimmed string. May not equal \a str.
  1536. @ingroup WebsRuntime
  1537. @stability Stable
  1538. */
  1539. PUBLIC char *strim(char *str, cchar *set, int where);
  1540. /**
  1541. Convert a string to upper case.
  1542. @description Convert a string to its upper case equivalent.
  1543. @param str String to convert. This string is modified.
  1544. @return Returns a pointer to the converted string. Will always equal str. Caller must not free.
  1545. @ingroup WebsRuntime
  1546. @stability Stable
  1547. */
  1548. PUBLIC char *supper(char *str);
  1549. /**
  1550. Callback function for events
  1551. @param data Opaque data argument
  1552. @param id Event ID
  1553. @ingroup WebsRuntime
  1554. @stability Stable
  1555. */
  1556. typedef void (*WebsEventProc)(void *data, int id);
  1557. /**
  1558. Start a callback event
  1559. @description This schedules an event to run once. The event can be rescheduled in the callback by invoking
  1560. websRestartEvent.
  1561. @param delay Delay in milliseconds in which to run the callback
  1562. @param proc Callback procedure function. Signature is: void (*fn)(void *data, int id)
  1563. @param data Data reference to pass to the callback
  1564. @return A positive integer event ID
  1565. @ingroup WebsRuntime
  1566. @stability Stable
  1567. */
  1568. PUBLIC int websStartEvent(int delay, WebsEventProc proc, void *data);
  1569. /**
  1570. Stop an event
  1571. @param id Event id allocated by websStartEvent
  1572. @return Integer handle index. Otherwise return -1 on allocation errors.
  1573. @ingroup WebsRuntime
  1574. @stability Stable
  1575. */
  1576. PUBLIC void websStopEvent(int id);
  1577. /**
  1578. Restart an event
  1579. @param id Event id allocated by websStartEvent
  1580. @param delay Delay in milliseconds till the event next runs
  1581. @ingroup WebsRuntime
  1582. @stability Stable
  1583. */
  1584. PUBLIC void websRestartEvent(int id, int delay);
  1585. /**
  1586. Run due events
  1587. @ingroup WebsRuntime
  1588. @return Time delay till the next event
  1589. @internal
  1590. */
  1591. PUBLIC int websRunEvents(void);
  1592. /* Forward declare */
  1593. struct WebsRoute;
  1594. struct WebsUser;
  1595. struct WebsSession;
  1596. struct Webs;
  1597. /********************************** Upload ************************************/
  1598. #if ME_GOAHEAD_UPLOAD
  1599. /**
  1600. File upload structure
  1601. @see websUploadOpen websLookupUpload websGetUpload
  1602. @defgroup WebsUpload WebsUpload
  1603. */
  1604. typedef struct WebsUpload {
  1605. char *filename; /**< Local (temp) name of the file */
  1606. char *clientFilename; /**< Client side name of the file */
  1607. char *contentType; /**< Content type */
  1608. ssize size; /**< Uploaded file size */
  1609. } WebsUpload;
  1610. /**
  1611. Open the file upload filter
  1612. @ingroup WebsUpload
  1613. @stability Stable
  1614. */
  1615. PUBLIC void websUploadOpen(void);
  1616. /**
  1617. Get the hash of uploaded files for the request
  1618. @param wp Webs request object
  1619. @return Hash table of uploaded files
  1620. @ingroup WebsUpload
  1621. @stability Stable
  1622. */
  1623. PUBLIC WebsHash websGetUpload(struct Webs *wp);
  1624. /**
  1625. Open the file upload filter
  1626. @param wp Webs request object
  1627. @param key Form upload name
  1628. @return Upload object for the uploaded file
  1629. @ingroup WebsUpload
  1630. @stability Stable
  1631. */
  1632. PUBLIC WebsUpload *websLookupUpload(struct Webs *wp, cchar *key);
  1633. #endif
  1634. /********************************** Defines ***********************************/
  1635. #define WEBS_MAX_PORT_LEN 16 /* Max digits in port number */
  1636. #define WEBS_HASH_INIT 67 /* Hash size for form table */
  1637. #define WEBS_SESSION_HASH 31 /* Hash size for session stores */
  1638. #define WEBS_SESSION_PRUNE (60*1000) /* Prune sessions every minute */
  1639. /*
  1640. The license agreement stipulates that you must not change this definition.
  1641. */
  1642. #define WEBS_NAME "Server: GoAhead-http"
  1643. /*
  1644. Request flags
  1645. */
  1646. #define WEBS_ACCEPTED 0x1 /**< TLS connection accepted */
  1647. #define WEBS_CHUNKING 0x2 /**< Currently chunking output body data */
  1648. #define WEBS_CLOSED 0x4 /**< Connection closed, ready to free */
  1649. #define WEBS_COOKIE 0x8 /**< Cookie supplied in request */
  1650. #if DEPRECATED || 1
  1651. #define WEBS_FINALIZED 0x10 /**< Output is finalized */
  1652. #endif
  1653. #define WEBS_FORM 0x20 /**< Request is a form (url encoded data) */
  1654. #define WEBS_HEADERS_CREATED 0x40 /**< Headers have been created and buffered */
  1655. #define WEBS_HTTP11 0x80 /**< Request is using HTTP/1.1 */
  1656. #define WEBS_JSON 0x100 /**< Request has a JSON payload */
  1657. #define WEBS_KEEP_ALIVE 0x200 /**< HTTP/1.1 keep alive */
  1658. #define WEBS_REROUTE 0x400 /**< Restart route matching */
  1659. #define WEBS_RESPONSE_TRACED 0x800 /**< Started tracing the response */
  1660. #define WEBS_SECURE 0x1000 /**< Connection uses SSL */
  1661. #define WEBS_UPLOAD 0x2000 /**< Multipart-mime file upload */
  1662. #define WEBS_VARS_ADDED 0x4000 /**< Query and body form vars added */
  1663. #if ME_GOAHEAD_LEGACY
  1664. #define WEBS_LOCAL 0x8000 /**< Request from local system */
  1665. #endif
  1666. /*
  1667. Incoming chunk encoding states. Used for tx and rx chunking.
  1668. */
  1669. #define WEBS_CHUNK_UNCHUNKED 0 /**< Data is not transfer-chunk encoded */
  1670. #define WEBS_CHUNK_START 1 /**< Start of a new chunk */
  1671. #define WEBS_CHUNK_HEADER 2 /**< Preparing tx chunk header */
  1672. #define WEBS_CHUNK_DATA 3 /**< Start of chunk data */
  1673. /*
  1674. Webs state
  1675. */
  1676. #define WEBS_BEGIN 0 /**< Beginning state */
  1677. #define WEBS_CONTENT 1 /**< Ready for body data */
  1678. #define WEBS_READY 2 /**< Ready to route and start handler */
  1679. #define WEBS_RUNNING 3 /**< Processing request */
  1680. #define WEBS_COMPLETE 4 /**< Request complete */
  1681. /*
  1682. Session names
  1683. */
  1684. #define WEBS_SESSION "-goahead-session-"
  1685. #define WEBS_SESSION_USERNAME "_:USERNAME:_" /* Username variable */
  1686. /*
  1687. WebsDone flags
  1688. */
  1689. #define WEBS_CODE_MASK 0xFFFF /**< Mask valid status codes */
  1690. #define WEBS_CLOSE 0x20000 /**< Close connection */
  1691. #define WEBS_NOLOG 0x40000 /**< Don't write error to log */
  1692. /**
  1693. Callback for write I/O events
  1694. */
  1695. typedef void (*WebsWriteProc)(struct Webs *wp);
  1696. /**
  1697. GoAhead request structure. This is a per-socket connection structure.
  1698. @defgroup Webs Webs
  1699. */
  1700. typedef struct Webs {
  1701. WebsBuf rxbuf; /**< Raw receive buffer */
  1702. WebsBuf input; /**< Receive buffer after de-chunking */
  1703. WebsBuf output; /**< Transmit buffer after chunking */
  1704. WebsBuf chunkbuf; /**< Pre-chunking data buffer */
  1705. WebsBuf *txbuf;
  1706. WebsTime since; /**< Parsed if-modified-since time */
  1707. WebsTime timestamp; /**< Last transaction with browser */
  1708. WebsHash vars; /**< CGI standard variables */
  1709. int timeout; /**< Timeout handle */
  1710. char ipaddr[ME_MAX_IP]; /**< Connecting ipaddress */
  1711. char ifaddr[ME_MAX_IP]; /**< Local interface ipaddress */
  1712. int rxChunkState; /**< Rx chunk encoding state */
  1713. ssize rxChunkSize; /**< Rx chunk size */
  1714. char *rxEndp; /**< Pointer to end of raw data in input beyond endp */
  1715. ssize lastRead; /**< Number of bytes last read from the socket */
  1716. bool eof; /**< If at the end of the request content */
  1717. char txChunkPrefix[16]; /**< Transmit chunk prefix */
  1718. char *txChunkPrefixNext; /**< Current I/O pos in txChunkPrefix */
  1719. ssize txChunkPrefixLen; /**< Length of prefix */
  1720. ssize txChunkLen; /**< Length of the chunk */
  1721. int txChunkState; /**< Transmit chunk state */
  1722. char *authDetails; /**< Http header auth details */
  1723. char *authResponse; /**< Outgoing auth header */
  1724. char *authType; /**< Authorization type (Basic/DAA) */
  1725. char *contentType; /**< Body content type */
  1726. char *cookie; /**< Request cookie string */
  1727. char *decodedQuery; /**< Decoded request query */
  1728. char *digest; /**< Password digest */
  1729. char *ext; /**< Path extension */
  1730. char *filename; /**< Document path name */
  1731. char *host; /**< Requested host */
  1732. char *method; /**< HTTP request method */
  1733. char *password; /**< Authorization password */
  1734. char *path; /**< Path name without query. This is decoded. */
  1735. char *protoVersion; /**< Protocol version (HTTP/1.1)*/
  1736. char *protocol; /**< Protocol scheme (normally http|https) */
  1737. char *putname; /**< PUT temporary filename */
  1738. char *query; /**< Request query. This is decoded. */
  1739. char *realm; /**< Realm field supplied in auth header */
  1740. char *referrer; /**< The referring page */
  1741. char *url; /**< Full request url. This is not decoded. */
  1742. char *userAgent; /**< User agent (browser) */
  1743. char *username; /**< Authorization username */
  1744. int sid; /**< Socket id (handler) */
  1745. int listenSid; /**< Listen Socket id */
  1746. int port; /**< Request port number */
  1747. int state; /**< Current state */
  1748. int flags; /**< Current flags -- see above */
  1749. int code; /**< Response status code */
  1750. int routeCount; /**< Route count limiter */
  1751. ssize rxLen; /**< Rx content length */
  1752. ssize rxRemaining; /**< Remaining content to read from client */
  1753. ssize txLen; /**< Tx content length header value */
  1754. int wid; /**< Index into webs */
  1755. #if ME_GOAHEAD_CGI
  1756. char *cgiStdin; /**< Filename for CGI program input */
  1757. int cgifd; /**< File handle for CGI program input */
  1758. #endif
  1759. #if !ME_ROM
  1760. int putfd; /**< File handle to write PUT data */
  1761. #endif
  1762. int docfd; /**< File descriptor for document being served */
  1763. ssize written; /**< Bytes actually transferred */
  1764. ssize putLen; /**< Bytes read by a PUT request */
  1765. uint finalized: 1; /**< Request has been completed */
  1766. uint error: 1; /**< Request has an error */
  1767. uint connError: 1; /**< Request has a connection error */
  1768. WebsHash responseCookies; /**< Outgoing cookies */
  1769. struct WebsSession *session; /**< Session record */
  1770. struct WebsRoute *route; /**< Request route */
  1771. struct WebsUser *user; /**< User auth record */
  1772. WebsWriteProc writeData; /**< Handler write I/O event callback. Used by fileHandler */
  1773. int encoded; /**< True if the password is MD5(username:realm:password) */
  1774. #if ME_GOAHEAD_DIGEST
  1775. char *cnonce; /**< check nonce */
  1776. char *digestUri; /**< URI found in digest header */
  1777. char *nonce; /**< opaque-to-client string sent by server */
  1778. char *nc; /**< nonce count */
  1779. char *opaque; /**< opaque value passed from server */
  1780. char *qop; /**< quality operator */
  1781. #endif
  1782. #if ME_GOAHEAD_UPLOAD
  1783. int upfd; /**< Upload file handle */
  1784. WebsHash files; /**< Uploaded files */
  1785. char *boundary; /**< Mime boundary (static) */
  1786. ssize boundaryLen; /**< Boundary length */
  1787. int uploadState; /**< Current file upload state */
  1788. WebsUpload *currentFile; /**< Current file context */
  1789. char *clientFilename; /**< Current file filename */
  1790. char *uploadTmp; /**< Current temp filename for upload data */
  1791. char *uploadVar; /**< Current upload form variable name */
  1792. #endif
  1793. void *ssl; /**< SSL context */
  1794. IPMI20_SESSION_T ipmi_session;
  1795. } Webs;
  1796. #if ME_GOAHEAD_LEGACY
  1797. #define WEBS_LEGACY_HANDLER 0x1 /* Using legacy calling sequence */
  1798. #endif
  1799. /**
  1800. GoAhead handler service callback
  1801. @param wp Webs request object
  1802. @return True if the handler serviced the request
  1803. @ingroup Webs
  1804. @stability Stable
  1805. */
  1806. typedef bool (*WebsHandlerProc)(Webs *wp);
  1807. /**
  1808. GoAhead handler close to release memory prior to shutdown.
  1809. @description This callback is invoked when GoAhead is shutting down.
  1810. @ingroup Webs
  1811. @stability Stable
  1812. */
  1813. typedef void (*WebsHandlerClose)(void);
  1814. /**
  1815. GoAhead handler object
  1816. @ingroup Webs
  1817. @stability Stable
  1818. */
  1819. typedef struct WebsHandler {
  1820. char *name; /**< Handler name */
  1821. WebsHandlerProc match; /**< Handler match callback */
  1822. WebsHandlerProc service; /**< Handler service callback */
  1823. WebsHandlerClose close; /**< Handler close callback */
  1824. int flags; /**< Handler control flags */
  1825. } WebsHandler;
  1826. /**
  1827. Action callback
  1828. @param wp Webs request object
  1829. @ingroup Webs
  1830. @stability Stable
  1831. */
  1832. typedef void (*WebsAction)(Webs *wp);
  1833. #if ME_GOAHEAD_LEGACY
  1834. typedef void (*WebsProc)(Webs *wp, char *path, char *query);
  1835. #endif
  1836. /**
  1837. Error code list
  1838. @ingroup Webs
  1839. @stability Stable
  1840. */
  1841. typedef struct WebsError {
  1842. int code; /**< HTTP error code */
  1843. char *msg; /**< HTTP error message */
  1844. } WebsError;
  1845. /**
  1846. Mime type list
  1847. @ingroup Webs
  1848. @stability Stable
  1849. */
  1850. typedef struct WebsMime {
  1851. char *type; /**< Mime type */
  1852. char *ext; /**< File extension */
  1853. } WebsMime;
  1854. /**
  1855. File information structure.
  1856. @ingroup Webs
  1857. @stability Stable
  1858. */
  1859. typedef struct WebsFileInfo {
  1860. ulong size; /**< File length */
  1861. int isDir; /**< Set if directory */
  1862. WebsTime mtime; /**< Modified time */
  1863. } WebsFileInfo;
  1864. /**
  1865. Compiled Rom Page Index
  1866. @ingroup Webs
  1867. @stability Stable
  1868. */
  1869. typedef struct WebsRomIndex {
  1870. char *path; /**< Web page URL path */
  1871. uchar *page; /**< Web page data */
  1872. int size; /**< Size of web page in bytes */
  1873. Offset pos; /**< Current read position */
  1874. } WebsRomIndex;
  1875. #if ME_ROM
  1876. /**
  1877. List of documents to service when built with ROM support
  1878. @ingroup Webs
  1879. @stability Stable
  1880. */
  1881. PUBLIC_DATA WebsRomIndex websRomIndex[];
  1882. #endif
  1883. #define WEBS_DECODE_TOKEQ 1 /**< Decode base 64 blocks up to a NULL or equals */
  1884. /**
  1885. Accept a new connection
  1886. @param sid Socket ID handle for the newly accepted socket
  1887. @param ipaddr IP address originating the connection.
  1888. @param port Port number originating the connection.
  1889. @param listenSid Socket ID of the listening socket
  1890. @return Zero if successful, otherwise -1
  1891. @ingroup Webs
  1892. @stability Stable
  1893. */
  1894. PUBLIC int websAccept(int sid, cchar *ipaddr, int port, int listenSid);
  1895. /**
  1896. Open the action handler
  1897. @ingroup Webs
  1898. @stability Stable
  1899. */
  1900. PUBLIC void websActionOpen(void);
  1901. /**
  1902. Allocate a new Webs object
  1903. @param sid Socket ID handle for the newly accepted socket
  1904. @return The webs[] handle index for the allocated Webs object
  1905. @ingroup Webs
  1906. @stability Stable
  1907. */
  1908. PUBLIC int websAlloc(int sid);
  1909. /**
  1910. Cancel the request timeout.
  1911. @description Handlers may choose to manually manage the request timeout. This routine will disable the
  1912. centralized management of the timeout for this request.
  1913. @param wp Webs request object
  1914. @ingroup Webs
  1915. @stability Stable
  1916. */
  1917. PUBLIC void websCancelTimeout(Webs *wp);
  1918. #if ME_GOAHEAD_CGI
  1919. /**
  1920. Open the CGI handler
  1921. @return Zero if successful, otherwise -1
  1922. @ingroup Webs
  1923. @stability Stable
  1924. */
  1925. PUBLIC int websCgiOpen(void);
  1926. /**
  1927. CGI handler service callback
  1928. @param wp Webs object
  1929. @return Returns 1 if the request was handled.
  1930. @ingroup Webs
  1931. @stability Stable
  1932. */
  1933. PUBLIC int websCgiHandler(Webs *wp);
  1934. /**
  1935. Poll for output from CGI processes and output.
  1936. @return Time delay till next poll
  1937. @ingroup Webs
  1938. @stability Stable
  1939. */
  1940. PUBLIC int websCgiPoll(void);
  1941. /* Internal */
  1942. PUBLIC bool cgiHandler(Webs *wp);
  1943. #endif /* ME_GOAHEAD_CGI */
  1944. /**
  1945. Close the core GoAhead web server module
  1946. @description Invoked when GoAhead is shutting down.
  1947. @ingroup Webs
  1948. @stability Stable
  1949. */
  1950. PUBLIC void websClose(void);
  1951. /**
  1952. Close an open file
  1953. @param fd Open file handle returned by websOpenFile
  1954. @ingroup Webs
  1955. @stability Stable
  1956. */
  1957. PUBLIC void websCloseFile(int fd);
  1958. /**
  1959. Compare a request variable
  1960. @param wp Webs request object
  1961. @param var Variable name
  1962. @param value Value to compare with
  1963. @return True if the value matches. Otherwise return 0
  1964. @ingroup Webs
  1965. @stability Stable
  1966. */
  1967. PUBLIC int websCompareVar(Webs *wp, cchar *var, cchar *value);
  1968. /**
  1969. Consume input from the request input buffer.
  1970. @description This is called by handlers when consuming data from the request input buffer.
  1971. This call updates the input service pointers and compacts the input buffer if required.
  1972. @param wp Webs request object
  1973. @param nbytes Number of bytes the handler has consumed from the input buffer.
  1974. @ingroup Webs
  1975. @stability Stable
  1976. */
  1977. PUBLIC void websConsumeInput(Webs *wp, ssize nbytes);
  1978. /**
  1979. Decode the string using base-64 encoding
  1980. @description This modifies the original string
  1981. @param str String to decode
  1982. @return The original string. Caller must not free.
  1983. @ingroup Webs
  1984. @stability Stable
  1985. */
  1986. PUBLIC char *websDecode64(char *str);
  1987. /**
  1988. Decode a block using base-46 encoding
  1989. @param str String to decode. The string must be null terminated.
  1990. @param len Reference to an integer holding the length of the decoded string.
  1991. @param flags Reserved.
  1992. @return The original string.
  1993. @ingroup Webs
  1994. @stability Stable
  1995. */
  1996. PUBLIC char *websDecode64Block(char *str, ssize *len, int flags);
  1997. /**
  1998. Decode a URL expanding %NN encoding
  1999. @description Supports insitu decoding. i.e. Input and output buffers may be the same.
  2000. @param decoded Buffer to hold the decoded URL
  2001. @param input Input URL or buffer to decode
  2002. @param len Length of the decoded buffer.
  2003. @ingroup Webs
  2004. @stability Stable
  2005. */
  2006. PUBLIC void websDecodeUrl(char *decoded, char *input, ssize len);
  2007. /**
  2008. Define a request handler
  2009. @param name Name of the handler
  2010. @param match Handler callback match procedure. Invoked to match the request with the handler.
  2011. The handler should return true to accept the request.
  2012. @param service Handler callback service procedure. Invoked to service each request.
  2013. @param close Handler callback close procedure. Called when GoAhead is shutting down.
  2014. @param flags Set to WEBS_LEGACY_HANDLER to support the legacy handler API calling sequence.
  2015. @return Zero if successful, otherwise -1.
  2016. @ingroup Webs
  2017. @stability Stable
  2018. */
  2019. PUBLIC int websDefineHandler(cchar *name, WebsHandlerProc match, WebsHandlerProc service, WebsHandlerClose close, int flags);
  2020. /**
  2021. Complete a request.
  2022. @description A handler should call websDone() to complete the request.
  2023. @param wp Webs request object
  2024. @ingroup Webs
  2025. @stability Stable
  2026. */
  2027. PUBLIC void websDone(Webs *wp);
  2028. /**
  2029. Encode a string using base-64 encoding
  2030. @description The string is encoded insitu.
  2031. @param str String to encode
  2032. @return The original string.
  2033. @ingroup Webs
  2034. @stability Stable
  2035. */
  2036. PUBLIC char *websEncode64(char *str);
  2037. /**
  2038. Encode a block using base-64 encoding
  2039. @description The string is encoded insitu.
  2040. @param str String to encode.
  2041. @param len Length of string to encode
  2042. @return The original string.
  2043. @ingroup Webs
  2044. @stability Stable
  2045. */
  2046. PUBLIC char *websEncode64Block(char *str, ssize len);
  2047. /**
  2048. Escape unsafe characters in a string
  2049. @param str String to escape
  2050. @return An allocated block containing the escaped string. Caller must free.
  2051. @ingroup Webs
  2052. @stability Stable
  2053. */
  2054. PUBLIC char *websEscapeHtml(cchar *str);
  2055. /**
  2056. Complete a request with an error response
  2057. @param wp Webs request object
  2058. @param code HTTP status code
  2059. @param fmt Message printf style format
  2060. @param ... Format args
  2061. @ingroup Webs
  2062. @stability Stable
  2063. */
  2064. PUBLIC void websError(Webs *wp, int code, cchar *fmt, ...);
  2065. /**
  2066. Get a message for a HTTP status code
  2067. @param code HTTP status code
  2068. @return Http status message. Caller must not free.
  2069. @ingroup Webs
  2070. @stability Stable
  2071. */
  2072. PUBLIC cchar *websErrorMsg(int code);
  2073. /**
  2074. Open and initialize the file handler
  2075. @ingroup Webs
  2076. @stability Stable
  2077. */
  2078. PUBLIC void websFileOpen(void);
  2079. /**
  2080. Flush buffered transmit data and compact the transmit buffer to make room for more data
  2081. @description This call initiates sending buffered data. If blocking mode is selected via the block parameter,
  2082. this call will wait until all the data has been sent to the O/S for transmission to the client.
  2083. If block is false, the flush will be initiated and the call will return immediately without blocking.
  2084. @param wp Webs request object
  2085. @param block Set to true to wait for all data to be written to the socket. Set to false to
  2086. write whatever the socket can absorb without blocking.
  2087. @return -1 for I/O errors. Return zero if there is more data remaining in the buffer. Return 1 if the
  2088. contents of the transmit buffer are fully written and the buffer is now empty.
  2089. @ingroup Webs
  2090. @stability Stable
  2091. */
  2092. PUBLIC int websFlush(Webs *wp, bool block);
  2093. /**
  2094. Free the webs request object.
  2095. @description Callers should call websDone to complete requests prior to invoking websFree.
  2096. @param wp Webs request object
  2097. @ingroup Webs
  2098. @stability Stable
  2099. */
  2100. PUBLIC void websFree(Webs *wp);
  2101. /**
  2102. Get the background execution flag
  2103. @description If GoAhead is invoked with --background, it will run as a daemon in the background.
  2104. @return True if GoAhead is running in the background.
  2105. @ingroup Webs
  2106. @stability Stable
  2107. */
  2108. PUBLIC int websGetBackground(void);
  2109. #if ME_GOAHEAD_CGI
  2110. /**
  2111. Get a unique temporary filename for CGI communications
  2112. @return Allocated filename string. Caller must free.
  2113. @ingroup Webs
  2114. @stability Stable
  2115. */
  2116. PUBLIC char *websGetCgiCommName(void);
  2117. #endif /* ME_GOAHEAD_CGI */
  2118. /**
  2119. Get the request cookie if supplied
  2120. @param wp Webs request object
  2121. @return Cookie string if defined, otherwise null. Caller must not free.
  2122. @ingroup Webs
  2123. @stability Stable
  2124. */
  2125. PUBLIC cchar *websGetCookie(Webs *wp);
  2126. /**
  2127. Get a date as a string
  2128. @description If sbuf is supplied, it is used to calculate the date. Otherwise, the current time is used.
  2129. @param sbuf File info object
  2130. @return An allocated date string. Caller should free.
  2131. @ingroup Webs
  2132. @stability Stable
  2133. */
  2134. PUBLIC char *websGetDateString(WebsFileInfo *sbuf);
  2135. /**
  2136. Get the debug flag
  2137. @description If GoAhead is invoked with --debugger, the debug flag will be set to true
  2138. @return True if GoAhead is running in debug mode.
  2139. @ingroup Webs
  2140. @stability Stable
  2141. */
  2142. PUBLIC int websGetDebug(void);
  2143. /**
  2144. Get the base file directory for a request
  2145. @description Returns the request route directory if defined, otherwise returns the documents directory.
  2146. @param wp Webs request object
  2147. @return Path name string. Caller should not free.
  2148. @ingroup Webs
  2149. @stability Stable
  2150. */
  2151. PUBLIC cchar *websGetDir(Webs *wp);
  2152. /**
  2153. Get the GoAhead base documents directory
  2154. @description The documents directory is defined at build time and may be overridden by the GoAhead command line.
  2155. @return Path string for the documents directory.
  2156. @ingroup Webs
  2157. @stability Stable
  2158. */
  2159. PUBLIC char *websGetDocuments(void);
  2160. /**
  2161. Get the request EOF status
  2162. @description The request EOF status is set to true when all the request body (POST|PUT) data has been received.
  2163. @param wp Webs request object
  2164. @return True if all the request body data has been received.
  2165. @ingroup Webs
  2166. @stability Stable
  2167. */
  2168. PUBLIC int websGetEof(Webs *wp);
  2169. /**
  2170. Get the request URI extension
  2171. @param wp Webs request object
  2172. @return The URI filename extension component. Caller should not free.
  2173. @ingroup Webs
  2174. @stability Stable
  2175. */
  2176. PUBLIC cchar *websGetExt(Webs *wp);
  2177. /**
  2178. Get the request filename
  2179. @description The URI is mapped to a filename by decoding and prepending with the request directory.
  2180. @param wp Webs request object
  2181. @return Filename string. Caller should not free.
  2182. @ingroup Webs
  2183. @stability Stable
  2184. */
  2185. PUBLIC cchar *websGetFilename(Webs *wp);
  2186. /**
  2187. Get the request host
  2188. @description The request host is set to the Host HTTP header value if it is present. Otherwise it is set to
  2189. the request URI hostname.
  2190. @param wp Webs request object
  2191. @return Host string. Caller should not free.
  2192. @ingroup Webs
  2193. @stability Stable
  2194. */
  2195. PUBLIC cchar *websGetHost(Webs *wp);
  2196. /**
  2197. Get the request interface address
  2198. @param wp Webs request object
  2199. @return Network interface string. Caller should not free.
  2200. @ingroup Webs
  2201. @stability Stable
  2202. */
  2203. PUBLIC cchar *websGetIfaddr(Webs *wp);
  2204. /**
  2205. Get the default index document name
  2206. @description The default index is "index.html" and can be updated via websSetIndex.
  2207. @return Index name string. Caller should not free.
  2208. @ingroup Webs
  2209. @stability Stable
  2210. */
  2211. PUBLIC cchar *websGetIndex(void);
  2212. /**
  2213. Get the request method
  2214. @param wp Webs request object
  2215. @return HTTP method string. Caller should not free.
  2216. @ingroup Webs
  2217. @stability Stable
  2218. */
  2219. PUBLIC cchar *websGetMethod(Webs *wp);
  2220. /**
  2221. Get the request password
  2222. @description The request password may be encoded depending on the authentication scheme.
  2223. See wp->encoded to test if it is encoded.
  2224. @param wp Webs request object
  2225. @return Password string. Caller should not free.
  2226. @ingroup Webs
  2227. @stability Stable
  2228. */
  2229. PUBLIC cchar *websGetPassword(Webs *wp);
  2230. /**
  2231. Get the request path
  2232. @description The URI path component excludes the http protocol, hostname, port, reference and query components.
  2233. It always beings with "/".
  2234. @param wp Webs request object
  2235. @return Request path string. Caller should not free.
  2236. @ingroup Webs
  2237. @stability Stable
  2238. */
  2239. PUBLIC cchar *websGetPath(Webs *wp);
  2240. /**
  2241. Get the request TCP/IP port
  2242. @param wp Webs request object
  2243. @return TCP/IP Port integer
  2244. @ingroup Webs
  2245. @stability Stable
  2246. */
  2247. PUBLIC int websGetPort(Webs *wp);
  2248. /**
  2249. Get the request HTTP protocol
  2250. @description This will be set to either "http" or "https"
  2251. @param wp Webs request object
  2252. @return Protocol string. Caller should not free.
  2253. @ingroup Webs
  2254. @stability Stable
  2255. */
  2256. PUBLIC cchar *websGetProtocol(Webs *wp);
  2257. /**
  2258. Get the request query component
  2259. @param wp Webs request object
  2260. @return Request query string. Caller should not free.
  2261. @ingroup Webs
  2262. @stability Stable
  2263. */
  2264. PUBLIC cchar *websGetQuery(Webs *wp);
  2265. /**
  2266. Get the server host name
  2267. @return Host name string. Caller should not free.
  2268. @ingroup Webs
  2269. @stability Stable
  2270. */
  2271. PUBLIC cchar *websGetServer(void);
  2272. /**
  2273. Get the server host name with port number.
  2274. @return Host name string with port number. Caller should not free.
  2275. @ingroup Webs
  2276. @stability Stable
  2277. */
  2278. PUBLIC cchar *websGetServerUrl(void);
  2279. /**
  2280. Get the server IP address
  2281. @return Server IP address string. Caller should not free.
  2282. @ingroup Webs
  2283. @stability Stable
  2284. */
  2285. PUBLIC cchar *websGetServerAddress(void);
  2286. /**
  2287. Get the server IP address with port number
  2288. @return Server IP:PORT address string. Caller should not free.
  2289. @ingroup Webs
  2290. @stability Stable
  2291. */
  2292. PUBLIC cchar *websGetServerAddressUrl(void);
  2293. /**
  2294. Get the request URI
  2295. @description This returns the request URI. This may be modified if the request is rewritten via websRewrite
  2296. @param wp Webs request object
  2297. @return URI string. Caller should not free.
  2298. @ingroup Webs
  2299. @stability Stable
  2300. */
  2301. PUBLIC cchar *websGetUrl(Webs *wp);
  2302. /**
  2303. Get the client User-Agent HTTP header
  2304. @param wp Webs request object
  2305. @return User-Agent string. Caller should not free.
  2306. @ingroup Webs
  2307. @stability Stable
  2308. */
  2309. PUBLIC cchar *websGetUserAgent(Webs *wp);
  2310. /**
  2311. Get the request username
  2312. @description If the request is authenticated, this call returns the username supplied during authentication.
  2313. @param wp Webs request object
  2314. @return Username string if defined, otherwise null. Caller should not free.
  2315. @ingroup Webs
  2316. @stability Stable
  2317. */
  2318. PUBLIC cchar *websGetUsername(Webs *wp);
  2319. /**
  2320. Get a request variable
  2321. @description Request variables are defined for HTTP headers of the form HTTP_*.
  2322. Some request handlers also define their own variables. For example: CGI environment variables.
  2323. @param wp Webs request object
  2324. @param name Variable name
  2325. @param defaultValue Default value to return if the variable is not defined
  2326. @return Variable value string. Caller should not free.
  2327. @ingroup Webs
  2328. @stability Stable
  2329. */
  2330. PUBLIC cchar *websGetVar(Webs *wp, cchar *name, cchar *defaultValue);
  2331. /**
  2332. Listen on a TCP/IP address endpoint
  2333. @description The URI is mapped to a filename by decoding and prepending with the request directory.
  2334. For IPv6 addresses, use the format: [aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh:iiii]:port.
  2335. @param endpoint IPv4 or IPv6 address on which to listen.
  2336. @return Positive integer holding a Socket ID handle if successful, otherwise -1.
  2337. @ingroup Webs
  2338. @stability Stable
  2339. */
  2340. PUBLIC int websListen(cchar *endpoint);
  2341. /**
  2342. Get an MD5 digest of a string
  2343. @param str String to analyze.
  2344. @return Allocated MD5 checksum. Caller should free.
  2345. @ingroup Webs
  2346. @stability Stable
  2347. */
  2348. PUBLIC char *websMD5(cchar *str);
  2349. /**
  2350. Get an MD5 digest of a block and optionally prepend a prefix.
  2351. @param buf Block to analyze
  2352. @param length Length of block
  2353. @param prefix Optional prefix to prepend to the MD5 sum.
  2354. @return Allocated MD5 checksum. Caller should free.
  2355. @ingroup Webs
  2356. @stability Stable
  2357. */
  2358. PUBLIC char *websMD5Block(cchar *buf, ssize length, cchar *prefix);
  2359. /**
  2360. Normalize a URI path
  2361. @description This removes "./", "../" and redundant separators.
  2362. @param path URI path to normalize
  2363. @return An allocated normalized URI path. Caller must free.
  2364. @ingroup Webs
  2365. @stability Stable
  2366. */
  2367. PUBLIC char *websNormalizeUriPath(cchar *path);
  2368. /**
  2369. Take not of the request activity and mark the time.
  2370. @description This is used to defer the request timeout whenever there is request I/O activity.
  2371. @param wp Webs request object
  2372. @ingroup Webs
  2373. @stability Stable
  2374. */
  2375. PUBLIC void websNoteRequestActivity(Webs *wp);
  2376. /**
  2377. Close the runtime code.
  2378. @description Called from websClose
  2379. @ingroup Webs
  2380. @internal
  2381. */
  2382. PUBLIC void websRuntimeClose(void);
  2383. /**
  2384. Open the runtime code.
  2385. @description Called from websOpen
  2386. @return Zero if successful
  2387. @ingroup Webs
  2388. @internal
  2389. */
  2390. PUBLIC int websRuntimeOpen(void);
  2391. /**
  2392. Open the web server
  2393. @description This initializes the web server and defines the documents directory.
  2394. @param documents Optional web documents directory. If set to null, the build time ME_GOAHEAD_DOCUMENTS value
  2395. is used for the documents directory.
  2396. @param routes Optional filename for a route configuration file to load. Additional route or
  2397. authentication configuration files can be loaded via websLoad.
  2398. @param routes Webs request object
  2399. @return Zero if successful, otherwise -1.
  2400. @ingroup Webs
  2401. @stability Stable
  2402. */
  2403. PUBLIC int websOpen(cchar *documents, cchar *routes);
  2404. /**
  2405. Close the O/S dependent code.
  2406. @description Called from websClose
  2407. @ingroup Webs
  2408. @internal
  2409. */
  2410. PUBLIC void websOsClose(void);
  2411. /**
  2412. Open the O/S dependent code.
  2413. @description Called from websOpen
  2414. @return Zero if successful, otherwise -1.
  2415. @ingroup Webs
  2416. @internal
  2417. */
  2418. PUBLIC int websOsOpen(void);
  2419. /**
  2420. Open the web page document for the current request
  2421. @param path Filename path to open
  2422. @param flags File open flags
  2423. @param mode Permissions mask
  2424. @return Positive file handle if successful, otherwise -1.
  2425. @ingroup Webs
  2426. @stability Stable
  2427. */
  2428. PUBLIC int websOpenFile(cchar *path, int flags, int mode);
  2429. /**
  2430. Open the options handler
  2431. @return Zero if successful, otherwise -1.
  2432. @ingroup Webs
  2433. @stability Stable
  2434. */
  2435. PUBLIC int websOptionsOpen(void);
  2436. /**
  2437. Close the document page
  2438. @param wp Webs request object
  2439. @ingroup Webs
  2440. @stability Stable
  2441. */
  2442. PUBLIC void websPageClose(Webs *wp);
  2443. /**
  2444. Test if the document page for the request corresponds to a directory
  2445. @param wp Webs request object
  2446. @return True if the filename is a directory
  2447. @ingroup Webs
  2448. @stability Stable
  2449. */
  2450. PUBLIC int websPageIsDirectory(Webs *wp);
  2451. /**
  2452. Open a web page document for a request
  2453. @param wp Webs request object
  2454. @param mode File open mode. Select from O_RDONLY and O_BINARY. Rom files systems ignore this argument.
  2455. @param perms Ignored
  2456. @return File handle if successful, otherwise -1.
  2457. @ingroup Webs
  2458. @stability Stable
  2459. */
  2460. PUBLIC int websPageOpen(Webs *wp, int mode, int perms);
  2461. /**
  2462. Read data from the request page document
  2463. @param wp Webs request object
  2464. @param buf Buffer for the read data
  2465. @param size Size of buf
  2466. @return Count of bytes read if successful, otherwise -1.
  2467. @ingroup Webs
  2468. @stability Stable
  2469. */
  2470. PUBLIC ssize websPageReadData(Webs *wp, char *buf, ssize size);
  2471. /**
  2472. Seek to a position in the request page document
  2473. @param wp Webs request object
  2474. @param offset Offset of location in the file to seek to. This is relative to the specified origin.
  2475. @param origin Set to SEEK_CUR, SEEK_SET or SEEK_END to position relative to the current position,
  2476. beginning or end of the document.
  2477. @ingroup Webs
  2478. @stability Stable
  2479. */
  2480. PUBLIC void websPageSeek(Webs *wp, Offset offset, int origin);
  2481. /**
  2482. Get file status for the current request document
  2483. @param wp Webs request object
  2484. @param sbuf File information structure to modify with file status
  2485. @return Zero if successful, otherwise -1.
  2486. @ingroup Webs
  2487. @stability Stable
  2488. */
  2489. PUBLIC int websPageStat(Webs *wp, WebsFileInfo *sbuf);
  2490. #if !ME_ROM
  2491. /**
  2492. Process request PUT body data
  2493. @description This routine is called by the core HTTP engine to process request PUT data.
  2494. @param wp Webs request object
  2495. @return True if processing the request can proceed.
  2496. @ingroup Webs
  2497. @stability Stable
  2498. */
  2499. PUBLIC bool websProcessPutData(Webs *wp);
  2500. #endif
  2501. /**
  2502. Pump the state machine
  2503. @description This routine will advance the connection state machine in response to events.
  2504. @param wp Webs request object
  2505. @ingroup Webs
  2506. @stability Stable
  2507. */
  2508. PUBLIC void websPump(Webs *wp);
  2509. /**
  2510. Define an action callback for use with the action handler.
  2511. @description The action handler binds a C function to a URI under "/action".
  2512. @param name URI path suffix. This suffix is added to "/action" to form the bound URI path.
  2513. @param fun Callback function. The signature is void (*WebsAction)(Webs *wp);
  2514. @return Zero if successful, otherwise -1.
  2515. @ingroup Webs
  2516. @stability Stable
  2517. */
  2518. PUBLIC int websDefineAction(cchar *name, void *fun);
  2519. /**
  2520. Read data from an open file
  2521. @param fd Open file handle returned by websOpenFile
  2522. @param buf Buffer for the read data
  2523. @param size Size of buf
  2524. @return Count of bytes read if successful, otherwise -1.
  2525. @ingroup Webs
  2526. @stability Stable
  2527. */
  2528. PUBLIC ssize websReadFile(int fd, char *buf, ssize size);
  2529. /**
  2530. Read all the data from a file
  2531. @param path File path to read from
  2532. @return An allocated buffer containing the file data with an appended null. Caller must free.
  2533. @ingroup Webs
  2534. @stability Stable
  2535. */
  2536. PUBLIC char *websReadWholeFile(cchar *path);
  2537. /**
  2538. Redirect the client to a new URL.
  2539. @description This creates a response to the client with a Location header directing the client to a new location.
  2540. The response uses a 302 HTTP status code.
  2541. @param wp Webs request object
  2542. @param url URL to direct the client to.
  2543. @ingroup Webs
  2544. @stability Stable
  2545. */
  2546. PUBLIC void websRedirect(Webs *wp, cchar *url);
  2547. /**
  2548. Redirect the client to a new URI
  2549. @description The routing configuration file can define redirection routes for various HTTP status codes.
  2550. This routine will utilize the appropriate route redirection based on the request route and specified status code.
  2551. @param wp Webs request object
  2552. @param status HTTP status code to use in selecting the route redirection.
  2553. @return Zero if successful, otherwise -1.
  2554. @ingroup Webs
  2555. @stability Stable
  2556. */
  2557. PUBLIC int websRedirectByStatus(Webs *wp, int status);
  2558. /**
  2559. Create and send a request response
  2560. @description This creates a response for the current request using the specified HTTP status code and
  2561. the supplied message.
  2562. @param wp Webs request object
  2563. @param status HTTP status code.
  2564. @param msg Response message body
  2565. @return Zero if successful, otherwise -1.
  2566. @ingroup Webs
  2567. @stability Stable
  2568. */
  2569. PUBLIC void websResponse(Webs *wp, int status, cchar *msg);
  2570. /**
  2571. Rewrite a request
  2572. @description Handlers may choose to not process a request but rather rewrite requests and then reroute.
  2573. @param wp Webs request object
  2574. @param url New request URL.
  2575. @return Zero if successful, otherwise -1.
  2576. @ingroup Webs
  2577. @stability Stable
  2578. */
  2579. PUBLIC int websRewriteRequest(Webs *wp, cchar *url);
  2580. /**
  2581. Open the file system module
  2582. @return Zero if successful, otherwise -1.
  2583. @ingroup Webs
  2584. @stability Stable
  2585. */
  2586. PUBLIC int websFsOpen(void);
  2587. /**
  2588. Close the file system module
  2589. @ingroup Webs
  2590. @stability Stable
  2591. */
  2592. PUBLIC void websFsClose(void);
  2593. /**
  2594. Seek to a position in the current request page document
  2595. @param fd Open file handle returned by websOpenFile
  2596. @param offset Location in the file to seek to.
  2597. @param origin Set to SEEK_CUR, SEEK_SET or SEEK_END to position relative to the current position,
  2598. beginning or end of the document.
  2599. @ingroup Webs
  2600. @stability Stable
  2601. */
  2602. PUBLIC Offset websSeekFile(int fd, Offset offset, int origin);
  2603. /**
  2604. Get file status for a file
  2605. @param path Filename path
  2606. @param sbuf File information structure to modify with file status
  2607. @return Zero if successful, otherwise -1.
  2608. @ingroup Webs
  2609. @stability Stable
  2610. */
  2611. PUBLIC int websStatFile(cchar *path, WebsFileInfo *sbuf);
  2612. /**
  2613. One line embedding API.
  2614. @description This call will also open auth.txt and route.txt for authentication and routing configuration.
  2615. @param endpoint IP:PORT address on which to listen
  2616. @param documents Directory containing web documents to serve
  2617. @ingroup Webs
  2618. @stability Stable
  2619. */
  2620. PUBLIC int websServer(cchar *endpoint, cchar *documents);
  2621. /**
  2622. Service I/O events until finished
  2623. @description This will wait for socket events and service those until *finished is set to true
  2624. @param finished Integer location to test. If set to true, then exit. Note: setting finished will not
  2625. automatically wake up the service routine.
  2626. @ingroup Webs
  2627. @stability Stable
  2628. */
  2629. PUBLIC void websServiceEvents(int *finished);
  2630. /**
  2631. Set the background processing flag
  2632. @param on Value to set the background flag to.
  2633. @ingroup Webs
  2634. @internal
  2635. */
  2636. PUBLIC void websSetBackground(int on);
  2637. /**
  2638. Define a background write I/O event callback
  2639. @param wp Webs request object
  2640. @param proc Write callback
  2641. */
  2642. PUBLIC void websSetBackgroundWriter(Webs *wp, WebsWriteProc proc);
  2643. /*
  2644. Flags for websSetCookie
  2645. */
  2646. #define WEBS_COOKIE_SECURE 0x1 /**< Flag for websSetCookie for secure cookies (https only) */
  2647. #define WEBS_COOKIE_HTTP 0x2 /**< Flag for websSetCookie for http cookies (http only) */
  2648. #define WEBS_COOKIE_SAME_LAX 0x4 /**< Flag for websSetCookie for SameSite=Lax */
  2649. #define WEBS_COOKIE_SAME_STRICT 0x8 /**< Flag for websSetCookie for SameSite=Strict */
  2650. /**
  2651. Define a cookie to include in the response
  2652. @param wp Webs request object
  2653. @param name Cookie name
  2654. @param value Cookie value
  2655. @param path URI path prefix applicable for this cookie
  2656. @param domain Domain applicable for this cookie
  2657. @param lifespan Cookie lifespan in seconds
  2658. @param flags Set to WEBS_COOKIE_SECURE for https only. Set to WEBS_COOKIE_HTTP for http only.
  2659. Otherwise the cookie applies to both http and https requests. Or in WEBS_COOKIE_SAME_LAX for SameSite=Lax
  2660. and WEBS_COOKIE_SAME_STRICT for SameSite=Strict.
  2661. @return Zero if successful, otherwise -1.
  2662. @ingroup Webs
  2663. @stability Stable
  2664. */
  2665. PUBLIC void websSetCookie(Webs *wp, cchar *name, cchar *value, cchar *path, cchar *domain, int lifespan, int flags);
  2666. /**
  2667. Set the debug processing flag
  2668. @param on Value to set the debug flag to.
  2669. @ingroup Webs
  2670. @internal
  2671. */
  2672. PUBLIC void websSetDebug(int on);
  2673. /**
  2674. Set the web documents directory
  2675. @description The web documents directory is used when resolving request URIs into filenames.
  2676. @param dir Directory path to use
  2677. @ingroup Webs
  2678. @stability Stable
  2679. */
  2680. PUBLIC void websSetDocuments(cchar *dir);
  2681. /**
  2682. Create the CGI environment variables for the current request.
  2683. @param wp Webs request object
  2684. @ingroup Webs
  2685. @stability Stable
  2686. */
  2687. PUBLIC void websSetEnv(Webs *wp);
  2688. /**
  2689. Create request variables for query and POST body data
  2690. @description This creates request variables if the request is a POST form (has a Content-Type of
  2691. application/x-www-form-urlencoded). The POST body data is consumed from the input buffer.
  2692. @param wp Webs request object
  2693. @ingroup Webs
  2694. @stability Stable
  2695. */
  2696. PUBLIC void websSetFormVars(Webs *wp);
  2697. /**
  2698. Define the host name for the server
  2699. @param host String host name
  2700. @ingroup Webs
  2701. @stability Stable
  2702. */
  2703. PUBLIC void websSetHost(cchar *host);
  2704. /**
  2705. Define the host IP address
  2706. @param ipaddr Host IP address
  2707. @ingroup Webs
  2708. @stability Stable
  2709. */
  2710. PUBLIC void websSetIpAddr(cchar *ipaddr);
  2711. /**
  2712. Create and send a request response
  2713. @description This creates a response for the current request using the specified HTTP status code and
  2714. the supplied message.
  2715. @param filename Web document name to use as the index. This should not contain any directory components.
  2716. @ingroup Webs
  2717. @stability Stable
  2718. */
  2719. PUBLIC void websSetIndex(cchar *filename);
  2720. /**
  2721. Create request variables for query string data
  2722. @param wp Webs request object
  2723. @ingroup Webs
  2724. @stability Stable
  2725. */
  2726. PUBLIC void websSetQueryVars(Webs *wp);
  2727. /**
  2728. Set the response HTTP status code
  2729. @param wp Webs request object
  2730. @param status HTTP status code
  2731. @ingroup Webs
  2732. @stability Stable
  2733. */
  2734. PUBLIC void websSetStatus(Webs *wp, int status);
  2735. /**
  2736. Set the response body content length
  2737. @param wp Webs request object
  2738. @param length Length value to use
  2739. @ingroup Webs
  2740. @stability Stable
  2741. */
  2742. PUBLIC void websSetTxLength(Webs *wp, ssize length);
  2743. /**
  2744. Set a request variable to a formatted string value
  2745. @description Request variables are defined for HTTP headers of the form HTTP_*.
  2746. Some request handlers also define their own variables. For example: CGI environment variables.
  2747. @param wp Webs request object
  2748. @param name Variable name to set
  2749. @param fmt Value format string
  2750. @param ... Args to format
  2751. @return the allocated WebsKey
  2752. @ingroup Webs
  2753. @stability Evolving
  2754. */
  2755. PUBLIC WebsKey *websSetVarFmt(Webs *wp, cchar *name, cchar *fmt, ...);
  2756. /**
  2757. Set a request variable to a string value
  2758. @description Request variables are defined for HTTP headers of the form HTTP_*.
  2759. Some request handlers also define their own variables. For example: CGI environment variables.
  2760. @param wp Webs request object
  2761. @param name Variable name to set
  2762. @param value Value to set
  2763. @return the allocated WebsKey
  2764. @ingroup Webs
  2765. @stability Evolving
  2766. */
  2767. PUBLIC WebsKey *websSetVar(Webs *wp, cchar *name, cchar *value);
  2768. /**
  2769. Test if a request variable is defined
  2770. @param wp Webs request object
  2771. @param name Variable name
  2772. @return True if the variable is defined
  2773. @ingroup Webs
  2774. @stability Stable
  2775. */
  2776. PUBLIC bool websTestVar(Webs *wp, cchar *name);
  2777. /**
  2778. Create a temporary filename
  2779. This does not guarantee the filename is unique or that it is not already in use by another application.
  2780. @param dir Directory to locate the temp file. Defaults to the O/S default temporary directory (usually /tmp)
  2781. @param prefix Filename prefix
  2782. @return An allocated filename string
  2783. @ingroup Webs
  2784. @stability Stable
  2785. */
  2786. PUBLIC char *websTempFile(cchar *dir, cchar *prefix);
  2787. /**
  2788. Open the date/time parsing module
  2789. @return Zero if successful, otherwise -1.
  2790. @ingroup Webs
  2791. @stability Evolving
  2792. */
  2793. PUBLIC int websTimeOpen(void);
  2794. /**
  2795. Close the date/time parsing module
  2796. @ingroup Webs
  2797. @stability Evolving
  2798. */
  2799. PUBLIC void websTimeClose(void);
  2800. /**
  2801. Parse a date/time string
  2802. @description Try to intelligently parse a date.
  2803. This is a tolerant parser. It is not validating and will do its best to parse any possible date string.
  2804. Supports the following date/time formats:
  2805. \n\n
  2806. ISO dates: 2009-05-21t16:06:05.000z
  2807. \n\n
  2808. Date: 07/28/2014, 07/28/08, Jan/28/2014, Jaunuary-28-2014, 28-jan-2014.
  2809. \n\n
  2810. Support date order: dd/mm/yy, mm/dd/yy and yyyy/mm/dd
  2811. \n\n
  2812. Support separators "/", ".", "-"
  2813. \n\n
  2814. Timezones: GMT|UTC[+-]NN[:]NN
  2815. \n\n
  2816. Time: 10:52[:23]
  2817. \n\n
  2818. @param time Reference to a
  2819. @param date Date/time string to parse
  2820. @param defaults Optionally supply missing components for the date/time. Set to NULL if not used.
  2821. @return Zero if successful, otherwise -1
  2822. @ingroup Webs
  2823. @stability Evolving
  2824. */
  2825. PUBLIC int websParseDateTime(WebsTime *time, cchar *date, struct tm *defaults);
  2826. /**
  2827. Parse a URL into its components
  2828. @param url URL to parse
  2829. @param buf Buffer to hold storage for various parsed components. Caller must free. NOTE: the parsed components may
  2830. point to locations in this buffer.
  2831. @param protocol Parsed URL protocol component
  2832. @param host Parsed hostname
  2833. @param port Parsed URL port
  2834. @param path Parsed URL path component
  2835. @param ext Parsed URL extension
  2836. @param reference Parsed URL reference portion (\#reference)
  2837. @param query Parsed URL query component
  2838. @return Zero if successful, otherwise -1.
  2839. @ingroup Webs
  2840. @stability Evolving
  2841. */
  2842. PUBLIC int websUrlParse(cchar *url, char **buf, char **protocol, char **host, char **port, char **path, char **ext,
  2843. char **reference, char **query);
  2844. /**
  2845. Test if a webs object is valid
  2846. @description After calling websDone, the websFree routine will have been called and the memory for the webs object
  2847. will be released. Call websValid to test a Webs object for validity.
  2848. @param wp Webs request object
  2849. @return True if the webs object is still valid and the request has not been completed.
  2850. @ingroup Webs
  2851. @stability Stable
  2852. */
  2853. PUBLIC bool websValid(Webs *wp);
  2854. /**
  2855. Validate a URI path as expected in a HTTP request line
  2856. @description This expects a URI beginning with "/" and containing only valid URI characters.
  2857. The URI is decoded, and normalized removing "../" and "." segments.
  2858. The URI must begin with a "/" both before and after decoding and normalization.
  2859. @param uri URI to validate.
  2860. @return A validated, normalized URI path. Caller must free.
  2861. @ingroup Webs
  2862. @stability Stable
  2863. */
  2864. PUBLIC char *websValidateUriPath(cchar *uri);
  2865. /**
  2866. Test if a URI is using only valid characters
  2867. Note this does not test if the URI is fully legal. Some components of the URI have restricted character sets
  2868. that this routine does not test. This tests if the URI has only characters valid to use in a URI before decoding.
  2869. i.e. It will permit %NN encodings. The set of valid characters is:
  2870. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=%"
  2871. @param uri Uri to test
  2872. @return True if the URI string is comprised of legal URI characters.
  2873. @ingroup Webs
  2874. @stability Evolving
  2875. */
  2876. PUBLIC bool websValidUriChars(cchar *uri);
  2877. /**
  2878. Write a set of standard response headers
  2879. @param wp Webs request object
  2880. @param contentLength Value for the Content-Length header which describes the length of the response body
  2881. @param redirect Value for the Location header which redirects the client to a new URL.
  2882. @ingroup Webs
  2883. @see websSetStatus
  2884. @stability Stable
  2885. */
  2886. PUBLIC void websWriteHeaders(Webs *wp, ssize contentLength, cchar *redirect);
  2887. /**
  2888. Signify the end of the response headers
  2889. @description This call concludes the response headers and writes a blank line to the response.
  2890. @param wp Webs request object
  2891. @ingroup Webs
  2892. @stability Stable
  2893. */
  2894. PUBLIC void websWriteEndHeaders(Webs *wp);
  2895. /**
  2896. Write a response header
  2897. @description This routine writes a response header. It should be invoked after calling websWriteHeaders
  2898. to write the standard headers and before websWriteEndHeaders.
  2899. This routine differs from websWrite in that it traces header values to the log.
  2900. @param wp Webs request object
  2901. @param key Header key value
  2902. @param fmt Header value format string.
  2903. @param ... Arguments to the format string.
  2904. @return Zero if successful, otherwise -1.
  2905. @ingroup Webs
  2906. @stability Stable
  2907. */
  2908. PUBLIC int websWriteHeader(Webs *wp, cchar *key, cchar *fmt, ...);
  2909. /**
  2910. Write data to the response
  2911. @description The data is buffered and will be sent to the client when the buffer is full or websFlush is
  2912. called.
  2913. @param wp Webs request object
  2914. @param fmt Printf style format string.
  2915. @param ... Arguments to the format string.
  2916. @return Count of bytes written
  2917. @ingroup Webs
  2918. @stability Stable
  2919. */
  2920. PUBLIC ssize websWrite(Webs *wp, cchar *fmt, ...);
  2921. /**
  2922. Write data to the open file
  2923. @param fd Open file handle returned by websOpenFile
  2924. @param buf Buffer for the read data
  2925. @param size Size of buf
  2926. @return Count of bytes read if successful, otherwise -1.
  2927. @ingroup Webs
  2928. @stability Stable
  2929. */
  2930. PUBLIC ssize websWriteFile(int fd, cchar *buf, ssize size);
  2931. /**
  2932. Write a block of data to the response
  2933. @description The data is buffered and will be sent to the client when the buffer is full or websFlush is
  2934. called. This routine will never return "short", it will always write all the data unless there are errors.
  2935. @param wp Webs request object
  2936. @param buf Buffer of data to write
  2937. @param size Length of buf
  2938. @return Count of bytes written or -1. This will always equal size if there are no errors.
  2939. @ingroup Webs
  2940. @stability Stable
  2941. */
  2942. PUBLIC ssize websWriteBlock(Webs *wp, cchar *buf, ssize size);
  2943. /**
  2944. Write a block of data to the network
  2945. @description This bypassed output buffering and is the lowest level write.
  2946. @param wp Webs request object
  2947. @param buf Buffer of data to write
  2948. @param size Length of buf
  2949. @return Count of bytes written. May be less than len if the socket is in non-blocking mode.
  2950. Returns -1 for errors and if the socket cannot absorb any more data. If the transport is saturated,
  2951. will return a negative error and errno will be set to EAGAIN or EWOULDBLOCK.
  2952. @ingroup Webs
  2953. @stability Stable
  2954. */
  2955. PUBLIC ssize websWriteSocket(Webs *wp, cchar *buf, ssize size);
  2956. #if ME_GOAHEAD_UPLOAD
  2957. /**
  2958. Process upload data for form, multipart mime file upload.
  2959. @param wp Webs request object
  2960. @return True if processing the request can proceed.
  2961. @ingroup Webs
  2962. @stability Stable
  2963. */
  2964. PUBLIC bool websProcessUploadData(Webs *wp);
  2965. /**
  2966. Free file upload data structures.
  2967. @param wp Webs request object
  2968. @ingroup Webs
  2969. @stability Stable
  2970. */
  2971. PUBLIC void websFreeUpload(Webs *wp);
  2972. #endif
  2973. #if ME_GOAHEAD_CGI
  2974. /**
  2975. Process CGI request body data.
  2976. @param wp Webs request object
  2977. @return True if processing the request can proceed.
  2978. @ingroup Webs
  2979. @stability Stable
  2980. */
  2981. PUBLIC bool websProcessCgiData(Webs *wp);
  2982. #endif
  2983. /************************************** Crypto ********************************/
  2984. /**
  2985. Get some random data
  2986. @param buf Reference to a buffer to hold the random data
  2987. @param length Size of the buffer
  2988. @param block Set to true if it is acceptable to block while accumulating entropy sufficient to provide good
  2989. random data. Setting to false will cause this API to not block and may return random data of a lower quality.
  2990. @ingroup Crypto
  2991. @stability Prototype.
  2992. */
  2993. PUBLIC int websGetRandomBytes(char *buf, ssize length, bool block);
  2994. /**
  2995. Encrypt a password using the Blowfish algorithm
  2996. @param password User's password to encrypt
  2997. @param salt Salt text to add to password. Helps to make each user's password unique.
  2998. @param rounds Number of times to encrypt. More times, makes the routine slower and passwords harder to crack.
  2999. @return The encrypted password.
  3000. @ingroup Crypto
  3001. @stability Prototype
  3002. */
  3003. PUBLIC char *websCryptPassword(cchar *password, cchar *salt, int rounds);
  3004. /**
  3005. Make salt for adding to a password.
  3006. @param size Size in bytes of the salt text.
  3007. @return The random salt text. Caller must free.
  3008. @ingroup Crypto
  3009. @stability Prototype
  3010. */
  3011. PUBLIC char *websMakeSalt(ssize size);
  3012. /**
  3013. Make a password hash for a plain-text password using the Blowfish algorithm.
  3014. @param password User's password to encrypt
  3015. @param saltLength Length of salt text to add to password. Helps to make each user's password unique.
  3016. @param rounds Number of times to encrypt. More times, makes the routine slower and passwords harder to crack.
  3017. @return The encrypted password. Caller must free.
  3018. @ingroup Crypto
  3019. @stability Prototype
  3020. */
  3021. PUBLIC char *websMakePassword(cchar *password, int saltLength, int rounds);
  3022. /**
  3023. Check a plain-text password against the defined hashed password.
  3024. @param plainTextPassword User's plain-text-password to check
  3025. @param passwordHash Required password in hashed format previously computed by websMakePassword.
  3026. @return True if the password is correct.
  3027. @ingroup Crypto
  3028. @stability Prototype
  3029. */
  3030. PUBLIC bool websCheckPassword(cchar *plainTextPassword, cchar *passwordHash);
  3031. /**
  3032. Get a password from the terminal console
  3033. @param prompt Text prompt to display before reading the password
  3034. @return The entered password. Caller must free.
  3035. @ingroup Crypto
  3036. @stability Prototype
  3037. */
  3038. PUBLIC char *websReadPassword(cchar *prompt);
  3039. /*************************************** JST ***********************************/
  3040. #if ME_GOAHEAD_JAVASCRIPT
  3041. /**
  3042. Javascript native function
  3043. @param jid JavaScript engine ID
  3044. @param wp Webs request object
  3045. @param argc Count of function arguments
  3046. @param argv Array of function arguments
  3047. @param defaultValue Default value to return if the variable is not defined
  3048. @return Return zero if successful, otherwise -1.
  3049. @ingroup Webs
  3050. @stability Stable
  3051. */
  3052. typedef int (*WebsJstProc)(int jid, Webs *wp, int argc, char **argv);
  3053. /**
  3054. Define a Javscript native function.
  3055. @description This routine binds a C function to a Javascript function. When the Javascript function is called,
  3056. the C function is invoked.
  3057. @param name Javascript function name
  3058. @param fn C function to invoke
  3059. @return Zero if successful, otherwise -1.
  3060. @ingroup Webs
  3061. @stability Stable
  3062. */
  3063. PUBLIC int websDefineJst(cchar *name, WebsJstProc fn);
  3064. /**
  3065. Open the Javascript module.
  3066. @return Zero if successful, otherwise -1.
  3067. @ingroup Webs
  3068. @stability Stable
  3069. */
  3070. PUBLIC int websJstOpen(void);
  3071. /**
  3072. Write data to the response
  3073. @param jid Javascript ID handle
  3074. @param wp Webs request object
  3075. @param argc Count of arguments
  3076. @param argv Array arguments
  3077. @return Zero if successful, otherwise -1.
  3078. @ingroup Webs
  3079. @stability Stable
  3080. */
  3081. PUBLIC int websJstWrite(int jid, Webs *wp, int argc, char **argv);
  3082. #endif
  3083. /*************************************** SSL ***********************************/
  3084. #if ME_COM_SSL
  3085. /*
  3086. Default MakeMe settings
  3087. */
  3088. #ifndef ME_GOAHEAD_SSL_AUTHORITY
  3089. #define ME_GOAHEAD_SSL_AUTHORITY ""
  3090. #endif
  3091. #ifndef ME_GOAHEAD_SSL_CACHE
  3092. #define ME_GOAHEAD_SSL_CACHE 512
  3093. #endif
  3094. #ifndef ME_GOAHEAD_SSL_CERTIFICATE
  3095. #define ME_GOAHEAD_SSL_CERTIFICATE ""
  3096. #endif
  3097. #ifndef ME_GOAHEAD_SSL_CIPHERS
  3098. #define ME_GOAHEAD_SSL_CIPHERS ""
  3099. #endif
  3100. #ifndef ME_GOAHEAD_SSL_KEY
  3101. #define ME_GOAHEAD_SSL_KEY ""
  3102. #endif
  3103. #ifndef ME_GOAHEAD_SSL_LOG_LEVEL
  3104. #define ME_GOAHEAD_SSL_LOG_LEVEL 4
  3105. #endif
  3106. #ifndef ME_GOAHEAD_SSL_RENEGOTIATE
  3107. #define ME_GOAHEAD_SSL_RENEGOTIATE 1
  3108. #endif
  3109. #ifndef ME_GOAHEAD_SSL_REVOKE
  3110. #define ME_GOAHEAD_SSL_REVOKE ""
  3111. #endif
  3112. #ifndef ME_GOAHEAD_SSL_TICKET
  3113. #define ME_GOAHEAD_SSL_TICKET 1
  3114. #endif
  3115. #ifndef ME_GOAHEAD_SSL_TIMEOUT
  3116. #define ME_GOAHEAD_SSL_TIMEOUT 86400
  3117. #endif
  3118. #ifndef ME_GOAHEAD_SSL_VERIFY
  3119. #define ME_GOAHEAD_SSL_VERIFY 0
  3120. #endif
  3121. #ifndef ME_GOAHEAD_SSL_VERIFY_ISSUER
  3122. #define ME_GOAHEAD_SSL_VERIFY_ISSUER 0
  3123. #endif
  3124. /**
  3125. Open the ssl module
  3126. @return Zero if successful, otherwise -1.
  3127. @ingroup Webs
  3128. @stability Stable
  3129. */
  3130. PUBLIC int sslOpen(void);
  3131. /**
  3132. Close the ssl module
  3133. @ingroup Webs
  3134. @stability Stable
  3135. */
  3136. PUBLIC void sslClose(void);
  3137. /**
  3138. Free a ssl connection associated with a request
  3139. @param wp Webs request object
  3140. @ingroup Webs
  3141. @stability Stable
  3142. */
  3143. PUBLIC void sslFree(Webs *wp);
  3144. /**
  3145. Upgrade a request connection to utilize SSL
  3146. @description This routine is invoked on a connection received on a secure listening socket
  3147. @param wp Webs request object
  3148. @return Zero if successful, otherwise -1.
  3149. @ingroup Webs
  3150. @stability Stable
  3151. */
  3152. PUBLIC int sslUpgrade(Webs *wp);
  3153. /**
  3154. Read data from a secure socket
  3155. @param wp Webs request object
  3156. @param buf Buffer into which to read data
  3157. @param len Size of buf
  3158. @return Count of bytes read if successful, otherwise -1.
  3159. @ingroup Webs
  3160. @stability Stable
  3161. */
  3162. PUBLIC ssize sslRead(Webs *wp, void *buf, ssize len);
  3163. /**
  3164. WRite data to a secure socket
  3165. @param wp Webs request object
  3166. @param buf Buffer from which to write data
  3167. @param len Size of buf
  3168. @return Count of bytes written if successful, otherwise -1.
  3169. @ingroup Webs
  3170. @stability Stable
  3171. */
  3172. PUBLIC ssize sslWrite(Webs *wp, void *buf, ssize len);
  3173. #endif /* ME_COM_SSL */
  3174. /*************************************** Route *********************************/
  3175. /**
  3176. Callback to prompt the user for their password
  3177. @param wp Webs request object
  3178. @ingroup Webs
  3179. @stability Stable
  3180. */
  3181. typedef void (*WebsAskLogin)(Webs *wp);
  3182. /**
  3183. Callback to verify the username and password
  3184. @param wp Webs request object
  3185. @return True if the password is verified
  3186. @ingroup Webs
  3187. @stability Stable
  3188. */
  3189. typedef bool (*WebsVerify)(Webs *wp);
  3190. /**
  3191. Callback to parse authentication details submitted with the web request
  3192. @param wp Webs request object
  3193. @return True if the details can be parsed
  3194. @ingroup Webs
  3195. @stability Stable
  3196. */
  3197. typedef bool (*WebsParseAuth)(Webs *wp);
  3198. /**
  3199. Request route structure
  3200. @defgroup WebsRoute WebsRoute
  3201. */
  3202. typedef struct WebsRoute {
  3203. char *prefix; /**< Route path prefix */
  3204. ssize prefixLen; /**< Prefix length */
  3205. char *dir; /**< Filesystem base directory for route documents */
  3206. char *protocol; /**< HTTP protocol to use for this route */
  3207. char *authType; /**< Authentication type */
  3208. WebsHandler *handler; /**< Request handler to service requests */
  3209. WebsHash abilities; /**< Required user abilities */
  3210. WebsHash extensions; /**< Permissible URI extensions */
  3211. WebsHash redirects; /**< Response redirections */
  3212. WebsHash methods; /**< Supported HTTP methods */
  3213. WebsAskLogin askLogin; /**< Route path prefix */
  3214. WebsParseAuth parseAuth; /**< Parse authentication details callback*/
  3215. WebsVerify verify; /**< Verify password callback */
  3216. int flags; /**< Route control flags */
  3217. } WebsRoute;
  3218. /**
  3219. Add a route to the routing tables
  3220. @param uri Matching URI prefix
  3221. @param handler Request handler to service routed requests
  3222. @param pos Position in the list of routes. Zero inserts at the front of the list. A value of -1 will append to the
  3223. end of the list.
  3224. @return A route object
  3225. @ingroup WebsRoute
  3226. @stability Stable
  3227. */
  3228. PUBLIC WebsRoute *websAddRoute(cchar *uri, cchar *handler, int pos);
  3229. /**
  3230. Close the route module
  3231. @ingroup WebsRoute
  3232. @stability Stable
  3233. */
  3234. PUBLIC void websCloseRoute(void);
  3235. /**
  3236. Load routing tables from the specified filename
  3237. @param path Route configuration filename
  3238. @return Zero if successful, otherwise -1.
  3239. @ingroup WebsRoute
  3240. @stability Stable
  3241. */
  3242. PUBLIC int websLoad(cchar *path);
  3243. /**
  3244. Open the routing module
  3245. @ingroup WebsRoute
  3246. @stability Stable
  3247. */
  3248. PUBLIC int websOpenRoute(void);
  3249. /**
  3250. Remove a route from the routing tables
  3251. @param uri Matching URI prefix
  3252. @return Zero if successful, otherwise -1.
  3253. @ingroup WebsRoute
  3254. @stability Stable
  3255. */
  3256. PUBLIC int websRemoveRoute(cchar *uri);
  3257. /**
  3258. Route a request
  3259. @description This routine will select a matching route and will invoke the selected route handler to service
  3260. the request. In the process, authentication and request rewriting may take place.
  3261. This routine is called internally by the request pipeline.
  3262. @param wp Webs request object
  3263. @ingroup WebsRoute
  3264. @stability Stable
  3265. */
  3266. PUBLIC void websRouteRequest(Webs *wp);
  3267. /**
  3268. Run a request handler
  3269. @description This routine will run the handler and route selected by #websRouteRequest.
  3270. This routine is called internally by the request pipeline.
  3271. @param wp Webs request object
  3272. @return True if the handler serviced the request. Return false to test other routes to handle this request.
  3273. This is for legacy handlers that do not have a match callback.
  3274. @ingroup WebsRoute
  3275. @stability Stable
  3276. */
  3277. PUBLIC bool websRunRequest(Webs *wp);
  3278. /**
  3279. Configure a route by adding matching criteria
  3280. @param route Route to modify
  3281. @param dir Set the route documents directory filename
  3282. @param protocol Set the matching HTTP protocol (http or https)
  3283. @param methods Hash of permissible HTTP methods. (GET, HEAD, POST, PUT)
  3284. @param extensions Hash of permissible URI filename extensions.
  3285. @param abilities Required user abilities. The user must be authenticated.
  3286. @param abilities Required user abilities. If abilities are required, the user must be authenticated.
  3287. @param redirects Set of applicable response redirections when completing the request.
  3288. @return Zero if successful, otherwise -1.
  3289. @ingroup WebsRoute
  3290. @stability Evolving
  3291. */
  3292. PUBLIC int websSetRouteMatch(WebsRoute *route, cchar *dir, cchar *protocol, WebsHash methods, WebsHash extensions,
  3293. WebsHash abilities, WebsHash redirects);
  3294. /**
  3295. Set route authentication scheme
  3296. @param route Route to modify
  3297. @param authType Set to "basic", "digest" or "form".
  3298. @return Zero if successful, otherwise -1.
  3299. @ingroup WebsRoute
  3300. @stability Stable
  3301. */
  3302. PUBLIC int websSetRouteAuth(WebsRoute *route, cchar *authType);
  3303. /*************************************** Auth **********************************/
  3304. #if ME_GOAHEAD_AUTH
  3305. #define WEBS_USIZE 128 /* Size of realm:username */
  3306. /**
  3307. GoAhead Authentication
  3308. @defgroup WebsAuth WebsAuth
  3309. */
  3310. /**
  3311. User definition structure
  3312. @ingroup WebsAuth
  3313. @stability Stable
  3314. */
  3315. typedef struct WebsUser {
  3316. char *name; /**< User name */
  3317. char *password; /**< User password (encrypted) */
  3318. char *roles; /**< User roles */
  3319. WebsHash abilities; /**< Resolved user abilities */
  3320. } WebsUser;
  3321. /**
  3322. Role definition structure
  3323. @ingroup WebsAuth
  3324. @stability Stable
  3325. */
  3326. typedef struct WebsRole {
  3327. WebsHash abilities; /**< Resolved role abilities */
  3328. } WebsRole;
  3329. /**
  3330. Add a role
  3331. @description The role is added to the list of roles
  3332. @param role Role name
  3333. @param abilities Hash of abilities for the role
  3334. @return The allocated role.
  3335. @ingroup WebsAuth
  3336. @stability Stable
  3337. */
  3338. PUBLIC WebsRole *websAddRole(cchar *role, WebsHash abilities);
  3339. /**
  3340. Add a user
  3341. @description The user is added to the list of users
  3342. @param username User name
  3343. @param password User password (encrypted)
  3344. @param roles Space separated list of roles. This may also contain abilities.
  3345. @return User object.
  3346. @ingroup WebsAuth
  3347. @stability Stable
  3348. */
  3349. PUBLIC WebsUser *websAddUser(cchar *username, cchar *password, cchar *roles);
  3350. /**
  3351. Authenticate a user
  3352. @description The user is authenticated if required by the selected request route.
  3353. @param wp Webs request object
  3354. @return True if the route does not require authentication or the user is authenticated successfully.
  3355. @ingroup WebsAuth
  3356. @stability Stable
  3357. */
  3358. PUBLIC bool websAuthenticate(Webs *wp);
  3359. /**
  3360. Test if a user possesses the required ability
  3361. @param wp Webs request object
  3362. @param ability Set of required abilities.
  3363. @return True if the user has the required ability.
  3364. @ingroup WebsAuth
  3365. @stability Stable
  3366. */
  3367. PUBLIC bool websCan(Webs *wp, WebsHash ability);
  3368. /**
  3369. Close the authentication module
  3370. @ingroup WebsAuth
  3371. @stability Stable
  3372. */
  3373. PUBLIC void websCloseAuth(void);
  3374. /**
  3375. Compute the abilities for all users by resolving roles into abilities
  3376. @ingroup WebsAuth
  3377. @stability Stable
  3378. */
  3379. PUBLIC void websComputeAllUserAbilities(void);
  3380. /**
  3381. Set the password store verify callback
  3382. @return verify WebsVerify callback function
  3383. @ingroup WebsAuth
  3384. @stability Stable
  3385. */
  3386. PUBLIC WebsVerify websGetPasswordStoreVerify(void);
  3387. /**
  3388. Get the roles hash
  3389. @return The roles hash object
  3390. @ingroup WebsAuth
  3391. @stability Stable
  3392. */
  3393. PUBLIC WebsHash websGetRoles(void);
  3394. /**
  3395. Get the users hash
  3396. @return The users hash object
  3397. @ingroup WebsAuth
  3398. @stability Stable
  3399. */
  3400. PUBLIC WebsHash websGetUsers(void);
  3401. /**
  3402. Login a user by verifying the login credentials.
  3403. @description This may be called by handlers to manually authenticate a user.
  3404. @param wp Webs request object
  3405. @param username User name
  3406. @param password User password (encrypted)
  3407. @return True if the user can be authenticated.
  3408. @ingroup WebsAuth
  3409. @stability Stable
  3410. */
  3411. PUBLIC bool websLoginUser(Webs *wp, cchar *username, cchar *password);
  3412. /**
  3413. Logout a user and remove the user login session.
  3414. @param wp Webs request object
  3415. @return True if successful.
  3416. @ingroup WebsAuth
  3417. @stability Stable
  3418. */
  3419. PUBLIC bool websLogoutUser(Webs *wp);
  3420. /**
  3421. Lookup if a user exists
  3422. @param username User name to search for
  3423. @return User object or null if the user cannot be found
  3424. @ingroup WebsAuth
  3425. @stability Stable
  3426. */
  3427. PUBLIC WebsUser *websLookupUser(cchar *username);
  3428. /**
  3429. Remove a role from the system
  3430. @param role Role name
  3431. @return Zero if successful, otherwise -1
  3432. @ingroup WebsAuth
  3433. @stability Stable
  3434. */
  3435. PUBLIC int websRemoveRole(cchar *role);
  3436. /**
  3437. Remove a user from the system
  3438. @param name User name
  3439. @return Zero if successful, otherwise -1
  3440. @ingroup WebsAuth
  3441. @stability Stable
  3442. */
  3443. PUBLIC int websRemoveUser(cchar *name);
  3444. /**
  3445. Open the authentication module
  3446. @param minimal Reserved. Set to zero.
  3447. @return True if the user has the required ability.
  3448. @ingroup WebsAuth
  3449. @stability Stable
  3450. */
  3451. PUBLIC int websOpenAuth(int minimal);
  3452. /**
  3453. Set the password store verify callback
  3454. @param verify WebsVerify callback function
  3455. @ingroup WebsAuth
  3456. @stability Stable
  3457. */
  3458. PUBLIC void websSetPasswordStoreVerify(WebsVerify verify);
  3459. /**
  3460. Set a password for the user
  3461. @param username User name
  3462. @param password Null terminated password string
  3463. @return Zero if successful, otherwise -1.
  3464. @ingroup WebsAuth
  3465. @stability Stable
  3466. */
  3467. PUBLIC int websSetUserPassword(cchar *username, cchar *password);
  3468. /**
  3469. Define the set of roles for a user
  3470. @param username User name
  3471. @param roles Space separated list of roles or abilities
  3472. @return Zero if successful, otherwise -1.
  3473. @ingroup WebsAuth
  3474. @stability Stable
  3475. */
  3476. PUBLIC int websSetUserRoles(cchar *username, cchar *roles);
  3477. /**
  3478. User password verification routine from a custom password back-end store.
  3479. @param wp Webs request object
  3480. @return True if the user password verifies.
  3481. @ingroup WebsAuth
  3482. @stability Stable
  3483. */
  3484. PUBLIC bool websVerifyPasswordFromCustom(Webs *wp);
  3485. /**
  3486. User password verification routine from auth.txt
  3487. @param wp Webs request object
  3488. @return True if the user password verifies.
  3489. @ingroup WebsAuth
  3490. @stability Stable
  3491. */
  3492. PUBLIC bool websVerifyPasswordFromFile(Webs *wp);
  3493. #if ME_COMPILER_HAS_PAM
  3494. /**
  3495. Verify a password using the system PAM password database.
  3496. @param wp Webs request object
  3497. @return True if the user password verifies.
  3498. @ingroup WebsAuth
  3499. @stability Stable
  3500. */
  3501. PUBLIC bool websVerifyPasswordFromPam(Webs *wp);
  3502. #endif
  3503. #endif /* ME_GOAHEAD_AUTH */
  3504. /************************************** Sessions *******************************/
  3505. /**
  3506. Session state storage
  3507. @defgroup WebsSession WebsSession
  3508. */
  3509. typedef struct WebsSession {
  3510. char *id; /**< Session ID key */
  3511. int lifespan; /**< Session inactivity timeout (secs) */
  3512. WebsTime expires; /**< When the session expires */
  3513. WebsHash cache; /**< Cache of session variables */
  3514. } WebsSession;
  3515. /**
  3516. Test if a user possesses the required ability
  3517. @param wp Webs request object
  3518. @param id Session ID to use. Set to null to allocate a new session ID.
  3519. @param lifespan Lifespan of the session in seconds.
  3520. @return Allocated session object
  3521. @ingroup WebsSession
  3522. @stability Stable
  3523. */
  3524. PUBLIC WebsSession *websAllocSession(Webs *wp, cchar *id, int lifespan);
  3525. /**
  3526. Test if a user possesses the required ability
  3527. @param wp Webs request object
  3528. @return Allocated session object
  3529. @ingroup WebsSession
  3530. @stability Stable
  3531. */
  3532. PUBLIC WebsSession *websCreateSession(Webs *wp);
  3533. /**
  3534. Destroy the webs session object
  3535. @description Useful to be called as part of the user logout process
  3536. @param wp Webs request object
  3537. @ingroup WebsSession
  3538. @stability Prototype
  3539. */
  3540. PUBLIC void websDestroySession(Webs *wp);
  3541. /**
  3542. Get the session ID
  3543. @param wp Webs request object
  3544. @return The session ID if session state storage is defined for this request. Caller must free.
  3545. @ingroup WebsSession
  3546. @stability Stable
  3547. */
  3548. PUBLIC char *websGetSessionID(Webs *wp);
  3549. /**
  3550. Get the session state object for the current request
  3551. @param wp Webs request object
  3552. @param create Set to true to create a new session if one does not already exist.
  3553. @return Session object
  3554. @ingroup WebsSession
  3555. @stability Stable
  3556. */
  3557. PUBLIC WebsSession *websGetSession(Webs *wp, int create);
  3558. /**
  3559. Get a session variable
  3560. @param wp Webs request object
  3561. @param name Session variable name
  3562. @param defaultValue Default value to return if the variable does not exist
  3563. @return Session variable value or default value if it does not exist
  3564. @ingroup WebsSession
  3565. @stability Stable
  3566. */
  3567. PUBLIC cchar *websGetSessionVar(Webs *wp, cchar *name, cchar *defaultValue);
  3568. /**
  3569. Remove a session variable
  3570. @param wp Webs request object
  3571. @param name Session variable name
  3572. @ingroup WebsSession
  3573. @stability Stable
  3574. */
  3575. PUBLIC void websRemoveSessionVar(Webs *wp, cchar *name);
  3576. /**
  3577. Set a session variable name value
  3578. @param wp Webs request object
  3579. @param name Session variable name
  3580. @param value Value to set the variable to
  3581. @return Zero if successful, otherwise -1
  3582. @ingroup WebsSession
  3583. @stability Stable
  3584. */
  3585. PUBLIC int websSetSessionVar(Webs *wp, cchar *name, cchar *value);
  3586. ////////////////////////////////////add by lusa///////////////////////////
  3587. PUBLIC void personInfoAction(Webs *wp);
  3588. //PUBLIC void getDeviceInfo(Webs *wp);
  3589. //PUBLIC void getDeviceInfoBak(Webs *wp);
  3590. PUBLIC void actionTest(Webs *wp);
  3591. PUBLIC void buy(Webs *wp);
  3592. ////////////////////////////////end/////////////////
  3593. /************************************ Legacy **********************************/
  3594. /*
  3595. Legacy mappings for pre GoAhead 3.X applications
  3596. This is a list of the name changes from GoAhead 2.X to GoAhead 3.x
  3597. To maximize forward compatibility, It is best to not use ME_GOAHEAD_LEGACY except as
  3598. a transitional compilation aid.
  3599. */
  3600. #if ME_GOAHEAD_LEGACY
  3601. #define B_L 0
  3602. #define a_assert assert
  3603. #define balloc walloc
  3604. #define bclose wcloseAlloc
  3605. #define bfree(loc, p) wfree(p)
  3606. #define bfreeSafe(loc, p) wfree(p)
  3607. #define bopen wopenAlloc
  3608. #define brealloc wrealloc
  3609. #define bstrdup sclone
  3610. #define emfReschedCallback websRestartEvent
  3611. #define emfSchedCallback websStartEvent
  3612. #define emfSchedProc WebsEventProc
  3613. #define emfSchedProcess websRunEvents
  3614. #define emfUnschedCallback websStopEvent
  3615. #define fmtStatic fmt
  3616. #define gassert assert
  3617. #define galloc walloc
  3618. #define gallocEntry wallocObject
  3619. #define gfree wfree
  3620. #define gFree wfreeHandle
  3621. #define grealloc wrealloc
  3622. #define gaccess access
  3623. #define gasctime asctime
  3624. #define gatoi atoi
  3625. #define gchmod chmod
  3626. #define wclose close
  3627. #define wclosedir closedir
  3628. #define gcreat creat
  3629. #define gctime ctime
  3630. #define gexecvp execvp
  3631. #define gfgets fgets
  3632. #define gfindclose _findclose
  3633. #define gfinddata_t _finddata_t
  3634. #define gfindfirst _findfirst
  3635. #define gfindnext _findnext
  3636. #define gfopen fopen
  3637. #define gfprintf fprintf
  3638. #define gfputs fputs
  3639. #define gfscanf fscanf
  3640. #define ggetcwd getcwd
  3641. #define ggetenv getenv
  3642. #define ggets gets
  3643. #define gisalnum isalnum
  3644. #define gisalpha isalpha
  3645. #define gisdigit isdigit
  3646. #define gislower islower
  3647. #define gisspace isspace
  3648. #define gisupper isupper
  3649. #define gisxdigit isxdigit
  3650. #define gloadModule loadModule
  3651. #define glseek lseek
  3652. #define gopendir opendir
  3653. #define gprintf printf
  3654. #define gread read
  3655. #define greaddir readdir
  3656. #define gremove remove
  3657. #define grename rename
  3658. #define gsprintf sprintf
  3659. #define gsscanf sscanf
  3660. #define gstat stat
  3661. #define gstrcat strcat
  3662. #define gstrchr strchr
  3663. #define gstrcmp strcmp
  3664. #define gstrcpy strcpy
  3665. #define gstrcspn strcspn
  3666. #define gstricmp strcmpci
  3667. #define gstritoa stritoa
  3668. #define gstrlen strlen
  3669. #define gstrlower strlower
  3670. #define gstrncat strncat
  3671. #define gstrncmp strncmp
  3672. #define gstrncpy strncpy
  3673. #define gstrnlen strnlen
  3674. #define gstrnset strnset
  3675. #define gstrrchr strrchr
  3676. #define gstrspn strspn
  3677. #define gstrstr strstr
  3678. #define gstrtok strtok
  3679. #define gstrtol strtol
  3680. #define gstrupper strupper
  3681. #define gtempnam websTempFile
  3682. #define gtolower tolower
  3683. #define gtoupper toupper
  3684. #define gunlink unlink
  3685. #define gvsprintf vsprintf
  3686. #define gwrite write
  3687. #define hAlloc wallocHandle
  3688. #define hAllocEntry wallocObject
  3689. #define hFree wfreeHandle
  3690. #define stritoa gstritoa
  3691. #define strlower gstrlower
  3692. #define strupper gstrupper
  3693. #define websAspDefine websDefineJst
  3694. #define websAspOpen websJstOpen
  3695. #define websAspRequest websJstRequest
  3696. #define websFormDefine websDefineAction
  3697. #define websGetDefaultDir websGetDocuments
  3698. #define websGetDefaultPage websGetIndex
  3699. #define websGetRequestDir(wp) wp->dir
  3700. #define websGetRequestIpAddr(wp) wp->ipaddr
  3701. #define websGetRequestFilename(wp) wp->filename
  3702. #define websGetRequestFlags(wp) wp->flags
  3703. #define websGetRequestLpath(wp) wp->filename
  3704. #define websGetRequestPath(wp) wp->path
  3705. #define websGetRequestPassword(wp) wp->password
  3706. #define websGetRequestUserName(wp) wp->username
  3707. #define websGetRequestWritten(wp) wp->written
  3708. #define websSetDefaultDir websSetDocuments
  3709. #define websSetDefaultPage websSetIndex
  3710. #define websSetRequestLpath websSetRequestFilename
  3711. #define websSetRequestWritten(wp, nbytes) if (1) { wp->written = nbytes; } else {}
  3712. #define websTimeoutCancel websCancelTimeout
  3713. #define websWriteDataNonBlock websWriteRaw
  3714. #define ringqOpen bufCreate
  3715. #define ringqClose bufFree
  3716. #define ringqLen bufLen
  3717. #define ringqPutc bufPutc
  3718. #define ringqInsertc bufInsertc
  3719. #define ringqPutStr bufPutStr
  3720. #define ringqGetc bufGetc
  3721. #define ringqGrow bufGrow
  3722. #define ringqPutBlk bufPutBlk
  3723. #define ringqPutBlkMax bufRoom
  3724. #define ringqPutBlkAdj bufAdjustEnd
  3725. #define ringqGetBlk bufGetBlk
  3726. #define ringqGetBlkMax bufGetBlkMax
  3727. #define ringqGetBlkAdj bufAdjustSTart
  3728. #define ringqFlush bufFlush
  3729. #define ringqCompact bufCompact
  3730. #define ringqReset bufReset
  3731. #define ringqAddNull bufAddNull
  3732. #define symCreate hashCreate
  3733. #define symClose hashFree
  3734. #define symLookup hashLookup
  3735. #define symEnter hashEnter
  3736. #define symDelete hashDelete
  3737. #define symWalk hashWalk
  3738. #define symFirst hashFirst
  3739. #define symNext hashNext
  3740. typedef Webs *webs_t;
  3741. typedef Webs WebsRec;
  3742. typedef Webs websType;
  3743. typedef WebsBuf ringq_t;
  3744. typedef WebsError websErrorType;
  3745. typedef WebsProc WebsFormProc;
  3746. typedef int (*WebsLegacyHandlerProc)(Webs *wp, char *prefix, char *dir, int flags);
  3747. typedef SocketHandler socketHandler_t;
  3748. typedef SocketAccept socketAccept_t;
  3749. typedef WebsType vtype_t;
  3750. typedef WebsHash sym_fd_t;
  3751. typedef WebsKey sym_t;
  3752. typedef WebsMime websMimeType;
  3753. typedef WebsSocket socket_t;
  3754. typedef WebsStat gstat_t;
  3755. typedef WebsValue value_t;
  3756. PUBLIC int fmtValloc(char **s, int n, cchar *fmt, va_list arg);
  3757. PUBLIC int fmtAlloc(char **s, int n, cchar *fmt, ...);
  3758. PUBLIC void websFooter(Webs *wp);
  3759. PUBLIC void websHeader(Webs *wp);
  3760. PUBLIC int websPublish(cchar *prefix, cchar *path);
  3761. PUBLIC void websSetRequestFilename(Webs *wp, cchar *filename);
  3762. PUBLIC int websUrlHandlerDefine(cchar *prefix, cchar *dir, int arg, WebsLegacyHandlerProc handler, int flags);
  3763. #if ME_ROM
  3764. typedef WebsRomIndex websRomIndexType;
  3765. #endif
  3766. #endif
  3767. #if ME_CUSTOMIZE
  3768. #include "customize.h"
  3769. #endif
  3770. #ifdef __cplusplus
  3771. }
  3772. #endif
  3773. #endif /* _h_GOAHEAD */
  3774. /*
  3775. Copyright (c) Embedthis Software. All Rights Reserved.
  3776. This software is distributed under commercial and open source licenses.
  3777. You may use the Embedthis GoAhead open source license or you may acquire
  3778. a commercial license from Embedthis Software. You agree to be fully bound
  3779. by the terms of either license. Consult the LICENSE.md distributed with
  3780. this software for full details and other copyrights.
  3781. */