esp.h 170 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436
  1. /*
  2. * Embedthis ESP Library Source
  3. */
  4. #include "me.h"
  5. #if ME_COM_ESP
  6. #include "osdep.h"
  7. #ifndef ESP_VERSION
  8. #define ESP_VERSION "9.0.2"
  9. #endif
  10. /*
  11. edi.h -- Embedded Database Interface (EDI).
  12. This interface sits atop a SQLite driver and the in-memory database MDB.
  13. Copyright (c) All Rights Reserved. See copyright notice at the bottom of the file.
  14. */
  15. #ifndef _h_EDI
  16. #define _h_EDI 1
  17. /********************************* Includes ***********************************/
  18. #include "http.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /****************************** Forward Declarations **************************/
  23. #if !DOXYGEN
  24. #endif
  25. /********************************** Defines ***********************************/
  26. /*
  27. Forward declare structures
  28. */
  29. struct Edi;
  30. struct EdiGrid;
  31. struct EdiProvider;
  32. struct EdiRec;
  33. struct EdiValidation;
  34. /**
  35. Edi service control structure
  36. @defgroup EdiService EdiService
  37. */
  38. typedef struct EdiService {
  39. MprHash *providers;
  40. MprHash *validations;
  41. } EdiService;
  42. /**
  43. Create the EDI service
  44. @return EdiService object
  45. @ingroup EdiService
  46. @stability Evolving
  47. @internal
  48. */
  49. PUBLIC EdiService *ediCreateService(void);
  50. /**
  51. Add a database provider.
  52. @description This should only be called by database providers.
  53. @ingroup EdiService
  54. @stability Evolving
  55. */
  56. PUBLIC void ediAddProvider(struct EdiProvider *provider);
  57. /**
  58. Field validation callback procedure
  59. @param vp Validation structure reference
  60. @param rec Record to validate
  61. @param fieldName Field name to validate
  62. @param value Field value to
  63. @ingroup EdiService
  64. @stability Evolving
  65. */
  66. typedef cchar *(*EdiValidationProc)(struct EdiValidation *vp, struct EdiRec *rec, cchar *fieldName, cchar *value);
  67. /**
  68. Validation structure
  69. @ingroup EdiService
  70. @stability Evolving
  71. */
  72. typedef struct EdiValidation {
  73. cchar *name; /**< Validation name */
  74. EdiValidationProc vfn; /**< Validation callback procedure */
  75. cvoid *data; /**< Custom data (managed) */
  76. cvoid *mdata; /**< Custom data (unmanaged) */
  77. } EdiValidation;
  78. /**
  79. Define a field validation procedure
  80. @param name Validation name
  81. @param vfn Validation callback to invoke when validating field data.
  82. @ingroup EdiService
  83. @stability Evolving
  84. */
  85. PUBLIC void ediDefineValidation(cchar *name, EdiValidationProc vfn);
  86. /**
  87. Add a field error message
  88. @param rec Record to update
  89. @param field Field name for the error message
  90. @param fmt Message format string
  91. @ingroup EdiService
  92. @stability Prototype
  93. */
  94. PUBLIC void ediAddFieldError(struct EdiRec *rec, cchar *field, cchar *fmt, ...);
  95. /*
  96. Field data type hints
  97. */
  98. #define EDI_TYPE_BINARY 1 /**< Arbitrary binary data */
  99. #define EDI_TYPE_BOOL 2 /**< Boolean true|false value */
  100. #define EDI_TYPE_DATE 3 /**< Date type (stored as epoch) */
  101. #define EDI_TYPE_FLOAT 4 /**< Floating point number */
  102. #define EDI_TYPE_INT 5 /**< Integer number */
  103. #define EDI_TYPE_STRING 6 /**< String */
  104. #define EDI_TYPE_TEXT 7 /**< Multi-line text */
  105. #define EDI_TYPE_MAX 8 /**< Max type + 1 */
  106. /*
  107. Field flags
  108. */
  109. #define EDI_AUTO_INC 0x1 /**< Field flag -- Automatic increments on new row */
  110. #define EDI_KEY 0x2 /**< Field flag -- Column is the ID key */
  111. #define EDI_INDEX 0x4 /**< Field flag -- Column is indexed */
  112. #define EDI_FOREIGN 0x8 /**< Field flag -- Column is a foreign key */
  113. #define EDI_NOT_NULL 0x10 /**< Field flag -- Column must not be null (not implemented) */
  114. #define EDI_READ_ONLY 0x20 /**< Field flag -- Field is read-only (not implemented) */
  115. /*
  116. Encodings
  117. */
  118. #define EDI_ENCODE_PREFIX 0x
  119. /**
  120. EDI Record field structure
  121. @description The EdiField stores record field data and minimal schema information such as the data type and
  122. source column name.
  123. @defgroup EdiField EdiField
  124. */
  125. typedef struct EdiField {
  126. cchar *value; /**< Field data value */
  127. cchar *name; /**< Field name. Sourced from the database column name */
  128. int type: 8; /**< Field data type. Set to one of EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE
  129. EDI_TYPE_FLOAT, EDI_TYPE_INT, EDI_TYPE_STRING, EDI_TYPE_TEXT */
  130. int valid: 8; /**< Field validity. Set to true if valid */
  131. int flags: 8; /**< Field flags. Flag mask set to EDI_AUTO_INC, EDI_KEY and/or EDI_INDEX */
  132. } EdiField;
  133. /**
  134. Database record structure
  135. @description Records may capture database row data, or may be free-standing without a backing database.
  136. @defgroup EdiRec EdiRec
  137. */
  138. typedef struct EdiRec {
  139. struct Edi *edi; /**< Database handle */
  140. MprHash *errors; /**< Hash of record errors */
  141. cchar *tableName; /**< Base table name for record */
  142. cchar *id; /**< Record key ID */
  143. int nfields; /**< Number of fields in record */
  144. int index; /**< Grid index for iteration */
  145. EdiField fields[ARRAY_FLEX]; /**< Field records */
  146. } EdiRec;
  147. #define EDI_GRID_READ_ONLY 0x1 /**< Grid contains pure database records, must not be modified */
  148. /**
  149. Grid structure
  150. @description A grid is a tabular (grid) of rows and records.
  151. Grids may capture database table data, or may be free-standing without a backing database.
  152. @defgroup EdiGrid EdiGrid
  153. */
  154. typedef struct EdiGrid {
  155. struct Edi *edi; /**< Database handle */
  156. cchar *tableName; /**< Base table name for grid */
  157. int flags; /**< Grid flags */
  158. int count; /**< Total count of available records matching query */
  159. int nrecords; /**< Number of records in grid */
  160. EdiRec *records[ARRAY_FLEX];/**< Grid records */
  161. } EdiGrid;
  162. /*
  163. Database flags
  164. */
  165. #define EDI_CREATE 0x1 /**< Create database if not present */
  166. #define EDI_AUTO_SAVE 0x2 /**< Auto-save database if modified in memory */
  167. #define EDI_NO_SAVE 0x4 /**< Prevent saving to disk */
  168. #define EDI_LITERAL 0x8 /**< Literal schema in ediOpen source parameter */
  169. #define EDI_SUPPRESS_SAVE 0x10 /**< Temporarily suppress auto-save */
  170. #define EDI_PRIVATE 0x20 /**< Create private clone of the database */
  171. typedef int (*EdiMigration)(struct Edi *db);
  172. /**
  173. Define database migration callbacks
  174. @param edi Database handle
  175. @param forw Forward migration callback. Of the form:
  176. int forw(Edi *edit);
  177. A successful return should be zero.
  178. @param back Backward migration callback. Of the form:
  179. int back(Edi *edit);
  180. A successful return should be zero.
  181. @ingroup EdiService
  182. @stability Evolving
  183. */
  184. PUBLIC void ediDefineMigration(struct Edi *edi, EdiMigration forw, EdiMigration back);
  185. /**
  186. Database structure
  187. @description The Embedded Database Interface (EDI) defines an abstract interface atop various relational
  188. database providers. Providers are supplied for SQLite and for the ESP Memory Database (MDB).
  189. @defgroup Edi Edi
  190. */
  191. typedef struct Edi {
  192. struct EdiProvider *provider; /**< Database provider */
  193. MprHash *schemaCache; /**< Cache of table schema in JSON */
  194. MprHash *validations; /**< Validations */
  195. MprMutex *mutex; /**< Multithread lock */
  196. cchar *path; /**< Database path */
  197. int flags; /**< Database flags */
  198. EdiMigration forw; /**< Forward migration callback */
  199. EdiMigration back; /**< Backward migration callback */
  200. char *errMsg; /**< Last error message */
  201. } Edi;
  202. /**
  203. Database provider interface
  204. @internal
  205. */
  206. typedef struct EdiProvider {
  207. cchar *name;
  208. int (*addColumn)(Edi *edi, cchar *tableName, cchar *columnName, int type, int flags);
  209. int (*addIndex)(Edi *edi, cchar *tableName, cchar *columnName, cchar *indexName);
  210. int (*addTable)(Edi *edi, cchar *tableName);
  211. int (*changeColumn)(Edi *edi, cchar *tableName, cchar *columnName, int type, int flags);
  212. void (*close)(Edi *edi);
  213. EdiRec *(*createRec)(Edi *edi, cchar *tableName);
  214. int (*deleteDatabase)(cchar *path);
  215. MprList *(*getColumns)(Edi *edi, cchar *tableName);
  216. int (*getColumnSchema)(Edi *edi, cchar *tableName, cchar *columnName, int *type, int *flags, int *cid);
  217. MprList *(*getTables)(Edi *edi);
  218. int (*getTableDimensions)(Edi *edi, cchar *tableName, int *numRows, int *numCols);
  219. int (*load)(Edi *edi, cchar *path);
  220. int (*lookupField)(Edi *edi, cchar *tableName, cchar *fieldName);
  221. Edi *(*open)(cchar *path, int flags);
  222. EdiGrid *(*query)(Edi *edi, cchar *cmd, int argc, cchar **argv, va_list vargs);
  223. EdiField (*readField)(Edi *edi, cchar *tableName, cchar *key, cchar *fieldName);
  224. EdiGrid *(*findGrid)(Edi *edi, cchar *tableName, cchar *query);
  225. EdiRec *(*readRec)(Edi *edi, cchar *tableName, cchar *key);
  226. int (*removeColumn)(Edi *edi, cchar *tableName, cchar *columnName);
  227. int (*removeIndex)(Edi *edi, cchar *tableName, cchar *indexName);
  228. int (*removeRec)(Edi *edi, cchar *tableName, cchar *key);
  229. int (*removeTable)(Edi *edi, cchar *tableName);
  230. int (*renameTable)(Edi *edi, cchar *tableName, cchar *newTableName);
  231. int (*renameColumn)(Edi *edi, cchar *tableName, cchar *columnName, cchar *newColumnName);
  232. int (*save)(Edi *edi);
  233. int (*updateField)(Edi *edi, cchar *tableName, cchar *key, cchar *fieldName, cchar *value);
  234. int (*updateRec)(Edi *edi, EdiRec *rec);
  235. } EdiProvider;
  236. /*************************** EDI Interface Wrappers **************************/
  237. /**
  238. Add a column to a table
  239. @param edi Database handle
  240. @param tableName Database table name
  241. @param columnName Database column name
  242. @param type Column data type. Set to one of EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE
  243. EDI_TYPE_FLOAT, EDI_TYPE_INT, EDI_TYPE_STRING, EDI_TYPE_TEXT
  244. @param flags Control column attributes. Set to a set of: EDI_AUTO_INC for auto incrementing columns,
  245. EDI_KEY if the column is the key column and/or EDI_INDEX to create an index on the column.
  246. @return Zero if successful. Otherwise a negative MPR error code.
  247. @ingroup Edi
  248. @stability Evolving
  249. */
  250. PUBLIC int ediAddColumn(Edi *edi, cchar *tableName, cchar *columnName, int type, int flags);
  251. /**
  252. Add an index to a table
  253. @param edi Database handle
  254. @param tableName Database table name
  255. @param columnName Database column name
  256. @param indexName Ignored. Set to null.
  257. @return Zero if successful. Otherwise a negative MPR error code.
  258. @ingroup Edi
  259. @stability Evolving
  260. */
  261. PUBLIC int ediAddIndex(Edi *edi, cchar *tableName, cchar *columnName, cchar *indexName);
  262. /**
  263. Add a table to a database
  264. @param edi Database handle
  265. @param tableName Database table name. Table names should be singular. Certain routines like ediJoin rely on being
  266. able to map foreign key fields of the form NameId by converting the Name to a database table.
  267. @return Zero if successful. Otherwise a negative MPR error code.
  268. @ingroup Edi
  269. @stability Evolving
  270. */
  271. PUBLIC int ediAddTable(Edi *edi, cchar *tableName);
  272. /**
  273. Add a validation
  274. @description Validations are run when calling ediUpdateRec. A validation is used to validate field data
  275. using builtin validators.
  276. @param edi Database handle
  277. @param name Validation name. Select from:
  278. @arg banned -- to validate field data against a regular express for banned content.
  279. @arg boolean -- to validate field data as "true" or "false"
  280. @arg date -- to validate field data as a date or time.
  281. @arg format -- to validate field data against a regular expression supplied in the "data" argument
  282. @arg integer -- to validate field data as an integral value
  283. @arg number -- to validate field data as a number. It may be an integer or floating point number.
  284. @arg present -- to validate field data as not null.
  285. @arg unique -- to validate field data as being unique in the database table.
  286. @param tableName Database table name
  287. @param columnName Database column name
  288. @param data Argument data for the validator. For example: the "format" validator requires a regular expression.
  289. @return Zero if successful. Otherwise a negative MPR error code.
  290. @ingroup Edi
  291. @stability Evolving
  292. */
  293. PUBLIC int ediAddValidation(Edi *edi, cchar *name, cchar *tableName, cchar *columnName, cvoid *data);
  294. /**
  295. Change a column schema definition
  296. @param edi Database handle
  297. @param tableName Database table name
  298. @param columnName Database column name
  299. @param type Column data type. Set to one of EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE
  300. EDI_TYPE_FLOAT, EDI_TYPE_INT, EDI_TYPE_STRING, EDI_TYPE_TEXT
  301. @param flags Control column attributes. Set to a set of: EDI_AUTO_INC for auto incrementing columns,
  302. EDI_KEY if the column is the key column and/or EDI_INDEX to create an index on the column.
  303. @return Zero if successful. Otherwise a negative MPR error code.
  304. @ingroup Edi
  305. @stability Evolving
  306. */
  307. PUBLIC int ediChangeColumn(Edi *edi, cchar *tableName, cchar *columnName, int type, int flags);
  308. /**
  309. Close a database
  310. @param edi Database handle
  311. @ingroup Edi
  312. @stability Evolving
  313. */
  314. PUBLIC void ediClose(Edi *edi);
  315. /**
  316. Clone a grid
  317. @param grid to clone
  318. @return A complete copy of a grid
  319. @ingroup Edi
  320. @stability Prototype
  321. */
  322. PUBLIC EdiGrid *ediCloneGrid(EdiGrid *grid);
  323. /**
  324. Create a new record based on the table's schema.
  325. @description This will create an empty record using the given database tableName to supply the record schema. It will
  326. not be saved to the database as the field values have not been assigned. Set field values using #ediSetField and
  327. #ediSetFields and then save to the database using #ediUpdateRec.
  328. Create a record based on the table's schema. Not saved to the database.
  329. Use #ediCreateBareRec to create a free-standing record without requiring a database.
  330. The record is allocated and room is reserved to store record values. No record field values are stored.
  331. @param edi Database handle
  332. @param tableName Database table name
  333. @return Record instance.
  334. @ingroup Edi
  335. @stability Evolving
  336. */
  337. PUBLIC EdiRec *ediCreateRec(Edi *edi, cchar *tableName);
  338. /**
  339. Delete the database at the given path.
  340. @param edi Database handle. This is required to identify the database provider. The database should be closed before
  341. deleting.
  342. @param path Database path name.
  343. @return Zero if successful. Otherwise a negative MPR error code.
  344. @ingroup Edi
  345. @stability Evolving
  346. */
  347. PUBLIC int ediDelete(Edi *edi, cchar *path);
  348. /**
  349. Display the grid to the debug log
  350. @description Used for debugging only.
  351. @param message Prefix message to output
  352. @param grid EDI grid
  353. @ingroup Edi
  354. @stability Prototype
  355. */
  356. PUBLIC void ediDumpGrid(cchar *message, EdiGrid *grid);
  357. /**
  358. Display a record to the debug log
  359. @description Used for debugging only.
  360. @param message Prefix message to output
  361. @param rec Record to log
  362. @ingroup Edi
  363. @stability Prototype
  364. */
  365. PUBLIC void ediDumpRec(cchar *message, EdiRec *rec);
  366. /**
  367. Get a list of database column names.
  368. @param edi Database handle
  369. @param tableName Database table name
  370. @return An MprList of column names in the given table.
  371. @ingroup Edi
  372. @stability Evolving
  373. */
  374. PUBLIC MprList *ediGetColumns(Edi *edi, cchar *tableName);
  375. /**
  376. Get the column schema
  377. @param edi Database handle
  378. @param tableName Database table name
  379. @param columnName Database column name
  380. @param type Output parameter to receive the column data type. Will be set to one of:
  381. EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE, EDI_TYPE_FLOAT, EDI_TYPE_INT, EDI_TYPE_STRING, EDI_TYPE_TEXT.
  382. Set to null if this data is not required.
  383. @param flags Output parameter to receive the column control flags. Will be set to one or more of:
  384. EDI_AUTO_INC, EDI_KEY and/or EDI_INDEX
  385. Set to null if this data is not required.
  386. @param cid Output parameter to receive the ordinal column index in the database table.
  387. Set to null if this data is not required.
  388. @return Zero if successful. Otherwise a negative MPR error code.
  389. @ingroup Edi
  390. @stability Evolving
  391. */
  392. PUBLIC int ediGetColumnSchema(Edi *edi, cchar *tableName, cchar *columnName, int *type, int *flags, int *cid);
  393. /**
  394. Get the schema for a record and format as JSON
  395. @param rec
  396. @ingroup EdiRec
  397. @stability Prototype
  398. */
  399. PUBLIC cchar *ediGetRecSchemaAsJson(EdiRec *rec);
  400. /**
  401. Get the next field in a record
  402. This is used as an iterator. For the first call, set fp to NULL.
  403. @param rec Record whose fields are iterated
  404. @param fp Field pointer
  405. @param offset Initial offset. Set to 1 to step over the ID field.
  406. @return The next field object. Returns NULL after the last field.
  407. @ingroup EdiRec
  408. @stability Prototype
  409. */
  410. PUBLIC EdiField *ediGetNextField(EdiRec *rec, EdiField *fp, int offset);
  411. /**
  412. Get the next record in a grid
  413. This is used as an iterator. For the first call, set rec to NULL.
  414. @param grid Grid whose records are iterated
  415. @param rec Record pointer
  416. @return The next record object. Returns NULL after the last record.
  417. @ingroup EdiGrid
  418. @stability Prototype
  419. */
  420. PUBLIC EdiRec *ediGetNextRec(EdiGrid *grid, EdiRec *rec);
  421. /**
  422. Get table dimensions information.
  423. @param edi Database handle
  424. @param tableName Database table name
  425. @param numRows Output parameter to receive the number of rows in the table
  426. Set to null if this data is not required.
  427. @param numCols Output parameter to receive the number of columns in the table
  428. Set to null if this data is not required.
  429. @return Zero if successful. Otherwise a negative MPR error code.
  430. @ingroup Edi
  431. @stability Evolving
  432. */
  433. PUBLIC int ediGetTableDimensions(Edi *edi, cchar *tableName, int *numRows, int *numCols);
  434. /**
  435. Get a table schema and format as JSON
  436. @param edi Database handle
  437. @param tableName Name of table to examine
  438. @ingroup Edi
  439. @stability Prototype
  440. */
  441. PUBLIC cchar *ediGetTableSchemaAsJson(Edi *edi, cchar *tableName);
  442. /**
  443. Get a list of database tables.
  444. @param edi Database handle
  445. @return An MprList of table names in the database.
  446. @ingroup Edi
  447. @stability Evolving
  448. */
  449. PUBLIC MprList *ediGetTables(Edi *edi);
  450. /**
  451. Convert an EDI database grid into a JSON string.
  452. @param grid EDI grid
  453. @param flags Reserved. Set to MPR_JSON_PRETTY for a prettier format.
  454. @return JSON string
  455. @ingroup Edi
  456. @stability Prototype
  457. */
  458. PUBLIC cchar *ediGridAsJson(EdiGrid *grid, int flags);
  459. /**
  460. Join grids
  461. @param edi Database handle
  462. @param ... Null terminated list of data grids. These are instances of EdiGrid.
  463. @return A joined grid.
  464. @ingroup Edi
  465. @stability Evolving
  466. */
  467. PUBLIC EdiGrid *ediJoin(Edi *edi, ...);
  468. /**
  469. Load the database file.
  470. @param edi Database handle
  471. @param path Database path name
  472. @return Zero if successful. Otherwise a negative MPR error code.
  473. @ingroup Edi
  474. @stability Evolving
  475. */
  476. PUBLIC int ediLoad(Edi *edi, cchar *path);
  477. /**
  478. Lookup a column field by name.
  479. @param edi Database handle
  480. @param tableName Database table name
  481. @param fieldName Database column field name
  482. @return The ordinal column index in the table if the column field is found. Otherwise returns a negative MPR error code.
  483. @ingroup Edi
  484. @stability Evolving
  485. */
  486. PUBLIC int ediLookupField(Edi *edi, cchar *tableName, cchar *fieldName);
  487. /**
  488. Lookup an EDI provider name
  489. @param providerName Name of the EDI provider
  490. @return The EDI provider object. Returns null if the provider cannot be found.
  491. @ingroup Edi
  492. @stability Evolving
  493. @internal
  494. */
  495. PUBLIC EdiProvider *ediLookupProvider(cchar *providerName);
  496. /**
  497. Open a database.
  498. @description This opens a database using the specified database provider.
  499. @param source Database path name. If using the "mdb" provider with the EDI_LITERAL flag, then the source argument can
  500. be set to a literal JSON database content string.
  501. @param provider Database provider. Set to "mdb" for the Memory Database or "sqlite" for the SQLite provider.
  502. @param flags Set to:
  503. @arg EDI_CREATE -- Create database if not present.
  504. @arg EDI_AUTO_SAVE -- Auto-save database if modified in memory. This option is only supported by the "mdb" provider.
  505. @arg EDI_NO_SAVE -- Prevent saving to disk. This option is only supported by the "mdb" provider.
  506. @arg EDI_LITERAL -- Literal schema in ediOpen source parameter. This option is only supported by the "mdb" provider.
  507. @return If successful, returns an EDI database instance object. Otherwise returns zero.
  508. @ingroup Edi
  509. @stability Evolving
  510. */
  511. PUBLIC Edi *ediOpen(cchar *source, cchar *provider, int flags);
  512. /**
  513. Clone a database
  514. @param edi Database to clone
  515. @return A copy of the database
  516. @ingroup Edi
  517. @stability Internal
  518. */
  519. PUBLIC Edi *ediClone(Edi *edi);
  520. /**
  521. Run a database query query.
  522. @description This runs a provider dependant query. For the SDB SQLite provider, this runs an SQL statement.
  523. The "mdb" provider does not implement this API. To do queries using the "mdb" provider, use:
  524. #ediFindRec, #ediFindGrid and #ediReadField.
  525. The query may contain positional parameters via argc/argv or via a va_list. These are recommended to mitigate SQL injection risk.
  526. @param edi Database handle
  527. @param cmd Query command to execute.
  528. @param argc Number of query parameters in argv
  529. @param argv Query parameter arguments
  530. @param vargs Query parameters supplied in a NULL terminated va_list.
  531. @return If succesful, returns tabular data in the form of an EgiGrid structure. Returns NULL on errors.
  532. @ingroup Edi
  533. @stability Evolving
  534. */
  535. PUBLIC EdiGrid *ediQuery(Edi *edi, cchar *cmd, int argc, cchar **argv, va_list vargs);
  536. /**
  537. Read a formatted field from the database
  538. @description This reads a field from the database and formats the result using an optional format string.
  539. If the field has a null or empty value, the supplied defaultValue will be returned.
  540. @param edi Database handle
  541. @param fmt Reserved and not yet implemented. Set to NULL.
  542. @param tableName Database table name
  543. @param key Row key column value to read.
  544. @param fieldName Column name to read
  545. @param defaultValue Default value to return if the field is null or empty.
  546. @return Field value or default value if field is null or empty. Returns null if no matching record is found.
  547. @ingroup Edi
  548. @stability Evolving
  549. */
  550. PUBLIC cchar *ediReadFieldValue(Edi *edi, cchar *fmt, cchar *tableName, cchar *key, cchar *fieldName, cchar *defaultValue);
  551. /**
  552. Read a field from the database.
  553. @description This reads a field from the database.
  554. @param edi Database handle
  555. @param tableName Database table name
  556. @param key Row key column value to read.
  557. @param fieldName Column name to read
  558. @return Field value or null if the no record is found. May return null or empty if the field is null or empty.
  559. @ingroup Edi
  560. @stability Evolving
  561. */
  562. PUBLIC EdiField ediReadField(Edi *edi, cchar *tableName, cchar *key, cchar *fieldName);
  563. /**
  564. Read matching records in a table
  565. @description This runs a SQL like query on the database and returns matching records in a grid. The query selects
  566. the rows that have matching fields.
  567. @param edi Database handle
  568. @param tableName Database table name
  569. @param query SQL like query expression. This arg is a printf style format string. When expanded, this will contain
  570. a SQL style query expression of the form: "Field Op Value AND field OP value ... LIMIT offset, limit".
  571. All fields may be matched by using the pseudo column name "*". Where OP is "==", "!=", "<", ">", "<=", ">=" or "><".
  572. @return A grid containing all matching records. Returns NULL if no matching records.
  573. @ingroup Edi
  574. @stability Evolving
  575. */
  576. PUBLIC EdiGrid *ediFindGrid(Edi *edi, cchar *tableName, cchar *query);
  577. /**
  578. Read one record.
  579. @description This runs a simple query on the database and selects the first matching record. The query selects
  580. a row that has a "field" that matches the given "value".
  581. @param edi Database handle
  582. @param tableName Database table name
  583. @param query SQL like query expression. This arg is a printf style format string. When expanded, this will contain
  584. a SQL style query expression of the form: "Field Op Value AND field OP value ... LIMIT offset, limit".
  585. All fields may be matched by using the pseudo column name "*". Where OP is "==", "!=", "<", ">", "<=", ">=" or "><".
  586. @return First matching record. Returns NULL if no matching records.
  587. @ingroup Edi
  588. @stability Deprecated
  589. */
  590. PUBLIC EdiRec *ediFindRec(Edi *edi, cchar *tableName, cchar *query);
  591. /**
  592. Read a record.
  593. @description Read a record from the given table as identified by the key value.
  594. @param edi Database handle
  595. @param tableName Database table name
  596. @param key Key value of the record to read
  597. @return Record instance of EdiRec.
  598. @ingroup Edi
  599. @stability Evolving
  600. */
  601. PUBLIC EdiRec *ediReadRec(Edi *edi, cchar *tableName, cchar *key);
  602. #if DEPRECATED || 1
  603. /**
  604. Read a table.
  605. @description This reads all the records in a table and returns a grid containing the results.
  606. @param edi Database handle
  607. @param tableName Database table name
  608. @return A grid containing all records in the table. Returns NULL if no matching records.
  609. @ingroup Edi
  610. @stability Deprecated
  611. */
  612. PUBLIC EdiGrid *ediReadTable(Edi *edi, cchar *tableName) ME_DEPRECATED("Use ediFindGrid instead");
  613. /**
  614. Read one record.
  615. @description This runs a simple query on the database and selects the first matching record. The query selects
  616. a row that has a "field" that matches the given "value".
  617. This API is deprecated, use ediFindGrid instead.
  618. @param edi Database handle
  619. @param tableName Database table name
  620. @param fieldName Database field name to evaluate
  621. @param operation Comparision operation. Set to "==", "!=", "<", ">", "<=" or ">=".
  622. @param value Data value to compare with the field values.
  623. @return First matching record. Returns NULL if no matching records.
  624. @ingroup Edi
  625. @stability Deprecated
  626. */
  627. PUBLIC EdiRec *ediFindRecWhere(Edi *edi, cchar *tableName, cchar *fieldName, cchar *operation, cchar *value) ME_DEPRECATED("Use ediFindGrid instead");
  628. /**
  629. Read matching records.
  630. @description This runs a simple query on the database and returns matching records in a grid. The query selects
  631. all rows that have a "field" that matches the given "value".
  632. This API is deprecated, use ediFindGrid instead.
  633. @param edi Database handle
  634. @param tableName Database table name
  635. @param fieldName Database field name to evaluate
  636. @param operation Comparision operation. Set to "==", "!=", "<", ">", "<=" or ">=".
  637. @param value Data value to compare with the field values.
  638. @return A grid containing all matching records. Returns NULL if no matching records.
  639. @ingroup Edi
  640. @stability Deprecated
  641. */
  642. PUBLIC EdiGrid *ediReadWhere(Edi *edi, cchar *tableName, cchar *fieldName, cchar *operation, cchar *value) ME_DEPRECATED("Use ediFindRec instead");
  643. #endif
  644. /**
  645. Convert an EDI database record into a JSON string.
  646. @param rec EDI record
  647. @param flags Reserved. Set to zero.
  648. @return JSON string
  649. @ingroup Edi
  650. @stability Prototype
  651. */
  652. PUBLIC cchar *ediRecAsJson(EdiRec *rec, int flags);
  653. /**
  654. Remove a column from a table.
  655. @param edi Database handle
  656. @param tableName Database table name
  657. @param columnName Database column name
  658. @return Zero if successful. Otherwise a negative MPR error code.
  659. @ingroup Edi
  660. @stability Evolving
  661. */
  662. PUBLIC int edRemoveColumn(Edi *edi, cchar *tableName, cchar *columnName);
  663. /**
  664. Remove a table index.
  665. @param edi Database handle
  666. @param tableName Database table name
  667. @param indexName Ignored. Set to null. This call will remove the table index.
  668. @return Zero if successful. Otherwise a negative MPR error code.
  669. @ingroup Edi
  670. @stability Evolving
  671. */
  672. PUBLIC int ediRemoveIndex(Edi *edi, cchar *tableName, cchar *indexName);
  673. #if KEEP
  674. /**
  675. Delete a row in a database table identified by the query expression
  676. @param edi Database handle
  677. @param tableName Database table name
  678. @param query SQL like query expression. This arg is a printf style format string. When expanded, this will contain
  679. a SQL style query expression of the form: "Field Op Value AND field OP value ... LIMIT offset, limit".
  680. All fields may be matched by using the pseudo column name "*". Where OP is "==", "!=", "<", ">", "<=", ">=" or "><".
  681. @return Zero if successful. Otherwise a negative MPR error code.
  682. @ingroup Edi
  683. @stability Evolving
  684. */
  685. PUBLIC int ediRemoveRec(Edi *edi, cchar *tableName, cchar *query);
  686. #endif
  687. /**
  688. Delete a row in a database table identified by a key value
  689. @param edi Database handle
  690. @param tableName Database table name
  691. @param key Key column value to delete.
  692. @return Zero if successful. Otherwise a negative MPR error code.
  693. @ingroup Edi
  694. @stability Evolving
  695. */
  696. PUBLIC int ediRemoveRec(Edi *edi, cchar *tableName, cchar *key);
  697. /**
  698. Remove a table from the database.
  699. @param edi Database handle
  700. @param tableName Database table name
  701. @return Zero if successful. Otherwise a negative MPR error code.
  702. @ingroup Edi
  703. @stability Evolving
  704. */
  705. PUBLIC int ediRemoveTable(Edi *edi, cchar *tableName);
  706. /**
  707. Rename a table.
  708. @param edi Database handle
  709. @param tableName Database table name
  710. @param newTableName New database table name
  711. @return Zero if successful. Otherwise a negative MPR error code.
  712. @ingroup Edi
  713. @stability Evolving
  714. */
  715. PUBLIC int ediRenameTable(Edi *edi, cchar *tableName, cchar *newTableName);
  716. /**
  717. Rename a column.
  718. @param edi Database handle
  719. @param tableName Database table name
  720. @param columnName Database column name
  721. @param newColumnName New column name
  722. @return Zero if successful. Otherwise a negative MPR error code.
  723. @ingroup Edi
  724. @stability Evolving
  725. */
  726. PUBLIC int ediRenameColumn(Edi *edi, cchar *tableName, cchar *columnName, cchar *newColumnName);
  727. /**
  728. Save in-memory database contents to disk.
  729. @description How this call behaves is provider dependant. If the provider is "mdb" and the database is not opened
  730. with AutoSave, then this call will save the in-memory contents. If the "mdb" database is opened with AutoSave,
  731. then this call will do nothing. For the "sdb" SQLite provider, this call does nothing.
  732. @param edi Database handle
  733. @return Zero if successful. Otherwise a negative MPR error code.
  734. @ingroup Edi
  735. @stability Evolving
  736. */
  737. PUBLIC int ediSave(Edi *edi);
  738. /**
  739. Set a record field without writing to the database.
  740. @description This routine updates the record object with the given value. The record will not be written
  741. to the database. To write to the database, use #ediUpdateRec.
  742. @param rec Record to update
  743. @param fieldName Record field name to update
  744. @param value Value to update
  745. @return The record instance if successful, otherwise NULL.
  746. @ingroup Edi
  747. @stability Evolving
  748. */
  749. PUBLIC EdiRec *ediSetField(EdiRec *rec, cchar *fieldName, cchar *value);
  750. /**
  751. Set a record field using a format string.
  752. @description This routine updates the record object with the given value. The record will not be written
  753. to the database. To write to the database, use #ediUpdateRec.
  754. @param rec Record to update
  755. @param fieldName Record field name to update
  756. @param fmt Format string
  757. @param ... Variable arguments for the format string
  758. @return The record instance if successful, otherwise NULL.
  759. @ingroup Edi
  760. @stability Evolving
  761. */
  762. PUBLIC EdiRec *ediSetFieldFmt(EdiRec *rec, cchar *fieldName, cchar *fmt, ...);
  763. /**
  764. Set record fields without writing to the database.
  765. @description This routine updates the record object with the given values. The "data' argument supplies
  766. the fieldNames and values. The data may come from the request params() or it can be manually
  767. created via #ediMakeJson.
  768. For example: ediSetFields(rec, mprParseJson("{ name: '%s', address: '%s' }", name, address))
  769. The record will not be written to the database. To write to the database, use #ediUpdateRec.
  770. @param rec Record to update
  771. @param data Json object of field to use for the update
  772. @return The record instance if successful, otherwise NULL.
  773. @ingroup Edi
  774. @stability Evolving
  775. */
  776. PUBLIC EdiRec *ediSetFields(EdiRec *rec, MprJson *data);
  777. /**
  778. Control whether the database accepts updates.
  779. @param edi Database handle
  780. @param on Set to true to make the database readonly, i.e. to disable all updates.
  781. @ingroup Edi
  782. @stability Prototype
  783. */
  784. PUBLIC void ediSetReadonly(Edi *edi, bool on);
  785. /**
  786. Create a private database for each client.
  787. @param edi Database handle
  788. @param on Set to true to clone the database for each connected client.
  789. @ingroup Edi
  790. @stability Internal
  791. */
  792. PUBLIC void ediSetPrivate(Edi *edi, bool on);
  793. /**
  794. Write a value to a database table field
  795. @description Update the value of a table field in the selected table row. Note: field validations are not run MOB.
  796. @param edi Database handle
  797. @param tableName Database table name
  798. @param key Key value for the table row to update.
  799. @param fieldName Column name to update
  800. @param value Value to write to the database field
  801. @return Zero if successful. Otherwise a negative MPR error code.
  802. @ingroup Edi
  803. @stability Evolving
  804. */
  805. PUBLIC int ediUpdateField(Edi *edi, cchar *tableName, cchar *key, cchar *fieldName, cchar *value);
  806. /**
  807. Write a formatted value to a database table field.
  808. @description Update the value of a table field in the selected table row. Note: field validations are not run.
  809. @param edi Database handle
  810. @param tableName Database table name
  811. @param key Key value for the table row to update.
  812. @param fieldName Column name to update
  813. @param fmt Value format string
  814. @param ... Variable arguments for the format string
  815. @return Zero if successful. Otherwise a negative MPR error code.
  816. @ingroup Edi
  817. @stability Evolving
  818. */
  819. PUBLIC int ediUpdateFieldFmt(Edi *edi, cchar *tableName, cchar *key, cchar *fieldName, cchar *fmt, ...);
  820. /**
  821. Write a record to the database.
  822. @description If the record is a new record and the "id" column is EDI_AUTO_INC, then the "id" will be assigned
  823. prior to saving the record.
  824. @param edi Database handle
  825. @param rec Record to write to the database.
  826. @return Zero if successful. Otherwise a negative MPR error code.
  827. @ingroup Edi
  828. @stability Evolving
  829. */
  830. PUBLIC int ediUpdateRec(Edi *edi, EdiRec *rec);
  831. /**
  832. Validate a record.
  833. @description Run defined field validations and return true if the record validates. Field validations are defined
  834. via #ediAddValidation calls. If any validations fail, error messages will be added to the record and can be
  835. retrieved via #ediGetRecErrors.
  836. @param rec Record to validate
  837. @return True if all field valiations pass.
  838. @ingroup Edi
  839. @stability Evolving
  840. */
  841. PUBLIC bool ediValidateRec(EdiRec *rec);
  842. /**************************** Convenience Routines ****************************/
  843. /**
  844. Create a bare grid.
  845. @description This creates an empty grid based on the given table's schema.
  846. @param edi Database handle
  847. @param tableName Database table name
  848. @param nrows Number of rows to reserve in the grid
  849. @return EdiGrid instance
  850. @ingroup Edi
  851. @stability Evolving
  852. */
  853. PUBLIC EdiGrid *ediCreateBareGrid(Edi *edi, cchar *tableName, int nrows);
  854. /**
  855. Create a bare, free-standing record.
  856. @description This creates an empty record based. The tableName and number of fields are defined
  857. in the record, but otherwise, the record's fields are uninitialized. This API is a low level API
  858. used internally by ESP and EDI.
  859. @param edi Database handle
  860. @param tableName Database table name
  861. @param nfields Number of fields to reserve in the record
  862. @return EdiGrid instance
  863. @ingroup Edi
  864. @stability Evolving
  865. */
  866. PUBLIC EdiRec *ediCreateBareRec(Edi *edi, cchar *tableName, int nfields);
  867. /**
  868. Filter the fields of a grid
  869. @param grid Grid to modify and filter
  870. @param fields Space separated list of record field names
  871. @param include Set to true to interpret the names as fields to include. If false, interpret the names
  872. as fields to reject.
  873. @return The filtered grid. Same reference as the input grid.
  874. @ingroup EdiGrid
  875. @stability Internal
  876. */
  877. PUBLIC EdiGrid *ediFilterGridFields(EdiGrid *grid, cchar *fields, int include);
  878. /**
  879. Filter the fields of a record
  880. @param rec Record to modify and filter
  881. @param fields Space separated list of record field names
  882. @param include Set to true to interpret the names as fields to include. If false, interpret the names
  883. as fields to reject.
  884. @return The filtered record. Same reference as the input record.
  885. @ingroup EdiRec
  886. @stability Internal
  887. */
  888. PUBLIC EdiRec *ediFilterRecFields(EdiRec *rec, cchar *fields, int include);
  889. /**
  890. Format a field value.
  891. @param fmt Printf style format string
  892. @param fp Field whoes value will be formatted
  893. @return Formatted value string
  894. @ingroup Edi
  895. @stability Evolving
  896. */
  897. PUBLIC cchar *ediFormatField(cchar *fmt, EdiField *fp);
  898. /**
  899. Get a record field
  900. @param rec Database record
  901. @param fieldName Field in the record to extract
  902. @return An EdiField structure containing the record field value and details.
  903. @ingroup Edi
  904. @stability Evolving
  905. */
  906. PUBLIC EdiField *ediGetField(EdiRec *rec, cchar *fieldName);
  907. /**
  908. Get a field value
  909. @param rec Database record
  910. @param fieldName Field in the record to extract
  911. @return A field value as a string.
  912. @ingroup Edi
  913. @stability Evolving
  914. */
  915. PUBLIC cchar *ediGetFieldValue(EdiRec *rec, cchar *fieldName);
  916. /**
  917. Get the data type of a record field.
  918. @param rec Record to examine
  919. @param fieldName Field to examine
  920. @return The field type. Returns one of: EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE, EDI_TYPE_FLOAT,
  921. EDI_TYPE_INT, EDI_TYPE_STRING, EDI_TYPE_TEXT.
  922. @ingroup Edi
  923. @stability Evolving
  924. */
  925. PUBLIC int ediGetFieldType(EdiRec *rec, cchar *fieldName);
  926. /**
  927. Get a list of grid column names.
  928. @param grid Database grid
  929. @return An MprList of column names in the given grid.
  930. @ingroup Edi
  931. @stability Evolving
  932. */
  933. PUBLIC MprList *ediGetGridColumns(EdiGrid *grid);
  934. /**
  935. Get the schema for a grid and format as JSON
  936. @param grid Grid to examine
  937. @ingroup EdiGrid
  938. @stability Prototype
  939. */
  940. PUBLIC cchar *ediGetGridSchemaAsJson(EdiGrid *grid);
  941. /**
  942. Get record validation errors.
  943. @param rec Database record
  944. @return A hash of validation errors. If validation passed, then this call returns NULL.
  945. @ingroup Edi
  946. @stability Evolving
  947. */
  948. PUBLIC MprHash *ediGetRecErrors(EdiRec *rec);
  949. /**
  950. Convert an EDI type to a string.
  951. @param type Column data type. Set to one of EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE
  952. EDI_TYPE_FLOAT, EDI_TYPE_INT, EDI_TYPE_STRING, EDI_TYPE_TEXT
  953. @return Type string. This will be set to one of: "binary", "bool", "date", "float", "int", "string" or "text".
  954. @ingroup Edi
  955. @stability Evolving
  956. */
  957. PUBLIC char *ediGetTypeString(int type);
  958. /**
  959. Make a JSON container of property values.
  960. @description This routine formats the given arguments, parses the result into a JSON object.
  961. @param fmt Printf style format string
  962. @param ... arguments
  963. @return MprJson instance
  964. @ingroup Edi
  965. @stability Evolving
  966. */
  967. PUBLIC MprJson *ediMakeJson(cchar *fmt, ...);
  968. /**
  969. Make a grid.
  970. @description This call makes a free-standing data grid based on the JSON format content string.
  971. @param content JSON format content string. The content should be an array of objects where each object is a
  972. set of property names and values.
  973. @return An EdiGrid instance
  974. @example:
  975. grid = ediMakeGrid("[ \\ \n
  976. { id: '1', country: 'Australia' }, \ \n
  977. { id: '2', country: 'China' }, \ \n
  978. ]");
  979. @ingroup Edi
  980. @stability Evolving
  981. */
  982. PUBLIC EdiGrid *ediMakeGrid(cchar *content);
  983. /**
  984. Make a record from a JSON fields object.
  985. @description This call makes a free-standing data record based on the JSON fields.
  986. @param tableName Name of the database table to initialize in the record.
  987. @param fields JSON object.
  988. @return An EdiRec instance
  989. @ingroup Edi
  990. @stability Prototype
  991. @see ediMakeRec ediMakeGrid
  992. */
  993. PUBLIC EdiRec *ediMakeRecFromJson(cchar *tableName, MprJson *fields);
  994. /**
  995. Make a record.
  996. @description This call makes a free-standing data record based on the JSON format content string.
  997. @param content JSON format content string. The content should be a set of property names and values.
  998. @return An EdiRec instance
  999. @example: rec = ediMakeRec("{ id: 1, title: 'Message One', body: 'Line one' }");
  1000. @ingroup Edi
  1001. @stability Evolving
  1002. */
  1003. PUBLIC EdiRec *ediMakeRec(cchar *content);
  1004. /**
  1005. Manage an EdiRec instance for garbage collection.
  1006. @param rec Record instance
  1007. @param flags GC management flag
  1008. @ingroup Edi
  1009. @stability Evolving
  1010. @internal
  1011. */
  1012. PUBLIC void ediManageEdiRec(EdiRec *rec, int flags);
  1013. /**
  1014. Parse an EDI type string.
  1015. @param type Type string set to one of: "binary", "bool", "date", "float", "int", "string" or "text".
  1016. @return Type code. Set to one of EDI_TYPE_BINARY, EDI_TYPE_BOOL, EDI_TYPE_DATE, EDI_TYPE_FLOAT, EDI_TYPE_INT,
  1017. EDI_TYPE_STRING, EDI_TYPE_TEXT.
  1018. @ingroup Edi
  1019. @stability Evolving
  1020. */
  1021. PUBLIC int ediParseTypeString(cchar *type);
  1022. /**
  1023. Pivot a grid swapping rows for columns
  1024. @param grid Source grid
  1025. @param flags Control flags. Set to EDI_PIVOT_FIELD_NAMES to use field names as the first column of data.
  1026. @result New pivoted grid
  1027. @ingroup EdiGrid
  1028. @stability Evolving
  1029. */
  1030. PUBLIC EdiGrid *ediPivotGrid(EdiGrid *grid, int flags);
  1031. /**
  1032. @internal
  1033. */
  1034. PUBLIC EdiGrid *ediSortGrid(EdiGrid *grid, cchar *sortColumn, int sortOrder);
  1035. #if ME_COM_MDB
  1036. PUBLIC void mdbInit(void);
  1037. #endif
  1038. #if ME_COM_SQLITE
  1039. PUBLIC void sdbInit(void);
  1040. #endif
  1041. #ifdef __cplusplus
  1042. } /* extern C */
  1043. #endif
  1044. #endif /* _h_EDI */
  1045. /*
  1046. Copyright (c) Embedthis Software. All Rights Reserved.
  1047. This software is distributed under a commercial license. Consult the LICENSE.md
  1048. distributed with this software for full details and copyrights.
  1049. */
  1050. /*
  1051. mdb.h -- Memory Database (MDB).
  1052. Copyright (c) All Rights Reserved. See copyright notice at the bottom of the file.
  1053. */
  1054. #ifndef _h_MDB
  1055. #define _h_MDB 1
  1056. /********************************* Includes ***********************************/
  1057. #include "http.h"
  1058. #if ME_COM_MDB
  1059. #ifdef __cplusplus
  1060. extern "C" {
  1061. #endif
  1062. /****************************** Forward Declarations **************************/
  1063. #if !DOXYGEN
  1064. #endif
  1065. /********************************** Tunables **********************************/
  1066. #define MDB_INCR 8 /**< Default memory allocation increment for MDB */
  1067. /*
  1068. Per column structure
  1069. */
  1070. typedef struct MdbCol {
  1071. char *name; /* Column name */
  1072. int type; /* Column type */
  1073. int flags; /* Column flags */
  1074. int cid; /* Column index in MdbSchema.cols */
  1075. int64 lastValue; /* Last value if auto-inc */
  1076. } MdbCol;
  1077. /*
  1078. Table schema
  1079. */
  1080. typedef struct MdbSchema {
  1081. int ncols; /* Number of columns in table */
  1082. int capacity; /* Capacity of cols */
  1083. MdbCol cols[ARRAY_FLEX]; /* Array of columns */
  1084. } MdbSchema;
  1085. /*
  1086. Per row structure
  1087. */
  1088. typedef struct MdbRow {
  1089. struct MdbTable *table; /* Reference to MdbTable */
  1090. int rid; /* Table index in MdbTable.row */
  1091. int nfields; /* Number of fields in fields */
  1092. cchar *fields[ARRAY_FLEX];/* All data stored as strings */
  1093. } MdbRow;
  1094. /*
  1095. Per table structure
  1096. */
  1097. typedef struct MdbTable {
  1098. char *name; /* Table name */
  1099. MdbSchema *schema; /* Table columns schema */
  1100. MprHash *index; /* Table index */
  1101. MdbCol *keyCol; /* Reference to the key column (unmanaged) */
  1102. MdbCol *indexCol; /* Reference to the index column (unmanaged) */
  1103. MprList *rows; /* Table row */
  1104. } MdbTable;
  1105. /*
  1106. Mdb flags
  1107. */
  1108. #define MDB_LOADING 0x1
  1109. /*
  1110. Per database structure
  1111. */
  1112. typedef struct Mdb {
  1113. Edi edi; /**< EDI database interface structure */
  1114. MprList *tables; /**< List of tables */
  1115. /*
  1116. When loading from file only (do not mark)
  1117. */
  1118. MdbTable *loadTable; /* Current table */
  1119. MdbCol *loadCol; /* Current column */
  1120. MdbRow *loadRow; /* Current row */
  1121. MprList *loadStack; /* State stack */
  1122. MprHash *validations; /**< Validations */
  1123. int loadCid; /* Current column index to load */
  1124. int loadState; /* Current state */
  1125. int loadNcols; /* Expected number of cols */
  1126. int lineNumber; /* Current line number in path */
  1127. } Mdb;
  1128. #ifdef __cplusplus
  1129. } /* extern C */
  1130. #endif
  1131. #endif /* ME_COM_MDB */
  1132. #endif /* _h_MDB */
  1133. /*
  1134. Copyright (c) Embedthis Software. All Rights Reserved.
  1135. This software is distributed under a commercial license. Consult the LICENSE.md
  1136. distributed with this software for full details and copyrights.
  1137. */
  1138. /*
  1139. esp.h -- Embedded Server Pages (ESP) Module handler.
  1140. Copyright (c) All Rights Reserved. See copyright notice at the bottom of the file.
  1141. */
  1142. #ifndef _h_ESP
  1143. #define _h_ESP 1
  1144. /********************************* Includes ***********************************/
  1145. #ifdef __cplusplus
  1146. extern "C" {
  1147. #endif
  1148. /********************************** Tunables **********************************/
  1149. #ifndef ME_ESP_ABBREV
  1150. #define ME_ESP_ABBREV 1 /**< Enable the ESP Abbreviated API */
  1151. #endif
  1152. #ifndef ME_ESP_EMAIL_TIMEOUT
  1153. #define ME_ESP_EMAIL_TIMEOUT (60 * 1000) /**< Timeout for sending email */
  1154. #endif
  1155. #ifndef ME_ESP_RELOAD_TIMEOUT
  1156. #define ME_ESP_RELOAD_TIMEOUT (5 * 1000) /**< Timeout for reloading esp modules */
  1157. #endif
  1158. #define ESP_TOK_INCR 1024 /**< Growth increment for ESP tokens */
  1159. #define ESP_LISTEN "4000" /**< Default listening endpoint for the esp program */
  1160. #define ESP_UNLOAD_TIMEOUT (10) /**< Very short timeout for reloading */
  1161. #define ESP_LIFESPAN (3600 * TPS) /**< Default generated content cache lifespan */
  1162. #define ESP_COMPILE_JSON "esp-compile.json" /**< Compile rules filename */
  1163. #if ME_64
  1164. #define ESP_VSKEY "HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VS7"
  1165. #else
  1166. #define ESP_VSKEY "HKLM\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7"
  1167. #endif
  1168. #ifndef ESP_VERSION
  1169. #define ESP_VERSION ME_VERSION
  1170. #endif
  1171. #ifndef ESP_MAJOR_VERSION
  1172. #define ESP_MAJOR_VERSION ME_MAJOR_VERSION
  1173. #ifndef ESP_MINOR_VERSION
  1174. #define ESP_MINOR_VERSION ME_MINOR_VERSION
  1175. #endif
  1176. #endif
  1177. /********************************** Defines ***********************************/
  1178. /*
  1179. Forward declare the EspAction
  1180. */
  1181. struct EspAction;
  1182. /**
  1183. Procedure callback
  1184. @ingroup Esp
  1185. @stability Evolving
  1186. */
  1187. typedef void (*EspLegacyProc)(HttpStream *stream);
  1188. typedef void (*EspProc)(HttpStream *stream, struct EspAction *action);
  1189. #define ESP_CONTENT_MARKER "${_ESP_CONTENT_MARKER_}" /* Layout content marker */
  1190. #if ME_WIN_LIKE
  1191. #define ESP_EXPORT __declspec(dllexport)
  1192. #else
  1193. #define ESP_EXPORT
  1194. #endif
  1195. #define ESP_EXPORT_STRING MPR_STRINGIFY(ESP_EXPORT)
  1196. #define ESP_FEEDBACK_VAR "__feedback__"
  1197. /*
  1198. Default VxWorks environment
  1199. */
  1200. #ifndef WIND_BASE
  1201. #define WIND_BASE "WIND_BASE-Not-Configured"
  1202. #endif
  1203. #ifndef WIND_HOME
  1204. #define WIND_HOME "WIND_HOME-Not-Configured"
  1205. #endif
  1206. #ifndef WIND_HOST_TYPE
  1207. #define WIND_HOST_TYPE "WIND_HOST_TYPE-Not-Configured"
  1208. #endif
  1209. #ifndef WIND_PLATFORM
  1210. #define WIND_PLATFORM "WIND_PLATFORM-Not-Configured"
  1211. #endif
  1212. #ifndef WIND_GNU_PATH
  1213. #define WIND_GNU_PATH "WIND_GNU_PATH-Not-Configured"
  1214. #endif
  1215. /********************************** Parsing ***********************************/
  1216. /**
  1217. ESP page parser structure
  1218. @defgroup EspParse EspParse
  1219. @see Esp
  1220. @internal
  1221. */
  1222. typedef struct EspState {
  1223. char *data; /**< Input data to parse */
  1224. char *next; /**< Next character in input */
  1225. int lineNumber; /**< Line number for error reporting */
  1226. MprBuf *token; /**< Current token */
  1227. MprBuf *global; /**< Accumulated compiled esp global code */
  1228. MprBuf *start; /**< Accumulated compiled esp start of function code */
  1229. MprBuf *end; /**< Accumulated compiled esp end of function code */
  1230. } EspState;
  1231. #define ESP_COMPILE_SYMBOLS 0 /**< Override to compile in debug mode. Defaults to same as Appweb */
  1232. #define ESP_COMPILE_OPTIMIZED 1 /**< Override to compile in release mode */
  1233. /**
  1234. Top level ESP structure. This is a singleton.
  1235. */
  1236. typedef struct Esp {
  1237. MprHash *databases; /**< Cloned databases */
  1238. MprEvent *databasesTimer; /**< Database prune timer */
  1239. MprHash *internalOptions; /**< Table of internal HTML control options */
  1240. MprThreadLocal *local; /**< Thread local data */
  1241. MprMutex *mutex; /**< Multithread lock */
  1242. EdiService *ediService; /**< Database service */
  1243. cchar *hostedDocuments; /**< Documents directory if hosted */
  1244. int compileMode; /**< Force a debug compile */
  1245. int inUse; /**< Active ESP request counter */
  1246. int reloading; /**< Reloading ESP and modules */
  1247. MprHash *vstudioEnv; /**< Visual Studio environment */
  1248. } Esp;
  1249. /**
  1250. Entry point for a loadable ESP module
  1251. @param route HttpRoute object
  1252. @param module Mpr module object
  1253. @return Zero if successful, otherwise a negative MPR error code.
  1254. @ingroup EspRoute
  1255. @stability Stable
  1256. */
  1257. typedef int (*EspModuleEntry)(struct HttpRoute *route, MprModule *module);
  1258. /**
  1259. ESP initialization entry point
  1260. @param module Module object if loaded as an MPR module.
  1261. @return Zero if successful, otherwise a negative MPR error code.
  1262. @ingroup Esp
  1263. @stability Evolving
  1264. */
  1265. PUBLIC int espOpen(MprModule *module);
  1266. /**
  1267. Initialize a static library ESP module
  1268. @description This invokes the ESP initializers for the required pre-compiled ESP shared library.
  1269. @param entry ESP initialization function.
  1270. @param appName Name of the ESP application
  1271. @param routeName Name of the route in the appweb.conf file for this ESP application or page
  1272. @return Zero if successful, otherwise a negative MPR error code.
  1273. @ingroup Esp
  1274. @stability Evolving
  1275. */
  1276. PUBLIC int espStaticInitialize(EspModuleEntry entry, cchar *appName, cchar *routeName);
  1277. /**
  1278. Add HTLM internal options to the Esp.options hash
  1279. @internal
  1280. */
  1281. PUBLIC void espInitHtmlOptions(Esp *esp);
  1282. /**
  1283. Initialize the ESP configuration file parser
  1284. @internal
  1285. */
  1286. PUBLIC int espInitParser(void);
  1287. /********************************** EspRoutes *********************************/
  1288. /**
  1289. EspRoute extended route configuration.
  1290. Note that HttpRoutes may share an EspRoute.
  1291. @defgroup EspRoute EspRoute
  1292. @see Esp
  1293. */
  1294. typedef struct EspRoute {
  1295. cchar *appName; /**< App module name */
  1296. struct EspRoute *top; /**< Top-level route for this application */
  1297. HttpRoute *route; /**< Back link to route */
  1298. EspProc commonController; /**< Common code for all controllers */
  1299. MprTime loaded; /**< When configuration was last loaded */
  1300. MprHash *actions; /**< Table of actions */
  1301. MprHash *env; /**< Environment variables for route */
  1302. MprHash *views; /**< Table of views */
  1303. cchar *currentSession; /**< Current login session when enforcing a single login */
  1304. cchar *configFile; /**< Path to config file */
  1305. cchar *compileCmd; /**< Compile command template */
  1306. cchar *linkCmd; /**< Link command template */
  1307. cchar *searchPath; /**< Search path to use when locating compiler/linker */
  1308. cchar *winsdk; /**< Windows SDK */
  1309. uint app: 1; /**< Is an esp mvc application */
  1310. uint combine: 1; /**< Combine C source into a single file */
  1311. uint compileMode: 1; /**< Compile the application debug or release mode */
  1312. uint compile: 1; /**< Enable recompiling the application or esp page */
  1313. uint encodeTypes: 1; /**< Encode data types in JSON API request/response */
  1314. uint keep: 1; /**< Keep intermediate source code after compiling */
  1315. uint update: 1; /**< Enable dynamically updating the application */
  1316. Edi *edi; /**< Default database for this route */
  1317. #if DEPRECATED && REMOVE
  1318. cchar *combineScript; /**< Combine mode script filename */
  1319. cchar *combineSheet; /**< Combine mode stylesheet filename */
  1320. #endif
  1321. } EspRoute;
  1322. #if DEPRECATED && REMOVE
  1323. /**
  1324. Add the specified pak to the pak.json packs list.
  1325. @param route HttpRoute defining the ESP application
  1326. @param name Desired pak name. For example: "vue-mvc"
  1327. @param version Pack version string.
  1328. @returns Zero if successful, otherwise a negative MPR error code.
  1329. @ingroup EspRoute
  1330. @stability Deprecated
  1331. */
  1332. PUBLIC void espAddPak(HttpRoute *route, cchar *name, cchar *version);
  1333. #endif
  1334. /**
  1335. Add a route for the home page.
  1336. @description This will add a home page to route ESP applications. This will add the following route:
  1337. <table>
  1338. <tr><td>Name</td><td>Method</td><td>Pattern</td><td>Target</td></tr>
  1339. <tr><td>home</td><td>GET,POST,PUT</td><td>^/$</td><td>index.esp</td></tr>
  1340. </table>
  1341. @param route Parent route from which to inherit configuration.
  1342. @ingroup EspRoute
  1343. @stability Evolving
  1344. */
  1345. PUBLIC void espAddHomeRoute(HttpRoute *route);
  1346. /**
  1347. Add a route set
  1348. @description This will add a set of routes. It will add a home route and optional routes depending on the route set.
  1349. <table>
  1350. <tr><td>Name</td><td>Method</td><td>Pattern</td><td>Target</td></tr>
  1351. <tr><td>home</td><td>GET,POST,PUT</td><td>^/$</td><td>index.esp</td></tr>
  1352. </table>
  1353. @param route Parent route from which to inherit configuration.
  1354. @param set Route set to select. Use "vue-mvc", or "html-mvc".
  1355. @ingroup EspRoute
  1356. @stability Stable
  1357. */
  1358. PUBLIC void espAddRouteSet(HttpRoute *route, cchar *set);
  1359. /**
  1360. Initialize ESP
  1361. @description This initializes a route for ESP. This may be called multiple times for different routes.
  1362. @param route Parent route from which to inherit configuration.
  1363. @param prefix Optional URI prefix for all application URIs.
  1364. @param path Pathname to the esp.json file.
  1365. @returns Zero if successful, otherwise a negative MPR error code.
  1366. @ingroup EspRoute
  1367. @stability Prototype
  1368. */
  1369. PUBLIC int espInit(HttpRoute *route, cchar *prefix, cchar *path);
  1370. /**
  1371. Load configuration for an ESP application
  1372. @description Load the application's esp.json and pak.json configuration files.
  1373. @param route Parent route from which to inherit configuration.
  1374. @returns Zero if successful, otherwise a negative MPR error code.
  1375. @ingroup EspRoute
  1376. @stability Prototype
  1377. */
  1378. PUBLIC int espLoadConfig(HttpRoute *route);
  1379. /**
  1380. Return the corresponding EspRoute for the given Route.
  1381. @description Returns the defined EspRoute for the given Route. Creates a new EspRoute if required.
  1382. @param route Parent route from which to inherit configuration.
  1383. @param create Set to true to create an EspRoute if a suitable one cannot be found.
  1384. @returns The EspRoute object.
  1385. @ingroup EspRoute
  1386. @stability Prototype
  1387. */
  1388. PUBLIC EspRoute *espRoute(HttpRoute *route, bool create);
  1389. /**
  1390. Add caching for response content.
  1391. @description This call configures caching for request responses. Caching may be used for any HTTP method,
  1392. though typically it is most useful for state-less GET requests. Output data may be uniquely cached for requests
  1393. with different request parameters (query, post and route parameters).
  1394. \n\n
  1395. When server-side caching is requested and manual-mode is not enabled, the request response will be automatically
  1396. cached. Subsequent client requests will revalidate the cached content with the server. If the server-side cached
  1397. content has not expired, a HTTP Not-Modified (304) response will be sent and the client will use its client-side
  1398. cached content. This results in a very fast transaction with the client as no response data is sent.
  1399. Server-side caching will cache both the response headers and content.
  1400. \n\n
  1401. If manual server-side caching is requested, the response will be automatically cached, but subsequent requests will
  1402. require the handler to explicitly send cached content by calling #httpWriteCached.
  1403. \n\n
  1404. If client-side caching is requested, a "Cache-Control" Http header will be sent to the client with the caching
  1405. "max-age" set to the lifesecs argument value. This causes the client to serve client-cached
  1406. content and to not contact the server at all until the max-age expires.
  1407. Alternatively, you can use #httpSetHeader to explicitly set a "Cache-Control header. For your reference, here are
  1408. some keywords that can be used in the Cache-Control Http header.
  1409. \n\n
  1410. "max-age" Max time in seconds the resource is considered fresh.
  1411. "s-maxage" Max time in seconds the resource is considered fresh from a shared cache.
  1412. "public" marks authenticated responses as cacheable.
  1413. "private" shared caches may not store the response.
  1414. "no-cache" cache must re-submit request for validation before using cached copy.
  1415. "no-store" response may not be stored in a cache.
  1416. "must-revalidate" forces clients to revalidate the request with the server.
  1417. "proxy-revalidate" similar to must-revalidate except only for proxy caches.
  1418. \n\n
  1419. Use client-side caching for static content that will rarely change or for content for which using "reload" in
  1420. the browser is an adequate solution to force a refresh. Use manual server-side caching for situations where you need to
  1421. explicitly control when and how cached data is returned to the client. For most other situations, use server-side
  1422. caching.
  1423. @param route HttpRoute object
  1424. @param uri URI to cache.
  1425. If the URI is set to "*" all URIs for that action are uniquely cached. If the request has POST data,
  1426. the URI may include such post data in a sorted query format. E.g. {uri: /buy?item=scarf&quantity=1}.
  1427. @param lifesecs Lifespan of cache items in seconds. If not set to positive integer, the lifesecs will
  1428. default to the route lifespan.
  1429. @param flags Cache control flags. Select ESP_CACHE_MANUAL to enable manual mode. In manual mode, cached content
  1430. will not be automatically sent. Use #httpWriteCached in the request handler to write previously cached content.
  1431. \n\n
  1432. Select ESP_CACHE_CLIENT to enable client-side caching. In this mode a "Cache-Control" Http header will be
  1433. sent to the client with the caching "max-age". WARNING: the client will not send any request for this URI
  1434. until the max-age timeout has expired.
  1435. \n\n
  1436. Select HTTP_CACHE_RESET to first reset existing caching configuration for this route.
  1437. \n\n
  1438. Select HTTP_CACHE_COMBINED, HTTP_CACHE_ONLY or HTTP_CACHE_UNIQUE to define the server-side caching mode. Only
  1439. one of these three mode flags should be specified.
  1440. \n\n
  1441. If the HTTP_CACHE_COMBINED flag is set, the request params (query, post data and route parameters) will be
  1442. ignored and all request for a given URI path will cache to the same cache record.
  1443. \n\n
  1444. Select HTTP_CACHE_UNIQUE to uniquely cache requests with different request parameters. The URIs specified in
  1445. uris should not contain any request parameters.
  1446. \n\n
  1447. Select HTTP_CACHE_ONLY to cache only the exact URI with parameters specified in uris. The parameters must be
  1448. in sorted www-urlencoded format. For example: /example.esp?hobby=sailing&name=john.
  1449. @return A count of the bytes actually written
  1450. @ingroup EspRoute
  1451. @stability Evolving
  1452. @internal
  1453. */
  1454. PUBLIC int espCache(HttpRoute *route, cchar *uri, int lifesecs, int flags);
  1455. /**
  1456. Compile an ESP page, controller or view
  1457. @description This compiles ESP resources into loadable, cached modules
  1458. @param route HttpRoute object
  1459. @param dispatcher Optional dispatcher to use when waiting for the compilation command.
  1460. @param source ESP source file name
  1461. @param module Output module file name
  1462. @param cacheName MD5 cache name. Not a full path
  1463. @param isView Set to "true" if the source is a view
  1464. @param errMsg Reference to receive an error message if the routine fails.
  1465. @return "True" if the compilation is successful. Errors are logged and sent back to the client if ShowErrors is true.
  1466. @ingroup EspRoute
  1467. @stability Evolving
  1468. @internal
  1469. */
  1470. PUBLIC bool espCompile(HttpRoute *route, MprDispatcher *dispatcher, cchar *source, cchar *module, cchar *cacheName,
  1471. int isView, char **errMsg);
  1472. /**
  1473. Convert an ESP web page into C code
  1474. @description This parses an ESP web page into an equivalent C source view.
  1475. @param route HttpRoute object
  1476. @param page ESP web page script.
  1477. @param path Pathname for the ESP web page. This is used to process include directives which are resolved relative
  1478. to this path.
  1479. @param cacheName MD5 cache name. Not a full path.
  1480. @param layout Default layout page. Deprecated.
  1481. @param state Reserved. Must set to NULL.
  1482. @param err Output parameter to hold any relevant error message.
  1483. @return Compiled script. Return NULL on errors.
  1484. @ingroup EspRoute
  1485. @stability Evolving
  1486. @internal
  1487. */
  1488. PUBLIC char *espBuildScript(HttpRoute *route, cchar *page, cchar *path, cchar *cacheName, cchar *layout,
  1489. EspState *state, char **err);
  1490. /**
  1491. Define an action for a URI pattern.
  1492. @description This creates a new route and binds the action function to a URI pattern.
  1493. @param route Parent route object from which to inherit settings when creating the new route.
  1494. @param pattern URI pattern to use to find the releavant route.
  1495. @param actionProc EspProc callback procedure to invoke when the action is requested.
  1496. @ingroup EspRoute
  1497. @stability Stable
  1498. */
  1499. PUBLIC int espBindProc(HttpRoute *route, cchar *pattern, void *actionProc);
  1500. /**
  1501. Define a common controller function to invoke before invoking for all controller actions.
  1502. @description A base controller function can be defined that will be called before calling any controller action. This emulates a super class constructor.
  1503. @param route HttpRoute object
  1504. @param baseProc Function to call just prior to invoking a controller action.
  1505. @ingroup EspRoute
  1506. @stability Evolving
  1507. */
  1508. PUBLIC void espController(HttpRoute *route, EspProc baseProc);
  1509. /**
  1510. Create an EspRoute object
  1511. @param route HttpRoute to associate with
  1512. @return EspRoute object
  1513. @internal
  1514. @stability Stable
  1515. */
  1516. PUBLIC EspRoute *espCreateRoute(HttpRoute *route);
  1517. #if DEPRECATE || 1
  1518. /**
  1519. Define a base controller function to invoke for all controller actions.
  1520. @description A base controller function can be defined that will be called before calling any controller action. This
  1521. emulates a super class constructor.
  1522. @param route HttpRoute object
  1523. @param baseProc Function to call just prior to invoking a controller action.
  1524. @ingroup EspRoute
  1525. @stability Deprecated
  1526. */
  1527. PUBLIC void espDefineBase(HttpRoute *route, EspLegacyProc baseProc) ME_DEPRECATED("Use espDefineCommon instead");
  1528. #endif
  1529. /**
  1530. Define a view
  1531. @description Views are ESP web pages that are executed to return presentation data back to the client.
  1532. @param route Http route object
  1533. @param path Path to the ESP view source code.
  1534. @param viewProc EspViewPrococ callback procedure to invoke when the view is requested.
  1535. @ingroup EspRoute
  1536. @stability Stable
  1537. */
  1538. PUBLIC void espDefineView(HttpRoute *route, cchar *path, void *viewProc);
  1539. /**
  1540. Expand a compile or link command template
  1541. @description This expands a command template and replaces "${tokens}" with their equivalent value. The supported
  1542. tokens are:
  1543. <ul>
  1544. <li>ARCH - Build architecture (i386, x86_64)</li>
  1545. <li>CC - Compiler pathname</li>
  1546. <li>DEBUG - Compiler debug options (-g, -Zi, -Od)</li>
  1547. <li>INC - Include directory (out/inc)</li>
  1548. <li>LIB - Library directory (out/lib, out/bin)</li>
  1549. <li>LIBS - Required libraries directory (esp, mpr)</li>
  1550. <li>OBJ - Name of compiled source (out/lib/view-MD5.o)</li>
  1551. <li>OUT - Output module (view_MD5.dylib)</li>
  1552. <li>SHLIB - Shared library extension (.lib, .so)</li>
  1553. <li>SHOBJ - Shared object extension (.dll, .so)</li>
  1554. <li>SRC - Path to source code for view or controller (already templated)</li>
  1555. <li>TMP - System temporary directory</li>
  1556. <li>WINSDK - Path to the Windows SDK</li>
  1557. <li>VS - Path to Visual Studio</li>
  1558. </ul>
  1559. @param route HttpRoute object
  1560. @param command Command to run
  1561. @param source ESP web page source pathname
  1562. @param module Output module pathname
  1563. @return An expanded command line
  1564. @ingroup EspRoute
  1565. @stability Evolving
  1566. @internal
  1567. */
  1568. PUBLIC char *espExpandCommand(HttpRoute *route, cchar *command, cchar *source, cchar *module);
  1569. /**
  1570. Get a configuration value from the ESP pak.json
  1571. @param route HttpRoute defining the ESP application
  1572. @param key Configuration property path. May contain dots.
  1573. @param defaultValue Default value to use if the configuration is not defined. May be null
  1574. @returns the Configuration string value
  1575. @ingroup EspRoute
  1576. @stability Stable
  1577. */
  1578. PUBLIC cchar *espGetConfig(HttpRoute *route, cchar *key, cchar *defaultValue);
  1579. #if DEPRECATED && REMOVE
  1580. /**
  1581. Test if the ESP application includes the specified pak
  1582. @description This tests the dependencies property specified pak.
  1583. @param route HttpRoute defining the ESP application
  1584. @param name Desired pak name. For example: "vue-mvc"
  1585. @returns True if the specified pak is supported
  1586. @ingroup EspRoute
  1587. @stability Deprecated
  1588. */
  1589. PUBLIC bool espHasPak(HttpRoute *route, cchar *name);
  1590. #endif
  1591. /**
  1592. Load the compiler rules from esp-compile.json
  1593. @param route HttpRoute object
  1594. @ingroup EspRoute
  1595. @stability Prototype
  1596. @internal
  1597. */
  1598. PUBLIC int espLoadCompilerRules(HttpRoute *route);
  1599. #if DEPRECATED && REMOVE
  1600. /**
  1601. Save the in-memory ESP pak.json configuration to the default location for the ESP application
  1602. defined by the specified route.
  1603. @param route HttpRoute defining the ESP application
  1604. @returns Zero if successful, otherwise a negative MPR error code.
  1605. @ingroup EspRoute
  1606. @stability Deprecated
  1607. */
  1608. PUBLIC int espSaveConfig(HttpRoute *route);
  1609. #endif
  1610. /**
  1611. Set a configuration value to the ESP pak.json.
  1612. @description This updates the in-memory copy of the pak.json only.
  1613. @param route HttpRoute defining the ESP application
  1614. @param key Configuration property path. May contain dots.
  1615. @param value Value to set the property to.
  1616. @returns Zero if successful, otherwise a negative MPR error code.
  1617. @ingroup EspRoute
  1618. @stability Evolving
  1619. */
  1620. PUBLIC int espSetConfig(HttpRoute *route, cchar *key, cchar *value);
  1621. /**
  1622. Set a private data reference for the current request
  1623. @param stream HttpStream object
  1624. @param data Data object to associate with the current request. This must be a managed reference.
  1625. @return Reference to private data
  1626. @ingroup Esp
  1627. @stability prototype
  1628. */
  1629. PUBLIC void espSetData(HttpStream *stream, void *data);
  1630. /**
  1631. Test if a configuration property from the ESP pak.json has a desired value.
  1632. @param route HttpRoute defining the ESP application
  1633. @param key Configuration property path. May contain dots.
  1634. @param desired Desired value to compare with.
  1635. @returns True if the configuration property has the desired value.
  1636. @ingroup EspRoute
  1637. @stability Evolving
  1638. */
  1639. PUBLIC bool espTestConfig(HttpRoute *route, cchar *key, cchar *desired);
  1640. /*
  1641. Internal
  1642. */
  1643. PUBLIC cchar *espGetVisualStudio(void);
  1644. PUBLIC void espManageEspRoute(EspRoute *eroute, int flags);
  1645. PUBLIC bool espModuleIsStale(HttpRoute *route, cchar *source, cchar *module, int *recompile);
  1646. PUBLIC int espOpenDatabase(HttpRoute *route, cchar *spec);
  1647. PUBLIC void espCloseDatabase(HttpRoute *route);
  1648. PUBLIC int espReloadDatabase(HttpRoute *route);
  1649. PUBLIC void espSetDefaultDirs(HttpRoute *route, bool app);
  1650. /********************************** Requests **********************************/
  1651. /**
  1652. View procedure callback.
  1653. @param stream Http stream object
  1654. @ingroup EspReq
  1655. @stability Stable
  1656. */
  1657. typedef void (*EspViewProc)(HttpStream *stream);
  1658. /**
  1659. ESP request structure
  1660. @defgroup EspReq EspReq
  1661. @stability Internal
  1662. @see Esp
  1663. */
  1664. typedef struct EspReq {
  1665. HttpRoute *route; /**< Route reference */
  1666. Esp *esp; /**< Convenient esp reference */
  1667. MprHash *feedback; /**< Feedback messages */
  1668. MprHash *lastFeedback; /**< Feedback messages from the last request */
  1669. HttpNotifier notifier; /**< Http state change notification callback */
  1670. void *data; /**< Custom data for request (managed) */
  1671. void *staticData; /**< Custom data for request (unmanaged) */
  1672. cchar *commandLine; /**< Command line for compile/link */
  1673. int autoFinalize; /**< Request is or will be auto-finalized */
  1674. int sessionProbed; /**< Already probed for session store */
  1675. int lastDomID; /**< Last generated DOM ID */
  1676. Edi *edi; /**< Database for this request */
  1677. } EspReq;
  1678. /**
  1679. Add a header to the transmission using a format string.
  1680. @description Add a header if it does not already exist.
  1681. @param stream HttpStream stream object
  1682. @param key Http response header key
  1683. @param fmt Printf style formatted string to use as the header key value
  1684. @param ... Arguments for fmt
  1685. @return Zero if successful, otherwise a negative MPR error code. Returns MPR_ERR_ALREADY_EXISTS if the header already
  1686. exists.
  1687. @ingroup EspReq
  1688. @stability Stable
  1689. */
  1690. PUBLIC void espAddHeader(HttpStream *stream, cchar *key, cchar *fmt, ...);
  1691. /**
  1692. Add a header to the transmission.
  1693. @description Add a header if it does not already exist.
  1694. @param stream HttpStream stream object
  1695. @param key Http response header key
  1696. @param value Value to set for the header
  1697. @return Zero if successful, otherwise a negative MPR error code. Returns MPR_ERR_ALREADY_EXISTS if the header already
  1698. exists.
  1699. @ingroup EspReq
  1700. @stability Stable
  1701. */
  1702. PUBLIC void espAddHeaderString(HttpStream *stream, cchar *key, cchar *value);
  1703. /**
  1704. Add a request parameter value if it is not already defined.
  1705. @param stream HttpStream stream object
  1706. @param var Name of the request parameter to set
  1707. @param value Value to set.
  1708. @ingroup EspReq
  1709. @stability Stable
  1710. */
  1711. PUBLIC void espAddParam(HttpStream *stream, cchar *var, cchar *value);
  1712. /**
  1713. Append a transmission header.
  1714. @description Set the header if it does not already exist. Append with a ", " separator if the header already exists.
  1715. @param stream HttpStream stream object
  1716. @param key Http response header key
  1717. @param fmt Printf style formatted string to use as the header key value
  1718. @param ... Arguments for fmt
  1719. @ingroup EspReq
  1720. @stability Stable
  1721. */
  1722. PUBLIC void espAppendHeader(HttpStream *stream, cchar *key, cchar *fmt, ...);
  1723. /**
  1724. Append a transmission header string.
  1725. @description Set the header if it does not already exist. Append with a ", " separator if the header already exists.
  1726. @param stream HttpStream stream object
  1727. @param key Http response header key
  1728. @param value Value to set for the header
  1729. @ingroup EspReq
  1730. @stability Stable
  1731. */
  1732. PUBLIC void espAppendHeaderString(HttpStream *stream, cchar *key, cchar *value);
  1733. /**
  1734. Auto-finalize transmission of the http request.
  1735. @description If auto-finalization is enabled via #espSetAutoFinalizing, this call will finalize writing Http response
  1736. data by writing the final chunk trailer if required. If using chunked transfers, a null chunk trailer is required
  1737. to signify the end of write data. If the request is already finalized, this call does nothing.
  1738. @param stream HttpStream stream object
  1739. @ingroup EspReq
  1740. @stability Stable
  1741. */
  1742. PUBLIC void espAutoFinalize(HttpStream *stream);
  1743. /**
  1744. Create a session state object.
  1745. @description The session state object can be used to share state between requests.
  1746. If a session has not already been created, this call will create a new session.
  1747. It will create a response cookie containing a session ID that will be sent to the client
  1748. with the response. Note: Objects are stored in the session state using JSON serialization.
  1749. @param stream HttpStream stream object
  1750. @return Session ID string
  1751. @ingroup EspReq
  1752. @stability Stable
  1753. */
  1754. PUBLIC cchar *espCreateSession(HttpStream *stream);
  1755. /**
  1756. Destroy a session state object.
  1757. @description This will destroy the server-side session state and
  1758. emit an expired cookie to the client to force it to erase the session cookie.
  1759. @param stream HttpStream stream object
  1760. @ingroup EspReq
  1761. @stability Stable
  1762. */
  1763. PUBLIC void espDestroySession(HttpStream *stream);
  1764. /**
  1765. Send mail using sendmail
  1766. @param stream HttpStream stream object
  1767. @param to Message recipient
  1768. @param from Message sender
  1769. @param subject Message subject
  1770. @param date Message creation date. Set to null to use the current date/time.
  1771. @param mime Message mime type. Set to null for text/plain.
  1772. @param message Message body
  1773. @param files MprList of files to send with the message.
  1774. @return Zero if the email is successfully sent.
  1775. @stability Evolving
  1776. */
  1777. PUBLIC int espEmail(HttpStream *stream, cchar *to, cchar *from, cchar *subject, MprTime date, cchar *mime,
  1778. cchar *message, MprList *files);
  1779. /**
  1780. Indicate the request is finalized.
  1781. @description Calling this routine indicates that the handler has fully finished processing the request including
  1782. processing all input, generating a full response and any other required processing. This call will invoke
  1783. #httpFinalizeOutput and then set the request finalized flag. If the request is already finalized, this call
  1784. does nothing. A handler MUST call httpFinalize when it has completed processing a request.
  1785. As background: there are three finalize concepts: HttpTx.finalizedOutput means the handler has generated all
  1786. the response output but it may not yet be fully transmited through the pipeline and to the network by the
  1787. connector. HttpTx.finalizedConnector means the connector has sent all the output to the network. HttpTx.finalized
  1788. means the application has fully processed the request including reading all the input data it wishes to read
  1789. and has generated all the output that will be generated. A fully finalized request has both HttpTx.finalized
  1790. and HttpTx.finalizedConnector true.
  1791. @param stream HttpStream stream object
  1792. @ingroup EspReq
  1793. @stability Stable
  1794. */
  1795. PUBLIC void espFinalize(HttpStream *stream);
  1796. /**
  1797. Flush transmit data.
  1798. @description This writes any buffered data and initiates writing to the peer. This will not block.
  1799. @param stream HttpStream stream object
  1800. @ingroup EspReq
  1801. @stability Stable
  1802. */
  1803. PUBLIC void espFlush(HttpStream *stream);
  1804. /**
  1805. Get the current route HttpAuth object.
  1806. @param stream HttpStream stream object
  1807. @return The HttpAuth object
  1808. @ingroup EspReq
  1809. @stability Stable
  1810. */
  1811. PUBLIC HttpAuth *espGetAuth(HttpStream *stream);
  1812. /**
  1813. Get the current request stream.
  1814. @return The HttpStream stream object
  1815. @ingroup EspReq
  1816. @stability Stable
  1817. */
  1818. PUBLIC HttpStream *espGetStream(void);
  1819. /**
  1820. Get the receive body content length.
  1821. @description Get the length of the receive body content (if any). This is used in servers to get the length of posted
  1822. data and, in clients, to get the response body length.
  1823. @param stream HttpStream stream object
  1824. @return A count of the response content data in bytes.
  1825. @ingroup EspReq
  1826. @stability Stable
  1827. */
  1828. PUBLIC MprOff espGetContentLength(HttpStream *stream);
  1829. /**
  1830. Get the receive body content type.
  1831. @description Get the content mime type of the receive body content (if any).
  1832. @param stream HttpStream stream object
  1833. @return Mime type of any receive content. Set to NULL if not posted data.
  1834. @ingroup EspReq
  1835. @stability Stable
  1836. */
  1837. PUBLIC cchar *espGetContentType(HttpStream *stream);
  1838. /**
  1839. Get a request cookie.
  1840. @description Get the cookie for the given name.
  1841. @param stream HttpStream stream object
  1842. @param name Cookie name to retrieve
  1843. @return Return the cookie value
  1844. Return null if the cookie is not defined.
  1845. @ingroup EspReq
  1846. @stability Stable
  1847. */
  1848. PUBLIC cchar *espGetCookie(HttpStream *stream, cchar *name);
  1849. /**
  1850. Get the request cookies.
  1851. @description Get the cookies defined in the current request. This returns the HTTP cookies header with all
  1852. cookies in one string.
  1853. @param stream HttpStream stream object
  1854. @return Return a string containing the cookies sent in the Http header of the last request
  1855. @ingroup EspReq
  1856. @stability Stable
  1857. */
  1858. PUBLIC cchar *espGetCookies(HttpStream *stream);
  1859. /**
  1860. Get the private data reference for the current request set via #setData
  1861. @param stream HttpStream object
  1862. @return Reference to private data
  1863. @ingroup EspReq
  1864. @stability prototype
  1865. */
  1866. PUBLIC void *espGetData(HttpStream *stream);
  1867. /**
  1868. Get the current database instance.
  1869. @description A route may have a default database configured via the EspDb Appweb.conf configuration directive.
  1870. The database will be opened when the web server initializes and will be shared between all requests using the route.
  1871. @return Edi EDI database handle
  1872. @ingroup EspReq
  1873. @stability Stable
  1874. */
  1875. PUBLIC Edi *espGetDatabase(HttpStream *stream);
  1876. /**
  1877. Get the current extended route information.
  1878. @return EspRoute instance
  1879. @ingroup EspReq
  1880. @stability Evolving
  1881. */
  1882. PUBLIC EspRoute *espGetEspRoute(HttpStream *stream);
  1883. /**
  1884. Get the default documents directory for the request route.
  1885. @param stream HttpStream stream object
  1886. @return A directory path name
  1887. @ingroup EspReq
  1888. @stability Stable
  1889. */
  1890. PUBLIC cchar *espGetDocuments(HttpStream *stream);
  1891. /**
  1892. Get a feedback message defined via #feedback
  1893. @param stream HttpStream object
  1894. @param type type of feedback message to retrieve. This may be set to any word, but the following feedback types
  1895. are typically supported as per RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  1896. @return Reference to the feedback message
  1897. @ingroup EspReq
  1898. @stability Evolving
  1899. */
  1900. PUBLIC cchar *espGetFeedback(HttpStream *stream, cchar *type);
  1901. /**
  1902. Get the current database grid reference.
  1903. @description The current grid is defined via #setGrid
  1904. @return EdiGrid instance
  1905. @ingroup EspReq
  1906. @stability Deprecated
  1907. @internal
  1908. */
  1909. PUBLIC EdiGrid *espGetGrid(HttpStream *stream);
  1910. /**
  1911. Get an rx http header.
  1912. @description Get a http response header for a given header key.
  1913. @param stream HttpStream stream object
  1914. @param key Name of the header to retrieve. This should be a lower case header name. For example: "Connection"
  1915. @return Value associated with the header key or null if the key did not exist in the response.
  1916. @ingroup EspReq
  1917. @stability Stable
  1918. */
  1919. PUBLIC cchar *espGetHeader(HttpStream *stream, cchar *key);
  1920. /**
  1921. Get the hash table of rx Http headers.
  1922. @description Get the internal hash table of rx headers
  1923. @param stream HttpStream stream object
  1924. @return Hash table. See MprHash for how to access the hash table.
  1925. @ingroup EspReq
  1926. @stability Stable
  1927. */
  1928. PUBLIC MprHash *espGetHeaderHash(HttpStream *stream);
  1929. /**
  1930. Get all the request http headers.
  1931. @description Get all the rx headers. The returned string formats all the headers in the form:
  1932. key: value\\nkey2: value2\\n...
  1933. @param stream HttpStream stream object
  1934. @return String containing all the headers. The caller must free this returned string.
  1935. @ingroup EspReq
  1936. @stability Stable
  1937. */
  1938. PUBLIC char *espGetHeaders(HttpStream *stream);
  1939. /**
  1940. Get the HTTP method.
  1941. @description This is a convenience API to return the Http method
  1942. @return The HttpStream.rx.method property
  1943. @ingroup EspReq
  1944. @stability Stable
  1945. */
  1946. PUBLIC cchar *espGetMethod(HttpStream *stream);
  1947. /**
  1948. Get a request parameter.
  1949. @description Get the value of a named request parameter. Request parameters are defined via www-urlencoded
  1950. query, post data contained in the request and route parameters. Route parameters are stored as JSON tree objects
  1951. and may contain nested properties.
  1952. @param stream HttpStream stream object
  1953. @param var Name of the request parameter to retrieve
  1954. @param defaultValue Default value to return if the variable is not defined. Can be null.
  1955. @return String containing the request parameter's value. Caller should not free.
  1956. @ingroup EspReq
  1957. @stability Stable
  1958. */
  1959. PUBLIC cchar *espGetParam(HttpStream *stream, cchar *var, cchar *defaultValue);
  1960. /**
  1961. Get a request pararmeter as an integer.
  1962. @description Get the value of a named request parameter. Request parameters are defined via www-urlencoded
  1963. query, post data contained in the request and route parameters. Request parameters are stored as JSON tree objects
  1964. and may contain nested properties.
  1965. @param stream HttpStream stream object
  1966. @param var Name of the request parameter to retrieve
  1967. @param defaultValue Default value to return if the variable is not defined. Can be null.
  1968. @return Integer containing the request parameter's value
  1969. @ingroup EspReq
  1970. @stability Evolving
  1971. */
  1972. PUBLIC int espGetIntParam(HttpStream *stream, cchar *var, int defaultValue);
  1973. /**
  1974. Get a request pararmeter as a JSON object.
  1975. @description Get the value of a named request parameter. Request parameters are defined via www-urlencoded
  1976. query, post data contained in the request and route parameters. Request parameters are stored as JSON tree objects
  1977. and may contain nested properties.
  1978. @param stream HttpStream stream object
  1979. @param var Name of the request parameter to retrieve
  1980. @return JSON parameter object.
  1981. @ingroup EspReq
  1982. @stability Evolving
  1983. */
  1984. PUBLIC MprJson *espGetParamObj(HttpStream *stream, cchar *var);
  1985. /**
  1986. Get the request parameters.
  1987. @description This call gets the request parameters for the current request.
  1988. @description Request parameters are defined via www-urlencoded query, post data contained in the request and route parameters.
  1989. Request parameters are stored as JSON tree objects and may contain nested properties.
  1990. @param stream HttpStream stream object
  1991. @return MprJson instance containing the request parameters
  1992. @ingroup EspReq
  1993. @stability Stable
  1994. */
  1995. PUBLIC MprJson *espGetParams(HttpStream *stream);
  1996. /**
  1997. Get the request URI path string.
  1998. @description This is a convenience API to return the request URI path. This is the request URI path after removing
  1999. query parameters. It does not include the application route prefix.
  2000. @return The espGetStream()->rx->pathInfo
  2001. @ingroup EspReq
  2002. @stability Evolving
  2003. */
  2004. PUBLIC cchar *espGetPath(HttpStream *stream);
  2005. /**
  2006. Get the request URI query string.
  2007. @description Get URI query string sent with the current request.
  2008. @param stream HttpStream stream object
  2009. @return String containing the request query string. Caller should not free.
  2010. @ingroup EspReq
  2011. @stability Stable
  2012. */
  2013. PUBLIC cchar *espGetQueryString(HttpStream *stream);
  2014. /**
  2015. Get the referring URI.
  2016. @description This returns the referring URI as described in the HTTP "referer" (yes the HTTP specification does
  2017. spell it incorrectly) header. If this header is not defined, this routine will return the home URI as returned
  2018. by uri("~").
  2019. @param stream HttpStream stream object
  2020. @return String URI back to the referring URI. If no referrer is defined, refers to the home URI.
  2021. @ingroup EspReq
  2022. @stability Stable
  2023. */
  2024. PUBLIC cchar *espGetReferrer(HttpStream *stream);
  2025. /**
  2026. Get the current route HttpRoute object.
  2027. @param stream HttpStream stream object
  2028. @return The HttpRoute object
  2029. @ingroup EspReq
  2030. @stability Stable
  2031. */
  2032. PUBLIC HttpRoute *espGetRoute(HttpStream *stream);
  2033. /**
  2034. Get the default database defined on a route.
  2035. @param route HttpRoute object
  2036. @return Database instance object
  2037. @ingroup EspReq
  2038. @stability Stable
  2039. */
  2040. PUBLIC Edi *espGetRouteDatabase(HttpRoute *route);
  2041. /**
  2042. Get a route variable
  2043. @description Get the value of a request route variable.
  2044. @param stream HttpStream stream object
  2045. @param var Name of the request parameter to retrieve
  2046. @return String containing the route variable value. Caller should not free.
  2047. @ingroup EspReq
  2048. @stability Evolving
  2049. */
  2050. PUBLIC cchar *espGetRouteVar(HttpStream *stream, cchar *var);
  2051. /**
  2052. Get the session state ID.
  2053. @description This will get the session and return the session ID. This will create a new session state storage area if
  2054. create is true and one does not already exist. This can be used to test if the session state exists for this
  2055. stream.
  2056. @param stream HttpStream stream object
  2057. @param create Set to true to create a new session if one does not already exist.
  2058. @return The session state identifier string.
  2059. @ingroup EspReq
  2060. @stability Evolving
  2061. */
  2062. PUBLIC cchar *espGetSessionID(HttpStream *stream, int create);
  2063. /**
  2064. Get the response status.
  2065. @param stream HttpStream stream object
  2066. @return An integer Http response code. Typically 200 is success.
  2067. @ingroup EspReq
  2068. @stability Stable
  2069. */
  2070. PUBLIC int espGetStatus(HttpStream *stream);
  2071. /**
  2072. Get the Http response status message.
  2073. @description The HTTP status message is supplied on the first line of the HTTP response.
  2074. @param stream HttpStream stream object
  2075. @returns A Http status message.
  2076. @ingroup EspReq
  2077. @stability Stable
  2078. */
  2079. PUBLIC cchar *espGetStatusMessage(HttpStream *stream);
  2080. /**
  2081. Get the uploaded files.
  2082. @description Get the list of uploaded files.
  2083. This list entries are HttpUploadFile objects.
  2084. @param stream HttpStream stream object
  2085. @return A list of HttpUploadFile objects.
  2086. @ingroup EspReq
  2087. @stability Stable
  2088. */
  2089. PUBLIC MprList *espGetUploads(HttpStream *stream);
  2090. /**
  2091. Get the request URI string.
  2092. @description This is a convenience API to return the request URI. This is the request URI after removing
  2093. query parameters. It includes any application route prefix.
  2094. @return The espGetStream()->rx->uri
  2095. @ingroup EspReq
  2096. @stability Stable
  2097. */
  2098. PUBLIC cchar *espGetUri(HttpStream *stream);
  2099. /**
  2100. Test if a current grid has been defined via #espSetGrid.
  2101. @return "True" if a current grid has been defined
  2102. @ingroup EspReq
  2103. @stability Deprecated
  2104. @internal
  2105. */
  2106. PUBLIC bool espHasGrid(HttpStream *stream);
  2107. /**
  2108. Test if a current record has been defined and save to the database.
  2109. @description This call returns "true" if a current record is defined and has been saved to the database with a
  2110. valid "id" field.
  2111. @return "True" if a current record with a valid "id" is defined.
  2112. @ingroup EspReq
  2113. @stability Deprecated
  2114. @internal
  2115. */
  2116. PUBLIC bool espHasRec(HttpStream *stream);
  2117. /**
  2118. Test if the request is being made on behalf of the current, single authenticated user.
  2119. @description Set esp.login.single to true to enable current session tracking.
  2120. @return true if the
  2121. @stability Evolving
  2122. @ingroup EspReq
  2123. */
  2124. PUBLIC bool espIsCurrentSession(HttpStream *stream);
  2125. /**
  2126. Test if the user is authenticated
  2127. @param stream HttpStream stream object
  2128. @return True if the username and password have been authenticated.
  2129. @ingroup EspReq
  2130. @stability Prototype
  2131. */
  2132. PUBLIC bool espIsAuthenticated(HttpStream *stream);
  2133. /**
  2134. Test if the receive input stream is at end-of-file.
  2135. @param stream HttpStream stream object
  2136. @return "True" if there is no more receive data to read
  2137. @ingroup EspReq
  2138. @stability Stable
  2139. */
  2140. PUBLIC bool espIsEof(HttpStream *stream);
  2141. /**
  2142. Test if the stream is using SSL and is secure.
  2143. @param stream HttpStream stream object
  2144. @return "True" if the stream is using SSL.
  2145. @ingroup EspReq
  2146. @stability Stable
  2147. */
  2148. PUBLIC bool espIsSecure(HttpStream *stream);
  2149. /**
  2150. Test if the request has been finalized.
  2151. @description This tests if #espFinalize or #httpFinalize has been called for a request.
  2152. @param stream HttpStream stream object
  2153. @return "True" if the request has been finalized.
  2154. @ingroup EspReq
  2155. @stability Stable
  2156. */
  2157. PUBLIC bool espIsFinalized(HttpStream *stream);
  2158. /**
  2159. Match a request parameter with an expected value.
  2160. @description Compare a request parameter and return "true" if it exists and its value matches.
  2161. @param stream HttpStream stream object
  2162. @param var Name of the request parameter
  2163. @param value Expected value to match
  2164. @return "True" if the value matches
  2165. @ingroup EspReq
  2166. @stability Stable
  2167. */
  2168. PUBLIC bool espMatchParam(HttpStream *stream, cchar *var, cchar *value);
  2169. /**
  2170. Read receive body content.
  2171. Use httpReadBlock for more options to read data.
  2172. @description Read body content from the client. This call does not block.
  2173. @param stream HttpStream stream object
  2174. @param buf Buffer to accept content data
  2175. @param size Size of the buffer
  2176. @return A count of bytes read into the buffer
  2177. @ingroup EspReq
  2178. @stability Stable
  2179. */
  2180. PUBLIC ssize espReceive(HttpStream *stream, char *buf, ssize size);
  2181. /**
  2182. Redirect the client.
  2183. @description Redirect the client to a new uri.
  2184. @param stream HttpStream stream object
  2185. @param status Http status code to send with the response
  2186. @param target New target uri for the client
  2187. @ingroup EspReq
  2188. @stability Stable
  2189. */
  2190. PUBLIC void espRedirect(HttpStream *stream, int status, cchar *target);
  2191. /**
  2192. Redirect the client back to the referrer
  2193. @description Redirect the client to the referring URI.
  2194. @param stream HttpStream stream object
  2195. @ingroup EspReq
  2196. @stability Stable
  2197. */
  2198. PUBLIC void espRedirectBack(HttpStream *stream);
  2199. /**
  2200. Remove a cookie
  2201. @param stream HttpStream stream object
  2202. @param name Cookie name
  2203. @ingroup EspReq
  2204. @stability Stable
  2205. */
  2206. PUBLIC void espRemoveCookie(HttpStream *stream, cchar *name);
  2207. /**
  2208. Remove a header from the transmission
  2209. @description Remove a header if present.
  2210. @param stream HttpStream stream object
  2211. @param key Http response header key
  2212. @return Zero if successful, otherwise a negative MPR error code.
  2213. @ingroup EspReq
  2214. @stability Stable
  2215. */
  2216. PUBLIC int espRemoveHeader(HttpStream *stream, cchar *key);
  2217. /**
  2218. Remove a session state variable
  2219. @param stream HttpStream stream object
  2220. @param name Variable name to set
  2221. @ingroup EspReq
  2222. @stability Stable
  2223. */
  2224. PUBLIC void espRemoveSessionVar(HttpStream *stream, cchar *name);
  2225. /**
  2226. Render a formatted string.
  2227. @description Render a formatted string of data into packets to the client. Data packets will be created
  2228. as required to store the write data. This call may block waiting for data to drain to the client and
  2229. may yield to the garbage collector.
  2230. @param stream HttpStream stream object
  2231. @param fmt Printf style formatted string
  2232. @param ... Arguments for fmt
  2233. @return A count of the bytes actually written
  2234. @ingroup EspReq
  2235. @stability Stable
  2236. */
  2237. PUBLIC ssize espRender(HttpStream *stream, cchar *fmt, ...);
  2238. /**
  2239. Render the client configuration string in JSON
  2240. @param stream HttpStream stream object
  2241. @return A count of the bytes actually written
  2242. @ingroup EspReq
  2243. @stability PRototype
  2244. */
  2245. PUBLIC ssize espRenderConfig(HttpStream *stream);
  2246. /**
  2247. Render a block of data to the client.
  2248. @description Render a block of data to the client. Data packets will be created as required to store the write data.
  2249. This call may block waiting for the client to absorb the data.
  2250. @param stream HttpStream stream object
  2251. @param buf Buffer containing the write data
  2252. @param size Size of the data in buf
  2253. @return A count of the bytes actually written
  2254. @ingroup EspReq
  2255. @stability Stable
  2256. */
  2257. PUBLIC ssize espRenderBlock(HttpStream *stream, cchar *buf, ssize size);
  2258. /**
  2259. Render cached content.
  2260. @description Render the saved, cached response from a prior request to this URI. This is useful if the caching
  2261. mode has been set to "manual".
  2262. @param stream HttpStream stream object
  2263. @return A count of the bytes actually written
  2264. @ingroup EspReq
  2265. @stability Stable
  2266. */
  2267. PUBLIC ssize espRenderCached(HttpStream *stream);
  2268. /**
  2269. Render an ESP document
  2270. @description If the document is an ESP page, it will be rendered as a view via #espRenderDocument.
  2271. Otherwise, it will be rendered using the fileHandler as a static document. This routine may yield.
  2272. @param stream Http stream object
  2273. @param path Relative pathname from route->documents to the document to render.
  2274. @ingroup EspReq
  2275. @stability Stable
  2276. */
  2277. PUBLIC void espRenderDocument(HttpStream *stream, cchar *path);
  2278. /**
  2279. Render an error message back to the client and finalize the request. The output is Html escaped for security.
  2280. @param stream HttpStream stream object
  2281. @param status Http status code
  2282. @param fmt Printf style message format
  2283. @return A count of the bytes actually written
  2284. @ingroup EspReq
  2285. @stability Stable
  2286. */
  2287. PUBLIC ssize espRenderError(HttpStream *stream, int status, cchar *fmt, ...);
  2288. /**
  2289. Render feedback messages.
  2290. @description Feedback messages for one-time messages that are sent to the client. For HTML clients, feedback
  2291. messages use the session state store and persist for only one request. For smart/thick clients, feedback messages
  2292. are sent as JSON responses via the espSendFeedback API. See #espSetFeedback for how to define feedback messages.
  2293. @param stream Http stream object
  2294. @param types Types of feedback message to retrieve. Set to "*" to retrieve all types of feedback.
  2295. This may be set to any word, but the following feedback types are typically supported as per
  2296. RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  2297. @return Number of bytes written
  2298. @ingroup EspControl
  2299. @stability Deprecated
  2300. @internal
  2301. */
  2302. PUBLIC ssize espRenderFeedback(HttpStream *stream, cchar *types);
  2303. /**
  2304. Render the contents of a file back to the client.
  2305. @param stream HttpStream stream object
  2306. @param path File path name
  2307. @return A count of the bytes actually written
  2308. @ingroup EspReq
  2309. @stability Stable
  2310. */
  2311. PUBLIC ssize espRenderFile(HttpStream *stream, cchar *path);
  2312. /**
  2313. Read a table from the current database
  2314. @param stream HttpStream stream object
  2315. @param tableName Database table name
  2316. @return An EDI grid containing data for the table.
  2317. @ingroup EspReq
  2318. @stability Evolving
  2319. */
  2320. PUBLIC EdiGrid *espReadTable(HttpStream *stream, cchar *tableName);
  2321. /**
  2322. Render a formatted string after HTML escaping
  2323. @description Render a formatted string of data and then HTML escape. Data packets will be created
  2324. as required to store the write data. This call may block waiting for data to drain to the client.
  2325. @param stream HttpStream stream object
  2326. @param fmt Printf style formatted string
  2327. @param ... Arguments for fmt
  2328. @return A count of the bytes actually written
  2329. @ingroup EspReq
  2330. @stability Stable
  2331. */
  2332. PUBLIC ssize espRenderSafe(HttpStream *stream, cchar *fmt, ...);
  2333. /**
  2334. Render a safe string of data to the client.
  2335. @description HTML escape a string and then write the string of data to the client.
  2336. Data packets will be created as required to store the write data. This call may block waiting for the data to
  2337. the client to drain.
  2338. @param stream HttpStream stream object
  2339. @param s String containing the data to write
  2340. @return A count of the bytes actually written
  2341. @ingroup EspReq
  2342. @stability Stable
  2343. */
  2344. PUBLIC ssize espRenderSafeString(HttpStream *stream, cchar *s);
  2345. /**
  2346. Render a string of data to the client
  2347. @description Render a string of data to the client. Data packets will be created
  2348. as required to store the write data. This call may block waiting for data to drain to the client.
  2349. @param stream HttpStream stream object
  2350. @param s String containing the data to write
  2351. @return A count of the bytes actually written
  2352. @ingroup EspReq
  2353. @stability Stable
  2354. */
  2355. PUBLIC ssize espRenderString(HttpStream *stream, cchar *s);
  2356. /**
  2357. Render the value of a request variable to the client.
  2358. If a request parameter is not found by the given name, consult the session store for a variable the same name.
  2359. @description This writes the value of a request variable after HTML escaping its value.
  2360. @param stream HttpStream stream object
  2361. @param name Request parameter variable name
  2362. @return A count of the bytes actually written
  2363. @ingroup EspReq
  2364. @stability Stable
  2365. */
  2366. PUBLIC ssize espRenderVar(HttpStream *stream, cchar *name);
  2367. /**
  2368. Render an ESP view page to the client
  2369. @param stream Http stream object
  2370. @param view View name. The view name is interpreted relative to the matching route documents directory and may omit
  2371. an ESP extension. This routine may yield.
  2372. @param flags Reserved. Set to zero.
  2373. @return true if a vew can be rendered.
  2374. @ingroup EspReq
  2375. @stability Evolving
  2376. */
  2377. PUBLIC bool espRenderView(HttpStream *stream, cchar *view, int flags);
  2378. /**
  2379. Send a database grid as a JSON string
  2380. @description The JSON string is rendered as part of an enclosing "{ data: JSON }" wrapper.
  2381. @param stream HttpStream stream object
  2382. @param grid EDI grid
  2383. @param flags Reserved. Set to zero.
  2384. @return Number of bytes rendered
  2385. @ingroup EspReq
  2386. @stability Evolving
  2387. */
  2388. PUBLIC ssize espSendGrid(HttpStream *stream, EdiGrid *grid, int flags);
  2389. /**
  2390. Send a database record as a JSON string
  2391. @description The JSON string is rendered as part of an enclosing "{ data: JSON }" wrapper.
  2392. @param stream HttpStream stream object
  2393. @param rec EDI record
  2394. @param flags Reserved. Set to zero.
  2395. @return Number of bytes rendered
  2396. @ingroup EspReq
  2397. @stability Evolving
  2398. */
  2399. PUBLIC ssize espSendRec(HttpStream *stream, EdiRec *rec, int flags);
  2400. /**
  2401. Send a JSON response result
  2402. @description This renders a JSON response including the request success status, feedback message and field errors.
  2403. The field errors apply to the current EDI record.
  2404. The format of the response is:
  2405. "{ error: 0/1, feedback: {messages}, fieldErrors: {messages}}" wrapper.
  2406. The feedback messages are created via the espSetFeedback API. Field errors are created by ESP validations.
  2407. @param stream HttpStream stream object
  2408. @param success True if the operation was a success.
  2409. @return Number of bytes sent.
  2410. @ingroup EspReq
  2411. @stability Evolving
  2412. */
  2413. PUBLIC ssize espSendResult(HttpStream *stream, bool success);
  2414. /**
  2415. Enable auto-finalizing for this request
  2416. @param stream HttpStream stream object
  2417. @param on Set to "true" to enable auto-finalizing.
  2418. @return "True" if auto-finalizing was enabled prior to this call
  2419. @ingroup EspReq
  2420. @stability Stable
  2421. */
  2422. PUBLIC bool espSetAutoFinalizing(HttpStream *stream, bool on);
  2423. /**
  2424. Set the current request stream.
  2425. @param stream The HttpStream stream object to define
  2426. @ingroup EspReq
  2427. @stability Stable
  2428. */
  2429. PUBLIC void espSetStream(HttpStream *stream);
  2430. /**
  2431. Define a content length header in the transmission.
  2432. @description This will define a "Content-Length: NNN" request header.
  2433. @param stream HttpStream stream object
  2434. @param length Numeric value for the content length header.
  2435. @ingroup EspReq
  2436. @stability Stable
  2437. */
  2438. PUBLIC void espSetContentLength(HttpStream *stream, MprOff length);
  2439. /**
  2440. Set a cookie in the transmission
  2441. @description Define a cookie to send in the transmission Http header
  2442. @param stream HttpStream stream object
  2443. @param name Cookie name
  2444. @param value Cookie value
  2445. @param path URI path to which the cookie applies
  2446. @param domain String Domain in which the cookie applies. Must have 2-3 "." and begin with a leading ".".
  2447. For example: domain: .example.com. Set to NULL to use the current connection's client domain.
  2448. Some browsers will accept cookies without the initial ".", but the spec: (RFC 2109) requires it.
  2449. @param lifespan Duration for the cookie to persist in msec. Set to a negative number to delete a cookie. Set to
  2450. zero for a "session" cookie that lives only for the user's session.
  2451. @param isSecure Set to "true" if the cookie only applies for SSL based connections.
  2452. @ingroup EspReq
  2453. @stability Stable
  2454. */
  2455. PUBLIC void espSetCookie(HttpStream *stream, cchar *name, cchar *value, cchar *path, cchar *domain, MprTicks lifespan,
  2456. bool isSecure);
  2457. /**
  2458. Set the transmission (response) content mime type
  2459. @description Set the mime type Http header in the transmission
  2460. @param stream HttpStream stream object
  2461. @param mimeType Mime type string
  2462. @ingroup EspReq
  2463. @stability Stable
  2464. */
  2465. PUBLIC void espSetContentType(HttpStream *stream, cchar *mimeType);
  2466. /**
  2467. Set this authenticated session as the current session.
  2468. @description Set esp.login.single to true to enable current session tracking.
  2469. @return true if the
  2470. @stability Evolving
  2471. @ingroup EspReq
  2472. */
  2473. PUBLIC void espSetCurrentSession(HttpStream *stream);
  2474. /**
  2475. Clear the current authenticated session
  2476. @stability Evolving
  2477. @ingroup EspReq
  2478. */
  2479. PUBLIC void espClearCurrentSession(HttpStream *stream);
  2480. /**
  2481. Set a feedback message
  2482. @description Feedback messages are a convenient way to aggregate messages state information in the response.
  2483. Feedback messages are removed at the completion of the request.
  2484. @param stream Http stream object
  2485. @param type type of feedback message. This may be set to any word, but the following feedback types
  2486. are typically supported as per RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  2487. @param fmt Printf style formatted string to use as the message
  2488. @ingroup EspReq
  2489. @stability Stable
  2490. */
  2491. PUBLIC void espSetFeedback(HttpStream *stream, cchar *type, cchar *fmt, ...);
  2492. /**
  2493. Send a feedback message
  2494. @param stream Http stream object
  2495. @param type type of feedback message. This may be set to any word, but the following feedback types
  2496. are typically supported as per RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  2497. @param fmt Printf style formatted string to use as the message
  2498. @param args Varargs style list
  2499. @ingroup EspReq
  2500. @stability Internal
  2501. @internal
  2502. */
  2503. PUBLIC void espSetFeedbackv(HttpStream *stream, cchar *type, cchar *fmt, va_list args);
  2504. /**
  2505. Set the current database grid
  2506. @return The grid instance. This permits chaining.
  2507. @ingroup EspReq
  2508. @stability Stable
  2509. */
  2510. PUBLIC EdiGrid *espSetGrid(HttpStream *stream, EdiGrid *grid);
  2511. /**
  2512. Set a transmission header
  2513. @description Set a Http header to send with the request. If the header already exists, its value is overwritten.
  2514. @param stream HttpStream stream object
  2515. @param key Http response header key
  2516. @param fmt Printf style formatted string to use as the header key value
  2517. @param ... Arguments for fmt
  2518. @ingroup EspReq
  2519. @stability Stable
  2520. */
  2521. PUBLIC void espSetHeader(HttpStream *stream, cchar *key, cchar *fmt, ...);
  2522. /**
  2523. Set a simple key/value transmission header
  2524. @description Set a Http header to send with the request. If the header already exists, its value is overwritten.
  2525. @param stream HttpStream stream object
  2526. @param key Http response header key
  2527. @param value String value for the key
  2528. @ingroup EspReq
  2529. @stability Stable
  2530. */
  2531. PUBLIC void espSetHeaderString(HttpStream *stream, cchar *key, cchar *value);
  2532. /**
  2533. Set an integer request parameter value
  2534. @description Set the value of a named request parameter to an integer value. Request parameters are defined via
  2535. www-urlencoded query or post data contained in the request.
  2536. @param stream HttpStream stream object
  2537. @param var Name of the request parameter to set
  2538. @param value Value to set.
  2539. @ingroup EspReq
  2540. @stability Stable
  2541. */
  2542. PUBLIC void espSetParamInt(HttpStream *stream, cchar *var, int value);
  2543. #define espSetIntParam espSetParamInt
  2544. /**
  2545. Define a notifier callback for this stream.
  2546. @description The notifier callback will be invoked for state changes and I/O events as requests are processed.
  2547. The supported events are:
  2548. <ul>
  2549. <li>HTTP_EVENT_STATE &mdash; The request is changing state. Valid states are:
  2550. HTTP_STATE_BEGIN, HTTP_STATE_CONNECTED, HTTP_STATE_FIRST, HTTP_STATE_CONTENT, HTTP_STATE_READY,
  2551. HTTP_STATE_RUNNING, HTTP_STATE_FINALIZED and HTTP_STATE_COMPLETE. A request will always visit all states and the
  2552. notifier will be invoked for each and every state. This is true even if the request has no content, the
  2553. HTTP_STATE_CONTENT will still be visited.</li>
  2554. <li>HTTP_EVENT_READABLE &mdash; There is data available to read</li>
  2555. <li>HTTP_EVENT_WRITABLE &mdash; The outgoing pipeline can absorb more data</li>
  2556. <li>HTTP_EVENT_ERROR &mdash; The request has encountered an error</li>
  2557. <li>HTTP_EVENT_DESTROY &mdash; The stream structure is about to be destoyed</li>
  2558. <li>HTTP_EVENT_OPEN &mdash; The application layer is now open</li>
  2559. <li>HTTP_EVENT_CLOSE &mdash; The application layer is now closed</li>
  2560. </ul>
  2561. Before the notifier is invoked, espSetStream is called to set the stream object in the thread local storage.
  2562. This enables the ESP Abbreviated API.
  2563. @param stream HttpStream stream object created via #httpCreateStream
  2564. @param notifier Notifier function.
  2565. @ingroup EspReq
  2566. @stability Stable
  2567. */
  2568. PUBLIC void espSetNotifier(HttpStream *stream, HttpNotifier notifier);
  2569. /**
  2570. Set the current database record
  2571. @description The current record is used to supply data to various abbreviated controls, such as: text(), input(),
  2572. checkbox and dropdown()
  2573. @param stream HttpStream stream object
  2574. @param rec Record object to define as the current record.
  2575. @return The grid instance. This permits chaining.
  2576. @ingroup EspReq
  2577. @stability Stable
  2578. */
  2579. PUBLIC EdiRec *espSetRec(HttpStream *stream, EdiRec *rec);
  2580. /**
  2581. Set a request parameter value
  2582. @description Set the value of a named request parameter to a string value. Parameters are defined via
  2583. requeset POST data or request URI queries. This API permits these initial request parameters to be set or
  2584. modified.
  2585. @param stream HttpStream stream object
  2586. @param var Name of the request parameter to set
  2587. @param value Value to set.
  2588. @ingroup EspReq
  2589. @stability Stable
  2590. */
  2591. PUBLIC void espSetParam(HttpStream *stream, cchar *var, cchar *value);
  2592. /**
  2593. Set a Http response status.
  2594. @description Set the Http response status for the request. This defaults to 200 (OK).
  2595. @param stream HttpStream stream object
  2596. @param status Http status code.
  2597. @ingroup EspReq
  2598. @stability Stable
  2599. */
  2600. PUBLIC void espSetStatus(HttpStream *stream, int status);
  2601. /**
  2602. Set a session variable.
  2603. @description
  2604. @param stream Http stream object
  2605. @param name Variable name to set
  2606. @param value Variable value to use
  2607. @return Zero if successful. Otherwise a negative MPR error code.
  2608. @ingroup HttpSession
  2609. @stability Stable
  2610. */
  2611. PUBLIC int espSetSessionVar(HttpStream *stream, cchar *name, cchar *value);
  2612. /**
  2613. Show request details
  2614. @description This e request details back to the client. This is useful as a debugging tool.
  2615. @param stream HttpStream stream object
  2616. @ingroup EspReq
  2617. @stability Stable
  2618. */
  2619. PUBLIC void espShowRequest(HttpStream *stream);
  2620. /**
  2621. Update the cached content for a request
  2622. @description Save the given content for future requests. This is useful if the caching mode has been set to "manual".
  2623. @param stream HttpStream stream object
  2624. @param uri Request URI to cache for
  2625. @param data Data to cache
  2626. @param lifesecs Time in seconds to cache the data
  2627. @ingroup EspReq
  2628. @stability Stable
  2629. */
  2630. PUBLIC void espUpdateCache(HttpStream *stream, cchar *uri, cchar *data, int lifesecs);
  2631. /**
  2632. Write a record to the database
  2633. @description The record will be saved to the database after running any field validations. If any field validations
  2634. fail to pass, the record will not be written and error details can be retrieved via #ediGetRecErrors.
  2635. If the record is a new record and the "id" column is EDI_AUTO_INC, then the "id" will be assigned
  2636. prior to saving the record.
  2637. @param stream HttpStream stream object
  2638. @param rec Record to write to the database.
  2639. @return "true" if the record can be successfully written.
  2640. @ingroup EspReq
  2641. @stability Stable
  2642. */
  2643. PUBLIC bool espUpdateRec(HttpStream *stream, EdiRec *rec);
  2644. /**
  2645. Create a URI.
  2646. @description Create a URI link by expansions tokens based on the current request and route state.
  2647. The target parameter may contain partial or complete URI information. The missing parts
  2648. are supplied using the current request and route tables. The resulting URI is a normalized, server-local
  2649. URI (that begins with "/"). The URI will include any defined route prefix, but will not include scheme, host or
  2650. port components.
  2651. @param stream HttpStream stream object
  2652. @param target The URI target. The target parameter can be a URI string or JSON style set of options.
  2653. The target will have any embedded "{tokens}" expanded by using token values from the request parameters.
  2654. If the target has an absolute URI path, that path is used directly after tokenization. If the target begins with
  2655. "~", that character will be replaced with the route prefix. This is a very convenient way to create application
  2656. top-level relative links.
  2657. \n\n
  2658. If the target is a string that begins with "{AT}" it will be interpreted as a controller/action pair of the
  2659. form "{AT}controller/action". If the "controller/" portion is absent, the current controller is used. If
  2660. the action component is missing, the "list" action is used. A bare "{AT}" refers to the "list" action
  2661. of the current controller.
  2662. \n\n
  2663. If the target starts with "{" it is interpreted as being a JSON style set of options that describe the link.
  2664. If the target is a relative URI path, it is appended to the current request URI path.
  2665. \n\n
  2666. If the target is a JSON style of options, it can specify the URI components: scheme, host, port, path, reference and
  2667. query. If these component properties are supplied, these will be combined to create a URI.
  2668. \n\n
  2669. If the target specifies either a controller/action or a JSON set of options, The URI will be created according
  2670. to the route URI template. The template may be explicitly specified
  2671. via a "route" target property. Otherwise, if an "action" property is specified, the route of the same
  2672. name will be used. If these don't result in a usable route, the "default" route will be used.
  2673. \n\n
  2674. These are the properties supported in a JSON style "{ ... }" target:
  2675. <ul>
  2676. <li>scheme String URI scheme portion</li>
  2677. <li>host String URI host portion</li>
  2678. <li>port Number URI port number</li>
  2679. <li>path String URI path portion</li>
  2680. <li>reference String URI path reference. Does not include "#"</li>
  2681. <li>query String URI query parameters. Does not include "?"</li>
  2682. <li>controller String controller name if using a controller-based route. This can also be specified via
  2683. the action option.</li>
  2684. <li>action String Action to invoke. This can be a URI string or a controller action of the form
  2685. {AT}controller/action.</li>
  2686. <li>route String Route name to use for the URI template</li>
  2687. </ul>
  2688. @return A normalized, server-local Uri string.
  2689. @example espUri(stream, "http://example.com/index.html", 0); <br/>
  2690. espUri(stream, "/path/to/index.html", 0); <br/>
  2691. espUri(stream, "../images/splash.png", 0); <br/>
  2692. espUri(stream, "~/client/images/splash.png", 0); <br/>
  2693. espUri(stream, "${app}/client/images/splash.png", 0); <br/>
  2694. espUri(stream, "@controller/checkout", 0); <br/>
  2695. espUri(stream, "@controller/") <br/>
  2696. espUri(stream, "@init") <br/>
  2697. espUri(stream, "@") <br/>
  2698. espUri(stream, "{ action: '@post/create' }", 0); <br/>
  2699. espUri(stream, "{ action: 'checkout' }", 0); <br/>
  2700. espUri(stream, "{ action: 'logout', controller: 'admin' }", 0); <br/>
  2701. espUri(stream, "{ action: 'admin/logout'", 0); <br/>
  2702. espUri(stream, "{ product: 'candy', quantity: '10', template: '/cart/${product}/${quantity}' }", 0); <br/>
  2703. espUri(stream, "{ route: '~/STAR/edit', action: 'checkout', id: '99' }", 0); <br/>
  2704. espUri(stream, "{ template: '~/client/images/${theme}/background.jpg', theme: 'blue' }", 0);
  2705. @ingroup EspReq
  2706. @stability Evolving
  2707. */
  2708. PUBLIC cchar *espUri(HttpStream *stream, cchar *target);
  2709. /************************************** Actions *******************************/
  2710. /**
  2711. Action definition
  2712. @stability Prototype
  2713. */
  2714. typedef struct EspAction {
  2715. cchar *target; /**< Route target string */
  2716. cchar *abilities; /**< Abilities or roles for action. Comma separated. */
  2717. EspProc callback; /**< Callback action */
  2718. } EspAction;
  2719. #if DEPRECATED || 1
  2720. /**
  2721. Define an action
  2722. @description Actions are C procedures that are invoked when specific URIs are routed to the controller/action pair.
  2723. This API is deprecated. Use #espAction instead.
  2724. @param route HttpRoute object
  2725. @param targetKey Target key used to select the action in a HttpRoute target. This is typically a URI prefix.
  2726. @param actionProc EspProc callback procedure to invoke when the action is requested.
  2727. @ingroup EspRoute
  2728. @stability Deprecated
  2729. */
  2730. PUBLIC void espDefineAction(HttpRoute *route, cchar *targetKey, EspProc actionProc) ME_DEPRECATED("Use espAction instead");
  2731. #endif
  2732. /**
  2733. Define an action
  2734. @description Actions are C procedures that are invoked when specific URIs are routed to the controller/action pair.
  2735. The action will require the specified abilities or roles.
  2736. @param route HttpRoute object
  2737. @param targetKey Target key used to select the action in a HttpRoute target. This is typically a URI prefix.
  2738. @param abilities String Comma separated list of abilities or roles. If set to the empty string, no specific abilities are required
  2739. but an authenticated user is required. Set to NULL if an authenticated user is not required.
  2740. @param actionProc EspProc callback procedure to invoke when the action is requested.
  2741. @ingroup EspRoute
  2742. @stability Prototype
  2743. */
  2744. PUBLIC void espAction(HttpRoute *route, cchar *targetKey, cchar *abilities, EspProc actionProc);
  2745. /***************************** Abbreviated Controls ***************************/
  2746. #if ME_ESP_ABBREV
  2747. /**
  2748. Abbreviated ESP API.
  2749. @description This is a short-form API that uses the current HttpStream stream object.
  2750. These APIs are designed to be terse and highly readable. Consequently, they are not prefixed with "esp".
  2751. @defgroup EspAbbrev EspAbbrev
  2752. @stability Stable
  2753. */
  2754. typedef struct EspAbbrev { int dummy; } EspAbbrev;
  2755. /******************************* Abbreviated API ******************************/
  2756. /**
  2757. Create an absolute URI with a scheme and host
  2758. @param target The URI target. See httpLink for details
  2759. @param ... arguments to the formatted target string
  2760. @return A normalized, absolute Uri string containing scheme and host.
  2761. @ingroup EspAbbrev
  2762. @stability Evolving
  2763. */
  2764. PUBLIC cchar *absuri(cchar *target, ...);
  2765. /**
  2766. Add a header to the transmission using a format string.
  2767. @description Add a header if it does not already exist.
  2768. @param key Http response header key
  2769. @param fmt Printf style formatted string to use as the header key value
  2770. @param ... Arguments for fmt
  2771. @return Zero if successful, otherwise a negative MPR error code. Returns MPR_ERR_ALREADY_EXISTS if the header already exists.
  2772. @ingroup EspAbbrev
  2773. @stability Evolving
  2774. */
  2775. PUBLIC void addHeader(cchar *key, cchar *fmt, ...);
  2776. /**
  2777. Add a request parameter value if not already defined.
  2778. @param name Name of the request parameter to set
  2779. @param value Value to set.
  2780. @ingroup EspAbbrev
  2781. @stability Evolving
  2782. */
  2783. PUBLIC void addParam(cchar *name, cchar *value);
  2784. /**
  2785. Test if a user has the required abilities
  2786. @param abilities Comma separated list of abilities to test for. If null, then use the required abilities defined
  2787. for the current request route.
  2788. @param warn If true, warn the user via #sendResult.
  2789. @return True if the user has all the required abilities
  2790. @ingroup EspAbbrev
  2791. @stability prototype
  2792. */
  2793. PUBLIC bool canUser(cchar *abilities, bool warn);
  2794. /**
  2795. Create a record and initialize field values
  2796. @description This will call #ediCreateRec to create a record based on the table's schema. It will then
  2797. call #setFields to update the record with the given data.
  2798. The record is remembered for this request as the "current" record and can be retrieved via: getRec().
  2799. The record is not written to the database. Use #updateRec to write to the database.
  2800. @param tableName Database table name
  2801. @param data Json object with field values
  2802. @return EdRec instance
  2803. @ingroup EspAbbrev
  2804. @stability Evolving
  2805. */
  2806. PUBLIC EdiRec *createRec(cchar *tableName, MprJson *data);
  2807. /**
  2808. Create a record from the request parameters
  2809. @description A new record is created with the request parameters in the specified table.
  2810. The record is remembered for this request as the "current" record and can be retrieved via: getRec().
  2811. @param table Database table to update
  2812. @return True if the update is successful.
  2813. @ingroup EspAbbrev
  2814. @stability Prototype
  2815. */
  2816. PUBLIC bool createRecByParams(cchar *table);
  2817. #if DEPRECATED || 1
  2818. /**
  2819. Create a record from the request parameters
  2820. @description A new record is created with the request parameters in the specified table.
  2821. The record is remembered for this request as the "current" record and can be retrieved via: getRec().
  2822. @param table Database table to update
  2823. @return True if the update is successful.
  2824. @ingroup EspAbbrev
  2825. @stability Deprecated
  2826. */
  2827. PUBLIC bool createRecFromParams(cchar *table) ME_DEPRECATED("Use updateRecFields(table, params(\"fields\") instead");
  2828. #endif
  2829. /**
  2830. Create a session state object.
  2831. @description The session state object can be used to share state between requests.
  2832. If a session has not already been created, this call will create a new session.
  2833. It will create a response cookie containing a session ID that will be sent to the client
  2834. with the response. Note: Objects are stored in the session state using JSON serialization.
  2835. @return Session ID string
  2836. @ingroup EspAbbrev
  2837. @stability Evolving
  2838. */
  2839. PUBLIC cchar *createSession(void);
  2840. /**
  2841. Destroy a session state object.
  2842. @description This will emit an expired cookie to the client to force it to erase the session cookie.
  2843. @ingroup EspAbbrev
  2844. @stability Evolving
  2845. */
  2846. PUBLIC void destroySession(void);
  2847. /**
  2848. Don't auto-finalize this request
  2849. @ingroup EspAbbrev
  2850. @stability Evolving
  2851. */
  2852. PUBLIC void dontAutoFinalize(void);
  2853. /**
  2854. Display the grid to the debug log
  2855. @param message Prefix message to output
  2856. @param grid EDI grid
  2857. @ingroup EspAbbrev
  2858. @stability Prototype
  2859. */
  2860. PUBLIC void dumpGrid(cchar *message, EdiGrid *grid);
  2861. /**
  2862. Display request parameters to the debug log
  2863. @param message Prefix message to output
  2864. @ingroup EspAbbrev
  2865. @stability Prototype
  2866. */
  2867. PUBLIC void dumpParams(cchar *message);
  2868. /**
  2869. Display a record to the debug log
  2870. @param message Prefix message to output
  2871. @param rec Record to log
  2872. @ingroup EspAbbrev
  2873. @stability Prototype
  2874. */
  2875. PUBLIC void dumpRec(cchar *message, EdiRec *rec);
  2876. /**
  2877. Finalize the response.
  2878. @description Signals the end of any and all response data and flushes any buffered write data to the client.
  2879. If the request has already been finalized, this call has no additional effect.
  2880. This routine calls #espFinalize.
  2881. @ingroup EspAbbrev
  2882. @stability Evolving
  2883. */
  2884. PUBLIC void finalize(void);
  2885. /**
  2886. Set a feedback message
  2887. @description Feedback messages are a convenient way to aggregate messages state information in the response.
  2888. The #getFeedback API can be used to retrieve feedback messages.
  2889. Feedback messages are removed at the completion of the request.
  2890. @param type type of feedback message. This may be set to any word, but the following feedback types
  2891. are typically supported as per RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  2892. @param fmt Printf style formatted string to use as the message
  2893. @return True if the request has been successful so far, i.e. there is not an error feedback message defined.
  2894. Return false if there is an error feedback defined.
  2895. This permits feedback to be chained as: sendResult(feedback("error", ...));
  2896. @ingroup EspAbbrev
  2897. @stability Evolving
  2898. */
  2899. PUBLIC bool feedback(cchar *type, cchar *fmt, ...);
  2900. /**
  2901. Build an EDI selection query from the request parameters for use by SPA applications.
  2902. @description This call creates an EDI "SQL style" query from the request parameters.
  2903. This call expects optional "fields" and "options" parameters with options.offset, options.limit and options.filter parameters. It examines each of the "fields" parameters to build an SQL "WHERE" expression testing the value of each field. The resulting expression looks like:
  2904. \n\n
  2905. field OP value AND field OP value .... LIMIT offset, limit
  2906. @return An EDI sql style selection query string suitable for use with #findRec and #findGrid
  2907. @ingroup EspAbbrev
  2908. @stability Prototype
  2909. */
  2910. PUBLIC cchar *findParams(void);
  2911. /**
  2912. Flush transmit data.
  2913. @description This writes any buffered data.
  2914. @ingroup EspAbbrev
  2915. @stability Evolving
  2916. */
  2917. PUBLIC void flush(void);
  2918. /**
  2919. Get the auth object for the current route
  2920. @ingroup EspAbbrev
  2921. @stability Prototype
  2922. */
  2923. PUBLIC HttpAuth *getAuth(void);
  2924. /**
  2925. Get a list of column names.
  2926. @param rec Database record.
  2927. @return An MprList of column names in the given table. If there is no record defined, an empty list is returned.
  2928. @ingroup EspAbbrev
  2929. @stability Evolving
  2930. */
  2931. PUBLIC MprList *getColumns(EdiRec *rec);
  2932. /**
  2933. Get the request cookies
  2934. @description Get the cookies defined in the current request.
  2935. @return Return a string containing the cookies sent in the Http header of the last request.
  2936. @ingroup EspAbbrev
  2937. @stability Evolving
  2938. */
  2939. PUBLIC cchar *getCookies(void);
  2940. /**
  2941. Get the HttpStream object
  2942. @description Before a view or controller is run, the current stream object for the request is saved in thread
  2943. local data. Most EspAbbrev APIs take an HttpStream object as an argument.
  2944. @return HttpStream stream instance object.
  2945. @ingroup EspAbbrev
  2946. @stability Evolving
  2947. */
  2948. PUBLIC HttpStream *getStream(void);
  2949. #if ME_COMPAT
  2950. /*
  2951. LEGACY redefinitions
  2952. */
  2953. #define getConn() getStream()
  2954. #define setConn(stream) setStream(stream)
  2955. #endif
  2956. /**
  2957. Get the receive body content length
  2958. @description Get the length of the receive body content (if any). This is used in servers to get the length of posted
  2959. data and in clients to get the response body length.
  2960. @return A count of the response content data in bytes.
  2961. @ingroup EspAbbrev
  2962. @stability Evolving
  2963. */
  2964. PUBLIC MprOff getContentLength(void);
  2965. /**
  2966. Get the receive body content type
  2967. @description Get the content mime type of the receive body content (if any).
  2968. @return Mime type of any receive content. Set to NULL if not posted data.
  2969. @ingroup EspAbbrev
  2970. @stability Evolving
  2971. */
  2972. PUBLIC cchar *getContentType(void);
  2973. /**
  2974. Get the private data reference for the current request set via #setData
  2975. @return Reference to private data
  2976. @ingroup EspAbbrev
  2977. @stability prototype
  2978. */
  2979. PUBLIC void *getData(void);
  2980. /**
  2981. Get the stream dispatcher object
  2982. @return MprDispatcher stream dispatcher instance object.
  2983. @ingroup EspAbbrev
  2984. @stability Evolving
  2985. */
  2986. PUBLIC MprDispatcher *getDispatcher(void);
  2987. /**
  2988. Get a feedback message defined via #feedback
  2989. @param type type of feedback message to retrieve. This may be set to any word, but the following feedback types
  2990. are typically supported as per RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  2991. @return Reference to private data
  2992. @ingroup EspAbbrev
  2993. @stability Evolving
  2994. */
  2995. PUBLIC cchar *getFeedback(cchar *type);
  2996. /**
  2997. Get the current database instance
  2998. @description A route may have a default database configured via the EspDb Appweb.conf configuration directive.
  2999. The database will be opened when the web server initializes and will be shared between all requests using the route.
  3000. @return Edi EDI database handle
  3001. @ingroup EspAbbrev
  3002. @stability Evolving
  3003. */
  3004. PUBLIC Edi *getDatabase(void);
  3005. /**
  3006. Get the extended route EspRoute structure
  3007. @return EspRoute instance
  3008. @ingroup EspAbbrev
  3009. @stability Evolving
  3010. */
  3011. PUBLIC EspRoute *getEspRoute(void);
  3012. /**
  3013. Get the default document root directory for the request route.
  3014. @return A directory path name
  3015. @ingroup EspAbbrev
  3016. @stability Evolving
  3017. */
  3018. PUBLIC cchar *getDocuments(void);
  3019. /**
  3020. Get a field from the current database record
  3021. @param rec Database record.
  3022. @param field Field name to return
  3023. @return String value for "field" in the current record.
  3024. @ingroup EspAbbrev
  3025. @stability Evolving
  3026. */
  3027. PUBLIC cchar *getField(EdiRec *rec, cchar *field);
  3028. /**
  3029. Get the current database grid
  3030. @description The current grid is defined via #setGrid
  3031. @return EdiGrid instance
  3032. @ingroup EspAbbrev
  3033. @stability Evolving
  3034. */
  3035. PUBLIC EdiGrid *getGrid(void);
  3036. /**
  3037. Get an rx http header.
  3038. @description Get a http response header for a given header key.
  3039. @param key Name of the header to retrieve. This should be a lower case header name. For example: "Connection".
  3040. @return Value associated with the header key or null if the key did not exist in the response.
  3041. @ingroup EspAbbrev
  3042. @stability Evolving
  3043. */
  3044. PUBLIC cchar *getHeader(cchar *key);
  3045. /**
  3046. Get the HTTP method
  3047. @description This is a convenience API to return the Http method
  3048. @return The HttpStream.rx.method property
  3049. @ingroup EspReq
  3050. @stability Evolving
  3051. */
  3052. PUBLIC cchar *getMethod(void);
  3053. /**
  3054. Get the HTTP URI query string
  3055. @description This is a convenience API to return the query string for the current request.
  3056. @return The espGetStream()->rx->parsedUri->query property
  3057. @ingroup EspAbbrev
  3058. @stability Evolving
  3059. */
  3060. PUBLIC cchar *getQuery(void);
  3061. /**
  3062. Get the referring URI
  3063. @description This returns the referring URI as described in the HTTP "referer" (yes the HTTP specification does
  3064. spell it incorrectly) header. If this header is not defined, this routine will return the home URI as returned
  3065. by uri("~").
  3066. @return String URI back to the referring URI. If no referrer is defined, refers to the home URI.
  3067. @ingroup EspAbbrev
  3068. @stability Evolving
  3069. */
  3070. PUBLIC cchar *getReferrer(void);
  3071. /**
  3072. Get the ESP request object
  3073. @return EspReq request instance object.
  3074. @ingroup EspAbbrev
  3075. @stability Evolving
  3076. */
  3077. PUBLIC EspReq *getReq(void);
  3078. /**
  3079. Get the HttpRoute object for the current route
  3080. @ingroup EspAbbrev
  3081. @stability Evolving
  3082. */
  3083. PUBLIC HttpRoute *getRoute(void);
  3084. /**
  3085. Get the security token.
  3086. @description To minimize form replay attacks, a security token may be required for POST requests on a route.
  3087. Client-side Javascript must then send this token as a request header in subsquent POST requests.
  3088. To configure a route to require security tokens, call #httpSetRouteXsrf.
  3089. @return the security token.
  3090. @ingroup EspAbbrev
  3091. @stability Evolving
  3092. */
  3093. PUBLIC cchar *getSecurityToken(void);
  3094. /**
  3095. Get a session state variable
  3096. @description The #session API is an alias for this routine.
  3097. @param name Variable name to get
  3098. @return The session variable value. Returns NULL if not set.
  3099. @ingroup EspAbbrev
  3100. @stability Evolving
  3101. */
  3102. PUBLIC cchar *getSessionVar(cchar *name);
  3103. /**
  3104. Get the session state ID.
  3105. @description This will get a session and return the session ID. This will create a new session state storage area if
  3106. one does not already exist.
  3107. @return The session state identifier string.
  3108. @ingroup EspAbbrev
  3109. @stability Evolving
  3110. */
  3111. PUBLIC cchar *getSessionID(void);
  3112. /**
  3113. Test if a field in the current record has input validation errors
  3114. @ingroup EspAbbrev
  3115. @stability Prototype
  3116. */
  3117. PUBLIC cchar *getFieldError(cchar *field);
  3118. /**
  3119. Get the request URI path string
  3120. @description This is a convenience API to return the request URI path. This is the portion after the application/route
  3121. prefix.
  3122. @return The espGetStream()->rx->pathInfo
  3123. @ingroup EspAbbrev
  3124. @stability Evolving
  3125. */
  3126. PUBLIC cchar *getPath(void);
  3127. /**
  3128. Get the current database record
  3129. @return EdiRec instance
  3130. @ingroup EspAbbrev
  3131. @stability Evolving
  3132. */
  3133. PUBLIC EdiRec *getRec(void);
  3134. /**
  3135. Get a field from the application pak.json configuration
  3136. @param field Property field name in pak.json. May contain dots.
  3137. @return The field value. Returns "" if the field is not found.
  3138. @ingroup EspAbbrev
  3139. @stability deprecated
  3140. */
  3141. PUBLIC cchar *getConfig(cchar *field);
  3142. /**
  3143. Get the uploaded files
  3144. @description Get the list of uploaded files.
  3145. @return A list of HttpUploadFile objects.
  3146. @ingroup EspAbbrev
  3147. @stability Evolving
  3148. */
  3149. PUBLIC MprList *getUploads(void);
  3150. /**
  3151. Get the request URI string
  3152. @description This is a convenience API to return the request URI.
  3153. @return The espGetStream()->rx->uri
  3154. @ingroup EspAbbrev
  3155. @stability Evolving
  3156. */
  3157. PUBLIC cchar *getUri(void);
  3158. /**
  3159. Test if a current grid has been defined
  3160. @return "true" if a current grid has been defined
  3161. @ingroup EspAbbrev
  3162. @stability Evolving
  3163. */
  3164. PUBLIC bool hasGrid(void);
  3165. /**
  3166. Test if a current record has been defined and save to the database
  3167. @description This call returns "true" if a current record is defined and has been saved to the database with a
  3168. valid "id" field.
  3169. @return "true" if a current record with a valid "id" is defined.
  3170. @ingroup EspAbbrev
  3171. @stability Evolving
  3172. */
  3173. PUBLIC bool hasRec(void);
  3174. /**
  3175. Render an input field as part of a form. This is a smart input control that will call the appropriate
  3176. input control based on the database record field data type. This control should not be used
  3177. if using the esp-vue-mvc or other similar client-side Javascript framework.
  3178. @param field Name for the input field. This defines the HTML element name and provides the source
  3179. of the initial value to display. The field should be a property of the form current record.
  3180. If this call is used without a form control record, the actual data value should be supplied via the
  3181. options.value property.
  3182. @param options These are in JSON string form and are converted to attributes to pass to the input element
  3183. @arg noescape Boolean Do not HTML escape the text before rendering.
  3184. @arg ... Other options are converted and rendered as HTML attributes.
  3185. @ingroup EspAbbrev
  3186. @stability Evolving
  3187. */
  3188. PUBLIC void input(cchar *field, cchar *options);
  3189. /**
  3190. Render an input field with a hidden XSRF security token.
  3191. @description Security tokens are used to help guard against CSRF threats.
  3192. This call will generate a hidden input field that includes the CSRF security token for the form.
  3193. This call should not be included in SPA client applications as the SPA framework should automatically
  3194. handle the security token.
  3195. @ingroup EspAbbrev
  3196. @stability Prototype
  3197. */
  3198. PUBLIC void inputSecurityToken(void);
  3199. /**
  3200. Get an integer request parameter
  3201. @description Get the value of a named request parameter. Request parameters are defined via www-urlencoded
  3202. query or post data contained in the request. This routine calls #espGetParam
  3203. @param name Name of the request parameter to retrieve
  3204. @return Integer containing the request parameter's value. Returns zero if not found.
  3205. @ingroup EspAbbrev
  3206. @stability Evolving
  3207. */
  3208. PUBLIC int paramInt(cchar *name);
  3209. #define intParam paramInt
  3210. /**
  3211. Test if the user is authenticated
  3212. @return True if the username and password have been authenticated.
  3213. @ingroup EspAbbrev
  3214. @stability Prototype
  3215. */
  3216. PUBLIC bool isAuthenticated(void);
  3217. /**
  3218. Test if the receive input stream is at end-of-file
  3219. @return "true" if there is no more receive data to read
  3220. @ingroup EspAbbrev
  3221. @stability Evolving
  3222. */
  3223. PUBLIC bool isEof(void);
  3224. /**
  3225. Test if a http request is finalized.
  3226. @description This tests if #espFinalize or #httpFinalize has been called for a request.
  3227. @return "true" if the request has been finalized.
  3228. @ingroup EspAbbrev
  3229. @stability Evolving
  3230. */
  3231. PUBLIC bool isFinalized(void);
  3232. /**
  3233. Test if the stream is using SSL and is secure
  3234. @return "true" if the stream is using SSL.
  3235. @ingroup EspAbbrev
  3236. @stability Evolving
  3237. */
  3238. PUBLIC bool isSecure(void);
  3239. /**
  3240. Make a hash table container of property values
  3241. @description This routine formats the given arguments, parses the result as a JSON string and returns an
  3242. equivalent hash of property values. The result after formatting should be of the form:
  3243. hash("{ key: 'value', key2: 'value', key3: 'value' }");
  3244. @param fmt Printf style format string
  3245. @param ... arguments
  3246. @return MprHash instance
  3247. @ingroup EspAbbrev
  3248. @stability Evolving
  3249. */
  3250. PUBLIC MprHash *makeHash(cchar *fmt, ...);
  3251. /**
  3252. Make a JSON object container of property values
  3253. @description This routine formats the given arguments, parses the result into a JSON object.
  3254. @param fmt Printf style format string
  3255. @param ... arguments
  3256. @return MprJson instance
  3257. @ingroup EspAbbrev
  3258. @stability Evolving
  3259. */
  3260. PUBLIC MprJson *makeJson(cchar *fmt, ...);
  3261. /**
  3262. Make a free-standing record
  3263. @description This call makes a free-standing data record based on the JSON format content string.
  3264. The record is not saved to the database.
  3265. @param content JSON format content string. The content should be a set of property names and values.
  3266. @return An EdiRec instance
  3267. @example: rec = ediMakeRec("{ id: 1, title: 'Message One', body: 'Line one' }");
  3268. @ingroup EspAbbrev
  3269. @stability Evolving
  3270. */
  3271. PUBLIC EdiRec *makeRec(cchar *content);
  3272. /**
  3273. Create a URI.
  3274. @description Create a URI link by expansions tokens based on the current request and route state.
  3275. The target parameter may contain partial or complete URI information. The missing parts
  3276. are supplied using the current request and route tables. The resulting URI is a normalized, server-local
  3277. URI (that begins with "/"). The URI will include any defined route prefix, but will not include scheme, host or
  3278. port components.
  3279. @param target The URI target. The target parameter can be a URI string or JSON style set of options.
  3280. The target will have any embedded "{tokens}" expanded by using token values from the request parameters.
  3281. If the target has an absolute URI path, that path is used directly after tokenization. If the target begins with
  3282. "~", that character will be replaced with the route prefix. This is a very convenient way to create application
  3283. top-level relative links.
  3284. \n\n
  3285. If the target is a string that begins with "{AT}" it will be interpreted as a controller/action pair of the
  3286. form "{AT}controller/action". If the "controller/" portion is absent, the current controller is used. If
  3287. the action component is missing, the "list" action is used. A bare "{AT}" refers to the "list" action
  3288. of the current controller.
  3289. \n\n
  3290. If the target starts with "{" it is interpreted as being a JSON style set of options that describe the link.
  3291. If the target is a relative URI path, it is appended to the current request URI path.
  3292. \n\n
  3293. If the target is a JSON style of options, it can specify the URI components: scheme, host, port, path, reference and
  3294. query. If these component properties are supplied, these will be combined to create a URI.
  3295. \n\n
  3296. If the target specifies either a controller/action or a JSON set of options, The URI will be created according
  3297. to the route URI template. The template may be explicitly specified
  3298. via a "route" target property. Otherwise, if an "action" property is specified, the route of the same
  3299. name will be used. If these don't result in a usable route, the "default" route will be used.
  3300. \n\n
  3301. These are the properties supported in a JSON style "{ ... }" target:
  3302. <ul>
  3303. <li>scheme String URI scheme portion</li>
  3304. <li>host String URI host portion</li>
  3305. <li>port Number URI port number</li>
  3306. <li>path String URI path portion</li>
  3307. <li>reference String URI path reference. Does not include "#"</li>
  3308. <li>query String URI query parameters. Does not include "?"</li>
  3309. <li>controller String controller name if using a controller-based route. This can also be specified via
  3310. the action option.</li>
  3311. <li>action String Action to invoke. This can be a URI string or a controller action of the form
  3312. {AT}controller/action.</li>
  3313. <li>route String Route name to use for the URI template</li>
  3314. </ul>
  3315. @return A normalized, server-local Uri string.
  3316. @example makeUri("http://example.com/index.html", 0); <br/>
  3317. makeUri("/path/to/index.html", 0); <br/>
  3318. makeUri("../images/splash.png", 0); <br/>
  3319. makeUri("~/client/images/splash.png", 0); <br/>
  3320. makeUri("${app}/client/images/splash.png", 0); <br/>
  3321. makeUri("@controller/checkout", 0); <br/>
  3322. makeUri("@controller/") <br/>
  3323. makeUri("@init") <br/>
  3324. makeUri("@") <br/>
  3325. makeUri("{ action: '@post/create' }", 0); <br/>
  3326. makeUri("{ action: 'checkout' }", 0); <br/>
  3327. makeUri("{ action: 'logout', controller: 'admin' }", 0); <br/>
  3328. makeUri("{ action: 'admin/logout'", 0); <br/>
  3329. makeUri("{ product: 'candy', quantity: '10', template: '/cart/${product}/${quantity}' }", 0); <br/>
  3330. makeUri("{ route: '~/STAR/edit', action: 'checkout', id: '99' }", 0); <br/>
  3331. makeUri("{ template: '~/client/images/${theme}/background.jpg', theme: 'blue' }", 0);
  3332. @ingroup EspReq
  3333. @stability Evolving
  3334. */
  3335. PUBLIC cchar *makeUri(cchar *target);
  3336. /**
  3337. Get an MD5 checksum
  3338. @param str String to hash
  3339. @returns An allocated MD5 checksum string.
  3340. @ingroup EspAbbrev
  3341. @stability Evolving
  3342. */
  3343. PUBLIC cchar *md5(cchar *str);
  3344. /**
  3345. Generate a onetime random string
  3346. @returns An MD5 encoded random string
  3347. @ingroup EspAbbrev
  3348. @stability Evolving
  3349. */
  3350. PUBLIC cchar *nonce(void);
  3351. /**
  3352. Test the the application mode
  3353. @description This is typically set to "debug" or "release". The mode is defined by the "profile" property in the pak.json.
  3354. @param check Mode to compare with the current application mode.
  3355. @return True if the current app mode matches the check mode
  3356. @ingroup EspAbbrev
  3357. @stability Prototype
  3358. */
  3359. PUBLIC bool modeIs(cchar *check);
  3360. /**
  3361. Get a request parameter
  3362. @description Get the value of a named request parameter. Request parameters are defined via www-urlencoded
  3363. query or post data contained in the request. This routine calls #espGetParam.
  3364. @param name Name of the request parameter to retrieve
  3365. @return String containing the request parameter's value. Caller should not free.
  3366. Returns NULL if the parameter is not defined.
  3367. @ingroup EspAbbrev
  3368. @stability Evolving
  3369. */
  3370. PUBLIC cchar *param(cchar *name);
  3371. /**
  3372. Get a collection of request parameters
  3373. @description This call gets request parameters for a given variable root.
  3374. Route tokens, request query data, and www-url encoded form data are all entered into the request parameters
  3375. @param var Root property of the params collection. Set to NULL for the root collection.
  3376. @return MprJson instance containing the request parameters
  3377. @ingroup EspAbbrev
  3378. @stability Evolving
  3379. */
  3380. PUBLIC MprJson *params(cchar *var);
  3381. /**
  3382. Read matching records in table from the database
  3383. @description This reads a table and returns a grid containing the table data.
  3384. The grid of records is remembered for this request as the "current" grid and can be retrieved via: getGrid().
  3385. @param tableName Database table name
  3386. @param select Selection format string. This is a printf style format string. This will contain a select criteria typically
  3387. of the form: "Field Op Value AND field OP value ...". All fields may be matched by using the pseudo column name "*".
  3388. OP is "==", "!=", "<", ">", "<=", ">=" or "><".
  3389. @return A grid containing all table rows. Returns NULL if the table cannot be found.
  3390. @ingroup EspAbbrev
  3391. @stability Evolving
  3392. */
  3393. PUBLIC EdiGrid *findGrid(cchar *tableName, cchar *select);
  3394. /**
  3395. Read a record identified by SQL style query expression
  3396. @description Read a record from the given table as described by the selection criteria.
  3397. The record is remembered for this request as the "current" record and can be retrieved via: getRec().
  3398. @param tableName Database table name
  3399. @param query SQL like query expression. This arg is a printf style format string. When expanded, this will contain
  3400. a SQL style query expression of the form: "Field Op Value AND field OP value ... LIMIT offset, limit".
  3401. All fields may be matched by using the pseudo column name "*". OP is "==", "!=", "<", ">", "<=", ">=" or "><".
  3402. @return Record instance of EdiRec.
  3403. @ingroup EspAbbrev
  3404. @stability Evolving
  3405. */
  3406. PUBLIC EdiRec *findRec(cchar *tableName, cchar *query);
  3407. /**
  3408. Read a record identified by key value
  3409. @description Read a record from the given table as identified by the key value.
  3410. The record is remembered for this request as the "current" record and can be retrieved via: getRec().
  3411. @param tableName Database table name
  3412. @param key Key value of the record to read
  3413. @return Record instance of EdiRec.
  3414. @ingroup EspAbbrev
  3415. @stability Prototype
  3416. */
  3417. PUBLIC EdiRec *readRec(cchar *tableName, cchar *key);
  3418. #if DEPRECATED || 1
  3419. /**
  3420. Read matching records
  3421. @description This runs a simple query on the database and returns matching records in a grid. The query selects
  3422. all rows that have a "field" that matches the given "value".
  3423. The grid of records is remembered for this request as the "current" grid and can be retrieved via: getGrid().
  3424. @param tableName Database table name
  3425. @param fieldName Database field name to evaluate
  3426. @param operation Comparison operation. Set to "==", "!=", "<", ">", "<=" or ">=".
  3427. @param value Data value to compare with the field values.
  3428. @return A grid containing all matching records. Returns NULL if no matching records.
  3429. @ingroup EspAbbrev
  3430. @stability Deprecated
  3431. */
  3432. PUBLIC EdiGrid *readWhere(cchar *tableName, cchar *fieldName, cchar *operation, cchar *value) ME_DEPRECATED("Use findGrid instead");
  3433. /**
  3434. Read one record
  3435. @description This runs a simple query on the database and selects the first matching record. The query selects
  3436. a row that has a "field" that matches the given "value".
  3437. The record is remembered for this request as the "current" record and can be retrieved via: getRec().
  3438. @param tableName Database table name
  3439. @param fieldName Database field name to evaluate
  3440. @param operation Comparison operation. Set to "==", "!=", "<", ">", "<=" or ">=".
  3441. @param value Data value to compare with the field values.
  3442. @return First matching record. Returns NULL if no matching records.
  3443. @ingroup EspAbbrev
  3444. @stability Deprecated
  3445. */
  3446. PUBLIC EdiRec *findRecWhere(cchar *tableName, cchar *fieldName, cchar *operation, cchar *value) ME_DEPRECATED("Use findRec instead");
  3447. /**
  3448. Read all the records in table from the database
  3449. @description This reads a table and returns a grid containing the table data.
  3450. The grid of records is remembered for this request as the "current" grid and can be retrieved via: getGrid().
  3451. @param tableName Database table name
  3452. @return A grid containing all table rows. Returns NULL if the table cannot be found.
  3453. @ingroup EspAbbrev
  3454. @stability Evolving
  3455. */
  3456. PUBLIC EdiGrid *readTable(cchar *tableName) ME_DEPRECATED("Use findGrid instead");
  3457. #endif
  3458. /**
  3459. Read receive body content
  3460. @description Read body content from the client. This will not block by default.
  3461. Use httpReadBlock for more options to read data.
  3462. @param buf Buffer to accept content data
  3463. @param size Size of the buffer
  3464. @return A count of bytes read into the buffer
  3465. @ingroup EspAbbrev
  3466. @stability Evolving
  3467. */
  3468. PUBLIC ssize receive(char *buf, ssize size);
  3469. /**
  3470. Redirect the client
  3471. @description Redirect the client to a new uri. This will redirect with an HTTP 302 status. If a different HTTP status
  3472. code is required, use #espRedirect.
  3473. @param target New target uri for the client
  3474. @ingroup EspAbbrev
  3475. @stability Evolving
  3476. */
  3477. PUBLIC void redirect(cchar *target);
  3478. /**
  3479. Redirect the client back to the referrer
  3480. @description Redirect the client to the referring URI.
  3481. @ingroup EspAbbrev
  3482. @stability Evolving
  3483. */
  3484. PUBLIC void redirectBack(void);
  3485. /**
  3486. Remove a cookie
  3487. @param name Cookie name
  3488. @ingroup EspAbbrev
  3489. @stability Evolving
  3490. */
  3491. PUBLIC void removeCookie(cchar *name);
  3492. #if KEEP
  3493. /**
  3494. Remove a record from a database table
  3495. @description Remove the record identified by the query expression.
  3496. As a sideeffect, if the removal succeeds, the feedback message {inform: "Deleted Record"} will be created.
  3497. If the removal fails, a feedback message {error: "Cannot delete Record"} will be created.
  3498. @param tableName Database table name
  3499. @param query SQL like query expression. This arg is a printf style format string. When expanded, this will contain
  3500. a SQL style query expression of the form: "Field Op Value AND field OP value ... LIMIT offset, limit".
  3501. All fields may be matched by using the pseudo column name "*". OP is "==", "!=", "<", ">", "<=", ">=" or "><".
  3502. @return True if the removal succeeds, otherwise false.
  3503. @ingroup EspAbbrev
  3504. @stability Prototype
  3505. */
  3506. PUBLIC bool removeRec(cchar *tableName, cchar *query);
  3507. #endif
  3508. /**
  3509. Remove a record from a database table
  3510. @description Remove the record identified by the key value from the given table.
  3511. If the removal succeeds, the feedback message {inform: "Deleted Record"} will be created. If the removal fails,
  3512. a feedback message {error: "Cannot delete Record"} will be created.
  3513. @param tableName Database table name
  3514. @param key Record key value.
  3515. @return True if the removal succeeds, otherwise false.
  3516. @ingroup EspAbbrev
  3517. @stability Evolving
  3518. */
  3519. PUBLIC bool removeRec(cchar *tableName, cchar *key);
  3520. /**
  3521. Remove a session state variable
  3522. @param name Variable name to set
  3523. @ingroup EspAbbrev
  3524. @stability Prototype
  3525. */
  3526. PUBLIC void removeSessionVar(cchar *name);
  3527. /**
  3528. Render a formatted string
  3529. @description Render a formatted string of data into packets to the client. Data packets will be created
  3530. as required to store the write data. This call may block waiting for data to drain to the client.
  3531. @param fmt Printf style formatted string
  3532. @param ... Arguments for fmt
  3533. @return A count of the bytes actually written
  3534. @ingroup EspAbbrev
  3535. @stability Evolving
  3536. */
  3537. PUBLIC ssize render(cchar *fmt, ...);
  3538. /**
  3539. Render cached content
  3540. @description Render the saved, cached response from a prior request to this URI. This is useful if the caching
  3541. mode has been set to "manual".
  3542. @return A count of the bytes actually written
  3543. @ingroup EspAbbrev
  3544. @stability Evolving
  3545. */
  3546. PUBLIC ssize renderCached(void);
  3547. /**
  3548. Render the pak.json
  3549. @return A count of the bytes actually written
  3550. @ingroup EspAbbrev
  3551. @stability Prototype
  3552. */
  3553. PUBLIC ssize renderConfig(void);
  3554. /**
  3555. Render an error message back to the client and finalize the request. The output is Html escaped for security.
  3556. @param status Http status code
  3557. @param fmt Printf style message format
  3558. @return A count of the bytes actually written
  3559. @ingroup EspAbbrev
  3560. @stability Evolving
  3561. */
  3562. PUBLIC void renderError(int status, cchar *fmt, ...);
  3563. /**
  3564. Render feedback messages.
  3565. @description Feedback notices are one-time messages that are passed to the next request (only).
  3566. See #espSetFeedback and #feedback for how to define feedback messages.
  3567. This API will render feedback messages as HTML in place of the renderFeedback call in ESP page.
  3568. @param types Types of feedback message to retrieve. Set to "*" to retrieve all types of feedback.
  3569. This may be set to any word, but the following feedback types are typically supported as per
  3570. RFC 5424: "debug", "info", "notice", "warn", "error", "critical".
  3571. @ingroup EspAbbrev
  3572. @stability Evolving
  3573. */
  3574. PUBLIC void renderFeedback(cchar *types);
  3575. /**
  3576. Render a file back to the client
  3577. @description Render a formatted string of data and then HTML escape. Data packets will be created
  3578. as required to store the write data. This call may block waiting for data to drain to the client.
  3579. @param path Filename of the file to send to the client.
  3580. @param ... Arguments for fmt
  3581. @return A count of the bytes actually written
  3582. @ingroup EspAbbrev
  3583. @stability Evolving
  3584. */
  3585. PUBLIC ssize renderFile(cchar *path);
  3586. /**
  3587. Render a formatted string after HTML escaping
  3588. @description Render a formatted string of data and then HTML escape. Data packets will be created
  3589. as required to store the write data. This call may block waiting for data to drain to the client.
  3590. @param fmt Printf style formatted string
  3591. @param ... Arguments for fmt
  3592. @return A count of the bytes actually written
  3593. @ingroup EspAbbrev
  3594. @stability Evolving
  3595. */
  3596. PUBLIC ssize renderSafe(cchar *fmt, ...);
  3597. /**
  3598. Render a string of data to the client
  3599. @description Render a string of data to the client. Data packets will be created
  3600. as required to store the write data. This call may block waiting for data to drain to the client.
  3601. @param s String containing the data to write
  3602. @return A count of the bytes actually written
  3603. @ingroup EspAbbrev
  3604. @stability Evolving
  3605. */
  3606. PUBLIC ssize renderString(cchar *s);
  3607. /**
  3608. Render the value of a request variable to the client.
  3609. If a request parameter is not found by the given name, consult the session store for a variable the same name.
  3610. @description This writes the value of a request variable after HTML escaping its value.
  3611. @param name Request parameter variable name
  3612. @return A count of the bytes actually written
  3613. @ingroup EspAbbrev
  3614. @stability Evolving
  3615. */
  3616. PUBLIC ssize renderVar(cchar *name);
  3617. /**
  3618. Render an ESP page to the client
  3619. @param view View name. The view name is interpreted relative to the matching route documents directory and may omit
  3620. an ESP extension.
  3621. @ingroup EspAbbrev
  3622. @stability Evolving
  3623. */
  3624. PUBLIC void renderView(cchar *view);
  3625. /**
  3626. Run a command
  3627. @description Run a command and return output.
  3628. @param command Command line and arguments to run.
  3629. @param input Input data to pass to the command. Set to null if not required.
  3630. @param output Pointer to accept command standard output response. Set to null if not required.
  3631. @param error Pointer to accept command standard error response. Set to null if not required.
  3632. @param flags MprCmd flags. Use MPR_CMD_DETACH to run in the background.
  3633. @param timeout Time in milliseconds to wait for the command to complete and exit.
  3634. @ingroup EspAbbrev
  3635. @stability Prototype
  3636. */
  3637. PUBLIC int runCmd(cchar *command, char *input, char **output, char **error, MprTicks timeout, int flags);
  3638. #if DEPRECATED && REMOVE
  3639. /**
  3640. Render scripts
  3641. @description This renders script elements for all matching filenames on the server.
  3642. @param patterns An enhanced glob-style expression pattern. The format is is a comma separated string of filename
  3643. expressions. Each expression may contain the wildcard tokens: "*" which matches any filename portion, "**" which matches
  3644. any filename portion in any subdirectory. An expression may be prefixed with "!" to exclude files of that expression.
  3645. @ingroup EspAbbrev
  3646. @stability Deprecated
  3647. */
  3648. PUBLIC void scripts(cchar *patterns);
  3649. #endif
  3650. /**
  3651. Send a database grid as a JSON string to the request client
  3652. @description The JSON string is rendered as part of an enclosing "{ data: JSON, schema: schema }" wrapper.
  3653. This API is used to send database data to clients.
  3654. @param grid EDI grid
  3655. @return Number of bytes sent
  3656. @ingroup EspReq
  3657. @stability Evolving
  3658. */
  3659. PUBLIC ssize sendGrid(EdiGrid *grid);
  3660. /**
  3661. Send a database record as a JSON string
  3662. @description The JSON string is rendered as part of an enclosing "{ data: JSON }" wrapper.
  3663. This API is used to send database data to client user interfaces such as VueJS or Aurelia clients.
  3664. @param rec EDI record
  3665. @return Number of bytes sent
  3666. @ingroup EspReq
  3667. @stability Evolving
  3668. */
  3669. PUBLIC ssize sendRec(EdiRec *rec);
  3670. /**
  3671. Send a JSON response result
  3672. @description This sends a JSON response including the request success status, feedback message and field errors.
  3673. This API is used to send controller action responses to client user interfaces such as VueJS or Aurelia clients.
  3674. The field errors apply to the current EDI record.
  3675. The format of the response is:
  3676. "{ success: STATUS, feedback: {messages}, fieldErrors: {messages}}" wrapper.
  3677. The feedback messages are created via the espSetFeedback API. Field errors are created by ESP validations.
  3678. @param status Request success status. Note: this is not the HTTP response status code.
  3679. @ingroup EspReq
  3680. @stability Evolving
  3681. */
  3682. PUBLIC void sendResult(bool status);
  3683. #if DEPRECATED && REMOVE
  3684. /**
  3685. Render stylesheets
  3686. @description This renders stylesheet elements for all matching filenames on the server.
  3687. @param patterns An enhanced glob-style expression pattern. The format is is a comma separated string of filename
  3688. expressions. Each expression may contain the wildcard tokens: "*" which matches any filename portion, "**" which matches
  3689. any filename portion in any subdirectory. An expression may be prefixed with "!" to exclude files of that expression.
  3690. @ingroup EspAbbrev
  3691. @stability Deprecated
  3692. */
  3693. PUBLIC void stylesheets(cchar *patterns);
  3694. #endif
  3695. /**
  3696. Add the security token to the response.
  3697. @description To minimize form replay attacks, a security token may be required for POST requests on a route.
  3698. This call will set a security token in the response as a response header and as a response cookie.
  3699. Client-side Javascript must then send this token as a request header in subsquent POST requests.
  3700. To configure a route to require security tokens, call #httpSetRouteXsrf.
  3701. @ingroup EspAbbrev
  3702. @stability Evolving
  3703. */
  3704. PUBLIC void securityToken(void);
  3705. /**
  3706. Get a session state variable
  3707. @description This is a convenient alias for #getSessionVar.
  3708. @param name Variable name to get
  3709. @return The session variable value. Returns NULL if not set.
  3710. @ingroup EspAbbrev
  3711. @stability Evolving
  3712. */
  3713. PUBLIC cchar *session(cchar *name);
  3714. /**
  3715. Define a cookie header to send with the response. The Path, Domain, and Expires properties can be set to null for
  3716. default values.
  3717. @param name Cookie name
  3718. @param value Cookie value
  3719. @param path Uri path to which the cookie applies
  3720. @param domain String Domain in which the cookie applies. Must have 2-3 "." and begin with a leading ".".
  3721. For example: domain: .example.com
  3722. Some browsers will accept cookies without the initial ".", but the spec: (RFC 2109) requires it.
  3723. @param lifespan Lifespan of the cookie in seconds.
  3724. @param isSecure Boolean Set to "true" if the cookie only applies for SSL based connections.
  3725. @ingroup EspAbbrev
  3726. @stability Evolving
  3727. */
  3728. PUBLIC void setCookie(cchar *name, cchar *value, cchar *path, cchar *domain, MprTicks lifespan, bool isSecure);
  3729. /**
  3730. Set the current request stream.
  3731. @param stream The HttpStream stream object to define
  3732. @ingroup EspAbbrev
  3733. @stability Evolving
  3734. */
  3735. PUBLIC void setStream(HttpStream *stream);
  3736. /**
  3737. Set the transmission (response) content mime type
  3738. @description Set the mime type Http header in the transmission
  3739. @param mimeType Mime type string
  3740. @ingroup EspAbbrev
  3741. @stability Evolving
  3742. */
  3743. PUBLIC void setContentType(cchar *mimeType);
  3744. /**
  3745. Set a private data reference for the current request
  3746. @return Reference to private data
  3747. @ingroup EspAbbrev
  3748. @stability prototype
  3749. */
  3750. PUBLIC void setData(void *data);
  3751. /**
  3752. Update a record field without writing to the database
  3753. @description This routine updates the record object with the given value. The record will not be written
  3754. to the database. To write to the database, use #updateRec
  3755. @param rec Record to update
  3756. @param fieldName Record field name to update
  3757. @param value Value to update
  3758. @return The record instance if successful, otherwise NULL.
  3759. @ingroup EspAbbrev
  3760. @stability Evolving
  3761. */
  3762. PUBLIC EdiRec *setField(EdiRec *rec, cchar *fieldName, cchar *value);
  3763. /**
  3764. Update record fields without writing to the database
  3765. @description This routine updates the record object with the given values. The "data' argument supplies
  3766. a hash of fieldNames and values. The "data' argument supplies the fieldNames and values as a JSON object. The data
  3767. may come from the request #params or it can be manually created via makeJson to convert a JSON
  3768. string into an options hash. For example: ediWriteFields(rec, params());
  3769. The record runs field validations before saving to the database.
  3770. @param rec Record to update
  3771. @param data Json object of field data.
  3772. @return The record instance if successful, otherwise NULL.
  3773. @ingroup EspAbbrev
  3774. @stability Evolving
  3775. */
  3776. PUBLIC EdiRec *setFields(EdiRec *rec, MprJson *data);
  3777. /**
  3778. Set the current database grid reference.
  3779. @description This sets the current database which is used by many APIs that operate on the current grid.
  3780. @return The grid instance. This permits chaining.
  3781. @ingroup EspAbbrev
  3782. @stability Evolving
  3783. */
  3784. PUBLIC EdiGrid *setGrid(EdiGrid *grid);
  3785. /**
  3786. Set a transmission header
  3787. @description Set a Http header to send with the request. If the header already exists, its value is overwritten.
  3788. @param key Http response header key
  3789. @param fmt Printf style formatted string to use as the header key value
  3790. @param ... Arguments for fmt
  3791. @ingroup EspAbbrev
  3792. @stability Evolving
  3793. */
  3794. PUBLIC void setHeader(cchar *key, cchar *fmt, ...);
  3795. /**
  3796. Set an integer request parameter value
  3797. @description Set the value of a named request parameter to an integer value. Request parameters are defined via
  3798. www-urlencoded query or post data contained in the request.
  3799. @param name Name of the request parameter to set
  3800. @param value Integer value to set.
  3801. @ingroup EspAbbrev
  3802. @stability Evolving
  3803. */
  3804. PUBLIC void setParamInt(cchar *name, int value);
  3805. #define setIntParam setParamInt
  3806. /**
  3807. Set a notifier callback for the stream.
  3808. This wraps the streamNotifier and calls espSetStream before invoking the notifier for stream events.
  3809. @param notifier Callback function
  3810. @ingroup EspAbbrev
  3811. @stability Evolving
  3812. */
  3813. PUBLIC void setNotifier(HttpNotifier notifier);
  3814. /**
  3815. Set a request parameter value
  3816. @description Set the value of a named request parameter to a string value. Parameters are defined via
  3817. requeset POST data or request URI queries. This API permits these initial request parameters to be set or
  3818. modified.
  3819. @param name Name of the request parameter to set
  3820. @param value Value to set.
  3821. @ingroup EspAbbrev
  3822. @stability Evolving
  3823. */
  3824. PUBLIC void setParam(cchar *name, cchar *value);
  3825. /**
  3826. Set the current database record
  3827. @description The current record is used to supply data to various abbreviated controls, such as: text(), input(),
  3828. checkbox and dropdown()
  3829. @return The grid instance. This permits chaining.
  3830. @ingroup EspAbbrev
  3831. @stability Evolving
  3832. */
  3833. PUBLIC EdiRec *setRec(EdiRec *rec);
  3834. /**
  3835. Set a session state variable
  3836. @param name Variable name to set
  3837. @param value Value to set
  3838. @ingroup EspAbbrev
  3839. @stability Evolving
  3840. */
  3841. PUBLIC void setSessionVar(cchar *name, cchar *value);
  3842. /**
  3843. Set a Http response status.
  3844. @description Set the Http response status for the request. This defaults to 200 (OK).
  3845. @param status Http status code.
  3846. @ingroup EspAbbrev
  3847. @stability Evolving
  3848. */
  3849. PUBLIC void setStatus(int status);
  3850. /**
  3851. Create a timeout event
  3852. @description invoke the given procedure after the timeout
  3853. @param proc Function to invoke
  3854. @param timeout Time in milliseconds to elapse before invoking the timeout
  3855. @param data Argument to pass to proc
  3856. @ingroup EspAbbrev
  3857. @stability Evolving
  3858. */
  3859. PUBLIC void setTimeout(void *proc, MprTicks timeout, void *data);
  3860. /**
  3861. Show request details
  3862. @description This echoes request details back to the client. This is useful as a debugging tool.
  3863. @ingroup EspAbbrev
  3864. @stability Evolving
  3865. */
  3866. PUBLIC void showRequest(void);
  3867. // FUTURE - document
  3868. PUBLIC EdiGrid *sortGrid(EdiGrid *grid, cchar *sortColumn, int sortOrder);
  3869. /**
  3870. Update the cached content for a request
  3871. @description Save the given content for future requests. This is useful if the caching mode has been set to "manual".
  3872. @param uri Request URI to cache for
  3873. @param data Data to cache
  3874. @param lifesecs Time in seconds to cache the data
  3875. @ingroup EspAbbrev
  3876. @stability Evolving
  3877. */
  3878. PUBLIC void updateCache(cchar *uri, cchar *data, int lifesecs);
  3879. /**
  3880. Write a value to a database table field
  3881. @description Update the value of a table field in the selected table row. Note: validations are not run.
  3882. @param tableName Database table name
  3883. @param key Key value for the table row to update.
  3884. @param fieldName Column name to update
  3885. @param value Value to write to the database field
  3886. @return "true" if the field can be successfully written.
  3887. @ingroup EspAbbrev
  3888. @stability Evolving
  3889. */
  3890. PUBLIC bool updateField(cchar *tableName, cchar *key, cchar *fieldName, cchar *value);
  3891. /**
  3892. Write field values to a database row
  3893. @description This routine updates the current record with the given data. The "data' argument supplies the
  3894. fieldNames and values as a JSON object. The data
  3895. may come from the request #params or it can be manually created via makeJson to convert a JSON
  3896. string into an options hash. For example: ediWriteFields(rec, params());
  3897. @param tableName Database table name
  3898. @param data Json object of fields to update
  3899. @return "true" if the field can be successfully written. Returns false if field validations fail.
  3900. @ingroup EspAbbrev
  3901. @stability Evolving
  3902. */
  3903. PUBLIC bool updateFields(cchar *tableName, MprJson *data);
  3904. /**
  3905. Save a record to the database
  3906. @description The record will be saved to the database after running any field validations. If any field validations
  3907. fail to pass, the record will not be written and error details can be retrieved via #ediGetRecErrors.
  3908. If the record is a new record and the "id" column is EDI_AUTO_INC, then the "id" will be assigned
  3909. prior to saving the record.
  3910. If the update succeeds, the feedback message {inform: "Saved Record"} will be created. If the update fails,
  3911. a feedback message {error: "Cannot save Record"} will be created.
  3912. @param rec Record to write to the database.
  3913. @return "true" if the record can be successfully written.
  3914. @ingroup EspAbbrev
  3915. @stability Evolving
  3916. */
  3917. PUBLIC bool updateRec(EdiRec *rec);
  3918. #if DEPRECATED || 1
  3919. /**
  3920. Update a record from the request parameters
  3921. @description The record identified by the params(id) is read and updated with the request parameters.
  3922. @param table Database table to update
  3923. @return True if the update is successful.
  3924. @ingroup EspAbbrev
  3925. @stability Deprecated
  3926. */
  3927. PUBLIC bool updateRecFromParams(cchar *table) ME_DEPRECATED("Use updateRecFields instead");
  3928. #endif
  3929. /**
  3930. Create a URI link.
  3931. @description Create a URI link based on a given target an expanding embedded tokens based on the current request and
  3932. route state. The target URI parameter may contain partial or complete URI information. The missing parts
  3933. are supplied using the current request and route tables.
  3934. @param target The URI target. The target parameter can be a URI string or JSON style set of options.
  3935. The target will have any embedded "{tokens}" expanded by using token values from the request parameters.
  3936. If the target has an absolute URI path, that path is used directly after tokenization. If the target begins with
  3937. "~", that character will be replaced with the route prefix. This is a very convenient way to create application
  3938. top-level relative links.
  3939. <br/>
  3940. If the target is a string that begins with "{AT}" it will be interpreted as a service/action pair of the
  3941. form "{AT}Service/action". If the "service/" portion is absent, the current service is used. If
  3942. the action component is missing, the "list" action is used. A bare "{AT}" refers to the "list" action
  3943. of the current service.
  3944. <br/>
  3945. If the target starts with "{" it is interpreted as being a JSON style set of options that describe the link.
  3946. If the target is a relative URI path, it is appended to the current request URI path.
  3947. <br/><br/>
  3948. If the is a JSON style of options, it can specify the URI components: scheme, host, port, path, reference and
  3949. query. If these component properties are supplied, these will be combined to create a URI.
  3950. <br/><br/>
  3951. If the target specifies either a service/action or a JSON set of options, The URI will be created according
  3952. to the route URI template. The template may be explicitly specified
  3953. via a "route" target property. Otherwise, if an "action" property is specified, the route of the same
  3954. name will be used. If these don't result in a usable route, the "default" route will be used.
  3955. <br/><br/>
  3956. These are the properties supported in a JSON style "{ ... }" target:
  3957. <ul>
  3958. <li>scheme String URI scheme portion</li>
  3959. <li>host String URI host portion</li>
  3960. <li>port Number URI port number</li>
  3961. <li>path String URI path portion</li>
  3962. <li>reference String URI path reference. Does not include "#"</li>
  3963. <li>query String URI query parameters. Does not include "?"</li>
  3964. <li>service String Service name if using a Service-based route. This can also be specified via
  3965. the action option.</li>
  3966. <li>action String Action to invoke. This can be a URI string or a Service action of the form
  3967. {AT}Service/action.</li>
  3968. <li>route String Route name to use for the URI template</li>
  3969. </ul>
  3970. @param ... arguments to the formatted target string
  3971. @return A normalized Uri string.
  3972. @ingroup EspAbbrev
  3973. @stability Evolving
  3974. @examples:
  3975. <pre>
  3976. uri("http://example.com/index.html");
  3977. uri("/path/to/index.html");
  3978. uri("../images/splash.png");
  3979. uri("~/static/images/splash.png");
  3980. uri("${app}/static/images/splash.png");
  3981. uri("@service/checkout");
  3982. uri("@service/") // Service = Service, action = index
  3983. uri("@init") // Current service, action = init
  3984. uri("@") // Current service, action = index
  3985. uri("{ action: '@post/create' }");
  3986. uri("{ action: 'checkout' }");
  3987. uri("{ action: 'logout', service: 'admin' }");
  3988. uri("{ action: 'admin/logout'");
  3989. uri("{ product: 'candy', quantity: '10', template: '/cart/${product}/${quantity}' }");
  3990. uri("{ route: '~/STAR/edit', action: 'checkout', id: '99' }");
  3991. uri("{ template: '~/static/images/${theme}/background.jpg', theme: 'blue' }");
  3992. </pre>
  3993. */
  3994. PUBLIC cchar *uri(cchar *target, ...);
  3995. #endif /* ME_ESP_ABBREV */
  3996. /*
  3997. LEGACY redefines
  3998. */
  3999. #define espGetConn espGetStream
  4000. #define espSetConn espSetStream
  4001. #if DEPRECATED && REMOVE
  4002. #define espGetFlash(stream, type) espGetFeedback(stream, type)
  4003. #define espRenderFlash(stream, types) espRenderFeedback(stream, types)
  4004. #define espSetFlashv(stream, type, fmt, args) espSetFeedbackv(stream, type, fmt, args)
  4005. #define getFlash(type) getFeedback(type)
  4006. #define renderFlash(types) renderFeedback(types)
  4007. PUBLIC void espSetFlash(HttpStream *stream, cchar *type, cchar *fmt, ...);
  4008. PUBLIC void flash(cchar *type, cchar *fmt, ...);
  4009. #endif /* DEPRECATED */
  4010. #ifdef __cplusplus
  4011. } /* extern C */
  4012. #endif
  4013. #endif /* _h_ESP */
  4014. /*
  4015. Copyright (c) Embedthis Software. All Rights Reserved.
  4016. This software is distributed under a commercial license. Consult the LICENSE.md
  4017. distributed with this software for full details and copyrights.
  4018. */
  4019. #endif /* ME_COM_ESP */