minimal_example.pp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007 Christian Grothoff (and other contributing authors)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. *)
  16. (**
  17. * @file minimal_example.pp (Original: minimal_example.c)
  18. * @brief minimal example for how to use libmicrohttpd
  19. * @author Christian Grothoff / Silvio Clécio
  20. *)
  21. program minimal_example;
  22. {$mode objfpc}{$H+}
  23. uses
  24. sysutils, cutils, libmicrohttpd;
  25. const
  26. PAGE: Pcchar = '<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>';
  27. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  28. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  29. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  30. const
  31. aptr: cint = 0;
  32. var
  33. me: Pcchar;
  34. response: PMHD_Response;
  35. ret: cint;
  36. begin
  37. me := cls;
  38. if 0 <> strcomp(method, 'GET') then
  39. Exit(MHD_NO);
  40. if @aptr <> ptr^ then
  41. begin
  42. ptr^ := @aptr;
  43. Exit(MHD_YES);
  44. end;
  45. ptr^ := nil;
  46. response := MHD_create_response_from_buffer(strlen(me), Pointer(me),
  47. MHD_RESPMEM_PERSISTENT);
  48. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  49. MHD_destroy_response(response);
  50. Result := ret;
  51. end;
  52. var
  53. d: PMHD_Daemon;
  54. begin
  55. if argc <> 2 then
  56. begin
  57. WriteLn(argv[0], ' PORT');
  58. Halt(1);
  59. end;
  60. d := MHD_start_daemon(// MHD_USE_SELECT_INTERNALLY or MHD_USE_DEBUG or MHD_USE_POLL,
  61. MHD_USE_SELECT_INTERNALLY or MHD_USE_DEBUG,
  62. // MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG or MHD_USE_POLL,
  63. // MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  64. StrToInt(argv[1]),
  65. nil, nil, @ahc_echo, PAGE,
  66. MHD_OPTION_CONNECTION_TIMEOUT, cuint(120),
  67. MHD_OPTION_END);
  68. if d = nil then
  69. Halt(1);
  70. ReadLn;
  71. MHD_stop_daemon(d);
  72. end.