minimal_example_comet.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007, 2008 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 generate an infinite stream with libmicrohttpd
  19. * @author Christian Grothoff / Silvio Clécio / Gilson Nunes
  20. *)
  21. program minimal_example_comet;
  22. {$mode objfpc}{$H+}
  23. uses
  24. sysutils, cutils, libmicrohttpd;
  25. function data_generator(cls: Pointer; pos: cuint64; buf: Pcchar;
  26. max: size_t): ssize_t; cdecl;
  27. begin
  28. if max < 80 then
  29. Exit(0);
  30. memset(buf, Ord('A'), max - 1);
  31. buf[79] := #10;
  32. Exit(80);
  33. end;
  34. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  35. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  36. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  37. const
  38. aptr: cint = 0;
  39. var
  40. response: PMHD_Response;
  41. ret: cint;
  42. begin
  43. if 0 <> strcomp(method, 'GET') then
  44. Exit(MHD_NO);
  45. if @aptr <> ptr^ then
  46. begin
  47. ptr^ := @aptr;
  48. Exit(MHD_YES);
  49. end;
  50. ptr^ := nil;
  51. response := MHD_create_response_from_callback(UInt64(MHD_SIZE_UNKNOWN), 80,
  52. @data_generator, nil, nil);
  53. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  54. MHD_destroy_response(response);
  55. Result := ret;
  56. end;
  57. var
  58. d: PMHD_Daemon;
  59. begin
  60. if argc <> 2 then
  61. begin
  62. WriteLn(argv[0], ' PORT');
  63. Halt(1);
  64. end;
  65. d := MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  66. StrToInt(argv[1]), nil, nil, @ahc_echo, nil, MHD_OPTION_END);
  67. if d = nil then
  68. Halt(1);
  69. ReadLn;
  70. MHD_stop_daemon(d);
  71. end.