wasm.http.api.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly HTTP API - imported functions and structures.
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit wasm.http.api;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses wasm.http.shared, wasm.logger.api;
  15. Type
  16. TWasmHTTPLogLevel = TWasmLogLevel;
  17. TWasmHTTPLogLevels = TWasmLogLevels;
  18. const
  19. hllTrace = wllTrace;
  20. hllDebug = wllDebug;
  21. hllInfo = wllInfo;
  22. hllWarning = wllWarning;
  23. hllError = wllError;
  24. hllCritical = wllCritical;
  25. Type
  26. TWasmString = record
  27. Data : PAnsiChar;
  28. Len : Longint;
  29. end;
  30. PWasmString = ^TWasmString;
  31. TWasmBuffer = record
  32. Data : PByte;
  33. Len : Longint;
  34. end;
  35. PWasmBuffer = ^TWasmBuffer;
  36. TWasmHTTPApiRequest = Record
  37. Url : TWasmString;
  38. Method : TWasmString;
  39. HeaderCount : Longint;
  40. Headers : PWasmString;
  41. Body : TWasmBuffer;
  42. Integrity : TWasmString;
  43. Redirect : Longint;
  44. Cache : Longint;
  45. KeepAlive : Longint;
  46. Mode : Longint;
  47. Priority : Longint;
  48. Referrer : TWasmString;
  49. ReferrerPolicy : TWasmString;
  50. AbortSignal : Longint;
  51. Credentials: Longint;
  52. end;
  53. PWasmHTTPAPIRequest = ^TWasmHTTPApiRequest;
  54. TWasmHTTPResponseEvent = procedure(aRequestID : Longint; aUserData : Pointer; aStatus : TWasmHTTPResponseStatus; var Deallocate : Boolean) of object;
  55. TWasmHTTPResponseCallback = procedure(aRequestID : Longint; aUserData : Pointer; aStatus : TWasmHTTPResponseStatus; var Deallocate : Boolean);
  56. TWasmHTTPLogHook = TWasmLogHook;
  57. function __wasmhttp_request_allocate(aRequest : PWasmHTTPAPIRequest; aUserData : Pointer; aRequestID : PWasmHTTPRequestID) : TWasmHTTPResult; external httpExportName name httpFN_RequestAllocate;
  58. function __wasmhttp_request_execute(aRequestID : TWasmHTTPRequestID) : TWasmHTTPResult; external httpExportName name httpFN_RequestExecute;
  59. function __wasmhttp_request_deallocate(aRequestID : TWasmHTTPRequestID) : TWasmHTTPResult; external httpExportName name httpFN_RequestDeAllocate;
  60. function __wasmhttp_request_abort(aRequestID : TWasmHTTPRequestID) : TWasmHTTPResult; external httpExportName name httpFN_RequestAbort;
  61. function __wasmhttp_response_get_status(aRequestID : TWasmHTTPRequestID; aStatus : PLongint) : TWasmHTTPResult; external httpExportName name httpFN_ResponseGetStatus;
  62. function __wasmhttp_response_get_statustext(aRequestID : TWasmHTTPRequestID; aStatusText : PByte; aMaxHeaderTextLen : PLongint) : TWasmHTTPResult; external httpExportName name httpFN_ResponseGetStatusText;
  63. function __wasmhttp_response_get_headercount(aRequestID : TWasmHTTPRequestID; var aHeaderCount : Longint) : TWasmHTTPResult; external httpExportName name httpFN_ResponseGetHeaderCount;
  64. function __wasmhttp_response_get_headername(aRequestID : TWasmHTTPRequestID; aHeaderIdx: Longint; aHeader : PByte; aMaxHeaderLen : PLongint) : TWasmHTTPResult; external httpExportName name httpFN_ResponseGetHeaderName;
  65. function __wasmhttp_response_get_header(aRequestID : TWasmHTTPRequestID; aHeaderName: PByte; aHeaderLen : Longint; aHeader : PByte; aMaxHeaderLen : PLongint) : TWasmHTTPResult; external httpExportName name httpFN_ResponseGetHeader;
  66. function __wasmhttp_response_get_body(aRequestID : TWasmHTTPRequestID; aBody : PByte; MaxBodyLen : PLongint) : TWasmHTTPResult; external httpExportName name httpFN_ResponseGetBody;
  67. function __wasmhttp_response_callback(aRequestID : TWasmHTTPRequestID; aUserData : Pointer; aStatus : TWasmHTTPResponseStatus) : TWasmHTTPResponseResult;
  68. procedure __wasmhttp_log(level : TWasmHTTPLogLevel; const Msg : String);
  69. procedure __wasmhttp_log(level : TWasmHTTPLogLevel; const Fmt : String; const Args : Array of const);
  70. var
  71. OnWasmHTTPResponse : TWasmHTTPResponseEvent;
  72. WasmHTTPResponseCallback : TWasmHTTPResponseCallback;
  73. EnableWasmHTTPLog : Boolean;
  74. implementation
  75. {$IFDEF FPC_DOTTEDUNITS}
  76. uses System.SysUtils;
  77. {$ELSE}
  78. uses sysutils;
  79. {$ENDIF}
  80. procedure __wasmhttp_log(level : TWasmHTTPLogLevel; const Msg : String);
  81. begin
  82. if not EnableWasmHTTPLog then
  83. exit;
  84. __wasm_log(level,'HTTP',Msg);
  85. end;
  86. procedure __wasmhttp_log(level : TWasmHTTPLogLevel; const Fmt : String; const Args : Array of const);
  87. begin
  88. if not EnableWasmHTTPLog then
  89. exit;
  90. __wasm_log(level,'HTTP',Fmt,Args);
  91. end;
  92. function __wasmhttp_response_callback(aRequestID : TWasmHTTPRequestID; aUserData : Pointer; aStatus : TWasmHTTPResponseStatus) : TWasmHTTPResponseResult;
  93. var
  94. B : Boolean;
  95. begin
  96. B:=True;
  97. try
  98. if Assigned(OnWasmHTTPResponse) then
  99. OnWasmHTTPResponse(aRequestID,aUSerData,aStatus,B)
  100. else if Assigned(WasmHTTPResponseCallback) then
  101. WasmHTTPResponseCallback(aRequestID,aUSerData,aStatus,B);
  102. if B then
  103. Result:=WASMHTTP_RESPONSE_DEALLOCATE
  104. else
  105. Result:=WASMHTTP_RESPONSE_SUCCESS;
  106. except
  107. on E : exception do
  108. begin
  109. __wasmhttp_log(hllError,'Exception %s during response callback: %s',[E.ClassName,E.Message]);
  110. Result:=WASMHTTP_RESPONSE_ERROR;
  111. end;
  112. end;
  113. end;
  114. exports __wasmhttp_response_callback;
  115. end.